Skip to content

Commit 676ae77

Browse files
author
Himani Anil Deshpande
committed
[SlurmTopo] Code linters
1 parent a8af115 commit 676ae77

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

cookbooks/aws-parallelcluster-slurm/files/default/head_node_slurm/slurm/pcluster_topology_generator.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied.
1111
# See the License for the specific language governing permissions and limitations under the License.
1212
import argparse
13-
import yaml
1413
import logging
15-
import traceback
1614
import os
15+
import traceback
16+
17+
import yaml
18+
1719
log = logging.getLogger()
1820

1921

@@ -43,12 +45,12 @@ def _load_cluster_config(input_file_path):
4345
return yaml.load(input_file, Loader=yaml.SafeLoader)
4446

4547

46-
def generate_topology_config_file(output_file: str, input_file: str, block_sizes: str):
48+
def generate_topology_config_file(output_file: str, input_file: str, block_sizes: str): # noqa: C901
4749
"""
4850
Generate Topology configuration file.
4951
5052
Generate topology.conf
51-
53+
5254
# This file is automatically generated by pcluster
5355
BlockName=block1 Nodes=queue-1-st-compute-resource-0-[1-9] #### 9 nodes
5456
BlockName=block2 Nodes=queue-1-st-compute-resource-0-[1-18] #### 18 nodes
@@ -81,15 +83,28 @@ def generate_topology_config_file(output_file: str, input_file: str, block_sizes
8183
else:
8284
continue
8385

84-
### Check for if reservation is for NVLink and size matches min_block_size_list
85-
if compute_resource_config.get('InstanceType') == 'p6e-gb200.36xlarge':
86+
# Check for if reservation is for NVLink and size matches min_block_size_list
87+
if compute_resource_config.get("InstanceType") == "p6e-gb200.36xlarge":
8688
if min_block_size_list == compute_min_count or max_block_size_list == compute_max_count:
8789
block_count += 1
88-
### Each Capacity Reservation ID is a Capacity Block and we associate each slurm block with a single capacity Block
89-
topology_config += "BlockName=Block" + str(block_count)+ " Nodes=" + str(queue_name) + "-" + str(node_type) + "-" + str(compute_resource_name) + "-[1-" + str(compute_max_count) + "]\n"
90-
91-
topology_config += "BlockSizes="+ str(block_sizes)+"\n"
92-
except(KeyError, AttributeError) as e:
90+
# Each Capacity Reservation ID is a Capacity Block,
91+
# we associate each slurm block with a single capacity Block
92+
topology_config += (
93+
"BlockName=Block"
94+
+ str(block_count)
95+
+ " Nodes="
96+
+ str(queue_name)
97+
+ "-"
98+
+ str(node_type)
99+
+ "-"
100+
+ str(compute_resource_name)
101+
+ "-[1-"
102+
+ str(compute_max_count)
103+
+ "]\n"
104+
)
105+
106+
topology_config += "BlockSizes=" + str(block_sizes) + "\n"
107+
except (KeyError, AttributeError) as e:
93108
if isinstance(e, KeyError):
94109
message = f"Unable to find key {e} in the configuration file."
95110
else:

test/unit/slurm/test_topology_generator.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and
1010
# limitations under the License.
1111

12-
import pytest
1312
import os
13+
14+
import pytest
1415
from assertpy import assert_that
1516
from pcluster_topology_generator import (
1617
cleanup_topology_config_file,
@@ -23,27 +24,21 @@ def _assert_files_are_equal(file, expected_file):
2324
assert_that(f.read()).is_equal_to(exp_f.read())
2425

2526

26-
@pytest.mark.parametrize("file_name_suffix", [
27-
"with_capacity_block",
28-
"no_capacity_block"
29-
])
27+
@pytest.mark.parametrize("file_name_suffix", ["with_capacity_block", "no_capacity_block"])
3028
def test_generate_topology_config(test_datadir, tmpdir, file_name_suffix):
31-
block_sizes = "9,18" if 'no' not in file_name_suffix else None
29+
block_sizes = "9,18" if "no" not in file_name_suffix else None
3230
file_name = "sample_" + file_name_suffix + ".yaml"
3331
input_file_path = str(test_datadir / file_name)
3432
output_file_name = "topology_" + file_name_suffix + ".conf"
3533
output_file_path = f"{tmpdir}/{output_file_name}"
3634
generate_topology_config_file(output_file_path, input_file_path, block_sizes)
37-
if 'no' in file_name_suffix:
35+
if "no" in file_name_suffix:
3836
assert_that(os.path.isfile(output_file_path)).is_equal_to(False)
3937
else:
4038
_assert_files_are_equal(output_file_path, test_datadir / "expected_outputs" / output_file_name)
4139

4240

43-
@pytest.mark.parametrize("file_exists", [
44-
True,
45-
False
46-
])
41+
@pytest.mark.parametrize("file_exists", [True, False])
4742
def test_cleanup_topology_config_file(mocker, tmpdir, file_exists):
4843
topology_file_path = tmpdir / "topology.conf"
4944
mocker.patch("os.path.exists", return_value=file_exists)
@@ -53,4 +48,3 @@ def test_cleanup_topology_config_file(mocker, tmpdir, file_exists):
5348
mock_remove.assert_called_once_with(str(topology_file_path))
5449
else:
5550
mock_remove.assert_not_called()
56-

0 commit comments

Comments
 (0)