-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b33b868
commit 8e3e909
Showing
7 changed files
with
222 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# | ||
# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
class Plugin: | ||
""" | ||
A class to represent plugins actions in a test scenario. | ||
Attributes | ||
name (str): Name of the plugin task. | ||
command (str): Shell command to be executed in the plugin. | ||
""" | ||
|
||
def __init__(self, name: str, command: str) -> None: | ||
""" | ||
Initialize a Plugin instance. | ||
Args: | ||
name (str): Name of the plugin task. | ||
command (str): Command to execute as part of the plugin. | ||
""" | ||
self.name = name | ||
self.command = command | ||
|
||
def __repr__(self) -> str: | ||
"""Return a string representation of the Plugin instance.""" | ||
return f"Plugin(name={self.name}, command={self.command})" | ||
|
||
def run(self) -> None: | ||
"""Execute the command in the plugin.""" | ||
print(f"Executing command '{self.command}' for task '{self.name}'") |
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
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES | ||
# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from pathlib import Path | ||
from typing import Generator | ||
from unittest.mock import Mock, patch | ||
|
||
import pytest | ||
from cloudai import Plugin, TestRun | ||
from cloudai.systems.slurm.strategy import SlurmCommandGenStrategy | ||
|
||
|
||
@pytest.fixture | ||
def command_gen_strategy(slurm_system: Mock) -> Generator[SlurmCommandGenStrategy, None, None]: | ||
cmd_args = {"test_arg": "test_value"} | ||
strategy = SlurmCommandGenStrategy(slurm_system, cmd_args) | ||
|
||
with patch.object(strategy, "generate_test_command", return_value="nccl-test-command"): | ||
yield strategy | ||
|
||
|
||
@pytest.fixture | ||
def testrun(tmp_path: Path) -> TestRun: | ||
mock_test_definition = Mock() | ||
mock_test_template = Mock() | ||
|
||
mock_test_definition.name = "test_job" | ||
mock_test_definition.extra_cmd_args = "" | ||
mock_test_template.name = "test_template" | ||
|
||
test = Mock() | ||
test.test_definition = mock_test_definition | ||
test.test_template = mock_test_template | ||
test.extra_cmd_args = "" | ||
|
||
return TestRun( | ||
name="test_job", | ||
test=test, | ||
output_path=tmp_path, | ||
num_nodes=2, | ||
nodes=["node1", "node2"], | ||
prologue=[], | ||
epilogue=[], | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"name,command", | ||
[ | ||
("setup_environment", "module load nccl"), | ||
("cleanup_environment", "module unload nccl"), | ||
], | ||
) | ||
def test_plugin_initialization(name: str, command: str) -> None: | ||
plugin = Plugin(name=name, command=command) | ||
assert plugin.name == name | ||
assert plugin.command == command | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"name,command,expected_repr", | ||
[ | ||
("setup_environment", "module load nccl", "Plugin(name=setup_environment, command=module load nccl)"), | ||
("cleanup_environment", "module unload nccl", "Plugin(name=cleanup_environment, command=module unload nccl)"), | ||
], | ||
) | ||
def test_plugin_repr(name: str, command: str, expected_repr: str) -> None: | ||
plugin = Plugin(name=name, command=command) | ||
assert repr(plugin) == expected_repr | ||
|
||
|
||
@patch("builtins.print") | ||
@pytest.mark.parametrize( | ||
"name,command,expected_output", | ||
[ | ||
("setup_environment", "module load nccl", "Executing command 'module load nccl' for task 'setup_environment'"), | ||
( | ||
"cleanup_environment", | ||
"module unload nccl", | ||
"Executing command 'module unload nccl' for task 'cleanup_environment'", | ||
), | ||
], | ||
) | ||
def test_plugin_run(mock_print: Mock, name: str, command: str, expected_output: str) -> None: | ||
plugin = Plugin(name=name, command=command) | ||
plugin.run() | ||
mock_print.assert_called_once_with(expected_output) | ||
|
||
|
||
def test_generate_srun_command_no_plugins(command_gen_strategy: SlurmCommandGenStrategy, testrun: TestRun) -> None: | ||
srun_command = command_gen_strategy.generate_test_command( | ||
{}, | ||
{"test_arg": "test_value"}, | ||
testrun.test.extra_cmd_args, | ||
) | ||
assert srun_command == "nccl-test-command" |