|
12 | 12 | # language governing permissions and limitations under the License.
|
13 | 13 | from __future__ import absolute_import
|
14 | 14 |
|
15 |
| -import pytest |
16 |
| - |
17 |
| -from sagemaker import get_execution_role |
18 |
| -from sklearn.datasets import load_iris |
19 |
| -from sklearn.model_selection import train_test_split |
20 |
| - |
21 | 15 | import os
|
| 16 | +import uuid |
| 17 | +from typing import Generator |
22 | 18 |
|
| 19 | +import numpy as np |
| 20 | +import pandas as pd |
| 21 | +import pytest |
| 22 | +from sagemaker_core.main.resources import TrainingJob |
23 | 23 | from sagemaker_core.main.shapes import (
|
24 | 24 | AlgorithmSpecification,
|
25 | 25 | Channel,
|
26 | 26 | DataSource,
|
27 |
| - S3DataSource, |
28 | 27 | OutputDataConfig,
|
29 | 28 | ResourceConfig,
|
| 29 | + S3DataSource, |
30 | 30 | StoppingCondition,
|
31 | 31 | )
|
32 |
| -import uuid |
33 |
| -from sagemaker.serve.builder.model_builder import ModelBuilder |
34 |
| -import pandas as pd |
35 |
| -import numpy as np |
36 |
| -from sagemaker.serve import InferenceSpec, SchemaBuilder |
37 |
| -from sagemaker_core.main.resources import TrainingJob |
| 32 | +from sklearn.datasets import load_iris |
| 33 | +from sklearn.model_selection import train_test_split |
38 | 34 | from xgboost import XGBClassifier
|
39 | 35 |
|
40 |
| -from sagemaker.serverless.serverless_inference_config import ServerlessInferenceConfig |
41 |
| - |
42 |
| -from sagemaker.s3_utils import s3_path_join |
| 36 | +from sagemaker import get_execution_role |
43 | 37 | from sagemaker.async_inference import AsyncInferenceConfig
|
| 38 | +from sagemaker.s3_utils import s3_path_join |
| 39 | +from sagemaker.serve import InferenceSpec, SchemaBuilder |
| 40 | +from sagemaker.serve.builder.model_builder import ModelBuilder |
| 41 | +from sagemaker.serverless.serverless_inference_config import ServerlessInferenceConfig |
44 | 42 | from tests.integ.utils import cleanup_model_resources
|
45 | 43 |
|
46 | 44 |
|
| 45 | +@pytest.fixture(autouse=True) |
| 46 | +def cleanup_endpoints(mb_sagemaker_session) -> Generator[None, None, None]: |
| 47 | + """Clean up any existing endpoints before and after tests.""" |
| 48 | + sagemaker_client = mb_sagemaker_session.sagemaker_client |
| 49 | + |
| 50 | + # Pre-test cleanup |
| 51 | + try: |
| 52 | + endpoints = sagemaker_client.list_endpoints() |
| 53 | + for endpoint in endpoints["Endpoints"]: |
| 54 | + try: |
| 55 | + sagemaker_client.delete_endpoint(EndpointName=endpoint["EndpointName"]) |
| 56 | + sagemaker_client.delete_endpoint_config( |
| 57 | + EndpointConfigName=endpoint["EndpointConfigName"] |
| 58 | + ) |
| 59 | + except Exception as e: |
| 60 | + print(f"Error cleaning up endpoint {endpoint['EndpointName']}: {e}") |
| 61 | + except Exception as e: |
| 62 | + print(f"Error listing endpoints: {e}") |
| 63 | + |
| 64 | + yield |
| 65 | + |
| 66 | + # Post-test cleanup |
| 67 | + try: |
| 68 | + endpoints = sagemaker_client.list_endpoints() |
| 69 | + for endpoint in endpoints["Endpoints"]: |
| 70 | + try: |
| 71 | + sagemaker_client.delete_endpoint(EndpointName=endpoint["EndpointName"]) |
| 72 | + sagemaker_client.delete_endpoint_config( |
| 73 | + EndpointConfigName=endpoint["EndpointConfigName"] |
| 74 | + ) |
| 75 | + except Exception as e: |
| 76 | + print(f"Error cleaning up endpoint {endpoint['EndpointName']}: {e}") |
| 77 | + except Exception as e: |
| 78 | + print(f"Error listing endpoints: {e}") |
| 79 | + |
| 80 | + |
47 | 81 | @pytest.fixture(scope="module")
|
48 | 82 | def xgboost_model_builder(mb_sagemaker_session):
|
49 | 83 | sagemaker_session = mb_sagemaker_session
|
|
0 commit comments