|
| 1 | +# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 4 | +# with the License. A copy of the License is located at |
| 5 | +# |
| 6 | +# http://aws.amazon.com/apache2.0/ |
| 7 | +# |
| 8 | +# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 9 | +# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | +import pytest |
| 12 | + |
| 13 | +from assertpy import assert_that |
| 14 | +from common.schedulers.slurm_commands import PENDING_RESOURCES_REASONS, SlurmJob |
| 15 | +from nodewatcher.plugins.slurm import hasPendingJobs |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "pending_jobs, expected_result", |
| 20 | + [ |
| 21 | + ( |
| 22 | + [SlurmJob(id="72", state="PD", nodes=5, cpus_total=15, cpus_min_per_node=3, pending_reason="Priority")], |
| 23 | + (True, False), |
| 24 | + ), |
| 25 | + ( |
| 26 | + [ |
| 27 | + SlurmJob( |
| 28 | + id="72", state="PD", nodes=5, cpus_total=15, cpus_min_per_node=3, pending_reason="Resources" |
| 29 | + ), # 5 3-slot tasks |
| 30 | + SlurmJob( |
| 31 | + id="73", state="PD", nodes=1, cpus_total=1, cpus_min_per_node=1, pending_reason="Resources" |
| 32 | + ), # 1 1-slot task |
| 33 | + SlurmJob( |
| 34 | + id="74", state="PD", nodes=2, cpus_total=2, cpus_min_per_node=1, pending_reason="Resources" |
| 35 | + ), # 2 1-slot tasks forced on 2 nodes |
| 36 | + SlurmJob( |
| 37 | + id="75", state="PD", nodes=3, cpus_total=12, cpus_min_per_node=4, pending_reason="Resources" |
| 38 | + ), # 3 4-slot tasks |
| 39 | + SlurmJob( |
| 40 | + id="76", state="PD", nodes=1, cpus_total=3, cpus_min_per_node=1, pending_reason="Resources" |
| 41 | + ), # 3 1-slot tasks |
| 42 | + ], |
| 43 | + (True, False), |
| 44 | + ), |
| 45 | + ([], (False, False)), |
| 46 | + (Exception, (False, True)), |
| 47 | + ], |
| 48 | + ids=["single_job", "multiple_jobs", "no_jobs", "failure"], |
| 49 | +) |
| 50 | +def test_has_pending_jobs(pending_jobs, expected_result, mocker): |
| 51 | + if pending_jobs is Exception: |
| 52 | + mock = mocker.patch("nodewatcher.plugins.slurm.get_pending_jobs_info", side_effect=Exception(), autospec=True) |
| 53 | + else: |
| 54 | + mock = mocker.patch("nodewatcher.plugins.slurm.get_pending_jobs_info", return_value=pending_jobs, autospec=True) |
| 55 | + |
| 56 | + instance_properties = {"slots": 4} |
| 57 | + max_cluster_size = 10 |
| 58 | + |
| 59 | + assert_that(hasPendingJobs(instance_properties, max_cluster_size)).is_equal_to(expected_result) |
| 60 | + mock.assert_called_with( |
| 61 | + filter_by_pending_reasons=PENDING_RESOURCES_REASONS, |
| 62 | + max_nodes_filter=max_cluster_size, |
| 63 | + max_slots_filter=instance_properties["slots"], |
| 64 | + ) |
0 commit comments