|
| 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