|
| 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 | +from copy import deepcopy |
| 15 | +import uuid |
| 16 | + |
| 17 | +import google.api_core.exceptions |
| 18 | +import google.auth |
| 19 | +from google.cloud import compute_v1 |
| 20 | +import pytest |
| 21 | + |
| 22 | +from samples.snippets.instances.create_start_instance.create_windows_instance import \ |
| 23 | + get_image_from_family |
| 24 | + |
| 25 | +from ..disks.delete import delete_disk |
| 26 | +from ..disks.list import list_disks |
| 27 | + |
| 28 | +PROJECT = google.auth.default()[1] |
| 29 | +ZONE = "europe-north1-c" |
| 30 | + |
| 31 | + |
| 32 | +def test_request_id(): |
| 33 | + disk = compute_v1.Disk() |
| 34 | + disk.size_gb = 20 |
| 35 | + disk.name = "test-disk-" + uuid.uuid4().hex[:10] |
| 36 | + disk.zone = ZONE |
| 37 | + disk.type_ = f"zones/{ZONE}/diskTypes/pd-standard" |
| 38 | + disk.source_image = get_image_from_family("debian-cloud", "debian-11").self_link |
| 39 | + |
| 40 | + disk2 = deepcopy(disk) |
| 41 | + disk2.name = "test-disk-" + uuid.uuid4().hex[:10] |
| 42 | + |
| 43 | + request = compute_v1.InsertDiskRequest() |
| 44 | + request.request_id = str(uuid.uuid4()) |
| 45 | + request.project = PROJECT |
| 46 | + request.zone = ZONE |
| 47 | + request.disk_resource = disk |
| 48 | + |
| 49 | + # Creating a different request, but with the same request_id |
| 50 | + # This should not be executed, because the previous request |
| 51 | + # has the same ID. |
| 52 | + request2 = deepcopy(request) |
| 53 | + request2.disk_resource = disk2 |
| 54 | + |
| 55 | + disk_client = compute_v1.DisksClient() |
| 56 | + try: |
| 57 | + operation = disk_client.insert(request) |
| 58 | + operation2 = disk_client.insert(request2) |
| 59 | + operation.result() |
| 60 | + operation2.result() |
| 61 | + except Exception as err: |
| 62 | + pytest.fail(f"There was an error: {err}") |
| 63 | + raise err |
| 64 | + else: |
| 65 | + disks = list_disks(PROJECT, ZONE) |
| 66 | + assert any(i_disk.name == disk.name for i_disk in disks) |
| 67 | + assert all(i_disk.name != disk2.name for i_disk in disks) |
| 68 | + finally: |
| 69 | + delete_disk(PROJECT, ZONE, disk.name) |
| 70 | + try: |
| 71 | + delete_disk(PROJECT, ZONE, disk2.name) |
| 72 | + except google.api_core.exceptions.NotFound: |
| 73 | + pass |
| 74 | + |
| 75 | + |
| 76 | +def test_request_id_op_id(): |
| 77 | + disk = compute_v1.Disk() |
| 78 | + disk.size_gb = 20 |
| 79 | + disk.name = "test-disk-" + uuid.uuid4().hex[:10] |
| 80 | + disk.zone = ZONE |
| 81 | + disk.type_ = f"zones/{ZONE}/diskTypes/pd-standard" |
| 82 | + disk.source_image = get_image_from_family("debian-cloud", "debian-11").self_link |
| 83 | + |
| 84 | + request = compute_v1.InsertDiskRequest() |
| 85 | + request.request_id = str(uuid.uuid4()) |
| 86 | + request.project = PROJECT |
| 87 | + request.zone = ZONE |
| 88 | + request.disk_resource = disk |
| 89 | + |
| 90 | + disk_client = compute_v1.DisksClient() |
| 91 | + |
| 92 | + try: |
| 93 | + op1 = disk_client.insert(request) |
| 94 | + op2 = disk_client.insert(request) |
| 95 | + op1.result() |
| 96 | + assert op1.name == op2.name |
| 97 | + finally: |
| 98 | + delete_disk(PROJECT, ZONE, disk.name) |
0 commit comments