Skip to content

Commit 0c30442

Browse files
Ygnasopenshift-merge-bot[bot]
authored andcommitted
update unit tests to work on both Python 3.9 and 3.11
1 parent 757a23c commit 0c30442

File tree

7 files changed

+67
-30
lines changed

7 files changed

+67
-30
lines changed

src/codeflare_sdk/common/kueue/test_kueue.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from ..utils.unit_test_support import get_local_queue, createClusterConfig
14+
from ..utils.unit_test_support import (
15+
apply_template,
16+
get_local_queue,
17+
createClusterConfig,
18+
get_template_variables,
19+
)
1520
from unittest.mock import patch
1621
from codeflare_sdk.ray.cluster.cluster import Cluster, ClusterConfiguration
1722
import yaml
@@ -52,21 +57,21 @@ def test_cluster_creation_no_aw_local_queue(mocker):
5257
config.local_queue = "local-queue-default"
5358
cluster = Cluster(config)
5459
assert cluster.resource_yaml == f"{aw_dir}unit-test-cluster-kueue.yaml"
55-
assert filecmp.cmp(
56-
f"{aw_dir}unit-test-cluster-kueue.yaml",
60+
expected_rc = apply_template(
5761
f"{parent}/tests/test_cluster_yamls/kueue/ray_cluster_kueue.yaml",
58-
shallow=True,
62+
get_template_variables(),
5963
)
6064

65+
with open(f"{aw_dir}unit-test-cluster-kueue.yaml", "r") as f:
66+
cluster_kueue = yaml.load(f, Loader=yaml.FullLoader)
67+
assert cluster_kueue == expected_rc
68+
6169
# With resources loaded in memory, no Local Queue specified.
6270
config = createClusterConfig()
6371
config.name = "unit-test-cluster-kueue"
6472
config.write_to_file = False
6573
cluster = Cluster(config)
66-
67-
with open(f"{parent}/tests/test_cluster_yamls/kueue/ray_cluster_kueue.yaml") as f:
68-
expected_rc = yaml.load(f, Loader=yaml.FullLoader)
69-
assert cluster.resource_yaml == expected_rc
74+
assert cluster.resource_yaml == expected_rc
7075

7176

7277
def test_aw_creation_local_queue(mocker):
@@ -86,22 +91,23 @@ def test_aw_creation_local_queue(mocker):
8691
config.local_queue = "local-queue-default"
8792
cluster = Cluster(config)
8893
assert cluster.resource_yaml == f"{aw_dir}unit-test-aw-kueue.yaml"
89-
assert filecmp.cmp(
90-
f"{aw_dir}unit-test-aw-kueue.yaml",
94+
expected_rc = apply_template(
9195
f"{parent}/tests/test_cluster_yamls/kueue/aw_kueue.yaml",
92-
shallow=True,
96+
get_template_variables(),
9397
)
9498

99+
with open(f"{aw_dir}unit-test-aw-kueue.yaml", "r") as f:
100+
aw_kueue = yaml.load(f, Loader=yaml.FullLoader)
101+
assert aw_kueue == expected_rc
102+
95103
# With resources loaded in memory, no Local Queue specified.
96104
config = createClusterConfig()
97105
config.name = "unit-test-aw-kueue"
98106
config.appwrapper = True
99107
config.write_to_file = False
100108
cluster = Cluster(config)
101109

102-
with open(f"{parent}/tests/test_cluster_yamls/kueue/aw_kueue.yaml") as f:
103-
expected_rc = yaml.load(f, Loader=yaml.FullLoader)
104-
assert cluster.resource_yaml == expected_rc
110+
assert cluster.resource_yaml == expected_rc
105111

106112

107113
def test_get_local_queue_exists_fail(mocker):

src/codeflare_sdk/common/utils/unit_test_support.py

+28
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import string
16+
import sys
1517
from codeflare_sdk.ray.cluster.cluster import (
1618
Cluster,
1719
ClusterConfiguration,
@@ -255,6 +257,32 @@ def arg_check_del_effect(group, version, namespace, plural, name, *args):
255257
assert name == "ray-dashboard-unit-test-cluster-ray"
256258

257259

260+
def apply_template(yaml_file_path, variables):
261+
with open(yaml_file_path, "r") as file:
262+
yaml_content = file.read()
263+
264+
# Create a Template instance and substitute the variables
265+
template = string.Template(yaml_content)
266+
filled_yaml = template.substitute(variables)
267+
268+
# Now load the filled YAML into a Python object
269+
return yaml.load(filled_yaml, Loader=yaml.FullLoader)
270+
271+
272+
def get_expected_image():
273+
python_version = sys.version_info
274+
if python_version.major == 3 and python_version.minor == 9:
275+
return "quay.io/modh/ray@sha256:0d715f92570a2997381b7cafc0e224cfa25323f18b9545acfd23bc2b71576d06"
276+
else:
277+
return "quay.io/modh/ray@sha256:db667df1bc437a7b0965e8031e905d3ab04b86390d764d120e05ea5a5c18d1b4"
278+
279+
280+
def get_template_variables():
281+
return {
282+
"image": get_expected_image(),
283+
}
284+
285+
258286
def arg_check_apply_effect(group, version, namespace, plural, body, *args):
259287
assert namespace == "ns"
260288
assert args == tuple()

src/codeflare_sdk/ray/cluster/test_config.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
# limitations under the License.
1414

1515
from codeflare_sdk.common.utils.unit_test_support import (
16+
apply_template,
1617
createClusterWrongType,
17-
get_local_queue,
1818
create_cluster_all_config_params,
19+
get_template_variables,
1920
)
2021
from codeflare_sdk.ray.cluster.cluster import ClusterConfiguration, Cluster
2122
from pathlib import Path
2223
import filecmp
2324
import pytest
24-
import yaml
2525
import os
2626

2727
parent = Path(__file__).resolve().parents[4] # project directory
@@ -36,9 +36,11 @@ def test_default_cluster_creation(mocker):
3636

3737
cluster = Cluster(ClusterConfiguration(name="default-cluster", namespace="ns"))
3838

39-
with open(f"{expected_clusters_dir}/ray/default-ray-cluster.yaml") as f:
40-
expected_rc = yaml.load(f, Loader=yaml.FullLoader)
41-
assert cluster.resource_yaml == expected_rc
39+
expected_rc = apply_template(
40+
f"{expected_clusters_dir}/ray/default-ray-cluster.yaml",
41+
get_template_variables(),
42+
)
43+
assert cluster.resource_yaml == expected_rc
4244

4345

4446
def test_default_appwrapper_creation(mocker):
@@ -50,9 +52,10 @@ def test_default_appwrapper_creation(mocker):
5052
ClusterConfiguration(name="default-appwrapper", namespace="ns", appwrapper=True)
5153
)
5254

53-
with open(f"{expected_clusters_dir}/ray/default-appwrapper.yaml") as f:
54-
expected_aw = yaml.load(f, Loader=yaml.FullLoader)
55-
assert cluster.resource_yaml == expected_aw
55+
expected_aw = apply_template(
56+
f"{expected_clusters_dir}/ray/default-appwrapper.yaml", get_template_variables()
57+
)
58+
assert cluster.resource_yaml == expected_aw
5659

5760

5861
def test_config_creation_all_parameters(mocker):

tests/test_cluster_yamls/kueue/aw_kueue.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ spec:
3838
template:
3939
spec:
4040
containers:
41-
- image: quay.io/modh/ray@sha256:0d715f92570a2997381b7cafc0e224cfa25323f18b9545acfd23bc2b71576d06
41+
- image: "${image}"
4242
imagePullPolicy: Always
4343
lifecycle:
4444
preStop:
@@ -103,7 +103,7 @@ spec:
103103
template:
104104
spec:
105105
containers:
106-
- image: quay.io/modh/ray@sha256:0d715f92570a2997381b7cafc0e224cfa25323f18b9545acfd23bc2b71576d06
106+
- image: "${image}"
107107
imagePullPolicy: Always
108108
lifecycle:
109109
preStop:

tests/test_cluster_yamls/kueue/ray_cluster_kueue.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ spec:
3838
template:
3939
spec:
4040
containers:
41-
- image: quay.io/modh/ray@sha256:0d715f92570a2997381b7cafc0e224cfa25323f18b9545acfd23bc2b71576d06
41+
- image: "${image}"
4242
imagePullPolicy: Always
4343
lifecycle:
4444
preStop:
@@ -103,7 +103,7 @@ spec:
103103
template:
104104
spec:
105105
containers:
106-
- image: quay.io/modh/ray@sha256:0d715f92570a2997381b7cafc0e224cfa25323f18b9545acfd23bc2b71576d06
106+
- image: "${image}"
107107
imagePullPolicy: Always
108108
lifecycle:
109109
preStop:

tests/test_cluster_yamls/ray/default-appwrapper.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ spec:
3636
template:
3737
spec:
3838
containers:
39-
- image: quay.io/modh/ray@sha256:0d715f92570a2997381b7cafc0e224cfa25323f18b9545acfd23bc2b71576d06
39+
- image: "${image}"
4040
imagePullPolicy: Always
4141
lifecycle:
4242
preStop:
@@ -101,7 +101,7 @@ spec:
101101
template:
102102
spec:
103103
containers:
104-
- image: quay.io/modh/ray@sha256:0d715f92570a2997381b7cafc0e224cfa25323f18b9545acfd23bc2b71576d06
104+
- image: "${image}"
105105
imagePullPolicy: Always
106106
lifecycle:
107107
preStop:

tests/test_cluster_yamls/ray/default-ray-cluster.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ spec:
2828
template:
2929
spec:
3030
containers:
31-
- image: quay.io/modh/ray@sha256:0d715f92570a2997381b7cafc0e224cfa25323f18b9545acfd23bc2b71576d06
31+
- image: "${image}"
3232
imagePullPolicy: Always
3333
lifecycle:
3434
preStop:
@@ -93,7 +93,7 @@ spec:
9393
template:
9494
spec:
9595
containers:
96-
- image: quay.io/modh/ray@sha256:0d715f92570a2997381b7cafc0e224cfa25323f18b9545acfd23bc2b71576d06
96+
- image: "${image}"
9797
imagePullPolicy: Always
9898
lifecycle:
9999
preStop:

0 commit comments

Comments
 (0)