-
Notifications
You must be signed in to change notification settings - Fork 316
/
e2e_base.py
101 lines (81 loc) · 3.28 KB
/
e2e_base.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
# -*- coding: utf-8 -*-
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.
#
import abc
import importlib
import os
import pytest
import uuid
from typing import Any, Dict, Generator
from google.api_core import exceptions
from google.cloud import aiplatform
from google.cloud import storage
from google.cloud.aiplatform import initializer
_PROJECT = os.getenv("BUILD_SPECIFIC_GCLOUD_PROJECT")
_LOCATION = "us-central1"
class TestEndToEnd(metaclass=abc.ABCMeta):
@property
@classmethod
@abc.abstractmethod
def _temp_prefix(cls) -> str:
"""Prefix to staging bucket and display names created by this end-to-end test.
Keep the string as short as possible and use kebab case, starting with a lowercase letter.
Example: `"temp-vertex-hpt-test"`
"""
pass
def setup_method(self):
importlib.reload(initializer)
importlib.reload(aiplatform)
@pytest.fixture()
def shared_state(self) -> Generator[Dict[str, Any], None, None]:
shared_state = {}
yield shared_state
@pytest.fixture()
def prepare_staging_bucket(
self, shared_state: Dict[str, Any]
) -> Generator[storage.bucket.Bucket, None, None]:
"""Create a staging bucket and store bucket resource object in shared state."""
staging_bucket_name = f"{self._temp_prefix.lower()}-{uuid.uuid4()}"[:63]
shared_state["staging_bucket_name"] = staging_bucket_name
storage_client = storage.Client(project=_PROJECT)
shared_state["storage_client"] = storage_client
shared_state["bucket"] = storage_client.create_bucket(
staging_bucket_name, location=_LOCATION
)
yield
@pytest.fixture()
def delete_staging_bucket(self, shared_state: Dict[str, Any]):
"""Delete the staging bucket and all it's contents"""
yield
# Get the staging bucket used for testing and wipe it
bucket = shared_state["bucket"]
bucket.delete(force=True)
@pytest.fixture(autouse=True)
def teardown(self, shared_state: Dict[str, Any]):
"""Delete every Vertex AI resource created during test"""
yield
# Bring all Endpoints to the front of the list
# Ensures Models are undeployed first before we attempt deletion
shared_state["resources"].sort(
key=lambda r: 1 if isinstance(r, aiplatform.Endpoint) else 2
)
for resource in shared_state["resources"]:
try:
if isinstance(resource, aiplatform.Endpoint):
resource.delete(force=True) # Undeploy model then delete endpoint
else:
resource.delete()
except exceptions.GoogleAPIError as e:
print(f"Could not delete resource: {resource} due to: {e}")