|
| 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 | +import time |
| 15 | +import uuid |
| 16 | + |
| 17 | +import google.auth |
| 18 | +from google.cloud import compute_v1 |
| 19 | +import pytest |
| 20 | + |
| 21 | + |
| 22 | +from ..images.get import get_image_from_family |
| 23 | +from ..instances.create import create_instance, disk_from_image |
| 24 | +from ..instances.delete import delete_instance |
| 25 | +from ..instances.resume import resume_instance |
| 26 | +from ..instances.suspend import suspend_instance |
| 27 | + |
| 28 | +PROJECT = google.auth.default()[1] |
| 29 | + |
| 30 | +INSTANCE_ZONE = "europe-central2-b" |
| 31 | + |
| 32 | + |
| 33 | +def _get_status(instance: compute_v1.Instance) -> compute_v1.Instance.Status: |
| 34 | + instance_client = compute_v1.InstancesClient() |
| 35 | + return instance_client.get( |
| 36 | + project=PROJECT, zone=INSTANCE_ZONE, instance=instance.name |
| 37 | + ).status |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def compute_instance(): |
| 42 | + instance_name = "test-instance-" + uuid.uuid4().hex[:10] |
| 43 | + newest_debian = get_image_from_family(project="ubuntu-os-cloud", family="ubuntu-2004-lts") |
| 44 | + disk_type = f"zones/{INSTANCE_ZONE}/diskTypes/pd-standard" |
| 45 | + disks = [disk_from_image(disk_type, 100, True, newest_debian.self_link)] |
| 46 | + instance = create_instance( |
| 47 | + PROJECT, INSTANCE_ZONE, instance_name, disks |
| 48 | + ) |
| 49 | + yield instance |
| 50 | + |
| 51 | + delete_instance(PROJECT, INSTANCE_ZONE, instance_name) |
| 52 | + |
| 53 | + |
| 54 | +def test_instance_suspend_resume(compute_instance): |
| 55 | + assert _get_status(compute_instance) == compute_v1.Instance.Status.RUNNING.name |
| 56 | + |
| 57 | + # Once the machine is running, give it some time to fully start all processes |
| 58 | + # before trying to suspend it |
| 59 | + time.sleep(45) |
| 60 | + |
| 61 | + suspend_instance(PROJECT, INSTANCE_ZONE, compute_instance.name) |
| 62 | + |
| 63 | + while _get_status(compute_instance) == compute_v1.Instance.Status.SUSPENDING.name: |
| 64 | + time.sleep(5) |
| 65 | + |
| 66 | + assert _get_status(compute_instance) == compute_v1.Instance.Status.SUSPENDED.name |
| 67 | + |
| 68 | + resume_instance(PROJECT, INSTANCE_ZONE, compute_instance.name) |
| 69 | + assert _get_status(compute_instance) == compute_v1.Instance.Status.RUNNING.name |
0 commit comments