|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# pylint: disable=protected-access,bad-continuation,missing-function-docstring |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | + |
| 20 | +from tests.unit.vertexai.genai.replays import pytest_helper |
| 21 | +from vertexai._genai import types |
| 22 | + |
| 23 | +_TEST_CLASS_METHODS = [ |
| 24 | + {"name": "query", "api_mode": ""}, |
| 25 | +] |
| 26 | + |
| 27 | + |
| 28 | +def test_delete_runtime_revision( |
| 29 | + client, |
| 30 | + mock_agent_engine_create_base64_encoded_tarball, |
| 31 | + mock_agent_engine_create_path_exists, |
| 32 | +): |
| 33 | + client._api_client._http_options.base_url = ( |
| 34 | + "https://us-central1-autopush-aiplatform.sandbox.googleapis.com/" |
| 35 | + ) |
| 36 | + client._api_client._http_options.api_version = "v1beta1" |
| 37 | + |
| 38 | + with ( |
| 39 | + mock_agent_engine_create_base64_encoded_tarball, |
| 40 | + mock_agent_engine_create_path_exists, |
| 41 | + ): |
| 42 | + agent_engine = client.agent_engines.create( |
| 43 | + config={ |
| 44 | + "display_name": "test-agent-engine-delete-runtime-revision", |
| 45 | + "source_packages": [ |
| 46 | + "test_module.py", |
| 47 | + "requirements.txt", |
| 48 | + ], |
| 49 | + "entrypoint_module": "test_module", |
| 50 | + "entrypoint_object": "test_object", |
| 51 | + "class_methods": _TEST_CLASS_METHODS, |
| 52 | + "http_options": { |
| 53 | + "base_url": "https://us-central1-autopush-aiplatform.sandbox.googleapis.com", |
| 54 | + "api_version": "v1beta1", |
| 55 | + }, |
| 56 | + }, |
| 57 | + ) |
| 58 | + # Create a second runtime revision, |
| 59 | + # since it's not possible to delete if there is only one runtime revision. |
| 60 | + with ( |
| 61 | + mock_agent_engine_create_base64_encoded_tarball, |
| 62 | + mock_agent_engine_create_path_exists, |
| 63 | + ): |
| 64 | + updated_agent_engine = client.agent_engines.update( |
| 65 | + name=agent_engine.api_resource.name, |
| 66 | + config={ |
| 67 | + "display_name": "test-agent-engine-update-traffic-with-agent-after-update", |
| 68 | + "source_packages": [ |
| 69 | + "test_module.py", |
| 70 | + "requirements.txt", |
| 71 | + ], |
| 72 | + "entrypoint_module": "test_module", |
| 73 | + "entrypoint_object": "test_object", |
| 74 | + "class_methods": _TEST_CLASS_METHODS, |
| 75 | + "http_options": { |
| 76 | + "base_url": "https://us-central1-autopush-aiplatform.sandbox.googleapis.com", |
| 77 | + "api_version": "v1beta1", |
| 78 | + }, |
| 79 | + }, |
| 80 | + ) |
| 81 | + |
| 82 | + runtime_revisions_list = client.agent_engines.runtime_revisions.list( |
| 83 | + name=updated_agent_engine.api_resource.name, |
| 84 | + ) |
| 85 | + assert len(runtime_revisions_list) == 2 |
| 86 | + revision_to_delete = runtime_revisions_list[1] |
| 87 | + operation = revision_to_delete.delete() |
| 88 | + assert isinstance(operation, types.DeleteAgentEngineRuntimeRevisionOperation) |
| 89 | + assert operation.done |
| 90 | + runtime_revisions_list = client.agent_engines.runtime_revisions.list( |
| 91 | + name=updated_agent_engine.api_resource.name, |
| 92 | + ) |
| 93 | + assert len(runtime_revisions_list) == 1 |
| 94 | + assert runtime_revisions_list[0].name != revision_to_delete.name |
| 95 | + client.agent_engines.delete(name=updated_agent_engine.api_resource.name, force=True) |
| 96 | + |
| 97 | + |
| 98 | +pytestmark = pytest_helper.setup( |
| 99 | + file=__file__, |
| 100 | + globals_for_file=globals(), |
| 101 | + test_method="agent_engines.runtime_revisions.delete", |
| 102 | +) |
| 103 | + |
| 104 | + |
| 105 | +pytest_plugins = ("pytest_asyncio",) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.asyncio |
| 109 | +async def test_delete_runtime_revision_async( |
| 110 | + client, |
| 111 | + mock_agent_engine_create_base64_encoded_tarball, |
| 112 | + mock_agent_engine_create_path_exists, |
| 113 | +): |
| 114 | + client._api_client._http_options.base_url = ( |
| 115 | + "https://us-central1-autopush-aiplatform.sandbox.googleapis.com/" |
| 116 | + ) |
| 117 | + client._api_client._http_options.api_version = "v1beta1" |
| 118 | + |
| 119 | + with ( |
| 120 | + mock_agent_engine_create_base64_encoded_tarball, |
| 121 | + mock_agent_engine_create_path_exists, |
| 122 | + ): |
| 123 | + agent_engine = client.agent_engines.create( |
| 124 | + config={ |
| 125 | + "display_name": "test-agent-engine-delete-runtime-revision", |
| 126 | + "source_packages": [ |
| 127 | + "test_module.py", |
| 128 | + "requirements.txt", |
| 129 | + ], |
| 130 | + "entrypoint_module": "test_module", |
| 131 | + "entrypoint_object": "test_object", |
| 132 | + "class_methods": _TEST_CLASS_METHODS, |
| 133 | + "http_options": { |
| 134 | + "base_url": "https://us-central1-autopush-aiplatform.sandbox.googleapis.com", |
| 135 | + "api_version": "v1beta1", |
| 136 | + }, |
| 137 | + }, |
| 138 | + ) |
| 139 | + # Create a second runtime revision, |
| 140 | + # since it's not possible to delete if there is only one runtime revision. |
| 141 | + with ( |
| 142 | + mock_agent_engine_create_base64_encoded_tarball, |
| 143 | + mock_agent_engine_create_path_exists, |
| 144 | + ): |
| 145 | + updated_agent_engine = client.agent_engines.update( |
| 146 | + name=agent_engine.api_resource.name, |
| 147 | + config={ |
| 148 | + "display_name": "test-agent-engine-update-traffic-with-agent-after-update", |
| 149 | + "source_packages": [ |
| 150 | + "test_module.py", |
| 151 | + "requirements.txt", |
| 152 | + ], |
| 153 | + "entrypoint_module": "test_module", |
| 154 | + "entrypoint_object": "test_object", |
| 155 | + "class_methods": _TEST_CLASS_METHODS, |
| 156 | + "http_options": { |
| 157 | + "base_url": "https://us-central1-autopush-aiplatform.sandbox.googleapis.com", |
| 158 | + "api_version": "v1beta1", |
| 159 | + }, |
| 160 | + }, |
| 161 | + ) |
| 162 | + |
| 163 | + runtime_revisions_list = await client.aio.agent_engines.runtime_revisions.list( |
| 164 | + name=updated_agent_engine.api_resource.name, |
| 165 | + ) |
| 166 | + assert len(runtime_revisions_list) == 2 |
| 167 | + revision_to_delete = runtime_revisions_list[1] |
| 168 | + operation = await revision_to_delete.delete() |
| 169 | + assert isinstance(operation, types.DeleteAgentEngineRuntimeRevisionOperation) |
| 170 | + client.agent_engines.delete(name=updated_agent_engine.api_resource.name, force=True) |
0 commit comments