Skip to content

Commit 321f5ed

Browse files
author
Himani Anil Deshpande
committed
Add unit tests
1 parent a4bb289 commit 321f5ed

File tree

1 file changed

+113
-1
lines changed

1 file changed

+113
-1
lines changed

test/unit/slurm/test_fleet_config_generator.py

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import pytest
1414
from assertpy import assert_that
15-
from pcluster_fleet_config_generator import ConfigurationFieldNotFoundError, CriticalError, generate_fleet_config_file
15+
from pcluster_fleet_config_generator import ConfigurationFieldNotFoundError, CriticalError, generate_fleet_config_file, get_total_min_count
1616

1717

1818
@pytest.mark.parametrize(
@@ -266,3 +266,115 @@ def _assert_files_are_equal(file, expected_file):
266266
expected_file_content = exp_f.read()
267267
expected_file_content = expected_file_content.replace("<DIR>", os.path.dirname(file))
268268
assert_that(f.read()).is_equal_to(expected_file_content)
269+
270+
@pytest.mark.parametrize(
271+
"cluster_config, expected_exception, expected_message, expected_total_min_count",
272+
[
273+
({}, CriticalError, "Unable to find key 'Scheduling' in the configuration file", 0),
274+
({"Scheduling": {}}, CriticalError, "Unable to find key 'SlurmQueues' in the configuration file", 0),
275+
({"Scheduling": {"SlurmQueues": []}}, None, None, 0),
276+
(
277+
{
278+
"Scheduling": {
279+
"SlurmQueues": [
280+
{
281+
"Name": "q1",
282+
"CapacityType": "ONDEMAND",
283+
"ComputeResources": [
284+
{"MinCount": 0, "Instances": [{"InstanceType": "test"}]},
285+
],
286+
}
287+
]
288+
}
289+
},
290+
None,
291+
None,0,
292+
),
293+
(
294+
{
295+
"Scheduling": {
296+
"SlurmQueues": [
297+
{
298+
"Name": "q1",
299+
"CapacityType": "ONDEMAND",
300+
"ComputeResources": [
301+
{"MinCount": 2, "Instances": [{"InstanceType": "test"}]},
302+
{"MinCount": 3, "InstanceType": "test"},
303+
],
304+
}
305+
]
306+
}
307+
},
308+
None,
309+
None,5,
310+
),
311+
(
312+
{
313+
"Scheduling": {
314+
"SlurmQueues": [
315+
{
316+
"Name": "q1",
317+
"CapacityType": "SPOT",
318+
"ComputeResources": [
319+
{
320+
"Name": "cr1",
321+
"Instances": [{"InstanceType": "test"}, {"InstanceType": "test-2"}],
322+
"MinCount": 3,
323+
"SpotPrice": "10",
324+
},
325+
{"Name": "cr2", "InstanceType": "test", "SpotPrice": "10", "MinCount": 9},
326+
],
327+
"Networking": {"SubnetIds": ["123", "456", "789"]},
328+
}
329+
]
330+
}
331+
},
332+
None,
333+
None,12,
334+
),
335+
(
336+
{
337+
"Scheduling": {
338+
"SlurmQueues": [
339+
{
340+
"Name": "q1",
341+
"CapacityType": "CAPACITY_BLOCK",
342+
"ComputeResources": [
343+
{
344+
"Name": "cr1",
345+
"Instances": [{"InstanceType": "test"}],
346+
"MinCount":2,
347+
"CapacityReservationTarget": {
348+
"CapacityReservationResourceGroupArn": "arn",
349+
},
350+
},
351+
{
352+
"Name": "cr2",
353+
"MinCount":2,
354+
"Instances": [{"InstanceType": "test"}],
355+
"CapacityReservationTarget": {
356+
"CapacityReservationId": "id",
357+
},
358+
},
359+
],
360+
"Networking": {"SubnetIds": ["123"]},
361+
}
362+
]
363+
}
364+
},
365+
None,
366+
None,4,
367+
),
368+
],
369+
)
370+
def test_get_total_min_count(mocker, tmpdir, cluster_config, expected_exception, expected_message, expected_total_min_count):
371+
mocker.patch("pcluster_fleet_config_generator._load_cluster_config", return_value=cluster_config)
372+
373+
if expected_message:
374+
with pytest.raises(expected_exception, match=expected_message):
375+
actual_min_count = get_total_min_count(input_file="fake")
376+
assert_that(actual_min_count).is_equal_to(expected_total_min_count)
377+
378+
else:
379+
actual_min_count = get_total_min_count(input_file="fake")
380+
assert_that(actual_min_count).is_equal_to(expected_total_min_count)

0 commit comments

Comments
 (0)