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

Ensure proper conversion of 'cache_docker_images_locally' to boolean #67

Merged
merged 1 commit into from
Jun 4, 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
5 changes: 4 additions & 1 deletion src/cloudai/parser/system_parser/slurm_system_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def safe_int(value):
except ValueError:
return None

def str_to_bool(value: str) -> bool:
return value.lower() in ("true", "1", "yes")

name = data.get("name")
if not name:
raise ValueError("Missing mandatory field: 'name'")
Expand Down Expand Up @@ -78,7 +81,7 @@ def safe_int(value):
gpus_per_node = safe_int(data.get("gpus_per_node"))
ntasks_per_node = safe_int(data.get("ntasks_per_node"))

cache_docker_images_locally = data.get("cache_docker_images_locally", False)
cache_docker_images_locally = str_to_bool(data.get("cache_docker_images_locally", "False"))

nodes_dict: Dict[str, SlurmNode] = {}
updated_partitions: Dict[str, List[SlurmNode]] = {}
Expand Down
84 changes: 84 additions & 0 deletions tests/test_slurm_system_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from typing import Any, Dict

import pytest
from cloudai.parser.system_parser.slurm_system_parser import SlurmSystemParser
from cloudai.systems.slurm import SlurmSystem


@pytest.fixture
def example_data() -> Dict[str, Any]:
return {
"name": "test_system",
"install_path": "/fake/path",
"output_path": "/fake/output",
"default_partition": "main",
"partitions": {
"main": {
"name": "main",
"nodes": ["node-[033-034]"],
"groups": {"group1": {"name": "group1", "nodes": ["node-033"]}},
},
"backup": {
"name": "backup",
"nodes": ["node-[01-02]"],
"groups": {"group2": {"name": "group2", "nodes": ["node-01"]}},
},
},
"cache_docker_images_locally": "True",
}


def test_parse_slurm_system_parser(example_data):
parser = SlurmSystemParser()
slurm_system = parser.parse(example_data)

assert isinstance(slurm_system, SlurmSystem)
assert slurm_system.name == "test_system"
assert slurm_system.install_path == "/fake/path"
assert slurm_system.output_path == "/fake/output"
assert slurm_system.default_partition == "main"
assert slurm_system.cache_docker_images_locally is True
assert "main" in slurm_system.partitions
assert "backup" in slurm_system.partitions
assert "group1" in slurm_system.groups["main"]
assert "group2" in slurm_system.groups["backup"]


@pytest.mark.parametrize(
"input_value, expected_result",
[
("True", True),
("False", False),
("true", True),
("false", False),
("1", True),
("0", False),
("yes", True),
("no", False),
],
)
def test_str_to_bool_conversion(input_value, expected_result):
parser = SlurmSystemParser()
result = parser.parse(
{
"name": "test_system",
"install_path": "/fake/path",
"output_path": "/fake/output",
"default_partition": "main",
"partitions": {
"main": {
"name": "main",
"nodes": ["node-[033-034]"],
"groups": {"group1": {"name": "group1", "nodes": ["node-033"]}},
},
"backup": {
"name": "backup",
"nodes": ["node-[01-02]"],
"groups": {"group2": {"name": "group2", "nodes": ["node-01"]}},
},
},
"cache_docker_images_locally": input_value,
}
)

assert result.cache_docker_images_locally == expected_result