Skip to content

Commit b19b87b

Browse files
authored
Resource retention Integration test (#194)
Issue #, if available: Description of changes: `test/e2e/resources/notebook_instance_lifecycle_retain.yaml` - Notebook Lifecycle CR with retain annotation `test/e2e/tests/test_retain_policy.py` - Resource retention Integration test. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent eb3671e commit b19b87b

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

test/e2e/common/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
DATA_QUALITY_JOB_DEFINITION_RESOURCE_PLURAL = "dataqualityjobdefinitions"
2020
MODEL_PACKAGE_GROUP_RESOURCE_PLURAL = "modelpackagegroups"
2121
MODEL_PACKAGE_RESOURCE_PLURAL = "modelpackages"
22+
NOTEBOOK_INSTANCE_LIFECYCLE_RESOURCE_PLURAL = "notebookinstancelifecycleconfigs"
2223

2324
# Job Type Resource Statuses
2425
LIST_JOB_STATUS_STOPPED = ("Stopped", "Stopping", "Completed")

test/e2e/resources/notebook_instance_lifecycle_config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ apiVersion: sagemaker.services.k8s.aws/v1alpha1
22
kind: NotebookInstanceLifecycleConfig
33
metadata:
44
name: $NOTEBOOK_INSTANCE_LFC_NAME
5+
annotations:
6+
services.k8s.aws/deletion-policy: $DELETION_POLICY
57
spec:
68
notebookInstanceLifecycleConfigName: $NOTEBOOK_INSTANCE_LFC_NAME
79
onStart:

test/e2e/tests/test_notebook_instance_lifecycle.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,22 @@
3232

3333
from e2e.replacement_values import REPLACEMENT_VALUES
3434
from time import sleep
35+
from e2e.common import config as cfg
3536

3637
DELETE_WAIT_PERIOD = 16
3738
DELETE_PERIOD_LENGTH = 30
39+
UPDATE_WAIT_PERIOD_IN_SECONDS = 5
40+
RESOURCE_PLURAL = cfg.NOTEBOOK_INSTANCE_LIFECYCLE_RESOURCE_PLURAL
3841

3942

4043
@pytest.fixture(scope="module")
4144
def notebook_instance_lifecycleConfig():
4245
notebook_instance_lfc_name = random_suffix_name("notebookinstancelfc", 50)
4346
replacements = REPLACEMENT_VALUES.copy()
4447
replacements["NOTEBOOK_INSTANCE_LFC_NAME"] = notebook_instance_lfc_name
48+
replacements["DELETION_POLICY"] = "delete"
4549
reference, spec, resource = create_sagemaker_resource(
46-
resource_plural="notebookinstancelifecycleconfigs",
50+
resource_plural=RESOURCE_PLURAL,
4751
resource_name=notebook_instance_lfc_name,
4852
spec_file="notebook_instance_lifecycle_config",
4953
replacements=replacements,
@@ -110,6 +114,10 @@ def test_create_update_delete(self, notebook_instance_lifecycleConfig):
110114
# We need to keep track of the current time so its best to just do
111115
# the update test with the create test.
112116
# update content is pip install six
117+
118+
# Sometimes creation can happen in under a second.
119+
sleep(UPDATE_WAIT_PERIOD_IN_SECONDS)
120+
113121
assert "lastModifiedTime" in resource["status"]
114122
last_modified_time = resource["status"]["lastModifiedTime"]
115123
update_content = "cGlwIGluc3RhbGwgc2l4"

test/e2e/tests/test_retain_policy.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
# not use this file except in compliance with the License. A copy of the
5+
# License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is distributed
10+
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
# express or implied. See the License for the specific language governing
12+
# permissions and limitations under the License.
13+
"""Integration tests for the Resource retain policy
14+
"""
15+
16+
import pytest
17+
import logging
18+
import botocore
19+
20+
from acktest.k8s import resource as k8s
21+
from acktest.resources import random_suffix_name
22+
from e2e import (
23+
service_marker,
24+
create_sagemaker_resource,
25+
sagemaker_client,
26+
)
27+
28+
29+
from e2e.replacement_values import REPLACEMENT_VALUES
30+
from e2e.common import config as cfg
31+
32+
RESOURCE_PLURAL = cfg.NOTEBOOK_INSTANCE_LIFECYCLE_RESOURCE_PLURAL
33+
RESOURCE_SPEC_FILE = "notebook_instance_lifecycle_config"
34+
35+
36+
@pytest.fixture(scope="module")
37+
def retained_notebook_instance_lifecycle_config():
38+
notebook_instance_lfc_name = random_suffix_name("notebookinstancelfc", 40)
39+
replacements = REPLACEMENT_VALUES.copy()
40+
replacements["NOTEBOOK_INSTANCE_LFC_NAME"] = notebook_instance_lfc_name
41+
replacements["DELETION_POLICY"] = "retain"
42+
reference, spec, resource = create_sagemaker_resource(
43+
resource_plural=RESOURCE_PLURAL,
44+
resource_name=notebook_instance_lfc_name,
45+
spec_file=RESOURCE_SPEC_FILE,
46+
replacements=replacements,
47+
)
48+
assert resource is not None
49+
yield (reference, resource, spec)
50+
51+
deletion_resp = delete_notebook_instance_lifecycle_config(
52+
notebook_instance_lfc_name
53+
)
54+
assert deletion_resp is not None
55+
56+
57+
def get_notebook_instance_lifecycle_config(notebook_instance_lfc_name: str):
58+
try:
59+
resp = sagemaker_client().describe_notebook_instance_lifecycle_config(
60+
NotebookInstanceLifecycleConfigName=notebook_instance_lfc_name
61+
)
62+
return resp
63+
except botocore.exceptions.ClientError as error:
64+
logging.error(
65+
f"SageMaker could not find a Notebook Instance Lifecycle Configuration with the name {notebook_instance_lfc_name}. Error {error}"
66+
)
67+
return None
68+
69+
70+
def delete_notebook_instance_lifecycle_config(notebook_instance_lfc_name: str):
71+
try:
72+
resp = sagemaker_client().delete_notebook_instance_lifecycle_config(
73+
NotebookInstanceLifecycleConfigName=notebook_instance_lfc_name
74+
)
75+
return resp
76+
except botocore.exceptions.ClientError as error:
77+
logging.error(
78+
f"SageMaker could not find a Notebook Instance Lifecycle Configuration with the name {notebook_instance_lfc_name}. Error {error}"
79+
)
80+
return None
81+
82+
83+
@service_marker
84+
class TestRetainPolicy:
85+
def test_retain_notebook_instance_lifecycle(
86+
self, retained_notebook_instance_lifecycle_config
87+
):
88+
(reference, resource, _) = retained_notebook_instance_lifecycle_config
89+
assert k8s.get_resource_exists(reference)
90+
91+
# Getting the resource name
92+
notebook_instance_lfc_name = resource["spec"].get(
93+
"notebookInstanceLifecycleConfigName", None
94+
)
95+
assert notebook_instance_lfc_name is not None
96+
notebook_instance_lfc_desc = get_notebook_instance_lifecycle_config(
97+
notebook_instance_lfc_name
98+
)
99+
assert (
100+
k8s.get_resource_arn(resource)
101+
== notebook_instance_lfc_desc["NotebookInstanceLifecycleConfigArn"]
102+
)
103+
104+
# Delete the CR in Kubernetes
105+
_, deleted = k8s.delete_custom_resource(reference)
106+
107+
assert deleted
108+
109+
# Verify that the resource was not deleted on SageMaker
110+
assert (
111+
get_notebook_instance_lifecycle_config(notebook_instance_lfc_name)
112+
is not None
113+
)

0 commit comments

Comments
 (0)