-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor SlurmSystem methods for bug fix and add unit tests
- Loading branch information
1 parent
67762e4
commit 0779737
Showing
2 changed files
with
131 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,56 @@ | ||
import pytest | ||
from cloudai.schema.system import SlurmSystem | ||
from cloudai.schema.system.slurm import SlurmNode, SlurmNodeState | ||
|
||
|
||
def test_parse_node_list_single() -> None: | ||
"""Test parsing a list with single node names.""" | ||
node_list = ["node1", "node2"] | ||
expected = ["node1", "node2"] | ||
assert SlurmSystem.parse_node_list(node_list) == expected | ||
@pytest.fixture | ||
def slurm_system(): | ||
nodes = [ | ||
SlurmNode(name="nodeA001", partition="main", state=SlurmNodeState.UNKNOWN_STATE), | ||
SlurmNode(name="nodeB001", partition="main", state=SlurmNodeState.UNKNOWN_STATE), | ||
] | ||
system = SlurmSystem( | ||
name="test_system", | ||
install_path="/fake/path", | ||
output_path="/fake/output", | ||
default_partition="main", | ||
partitions={"main": nodes}, | ||
) | ||
return system | ||
|
||
|
||
def test_parse_node_list_range() -> None: | ||
"""Test parsing a list with a range of node names.""" | ||
node_list = ["node[1-3]", "node5"] | ||
expected = ["node1", "node2", "node3", "node5"] | ||
assert SlurmSystem.parse_node_list(node_list) == expected | ||
def test_parse_squeue_output(slurm_system): | ||
squeue_output = "nodeA001|root\nnodeA002|user" | ||
expected_map = {"nodeA001": "root", "nodeA002": "user"} | ||
result = slurm_system.parse_squeue_output(squeue_output) | ||
assert result == expected_map | ||
|
||
|
||
def test_parse_node_list_zero_padding() -> None: | ||
"""Test parsing a list with zero-padded node ranges.""" | ||
node_list = ["node[001-003]", "node005"] | ||
expected = ["node001", "node002", "node003", "node005"] | ||
assert SlurmSystem.parse_node_list(node_list) == expected | ||
def test_parse_squeue_output_with_node_ranges_and_root_user(slurm_system): | ||
squeue_output = "nodeA[001-008],nodeB[001-008]|root" | ||
user_map = slurm_system.parse_squeue_output(squeue_output) | ||
|
||
expected_nodes = [f"nodeA{str(i).zfill(3)}" for i in range(1, 9)] + [f"nodeB{str(i).zfill(3)}" for i in range(1, 9)] | ||
expected_map = {node: "root" for node in expected_nodes} | ||
|
||
assert user_map == expected_map, "All nodes should be mapped to 'root'" | ||
|
||
|
||
def test_parse_sinfo_output(slurm_system): | ||
sinfo_output = ( | ||
"PARTITION AVAIL TIMELIMIT NODES STATE NODELIST\n" | ||
"main up infinite 1 idle nodeA001\n" | ||
"main up infinite 1 idle nodeB001" | ||
) | ||
node_user_map = {"nodeA001": "root", "nodeB001": "user"} | ||
slurm_system.parse_sinfo_output(sinfo_output, node_user_map) | ||
assert slurm_system.partitions["main"][0].state == SlurmNodeState.IDLE | ||
assert slurm_system.partitions["main"][1].state == SlurmNodeState.IDLE | ||
|
||
|
||
def test_update_node_states_with_mocked_outputs(slurm_system): | ||
squeue_output = "nodeA001|root" | ||
sinfo_output = "PARTITION AVAIL TIMELIMIT NODES STATE NODELIST\nmain up infinite 1 idle nodeA001" | ||
slurm_system.update_node_states(squeue_output=squeue_output, sinfo_output=sinfo_output) | ||
assert slurm_system.partitions["main"][0].state == SlurmNodeState.IDLE | ||
assert slurm_system.partitions["main"][0].user == "root" |