Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Mikayla Thompson <thomika@amazon.com>
  • Loading branch information
mikaylathompson committed Jun 25, 2024
1 parent b759838 commit 53095aa
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"min_replicas": {"type": "integer", "min": 0, "required": False},
"index_allowlist": list_schema(required=False),
"index_template_allowlist": list_schema(required=False),
"component-template-allowlist": list_schema(required=False)
"component_template_allowlist": list_schema(required=False)
}


Expand All @@ -75,7 +75,7 @@ def __init__(self, config, target_cluster: Cluster, snapshot: Optional[Snapshot]
self._min_replicas = config.get("min_replicas", 0)
self._index_allowlist = config.get("index_allowlist", None)
self._index_template_allowlist = config.get("index_template_allowlist", None)
self._component_template_allowlist = config.get("component-template-allowlist", None)
self._component_template_allowlist = config.get("component_template_allowlist", None)
logger.debug(f"Min replicas: {self._min_replicas}")
logger.debug(f"Index allowlist: {self._index_allowlist}")
logger.debug(f"Index template allowlist: {self._index_template_allowlist}")
Expand Down Expand Up @@ -163,13 +163,13 @@ def migrate(self, detached_log=None) -> CommandResult:
command.append("--target-insecure")

if self._index_allowlist:
command.extend(["--index-allowlist", ", ".join(self._index_allowlist)])
command.extend(["--index-allowlist", ",".join(self._index_allowlist)])

if self._index_template_allowlist:
command.extend(["--index-template-allowlist", ", ".join(self._index_template_allowlist)])
command.extend(["--index-template-allowlist", ",".join(self._index_template_allowlist)])

if self._component_template_allowlist:
command.extend(["--component-template-allowlist", ", ".join(self._component_template_allowlist)])
command.extend(["--component-template-allowlist", ",".join(self._component_template_allowlist)])

if password_field_index:
display_command = command[:password_field_index] + ["********"] + command[password_field_index:]

Check warning on line 175 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/metadata.py

View check run for this annotation

Codecov / codecov/patch

TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/metadata.py#L175

Added line #L175 was not covered by tests
Expand All @@ -184,7 +184,7 @@ def migrate(self, detached_log=None) -> CommandResult:
def _run_as_synchronous_process(self, command) -> CommandResult:
try:
# Pass None to stdout and stderr to not capture output and show in terminal
subprocess.run(command, stdout=None, stderr=None, text=True)
subprocess.run(command, stdout=None, stderr=None, text=True, check=True)
logger.info(f"Metadata migration for snapshot {self._snapshot_name} completed")
return CommandResult(success=True, value="Metadata migration completed")
except subprocess.CalledProcessError as e:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,11 @@ def test_cli_cat_indices_e2e(runner, env):
assert 'TARGET CLUSTER' in result.output
assert source_cat_indices in result.output
assert target_cat_indices in result.output


def test_cli_metadata_migrate(runner, env, mocker):
mock = mocker.patch("subprocess.run")
result = runner.invoke(cli, ['--config-file', str(VALID_SERVICES_YAML), 'metadata', 'migrate'],
catch_exceptions=True)
mock.assert_called_once()
assert result.exit_code == 0
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import pytest
from console_link.models.cluster import AuthMethod
from console_link.models.metadata import Metadata
from console_link.models.snapshot import S3Snapshot
from console_link.models.snapshot import FileSystemSnapshot, S3Snapshot
from tests.utils import create_valid_cluster


@pytest.fixture()
def s3_snapshot():
snapshot_config = {
"snapshot_name": "reindex_from_snapshot",
"s3": {
"repo_uri": "s3://my-bucket",
"aws_region": "us-east-1"
}
}
return S3Snapshot(snapshot_config, create_valid_cluster(auth_type=AuthMethod.NO_AUTH),
create_valid_cluster(auth_type=AuthMethod.NO_AUTH))


@pytest.fixture()
def fs_snapshot():
snapshot_config = {
"snapshot_name": "reindex_from_snapshot",
"fs": {
"repo_path": "/path/for/repo"
}
}
return FileSystemSnapshot(snapshot_config, create_valid_cluster(auth_type=AuthMethod.NO_AUTH),
create_valid_cluster(auth_type=AuthMethod.NO_AUTH))


def test_metadata_init_with_fully_specified_config_succeeds():
config = {
"from_snapshot": {
Expand All @@ -21,7 +47,7 @@ def test_metadata_init_with_fully_specified_config_succeeds():
"index_template_allowlist": [
"my_index_template", "my_second_index_template"
],
"component-template-allowlist": [
"component_template_allowlist": [
"my_component_template", "my_second_component_template"
]
}
Expand Down Expand Up @@ -72,56 +98,32 @@ def test_metadata_init_with_partial_snapshot_config_no_external_snapshot_fails()
assert 'required field' == excinfo.value.args[0]['from_snapshot'][0]['s3'][0]['repo_uri'][0]


def test_metadata_init_with_minimal_config_and_external_snapshot_succeeds():
snapshot_config = {
"snapshot_name": "reindex_from_snapshot",
"s3": {
"repo_uri": "s3://my-bucket",
"aws_region": "us-east-1"
}
}
snapshot = S3Snapshot(snapshot_config, create_valid_cluster(), create_valid_cluster())
def test_metadata_init_with_minimal_config_and_external_snapshot_succeeds(s3_snapshot):
config = {
"from_snapshot": None,
}
metadata = Metadata(config, create_valid_cluster(), snapshot)
metadata = Metadata(config, create_valid_cluster(), s3_snapshot)
assert metadata._config == config
assert metadata._snapshot_name == snapshot_config["snapshot_name"]
assert metadata._snapshot_name == s3_snapshot.snapshot_name
assert isinstance(metadata, Metadata)


@pytest.mark.skip("Need to tighten up the schema specification to catch this case")
def test_metadata_init_with_partial_config_and_external_snapshot_fails():
snapshot_config = {
"snapshot_name": "reindex_from_snapshot",
"s3": {
"repo_uri": "s3://my-bucket",
"aws_region": "us-east-1"
}
}
snapshot = S3Snapshot(snapshot_config, create_valid_cluster(), create_valid_cluster())
def test_metadata_init_with_partial_config_and_external_snapshot_fails(s3_snapshot):
config = {
"from_snapshot": {
"local_dir": "/tmp/s3",
"snapshot_name": "reindex_from_snapshot",
}
}
with pytest.raises(ValueError) as excinfo:
Metadata(config, create_valid_cluster(), snapshot)
Metadata(config, create_valid_cluster(), s3_snapshot)
print(excinfo)
assert 's3' in excinfo.value.args[0]['from_snapshot'][0]
assert 'required field' == excinfo.value.args[0]['from_snapshot'][0]['s3'][0]


def test_full_config_and_snapshot_gives_priority_to_config():
snapshot_config = {
"snapshot_name": "reindex_from_snapshot",
"s3": {
"repo_uri": "s3://my-bucket",
"aws_region": "us-east-1"
}
}
snapshot = S3Snapshot(snapshot_config, create_valid_cluster(), create_valid_cluster())
def test_full_config_and_snapshot_gives_priority_to_config(s3_snapshot):
config = {
"from_snapshot": {
"local_dir": "/tmp/s3",
Expand All @@ -132,9 +134,143 @@ def test_full_config_and_snapshot_gives_priority_to_config():
},
}
}
metadata = Metadata(config, create_valid_cluster(), snapshot)
metadata = Metadata(config, create_valid_cluster(), s3_snapshot)
assert isinstance(metadata, Metadata)
assert metadata._snapshot_name == config["from_snapshot"]["snapshot_name"]
assert metadata._s3_uri == config["from_snapshot"]["s3"]["repo_uri"]
assert metadata._aws_region == config["from_snapshot"]["s3"]["aws_region"]
assert metadata._local_dir == config["from_snapshot"]["local_dir"]


def test_metadata_with_s3_snapshot_makes_correct_subprocess_call(mocker):
config = {
"from_snapshot": {
"snapshot_name": "reindex_from_snapshot",
"local_dir": "/tmp/s3",
"s3": {
"repo_uri": "s3://my-bucket",
"aws_region": "us-east-1"
},
}
}
target = create_valid_cluster(auth_type=AuthMethod.NO_AUTH)
metadata = Metadata(config, target, None)

mock = mocker.patch("subprocess.run")
metadata.migrate()

mock.assert_called_once_with([
"/root/metadataMigration/bin/MetadataMigration",
"--snapshot-name", config["from_snapshot"]["snapshot_name"],
"--target-host", target.endpoint,
"--min-replicas", '0',
"--s3-local-dir", config["from_snapshot"]["local_dir"],
"--s3-repo-uri", config["from_snapshot"]["s3"]["repo_uri"],
"--s3-region", config["from_snapshot"]["s3"]["aws_region"],
'--target-insecure'
], stdout=None, stderr=None, text=True, check=True
)


def test_metadata_with_fs_snapshot_makes_correct_subprocess_call(mocker):
config = {
"from_snapshot": {
"snapshot_name": "reindex_from_snapshot",
"fs": {
"repo_path": "path/to/repo"
},
}
}
target = create_valid_cluster(auth_type=AuthMethod.NO_AUTH)
metadata = Metadata(config, target, None)

mock = mocker.patch("subprocess.run")
metadata.migrate()

mock.assert_called_once_with([
"/root/metadataMigration/bin/MetadataMigration",
"--snapshot-name", config["from_snapshot"]["snapshot_name"],
"--target-host", target.endpoint,
"--min-replicas", '0',
"--file-system-repo-path", config["from_snapshot"]["fs"]["repo_path"],
'--target-insecure'
], stdout=None, stderr=None, text=True, check=True
)


def test_metadata_with_min_replicas_makes_correct_subprocess_call(mocker):
config = {
"from_snapshot": {
"snapshot_name": "reindex_from_snapshot",
"fs": {
"repo_path": "path/to/repo"
},
},
"min_replicas": 2
}
target = create_valid_cluster(auth_type=AuthMethod.NO_AUTH)
metadata = Metadata(config, target, None)

mock = mocker.patch("subprocess.run")
metadata.migrate()

mock.assert_called_once_with([
"/root/metadataMigration/bin/MetadataMigration",
"--snapshot-name", config["from_snapshot"]["snapshot_name"],
"--target-host", target.endpoint,
"--min-replicas", '2',
"--file-system-repo-path", config["from_snapshot"]["fs"]["repo_path"],
'--target-insecure'
], stdout=None, stderr=None, text=True, check=True
)


def test_metadata_with_allowlists_makes_correct_subprocess_call(mocker):
config = {
"from_snapshot": {
"snapshot_name": "reindex_from_snapshot",
"fs": {
"repo_path": "path/to/repo"
},
},
"index_allowlist": ["index1", "index2"],
"index_template_allowlist": ["index_template1", "index_template2"],
"component_template_allowlist": ["component_template1", "component_template2"]
}
target = create_valid_cluster(auth_type=AuthMethod.NO_AUTH)
metadata = Metadata(config, target, None)

mock = mocker.patch("subprocess.run")
metadata.migrate()

mock.assert_called_once_with([
"/root/metadataMigration/bin/MetadataMigration",
"--snapshot-name", config["from_snapshot"]["snapshot_name"],
"--target-host", target.endpoint,
"--min-replicas", '0',
"--file-system-repo-path", config["from_snapshot"]["fs"]["repo_path"],
"--target-insecure",
"--index-allowlist", "index1,index2",
"--index-template-allowlist", "index_template1,index_template2",
"--component-template-allowlist", "component_template1,component_template2"
], stdout=None, stderr=None, text=True, check=True
)


def test_metadata_migrate_detached_makes_correct_subprocess_call(mocker):
config = {
"from_snapshot": {
"snapshot_name": "reindex_from_snapshot",
"fs": {
"repo_path": "path/to/repo"
},
},
"min_replicas": 2,
}
target = create_valid_cluster(auth_type=AuthMethod.NO_AUTH)
metadata = Metadata(config, target, None)

mock = mocker.patch("subprocess.Popen")
metadata.migrate(detached_log="/tmp/log_file.log")

mock.assert_called_once()

0 comments on commit 53095aa

Please sign in to comment.