File size: 18,392 Bytes
1856027 | 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | # -*- coding: utf-8 -*-
# Copyright 2023 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 copy
import os
import shutil
import tempfile
from typing import Any
from unittest import mock
from unittest.mock import patch
import uuid
from google import auth
from google.api_core import operation as ga_operation
from google.auth import credentials as auth_credentials
from google.cloud import storage
from google.cloud.aiplatform import base
from google.cloud.aiplatform.compat.services import (
feature_online_store_admin_service_client,
)
from google.cloud.aiplatform.compat.services import (
feature_registry_service_client,
)
from google.cloud.aiplatform_v1beta1.services.feature_registry_service import (
FeatureRegistryServiceClient,
)
from google.cloud.aiplatform.compat.services import job_service_client
from google.cloud.aiplatform.compat.types import (
custom_job as gca_custom_job_compat,
)
from google.cloud.aiplatform.compat.types import io as gca_io_compat
from google.cloud.aiplatform.compat.types import (
job_state as gca_job_state_compat,
)
from google.cloud.aiplatform_v1beta1.services.persistent_resource_service import (
PersistentResourceServiceClient,
)
from google.cloud.aiplatform_v1beta1.types.persistent_resource import (
PersistentResource,
ResourcePool,
ResourceRuntimeSpec,
ServiceAccountSpec,
)
from feature_store_constants import (
_TEST_BIGTABLE_FOS1,
_TEST_EMBEDDING_FV1,
_TEST_ESF_OPTIMIZED_FOS,
_TEST_ESF_OPTIMIZED_FOS2,
_TEST_FG1,
_TEST_FG1_F1,
_TEST_FG1_F2,
_TEST_FG1_FM1,
_TEST_FV1,
_TEST_FV3,
_TEST_OPTIMIZED_EMBEDDING_FV,
_TEST_OPTIMIZED_FV1,
_TEST_OPTIMIZED_FV2,
_TEST_PSC_OPTIMIZED_FOS,
)
import pytest
_TEST_PROJECT = "test-project"
_TEST_PROJECT_NUMBER = "12345678"
_TEST_LOCATION = "us-central1"
_TEST_PARENT = f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}"
_TEST_DISPLAY_NAME = f"{_TEST_PARENT}/customJobs/12345"
_TEST_BUCKET_NAME = "gs://test_bucket"
_TEST_BASE_OUTPUT_DIR = f"{_TEST_BUCKET_NAME}/test_base_output_dir"
_TEST_SERVICE_ACCOUNT = f"{_TEST_PROJECT_NUMBER}-compute@developer.gserviceaccount.com"
_TEST_INPUTS = [
"--arg_0=string_val_0",
"--arg_1=string_val_1",
"--arg_2=int_val_0",
"--arg_3=int_val_1",
]
_TEST_IMAGE_URI = "test_image_uri"
_TEST_MACHINE_TYPE = "test_machine_type"
_TEST_WORKER_POOL_SPEC = [
{
"machine_spec": {
"machine_type": _TEST_MACHINE_TYPE,
},
"replica_count": 1,
"container_spec": {
"image_uri": _TEST_IMAGE_URI,
"args": _TEST_INPUTS,
},
}
]
_TEST_CUSTOM_JOB_PROTO = gca_custom_job_compat.CustomJob(
display_name=_TEST_DISPLAY_NAME,
job_spec={
"worker_pool_specs": _TEST_WORKER_POOL_SPEC,
"base_output_directory": gca_io_compat.GcsDestination(
output_uri_prefix=_TEST_BASE_OUTPUT_DIR
),
},
labels={"trained_by_vertex_ai": "true"},
)
_TEST_REQUEST_RUNNING_DEFAULT = PersistentResource(
resource_runtime_spec=ResourceRuntimeSpec(service_account_spec=ServiceAccountSpec())
)
resource_pool = ResourcePool()
resource_pool.machine_spec.machine_type = "n1-standard-4"
resource_pool.replica_count = 1
resource_pool.disk_spec.boot_disk_type = "pd-ssd"
resource_pool.disk_spec.boot_disk_size_gb = 100
_TEST_REQUEST_RUNNING_DEFAULT.resource_pools = [resource_pool]
_TEST_PERSISTENT_RESOURCE_RUNNING = PersistentResource(state="RUNNING")
_TEST_PERSISTENT_RESOURCE_SERVICE_ACCOUNT_RUNNING = PersistentResource(
state="RUNNING",
resource_runtime_spec=ResourceRuntimeSpec(
service_account_spec=ServiceAccountSpec(
enable_custom_service_account=True, service_account=_TEST_SERVICE_ACCOUNT
)
),
)
@pytest.fixture(scope="module")
def google_auth_mock():
with mock.patch.object(auth, "default") as auth_mock:
auth_mock.return_value = (
auth_credentials.AnonymousCredentials(),
"test-project",
)
yield auth_mock
@pytest.fixture
def mock_storage_blob(mock_filesystem):
"""Mocks the storage Blob API.
Replaces the Blob factory method by a simpler method that records the
destination_file_uri and, instead of uploading the file to gcs, copying it
to the fake local file system.
"""
class MockStorageBlob:
"""Mocks storage.Blob."""
def __init__(self, destination_file_uri: str, client: Any):
del client
self.destination_file_uri = destination_file_uri
@classmethod
def from_string(cls, destination_file_uri: str, client: Any):
if destination_file_uri.startswith("gs://"):
# Do not copy files to gs:// since it's not a valid path in the fake
# filesystem.
destination_file_uri = destination_file_uri.split("/")[-1]
return cls(destination_file_uri, client)
def upload_from_filename(self, filename: str):
shutil.copy(filename, self.destination_file_uri)
def download_to_filename(self, filename: str):
"""To be replaced by an implementation of testing needs."""
raise NotImplementedError
with mock.patch.object(storage, "Blob", new=MockStorageBlob) as storage_blob:
yield storage_blob
@pytest.fixture
def mock_storage_blob_tmp_dir(tmp_path):
"""Mocks the storage Blob API.
Replaces the Blob factory method by a simpler method that records the
destination_file_uri and, instead of uploading the file to gcs, copying it
to a temporaray path in the local file system.
"""
class MockStorageBlob:
"""Mocks storage.Blob."""
def __init__(self, destination_file_uri: str, client: Any):
del client
self.destination_file_uri = destination_file_uri
@classmethod
def from_string(cls, destination_file_uri: str, client: Any):
if destination_file_uri.startswith("gs://"):
# Do not copy files to gs:// since it's not a valid path in the fake
# filesystem.
destination_file_uri = os.fspath(
tmp_path / destination_file_uri.split("/")[-1]
)
return cls(destination_file_uri, client)
def upload_from_filename(self, filename: str):
shutil.copy(filename, self.destination_file_uri)
def download_to_filename(self, filename: str):
"""To be replaced by an implementation of testing needs."""
raise NotImplementedError
with mock.patch.object(storage, "Blob", new=MockStorageBlob) as storage_blob:
yield storage_blob
@pytest.fixture
def mock_gcs_upload():
def fake_upload_to_gcs(local_filename: str, gcs_destination: str):
if gcs_destination.startswith("gs://") or gcs_destination.startswith("gcs/"):
raise ValueError("Please don't use the real gcs path with mock_gcs_upload.")
# instead of upload, just copy the file.
shutil.copyfile(local_filename, gcs_destination)
with mock.patch(
"google.cloud.aiplatform.utils.gcs_utils.upload_to_gcs",
new=fake_upload_to_gcs,
) as gcs_upload:
yield gcs_upload
@pytest.fixture
def mock_temp_dir():
with mock.patch.object(tempfile, "TemporaryDirectory") as temp_dir_mock:
yield temp_dir_mock
@pytest.fixture
def mock_named_temp_file():
with mock.patch.object(tempfile, "NamedTemporaryFile") as named_temp_file_mock:
yield named_temp_file_mock
@pytest.fixture
def mock_create_custom_job():
with mock.patch.object(
job_service_client.JobServiceClient, "create_custom_job"
) as create_custom_job_mock:
custom_job_proto = copy.deepcopy(_TEST_CUSTOM_JOB_PROTO)
custom_job_proto.name = _TEST_DISPLAY_NAME
custom_job_proto.state = gca_job_state_compat.JobState.JOB_STATE_PENDING
create_custom_job_mock.return_value = custom_job_proto
yield create_custom_job_mock
@pytest.fixture
def mock_get_custom_job_succeeded():
with mock.patch.object(
job_service_client.JobServiceClient, "get_custom_job"
) as get_custom_job_mock:
custom_job_proto = copy.deepcopy(_TEST_CUSTOM_JOB_PROTO)
custom_job_proto.name = _TEST_DISPLAY_NAME
custom_job_proto.state = gca_job_state_compat.JobState.JOB_STATE_SUCCEEDED
get_custom_job_mock.return_value = custom_job_proto
yield get_custom_job_mock
@pytest.fixture
def mock_blob_upload_from_filename():
with mock.patch.object(storage.Blob, "upload_from_filename") as upload_mock:
yield upload_mock
@pytest.fixture
def mock_blob_download_to_filename():
with mock.patch.object(storage.Blob, "download_to_filename") as download_mock:
yield download_mock
@pytest.fixture
def mock_uuid():
with mock.patch.object(uuid, "uuid4") as uuid_mock:
uuid_mock.return_value = 0
yield uuid_mock
@pytest.fixture
def base_logger_mock():
with patch.object(
base._LOGGER,
"info",
wraps=base._LOGGER.info,
) as logger_mock:
yield logger_mock
@pytest.fixture
def persistent_resource_running_mock():
with mock.patch.object(
PersistentResourceServiceClient,
"get_persistent_resource",
) as persistent_resource_running_mock:
persistent_resource_running_mock.return_value = (
_TEST_PERSISTENT_RESOURCE_RUNNING
)
yield persistent_resource_running_mock
@pytest.fixture
def persistent_resource_service_account_running_mock():
with mock.patch.object(
PersistentResourceServiceClient,
"get_persistent_resource",
) as persistent_resource_service_account_running_mock:
persistent_resource_service_account_running_mock.return_value = (
_TEST_PERSISTENT_RESOURCE_SERVICE_ACCOUNT_RUNNING
)
yield persistent_resource_service_account_running_mock
@pytest.fixture
def persistent_resource_exception_mock():
with mock.patch.object(
PersistentResourceServiceClient,
"get_persistent_resource",
) as persistent_resource_exception_mock:
persistent_resource_exception_mock.side_effect = Exception
yield persistent_resource_exception_mock
@pytest.fixture
def create_persistent_resource_default_mock():
with mock.patch.object(
PersistentResourceServiceClient,
"create_persistent_resource",
) as create_persistent_resource_default_mock:
create_persistent_resource_lro_mock = mock.Mock(ga_operation.Operation)
create_persistent_resource_lro_mock.result.return_value = (
_TEST_REQUEST_RUNNING_DEFAULT
)
create_persistent_resource_default_mock.return_value = (
create_persistent_resource_lro_mock
)
yield create_persistent_resource_default_mock
@pytest.fixture
def get_fos_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"get_feature_online_store",
) as get_fos_mock:
get_fos_mock.return_value = _TEST_BIGTABLE_FOS1
yield get_fos_mock
@pytest.fixture
def get_esf_optimized_fos_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"get_feature_online_store",
) as get_fos_mock:
get_fos_mock.return_value = _TEST_ESF_OPTIMIZED_FOS
yield get_fos_mock
@pytest.fixture
def get_psc_optimized_fos_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"get_feature_online_store",
) as get_fos_mock:
get_fos_mock.return_value = _TEST_PSC_OPTIMIZED_FOS
yield get_fos_mock
@pytest.fixture
def get_esf_optimized_fos_no_endpoint_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"get_feature_online_store",
) as get_fos_mock:
get_fos_mock.return_value = _TEST_ESF_OPTIMIZED_FOS2
yield get_fos_mock
@pytest.fixture
def create_bigtable_fos_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"create_feature_online_store",
) as create_bigtable_fos_mock:
create_fos_lro_mock = mock.Mock(ga_operation.Operation)
create_fos_lro_mock.result.return_value = _TEST_BIGTABLE_FOS1
create_bigtable_fos_mock.return_value = create_fos_lro_mock
yield create_bigtable_fos_mock
@pytest.fixture
def create_esf_optimized_fos_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"create_feature_online_store",
) as create_esf_optimized_fos_mock:
create_fos_lro_mock = mock.Mock(ga_operation.Operation)
create_fos_lro_mock.result.return_value = _TEST_ESF_OPTIMIZED_FOS
create_esf_optimized_fos_mock.return_value = create_fos_lro_mock
yield create_esf_optimized_fos_mock
@pytest.fixture
def create_psc_optimized_fos_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"create_feature_online_store",
) as create_psc_optimized_fos_mock:
create_fos_lro_mock = mock.Mock(ga_operation.Operation)
create_fos_lro_mock.result.return_value = _TEST_PSC_OPTIMIZED_FOS
create_psc_optimized_fos_mock.return_value = create_fos_lro_mock
yield create_psc_optimized_fos_mock
@pytest.fixture
def get_fv_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"get_feature_view",
) as get_fv_mock:
get_fv_mock.return_value = _TEST_FV1
yield get_fv_mock
@pytest.fixture
def get_rag_fv_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"get_feature_view",
) as get_rag_fv_mock:
get_rag_fv_mock.return_value = _TEST_FV3
yield get_rag_fv_mock
@pytest.fixture
def create_bq_fv_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"create_feature_view",
) as create_bq_fv_mock:
create_bq_fv_lro_mock = mock.Mock(ga_operation.Operation)
create_bq_fv_lro_mock.result.return_value = _TEST_FV1
create_bq_fv_mock.return_value = create_bq_fv_lro_mock
yield create_bq_fv_mock
@pytest.fixture
def create_rag_fv_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"create_feature_view",
) as create_rag_fv_mock:
create_rag_fv_lro_mock = mock.Mock(ga_operation.Operation)
create_rag_fv_lro_mock.result.return_value = _TEST_FV3
create_rag_fv_mock.return_value = create_rag_fv_lro_mock
yield create_rag_fv_mock
@pytest.fixture
def create_embedding_fv_from_bq_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"create_feature_view",
) as create_embedding_fv_mock:
create_embedding_fv_mock_lro = mock.Mock(ga_operation.Operation)
create_embedding_fv_mock_lro.result.return_value = _TEST_OPTIMIZED_EMBEDDING_FV
create_embedding_fv_mock.return_value = create_embedding_fv_mock_lro
yield create_embedding_fv_mock
@pytest.fixture
def get_optimized_embedding_fv_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"get_feature_view",
) as get_fv_mock:
get_fv_mock.return_value = _TEST_OPTIMIZED_EMBEDDING_FV
yield get_fv_mock
@pytest.fixture
def get_optimized_fv_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"get_feature_view",
) as get_optimized_fv_mock:
get_optimized_fv_mock.return_value = _TEST_OPTIMIZED_FV1
yield get_optimized_fv_mock
@pytest.fixture
def get_embedding_fv_mock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"get_feature_view",
) as get_embedding_fv_mock:
get_embedding_fv_mock.return_value = _TEST_EMBEDDING_FV1
yield get_embedding_fv_mock
@pytest.fixture
def get_optimized_fv_no_endpointmock():
with patch.object(
feature_online_store_admin_service_client.FeatureOnlineStoreAdminServiceClient,
"get_feature_view",
) as get_optimized_fv_no_endpointmock:
get_optimized_fv_no_endpointmock.return_value = _TEST_OPTIMIZED_FV2
yield get_optimized_fv_no_endpointmock
@pytest.fixture
def get_fg_mock():
with patch.object(
feature_registry_service_client.FeatureRegistryServiceClient,
"get_feature_group",
) as get_fg_mock:
get_fg_mock.return_value = _TEST_FG1
yield get_fg_mock
@pytest.fixture
def get_feature_mock():
with patch.object(
feature_registry_service_client.FeatureRegistryServiceClient,
"get_feature",
) as get_fg_mock:
get_fg_mock.return_value = _TEST_FG1_F1
yield get_fg_mock
@pytest.fixture
def get_feature_with_version_column_mock():
with patch.object(
feature_registry_service_client.FeatureRegistryServiceClient,
"get_feature",
) as get_fg_mock:
get_fg_mock.return_value = _TEST_FG1_F2
yield get_fg_mock
@pytest.fixture
def get_feature_monitor_mock():
with patch.object(
FeatureRegistryServiceClient,
"get_feature_monitor",
) as get_fg_mock:
get_fg_mock.return_value = _TEST_FG1_FM1
yield get_fg_mock
|