diff --git a/.pytest.ini b/.pytest.ini new file mode 100644 index 0000000000..7c21b17567 --- /dev/null +++ b/.pytest.ini @@ -0,0 +1,16 @@ +# Copyright 2024 Google LLC +# +# 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. + +[pytest] +filterwarnings = ignore::DeprecationWarning diff --git a/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/README.md b/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/README.md deleted file mode 100644 index 8452813f25..0000000000 --- a/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# Unit tests - -```sh -# cwd is scripts/tests -$ pytest -W ignore::DeprecationWarning -``` diff --git a/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/common.py b/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/common.py new file mode 100644 index 0000000000..11cc491cd2 --- /dev/null +++ b/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/common.py @@ -0,0 +1,53 @@ +# Copyright 2024 "Google LLC" +# +# 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 typing import Optional +import sys +from dataclasses import dataclass, field + +SCRIPTS_DIR = "community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts" +if SCRIPTS_DIR not in sys.path: + sys.path.append(SCRIPTS_DIR) # TODO: make this more robust + + +# TODO: use "real" classes once they are defined (instead of NSDict) +@dataclass +class TstNodeset: + nodeset_name: str + node_count_static: int = 0 + node_count_dynamic_max: int = 0 + + +@dataclass +class TstCfg: + slurm_cluster_name: str = "m22" + nodeset: dict[str, TstNodeset] = field(default_factory=dict) + nodeset_tpu: dict[str, TstNodeset] = field(default_factory=dict) + output_dir: Optional[str] = None + + +@dataclass +class TstTPU: # to prevent client initialization durint "TPU.__init__" + vmcount: int + + +def make_to_hostnames_mock(tbl: Optional[dict[str, list[str]]]): + tbl = tbl or {} + + def se(k: str) -> list[str]: + if k not in tbl: + raise AssertionError(f"to_hostnames mock: unexpected nodelist: '{k}'") + return tbl[k] + + return se diff --git a/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/test_topology.py b/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/test_topology.py index 0b114e03e7..aedd35745a 100644 --- a/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/test_topology.py +++ b/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/test_topology.py @@ -12,51 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Optional import mock -import sys +from common import TstCfg, TstNodeset, TstTPU, make_to_hostnames_mock -if ".." not in sys.path: - sys.path.append("..") # TODO: make this more robust import util import conf - -from dataclasses import dataclass, field import tempfile - -# TODO: use "real" classes once they are defined (instead of NSDict) -@dataclass -class TstNodeset: - nodeset_name: str - node_count_static: int = 0 - node_count_dynamic_max: int = 0 - - -@dataclass -class TstCfg: - slurm_cluster_name: str = "m22" - nodeset: dict[str, TstNodeset] = field(default_factory=dict) - nodeset_tpu: dict[str, TstNodeset] = field(default_factory=dict) - output_dir: Optional[str] = None - - -@dataclass -class TstTPU: # to prevent client initialization durint "TPU.__init__" - vmcount: int - - -def make_to_hostnames_mock(tbl: Optional[dict[str, list[str]]]): - tbl = tbl or {} - - def se(k: str) -> list[str]: - if k not in tbl: - raise AssertionError(f"to_hostnames mock: unexpected nodelist: '{k}'") - return tbl[k] - - return se - - def test_gen_topology_conf_empty(): cfg = TstCfg(output_dir=tempfile.mkdtemp()) conf.gen_topology_conf(util.Lookup(cfg)) diff --git a/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/test_util.py b/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/test_util.py index 5e11c3e65f..cf255669aa 100644 --- a/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/test_util.py +++ b/community/modules/scheduler/schedmd-slurm-gcp-v6-controller/modules/slurm_files/scripts/tests/test_util.py @@ -12,17 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys import pytest - -if ".." not in sys.path: - sys.path.append("..") # TODO: make this more robust +import common # needed to import util import util from google.api_core.client_options import ClientOptions # noqa: E402 # Note: need to install pytest-mock - @pytest.mark.parametrize( "name,expected", [