Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extra args command parsing used by Metadata migration #931

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from console_link.models.schema_tools import list_schema
from console_link.models.cluster import AuthMethod, Cluster
from console_link.models.snapshot import S3Snapshot, Snapshot, FileSystemSnapshot
from typing import Any, Dict, List

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -133,6 +134,35 @@
self._snapshot_location = "fs"
self._repo_path = snapshot.repo_path

def _appendArgs(self, commands: Dict[str, Any], args_to_add: List[str]) -> None:
if args_to_add is None:
return

def isCommand(arg: str) -> bool:
if arg is None:
return False

Check warning on line 143 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#L143

Added line #L143 was not covered by tests
return arg.startswith('--') or arg.startswith('-')

def isValue(arg: str) -> bool:
if arg is None:
return False

Check warning on line 148 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#L148

Added line #L148 was not covered by tests
return not isCommand(arg)

i = 0
while i < len(args_to_add):
arg = args_to_add[i]
nextArg = args_to_add[i + 1] if (i + 1 < len(args_to_add)) else None

if isCommand(arg) and isValue(nextArg):
commands[arg] = nextArg
i += 2 # Move past the command and value
elif isCommand(arg):
commands[arg] = None
i += 1 # Move past the command, its a flag
else:
logger.warning(f"Ignoring extra value {arg}, there was no command name before it")
i += 1

def migrate(self, detached_log=None, extra_args=None) -> CommandResult:
logger.info("Starting metadata migration")
command_base = "/root/metadataMigration/bin/MetadataMigration"
Expand Down Expand Up @@ -185,16 +215,7 @@
command_args.update({"--otel-collector-endpoint": self._otel_endpoint})

# Extra args might not be represented with dictionary, so convert args to list and append commands
cmd_args = []
for key, value in command_args.items():
cmd_args.append(key)
if value is not None:
cmd_args.append(value)

if extra_args:
it = iter(extra_args)
for arg in it:
cmd_args.append(arg)
self._appendArgs(command_args, extra_args)

command_runner = CommandRunner(command_base, command_args,
sensitive_fields=["--target-password"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,42 @@ def test_metadata_with_target_sigv4_makes_correct_subprocess_call(mocker):
"--target-insecure",
], stdout=None, stderr=None, text=True, check=True
)


def test_metadata_init_with_minimal_config_and_extra_args(mocker):
config = {
"from_snapshot": {
"snapshot_name": "reindex_from_snapshot",
"s3": {
"repo_uri": "s3://my-bucket",
"aws_region": "us-east-1"
},
}
}
metadata = Metadata(config, create_valid_cluster(), None)

mock = mocker.patch("subprocess.run")
metadata.migrate(extra_args=[
"--foo", "bar", # Pair of command and value
"--flag", # Flag with no value afterward
"--bar", "baz", # Another pair of command and value
"bazzy" # Lone value, will be ignored
])

print(mock.call_args_list)

mock.assert_called_once_with([
'/root/metadataMigration/bin/MetadataMigration',
"--snapshot-name", config["from_snapshot"]["snapshot_name"],
'--target-host', 'https://opensearchtarget:9200',
'--min-replicas', '0',
"--s3-local-dir", mocker.ANY,
"--s3-repo-uri", config["from_snapshot"]["s3"]["repo_uri"],
"--s3-region", config["from_snapshot"]["s3"]["aws_region"],
'--target-username', 'admin',
'--target-password', 'myStrongPassword123!',
'--target-insecure',
'--foo', 'bar',
'--flag',
'--bar', 'baz'
], stdout=None, stderr=None, text=True, check=True)