|
| 1 | +# Copyright 2022 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 | +import os |
| 16 | +import uuid |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from google.auth.credentials import AnonymousCredentials |
| 21 | +from google.cloud import storage |
| 22 | +from google.cloud.exceptions import NotFound |
| 23 | + |
| 24 | + |
| 25 | +"""Environment variable or default host for Storage testbench emulator.""" |
| 26 | +_HOST = os.environ.get("STORAGE_EMULATOR_HOST", "http://localhost:9000") |
| 27 | + |
| 28 | + |
| 29 | +"""Emulated project information for the storage testbench.""" |
| 30 | +_CONF_TEST_PROJECT_ID = "my-project-id" |
| 31 | +_CONF_TEST_SERVICE_ACCOUNT_EMAIL = ( |
| 32 | + |
| 33 | +) |
| 34 | +_CONF_TEST_PUBSUB_TOPIC_NAME = "my-topic-name" |
| 35 | + |
| 36 | +"""Create content payload in different sizes.""" |
| 37 | + |
| 38 | + |
| 39 | +def _create_block(desired_kib): |
| 40 | + line = "abc123XYZ" * 14 + "!" + "\n" |
| 41 | + return 1024 * int(desired_kib / len(line)) * line |
| 42 | + |
| 43 | + |
| 44 | +_STRING_CONTENT = "hello world" |
| 45 | +_SIZE_16MB = 16384 # 16*1024 KiB |
| 46 | + |
| 47 | + |
| 48 | +######################################################################################################################################## |
| 49 | +### Pytest Fixtures to Populate Retry Conformance Test Resources ####################################################################### |
| 50 | +######################################################################################################################################## |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture |
| 54 | +def client(): |
| 55 | + client = storage.Client( |
| 56 | + project=_CONF_TEST_PROJECT_ID, |
| 57 | + credentials=AnonymousCredentials(), |
| 58 | + client_options={"api_endpoint": _HOST}, |
| 59 | + ) |
| 60 | + return client |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture |
| 64 | +def bucket(client): |
| 65 | + bucket = client.bucket(uuid.uuid4().hex) |
| 66 | + client.create_bucket(bucket) |
| 67 | + yield bucket |
| 68 | + try: |
| 69 | + bucket.delete(force=True) |
| 70 | + except NotFound: # in cases where bucket is deleted within the test |
| 71 | + pass |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture |
| 75 | +def object(client, bucket): |
| 76 | + blob = client.bucket(bucket.name).blob(uuid.uuid4().hex) |
| 77 | + blob.upload_from_string(_STRING_CONTENT) |
| 78 | + blob.reload() |
| 79 | + yield blob |
| 80 | + try: |
| 81 | + blob.delete() |
| 82 | + except NotFound: # in cases where object is deleted within the test |
| 83 | + pass |
| 84 | + |
| 85 | + |
| 86 | +@pytest.fixture |
| 87 | +def notification(client, bucket): |
| 88 | + notification = client.bucket(bucket.name).notification( |
| 89 | + topic_name=_CONF_TEST_PUBSUB_TOPIC_NAME |
| 90 | + ) |
| 91 | + notification.create() |
| 92 | + notification.reload() |
| 93 | + yield notification |
| 94 | + try: |
| 95 | + notification.delete() |
| 96 | + except NotFound: # in cases where notification is deleted within the test |
| 97 | + pass |
| 98 | + |
| 99 | + |
| 100 | +@pytest.fixture |
| 101 | +def hmac_key(client): |
| 102 | + hmac_key, _secret = client.create_hmac_key( |
| 103 | + service_account_email=_CONF_TEST_SERVICE_ACCOUNT_EMAIL, |
| 104 | + project_id=_CONF_TEST_PROJECT_ID, |
| 105 | + ) |
| 106 | + yield hmac_key |
| 107 | + try: |
| 108 | + hmac_key.state = "INACTIVE" |
| 109 | + hmac_key.update() |
| 110 | + hmac_key.delete() |
| 111 | + except NotFound: # in cases where hmac_key is deleted within the test |
| 112 | + pass |
| 113 | + |
| 114 | + |
| 115 | +@pytest.fixture |
| 116 | +def file_data(client, bucket): |
| 117 | + blob = client.bucket(bucket.name).blob(uuid.uuid4().hex) |
| 118 | + payload = _create_block(_SIZE_16MB) |
| 119 | + blob.upload_from_string(payload) |
| 120 | + yield blob, payload |
| 121 | + try: |
| 122 | + blob.delete() |
| 123 | + except NotFound: # in cases where object is deleted within the test |
| 124 | + pass |
0 commit comments