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: type parsing for ces,actions,credentials #118

Merged
merged 1 commit into from
Feb 28, 2024
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
51 changes: 19 additions & 32 deletions seqerakit/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ def parse_all_yaml(file_paths, destroy=False):
def parse_block(block_name, item):
# Define the mapping from block names to functions.
block_to_function = {
"credentials": parse_credentials_block,
"compute-envs": parse_compute_envs_block,
"credentials": parse_type_block,
"compute-envs": parse_type_block,
"actions": parse_type_block,
"teams": parse_teams_block,
"actions": parse_actions_block,
"datasets": parse_datasets_block,
"pipelines": parse_pipelines_block,
"launch": parse_launch_block,
Expand All @@ -141,27 +141,28 @@ def parse_generic_block(item):
return cmd_args


def parse_credentials_block(item):
def parse_type_block(item, priority_keys=["type", "config-mode", "file-path"]):
cmd_args = []
for key, value in item.items():
if key == "type":
cmd_args.append(str(value))
elif isinstance(value, bool):
if value:
cmd_args.append(f"--{key}")
else:
cmd_args.extend([f"--{key}", str(value)])
return cmd_args

# Ensure at least one of 'type' or 'file-path' is present
if not any(key in item for key in ["type", "file-path"]):
raise ValueError(
"Please specify at least 'type' or 'file-path' for creating the resource."
)

# Process priority keys first
for key in priority_keys:
if key in item:
cmd_args.append(str(item[key]))
del item[key] # Remove the key to avoid repeating in args

def parse_compute_envs_block(item):
cmd_args = []
for key, value in item.items():
if key == "file-path" or key == "type" or key == "config-mode":
cmd_args.append(str(value))
elif isinstance(value, bool):
if isinstance(value, bool):
if value:
cmd_args.append(f"--{key}")
elif key == "params":
temp_file_name = utils.create_temp_yaml(value)
cmd_args.extend(["--params-file", temp_file_name])
else:
cmd_args.extend([f"--{key}", str(value)])
return cmd_args
Expand Down Expand Up @@ -194,20 +195,6 @@ def parse_teams_block(item):
return (cmd_args, members_cmd_args)


def parse_actions_block(item):
cmd_args = []
temp_file_name = None
for key, value in item.items():
if key == "type":
cmd_args.append(str(value))
elif key == "params":
temp_file_name = utils.create_temp_yaml(value)
cmd_args.extend(["--params-file", temp_file_name])
else:
cmd_args.extend([f"--{key}", str(value)])
return cmd_args


def parse_datasets_block(item):
cmd_args = []
for key, value in item.items():
Expand Down
65 changes: 61 additions & 4 deletions tests/unit/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ def test_create_mock_dataset_yaml(mock_yaml_file):
assert result["datasets"] == expected_block_output


def test_create_mock_computeevs_yaml(mock_yaml_file):
def test_create_mock_computeevs_source_yaml(mock_yaml_file):
test_data = {
"compute-envs": [
{
"name": "test_computeenv",
"workspace": "my_organization/my_workspace",
"credentials": "my_credentials",
"file-path": "./examples/yaml/computeenvs/computeenvs.yaml",
"file-path": "./computeenvs/computeenv.json",
"wait": "AVAILABLE",
"fusion-v2": True,
"fargate": False,
Expand All @@ -149,9 +149,9 @@ def test_create_mock_computeevs_yaml(mock_yaml_file):
expected_block_output = [
{
"cmd_args": [
"./computeenvs/computeenv.json",
"--credentials",
"my_credentials",
"./examples/yaml/computeenvs/computeenvs.yaml",
"--fusion-v2",
"--name",
"test_computeenv",
Expand All @@ -171,6 +171,43 @@ def test_create_mock_computeevs_yaml(mock_yaml_file):
assert result["compute-envs"] == expected_block_output


def test_create_mock_computeevs_cli_yaml(mock_yaml_file):
test_data = {
"compute-envs": [
{
"name": "test_computeenv",
"workspace": "my_organization/my_workspace",
"credentials": "my_credentials",
"type": "aws-batch",
"config-mode": "forge",
"wait": "AVAILABLE",
}
],
}

expected_block_output = [
{
"cmd_args": [
"aws-batch",
"forge",
"--credentials",
"my_credentials",
"--name",
"test_computeenv",
"--wait",
"AVAILABLE",
"--workspace",
"my_organization/my_workspace",
],
"overwrite": False,
}
]
file_path = mock_yaml_file(test_data)
result = helper.parse_all_yaml([file_path])
assert "compute-envs" in result
assert result["compute-envs"] == expected_block_output


def test_create_mock_pipeline_add_yaml(mock_yaml_file):
test_data = {
"pipelines": [
Expand All @@ -191,7 +228,6 @@ def test_create_mock_pipeline_add_yaml(mock_yaml_file):
}
]
}

# params file cmds parsed separately
expected_block_output = [
{
Expand Down Expand Up @@ -239,3 +275,24 @@ def test_empty_yaml_file(mock_yaml_file):
assert f"The file '{file_path}' is empty or does not contain valid data." in str(
e.value
)


def test_error_type_yaml_file(mock_yaml_file):
test_data = {
"compute-envs": [
{
"name": "test_computeenv",
"workspace": "my_organization/my_workspace",
"credentials": "my_credentials",
"wait": "AVAILABLE",
}
],
}
file_path = mock_yaml_file(test_data)

with pytest.raises(ValueError) as e:
helper.parse_all_yaml([file_path])
assert (
"Please specify at least 'type' or 'file-path' for creating the resource."
in str(e.value)
)
Loading