forked from aws-controllers-k8s/sagemaker-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_processingjob.py
180 lines (148 loc) · 6.18 KB
/
test_processingjob.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
# 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 ProcessingJob 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
RESOURCE_PLURAL = "processingjobs"
@pytest.fixture(scope="function")
def kmeans_processing_job():
resource_name = random_suffix_name("kmeans-processingjob", 32)
replacements = REPLACEMENT_VALUES.copy()
replacements["PROCESSING_JOB_NAME"] = resource_name
reference, _, resource = create_sagemaker_resource(
resource_plural=RESOURCE_PLURAL,
resource_name=resource_name,
spec_file="kmeans_processingjob",
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_processing_job(processing_job_name: str):
try:
processing_job = sagemaker_client().describe_processing_job(
ProcessingJobName=processing_job_name
)
return processing_job
except botocore.exceptions.ClientError as error:
logging.error(
f"SageMaker could not find a processing job with the name {processing_job_name}. Error {error}"
)
return None
def get_processing_sagemaker_status(processing_job_name: str):
processing_sm_desc = get_sagemaker_processing_job(processing_job_name)
return processing_sm_desc["ProcessingJobStatus"]
def get_processing_resource_status(reference: k8s.CustomResourceReference):
resource = k8s.get_resource(reference)
assert "processingJobStatus" in resource["status"]
return resource["status"]["processingJobStatus"]
@service_marker
class TestProcessingJob:
def _wait_resource_processing_status(
self,
reference: k8s.CustomResourceReference,
expected_status: str,
wait_periods: int = 60,
period_length: int = 30,
):
return wait_for_status(
expected_status,
wait_periods,
period_length,
get_processing_resource_status,
reference,
)
def _wait_sagemaker_processing_status(
self,
processing_job_name,
expected_status: str,
wait_periods: int = 60,
period_length: int = 30,
):
return wait_for_status(
expected_status,
wait_periods,
period_length,
get_processing_sagemaker_status,
processing_job_name,
)
def _assert_processing_status_in_sync(
self, processing_job_name, reference, expected_status
):
assert (
self._wait_sagemaker_processing_status(processing_job_name, expected_status)
== self._wait_resource_processing_status(reference, expected_status)
== expected_status
)
def test_stopped(self, kmeans_processing_job):
(reference, resource) = kmeans_processing_job
assert k8s.get_resource_exists(reference)
processing_job_name = resource["spec"].get("processingJobName", None)
assert processing_job_name is not None
processing_job_desc = get_sagemaker_processing_job(processing_job_name)
assert k8s.get_resource_arn(resource) == processing_job_desc["ProcessingJobArn"]
assert processing_job_desc["ProcessingJobStatus"] == cfg.JOB_STATUS_INPROGRESS
assert k8s.wait_on_condition(reference, "ACK.ResourceSynced", "False")
self._assert_processing_status_in_sync(
processing_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
)
processing_job_desc = get_sagemaker_processing_job(processing_job_name)
assert processing_job_desc["ProcessingJobStatus"] in cfg.LIST_JOB_STATUS_STOPPED
@pytest.mark.canary
def test_completed(self, kmeans_processing_job):
(reference, resource) = kmeans_processing_job
assert k8s.get_resource_exists(reference)
processing_job_name = resource["spec"].get("processingJobName", None)
assert processing_job_name is not None
processing_job_desc = get_sagemaker_processing_job(processing_job_name)
processing_job_arn = processing_job_desc["ProcessingJobArn"]
assert k8s.get_resource_arn(resource) == processing_job_arn
assert processing_job_desc["ProcessingJobStatus"] == cfg.JOB_STATUS_INPROGRESS
assert k8s.wait_on_condition(reference, "ACK.ResourceSynced", "False")
self._assert_processing_status_in_sync(
processing_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(processing_job_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
)