forked from aws-controllers-k8s/sagemaker-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_hpo.py
181 lines (151 loc) · 5.94 KB
/
test_hpo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Copyright Amazon.com Inc. or its 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. A copy of the
# License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.
"""Integration tests for the SageMaker HyperParameterTuning API.
"""
import botocore
import pytest
import logging
from typing import Dict
from acktest.resources import random_suffix_name
from acktest.k8s import resource as k8s
from e2e import (
service_marker,
create_sagemaker_resource,
delete_custom_resource,
wait_for_status,
sagemaker_client,
assert_tags_in_sync,
)
from e2e.replacement_values import REPLACEMENT_VALUES
from e2e.bootstrap_resources import get_bootstrap_resources
from e2e.common import config as cfg
from time import sleep
RESOURCE_PLURAL = "hyperparametertuningjobs"
@pytest.fixture(scope="function")
def xgboost_hpojob():
resource_name = random_suffix_name("xgboost-hpojob", 32)
replacements = REPLACEMENT_VALUES.copy()
replacements["HPO_JOB_NAME"] = resource_name
reference, _, resource = create_sagemaker_resource(
resource_plural=RESOURCE_PLURAL,
resource_name=resource_name,
spec_file="xgboost_hpojob",
replacements=replacements,
)
assert resource is not None
if k8s.get_resource_arn(resource) is None:
logging.error(
f"ARN for this resource is None, resource status is: {resource['status']}"
)
assert k8s.get_resource_arn(resource) is not None
yield (reference, resource)
assert delete_custom_resource(
reference, cfg.JOB_DELETE_WAIT_PERIODS, cfg.JOB_DELETE_WAIT_LENGTH
)
def get_sagemaker_hpo_job(hpo_job_name: str):
try:
hpo_desc = sagemaker_client().describe_hyper_parameter_tuning_job(
HyperParameterTuningJobName=hpo_job_name
)
return hpo_desc
except botocore.exceptions.ClientError as error:
logging.error(
f"SageMaker could not find an hpo job with the name {hpo_job_name}. Error {error}"
)
return None
def get_hpo_sagemaker_status(hpo_job_name: str):
hpo_sm_desc = get_sagemaker_hpo_job(hpo_job_name)
return hpo_sm_desc["HyperParameterTuningJobStatus"]
def get_hpo_resource_status(reference: k8s.CustomResourceReference):
resource = k8s.get_resource(reference)
assert "hyperParameterTuningJobStatus" in resource["status"]
return resource["status"]["hyperParameterTuningJobStatus"]
@service_marker
class TestHPO:
def _wait_resource_hpo_status(
self,
reference: k8s.CustomResourceReference,
expected_status: str,
wait_periods: int = 45,
period_length: int = 30,
):
return wait_for_status(
expected_status,
wait_periods,
period_length,
get_hpo_resource_status,
reference,
)
def _wait_sagemaker_hpo_status(
self,
hpo_job_name,
expected_status: str,
wait_periods: int = 45,
period_length: int = 30,
):
return wait_for_status(
expected_status,
wait_periods,
period_length,
get_hpo_sagemaker_status,
hpo_job_name,
)
def _assert_hpo_status_in_sync(self, hpo_job_name, reference, expected_status):
assert (
self._wait_sagemaker_hpo_status(hpo_job_name, expected_status)
== self._wait_resource_hpo_status(reference, expected_status)
== expected_status
)
def test_stopped(self, xgboost_hpojob):
(reference, resource) = xgboost_hpojob
assert k8s.get_resource_exists(reference)
hpo_job_name = resource["spec"].get("hyperParameterTuningJobName", None)
assert hpo_job_name is not None
hpo_sm_desc = get_sagemaker_hpo_job(hpo_job_name)
assert (
k8s.get_resource_arn(resource) == hpo_sm_desc["HyperParameterTuningJobArn"]
)
assert hpo_sm_desc["HyperParameterTuningJobStatus"] == cfg.JOB_STATUS_INPROGRESS
assert k8s.wait_on_condition(reference, "ACK.ResourceSynced", "False")
self._assert_hpo_status_in_sync(
hpo_job_name, reference, cfg.JOB_STATUS_INPROGRESS
)
# Delete the k8s resource.
assert delete_custom_resource(
reference, cfg.JOB_DELETE_WAIT_PERIODS, cfg.JOB_DELETE_WAIT_LENGTH
)
hpo_sm_desc = get_sagemaker_hpo_job(hpo_job_name)
assert (
hpo_sm_desc["HyperParameterTuningJobStatus"] in cfg.LIST_JOB_STATUS_STOPPED
)
@pytest.mark.canary
def test_completed(self, xgboost_hpojob):
(reference, resource) = xgboost_hpojob
assert k8s.get_resource_exists(reference)
hpo_job_name = resource["spec"].get("hyperParameterTuningJobName", None)
assert hpo_job_name is not None
hpo_sm_desc = get_sagemaker_hpo_job(hpo_job_name)
hpo_arn = hpo_sm_desc["HyperParameterTuningJobArn"]
assert k8s.get_resource_arn(resource) == hpo_arn
assert hpo_sm_desc["HyperParameterTuningJobStatus"] == cfg.JOB_STATUS_INPROGRESS
assert k8s.wait_on_condition(reference, "ACK.ResourceSynced", "False")
self._assert_hpo_status_in_sync(
hpo_job_name, reference, cfg.JOB_STATUS_COMPLETED
)
assert k8s.wait_on_condition(reference, "ACK.ResourceSynced", "True")
resource_tags = resource["spec"].get("tags", None)
assert_tags_in_sync(hpo_arn, resource_tags)
# Check that you can delete a completed resource from k8s
assert delete_custom_resource(
reference, cfg.JOB_DELETE_WAIT_PERIODS, cfg.JOB_DELETE_WAIT_LENGTH
)