|
| 1 | +# Copyright 2021 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 | +import base64 |
| 15 | +import random |
| 16 | +import string |
| 17 | +import time |
| 18 | +import uuid |
| 19 | + |
| 20 | +import google.auth |
| 21 | +from google.cloud import compute_v1 |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +from samples.snippets.sample_start_stop import start_instance, start_instance_with_encryption_key, stop_instance |
| 26 | + |
| 27 | +PROJECT = google.auth.default()[1] |
| 28 | + |
| 29 | +INSTANCE_ZONE = "europe-central2-b" |
| 30 | + |
| 31 | +KEY = "".join(random.sample(string.ascii_letters, 32)) |
| 32 | +KEY_B64 = base64.b64encode(KEY.encode()) # for example: b'VEdORldtY3NKellPdWRDcUF5YlNVREtJdm5qaFJYSFA=' |
| 33 | + |
| 34 | + |
| 35 | +def _make_disk(raw_key: bytes = None): |
| 36 | + disk = compute_v1.AttachedDisk() |
| 37 | + initialize_params = compute_v1.AttachedDiskInitializeParams() |
| 38 | + initialize_params.source_image = ( |
| 39 | + "projects/debian-cloud/global/images/family/debian-10" |
| 40 | + ) |
| 41 | + initialize_params.disk_size_gb = "10" |
| 42 | + disk.initialize_params = initialize_params |
| 43 | + disk.auto_delete = True |
| 44 | + disk.boot = True |
| 45 | + disk.type_ = compute_v1.AttachedDisk.Type.PERSISTENT |
| 46 | + |
| 47 | + if raw_key: |
| 48 | + disk.disk_encryption_key = compute_v1.CustomerEncryptionKey() |
| 49 | + disk.disk_encryption_key.raw_key = raw_key |
| 50 | + |
| 51 | + return disk |
| 52 | + |
| 53 | + |
| 54 | +def _make_request(disk: compute_v1.AttachedDisk): |
| 55 | + network_interface = compute_v1.NetworkInterface() |
| 56 | + network_interface.name = 'default' |
| 57 | + network_interface.access_configs = [] |
| 58 | + |
| 59 | + # Collect information into the Instance object. |
| 60 | + instance = compute_v1.Instance() |
| 61 | + instance.name = "i" + uuid.uuid4().hex[:10] |
| 62 | + instance.disks = [disk] |
| 63 | + full_machine_type_name = f"zones/{INSTANCE_ZONE}/machineTypes/e2-micro" |
| 64 | + instance.machine_type = full_machine_type_name |
| 65 | + instance.network_interfaces = [network_interface] |
| 66 | + |
| 67 | + # Prepare the request to insert an instance. |
| 68 | + request = compute_v1.InsertInstanceRequest() |
| 69 | + request.zone = INSTANCE_ZONE |
| 70 | + request.project = PROJECT |
| 71 | + request.instance_resource = instance |
| 72 | + return request |
| 73 | + |
| 74 | + |
| 75 | +def _create_instance(request: compute_v1.InsertInstanceRequest): |
| 76 | + instance_client = compute_v1.InstancesClient() |
| 77 | + operation_client = compute_v1.ZoneOperationsClient() |
| 78 | + |
| 79 | + operation = instance_client.insert(request=request) |
| 80 | + operation_client.wait(operation=operation.name, zone=INSTANCE_ZONE, project=PROJECT) |
| 81 | + |
| 82 | + return instance_client.get(project=PROJECT, zone=INSTANCE_ZONE, instance=request.instance_resource.name) |
| 83 | + |
| 84 | + |
| 85 | +def _delete_instance(instance: compute_v1.Instance): |
| 86 | + instance_client = compute_v1.InstancesClient() |
| 87 | + operation_client = compute_v1.ZoneOperationsClient() |
| 88 | + |
| 89 | + operation = instance_client.delete(project=PROJECT, zone=INSTANCE_ZONE, instance=instance.name) |
| 90 | + operation_client.wait(operation=operation.name, zone=INSTANCE_ZONE, project=PROJECT) |
| 91 | + |
| 92 | + |
| 93 | +def _get_status(instance: compute_v1.Instance) -> compute_v1.Instance.Status: |
| 94 | + instance_client = compute_v1.InstancesClient() |
| 95 | + return instance_client.get(project=PROJECT, zone=INSTANCE_ZONE, instance=instance.name).status |
| 96 | + |
| 97 | + |
| 98 | +@pytest.fixture |
| 99 | +def compute_instance(): |
| 100 | + disk = _make_disk() |
| 101 | + request = _make_request(disk) |
| 102 | + |
| 103 | + instance = _create_instance(request) |
| 104 | + |
| 105 | + yield instance |
| 106 | + |
| 107 | + _delete_instance(instance) |
| 108 | + |
| 109 | + |
| 110 | +@pytest.fixture |
| 111 | +def compute_encrypted_instance(): |
| 112 | + disk = _make_disk(KEY_B64) |
| 113 | + request = _make_request(disk) |
| 114 | + |
| 115 | + instance = _create_instance(request) |
| 116 | + |
| 117 | + yield instance |
| 118 | + |
| 119 | + _delete_instance(instance) |
| 120 | + |
| 121 | + |
| 122 | +def test_instance_operations(compute_instance): |
| 123 | + assert _get_status(compute_instance) == compute_v1.Instance.Status.RUNNING |
| 124 | + |
| 125 | + stop_instance(PROJECT, INSTANCE_ZONE, compute_instance.name) |
| 126 | + |
| 127 | + while _get_status(compute_instance) == compute_v1.Instance.Status.STOPPING: |
| 128 | + # Since we can't configure timeout parameter for operation wait() (b/188037306) |
| 129 | + # We need to do some manual waiting for the stopping to finish... |
| 130 | + time.sleep(5) |
| 131 | + |
| 132 | + assert _get_status(compute_instance) == compute_v1.Instance.Status.TERMINATED |
| 133 | + |
| 134 | + start_instance(PROJECT, INSTANCE_ZONE, compute_instance.name) |
| 135 | + assert _get_status(compute_instance) == compute_v1.Instance.Status.RUNNING |
| 136 | + |
| 137 | + |
| 138 | +def test_instance_encrypted(compute_encrypted_instance): |
| 139 | + assert _get_status(compute_encrypted_instance) == compute_v1.Instance.Status.RUNNING |
| 140 | + |
| 141 | + stop_instance(PROJECT, INSTANCE_ZONE, compute_encrypted_instance.name) |
| 142 | + while _get_status(compute_encrypted_instance) == compute_v1.Instance.Status.STOPPING: |
| 143 | + # Since we can't configure timeout parameter for operation wait() (b/188037306) |
| 144 | + # We need to do some manual waiting for the stopping to finish... |
| 145 | + time.sleep(5) |
| 146 | + |
| 147 | + assert _get_status(compute_encrypted_instance) == compute_v1.Instance.Status.TERMINATED |
| 148 | + |
| 149 | + start_instance_with_encryption_key(PROJECT, INSTANCE_ZONE, compute_encrypted_instance.name, KEY_B64) |
| 150 | + assert _get_status(compute_encrypted_instance) == compute_v1.Instance.Status.RUNNING |
0 commit comments