forked from aws-controllers-k8s/sagemaker-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_monitoring_schedule.py
210 lines (176 loc) · 6.74 KB
/
test_monitoring_schedule.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# 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 MonitoringSchedule API.
"""
import botocore
import time
import pytest
import logging
from e2e import (
service_marker,
create_sagemaker_resource,
delete_custom_resource,
wait_for_status,
assert_tags_in_sync,
)
from e2e.replacement_values import REPLACEMENT_VALUES
from e2e.common import config as cfg
from e2e.common.fixtures import (
xgboost_churn_data_quality_job_definition,
xgboost_churn_endpoint,
)
from acktest.k8s import resource as k8s
from acktest.resources import random_suffix_name
RESOURCE_PLURAL = "monitoringschedules"
# Access variable so it is loaded as a fixture
_accessed = xgboost_churn_data_quality_job_definition, xgboost_churn_endpoint
@pytest.fixture(scope="module")
def xgboost_churn_data_quality_monitoring_schedule(
xgboost_churn_data_quality_job_definition,
):
(_, job_definition_resource) = xgboost_churn_data_quality_job_definition
job_definition_name = job_definition_resource["spec"].get("jobDefinitionName")
monitoring_schedule_name = random_suffix_name("monitoring-schedule", 32)
replacements = REPLACEMENT_VALUES.copy()
replacements["MONITORING_SCHEDULE_NAME"] = monitoring_schedule_name
replacements["JOB_DEFINITION_NAME"] = job_definition_name
replacements["MONITORING_TYPE"] = "DataQuality"
reference, spec, resource = create_sagemaker_resource(
resource_plural=RESOURCE_PLURAL,
resource_name=monitoring_schedule_name,
spec_file="monitoring_schedule_base",
replacements=replacements,
)
assert resource is not None
yield (reference, resource, spec)
assert delete_custom_resource(
reference, cfg.DELETE_WAIT_PERIOD, cfg.DELETE_WAIT_LENGTH
)
def get_sagemaker_monitoring_schedule(sagemaker_client, monitoring_schedule_name):
try:
response = sagemaker_client.describe_monitoring_schedule(
MonitoringScheduleName=monitoring_schedule_name
)
return response
except botocore.exceptions.ClientError as error:
logging.error(
f"Could not find Monitoring Schedule with name {monitoring_schedule_name}. Error {error}"
)
return None
def get_monitoring_schedule_sagemaker_status(
sagemaker_client, monitoring_schedule_name
):
return sagemaker_client.describe_monitoring_schedule(
MonitoringScheduleName=monitoring_schedule_name
)["MonitoringScheduleStatus"]
def get_monitoring_schedule_resource_status(reference: k8s.CustomResourceReference):
resource = k8s.get_resource(reference)
assert "monitoringScheduleStatus" in resource["status"]
return resource["status"]["monitoringScheduleStatus"]
def wait_sagemaker_monitoring_schedule_status(
sagemaker_client,
monitoring_schedule_name,
expected_status: str,
wait_periods: int = 6,
period_length: int = 30,
):
return wait_for_status(
expected_status,
wait_periods,
period_length,
get_monitoring_schedule_sagemaker_status,
sagemaker_client,
monitoring_schedule_name,
)
def wait_resource_monitoring_schedule_status(
reference: k8s.CustomResourceReference,
expected_status: str,
wait_periods: int = 6,
period_length: int = 30,
):
return wait_for_status(
expected_status,
wait_periods,
period_length,
get_monitoring_schedule_resource_status,
reference,
)
@service_marker
@pytest.mark.canary
class TestMonitoringSchedule:
STATUS_PENDING: str = "Pending"
STATUS_SCHEDULED: str = "Scheduled"
def _assert_monitoring_schedule_status_in_sync(
self,
sagemaker_client,
schedule_name,
reference,
expected_status,
wait_periods: int = 7,
period_length: int = 30,
):
assert (
wait_sagemaker_monitoring_schedule_status(
sagemaker_client, schedule_name, expected_status
)
== wait_resource_monitoring_schedule_status(reference, expected_status, 2)
== expected_status
)
def test_smoke(
self, sagemaker_client, xgboost_churn_data_quality_monitoring_schedule
):
(reference, resource, spec) = xgboost_churn_data_quality_monitoring_schedule
assert k8s.get_resource_exists(reference)
monitoring_schedule_name = resource["spec"].get("monitoringScheduleName")
monitoring_schedule_desc = get_sagemaker_monitoring_schedule(
sagemaker_client, monitoring_schedule_name
)
monitoring_schedule_arn = monitoring_schedule_desc["MonitoringScheduleArn"]
assert k8s.get_resource_arn(resource) == monitoring_schedule_arn
self._assert_monitoring_schedule_status_in_sync(
sagemaker_client, monitoring_schedule_name, reference, self.STATUS_SCHEDULED
)
assert k8s.wait_on_condition(reference, "ACK.ResourceSynced", "True")
resource_tags = resource["spec"].get("tags", None)
assert_tags_in_sync(monitoring_schedule_arn, resource_tags)
# Update the resource
new_cron_expression = "cron(0 * * * ? *)"
spec["spec"]["monitoringScheduleConfig"]["scheduleConfig"][
"scheduleExpression"
] = new_cron_expression
resource = k8s.patch_custom_resource(reference, spec)
resource = k8s.wait_resource_consumed_by_controller(reference)
assert resource is not None
self._assert_monitoring_schedule_status_in_sync(
sagemaker_client, monitoring_schedule_name, reference, self.STATUS_SCHEDULED
)
assert k8s.wait_on_condition(reference, "ACK.ResourceSynced", "True")
latest_schedule = get_sagemaker_monitoring_schedule(
sagemaker_client, monitoring_schedule_name
)
assert (
latest_schedule["MonitoringScheduleConfig"]["ScheduleConfig"][
"ScheduleExpression"
]
== new_cron_expression
)
# Delete the k8s resource.
assert delete_custom_resource(
reference, cfg.DELETE_WAIT_PERIOD, cfg.DELETE_WAIT_LENGTH
)
assert (
get_sagemaker_monitoring_schedule(
sagemaker_client, monitoring_schedule_name
)
is None
)