|
| 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 uuid |
| 16 | + |
| 17 | +import google.auth |
| 18 | +from google.cloud import batch_v1 |
| 19 | +from google.cloud import compute_v1 |
| 20 | +from google.cloud import resourcemanager_v3 |
| 21 | +import pytest |
| 22 | + |
| 23 | + |
| 24 | +from .test_basics import _test_body |
| 25 | + |
| 26 | +from ..create.create_with_template import create_script_job_with_template |
| 27 | + |
| 28 | +PROJECT = google.auth.default()[1] |
| 29 | + |
| 30 | +PROJECT_NUMBER = resourcemanager_v3.ProjectsClient().get_project(name=f"projects/{PROJECT}").name.split("/")[1] |
| 31 | + |
| 32 | +REGION = 'europe-north1' |
| 33 | + |
| 34 | +TIMEOUT = 600 # 10 minutes |
| 35 | + |
| 36 | +WAIT_STATES = { |
| 37 | + batch_v1.JobStatus.State.STATE_UNSPECIFIED, |
| 38 | + batch_v1.JobStatus.State.QUEUED, |
| 39 | + batch_v1.JobStatus.State.RUNNING, |
| 40 | + batch_v1.JobStatus.State.SCHEDULED, |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def job_name(): |
| 46 | + return f"test-job-{uuid.uuid4().hex[:10]}" |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture |
| 50 | +def instance_template(): |
| 51 | + disk = compute_v1.AttachedDisk() |
| 52 | + initialize_params = compute_v1.AttachedDiskInitializeParams() |
| 53 | + initialize_params.source_image = ( |
| 54 | + "projects/ubuntu-os-cloud/global/images/family/ubuntu-2204-lts" |
| 55 | + ) |
| 56 | + initialize_params.disk_size_gb = 25 |
| 57 | + initialize_params.disk_type = 'pd-balanced' |
| 58 | + disk.initialize_params = initialize_params |
| 59 | + disk.auto_delete = True |
| 60 | + disk.boot = True |
| 61 | + |
| 62 | + network_interface = compute_v1.NetworkInterface() |
| 63 | + network_interface.name = "global/networks/default" |
| 64 | + |
| 65 | + access = compute_v1.AccessConfig() |
| 66 | + access.type_ = compute_v1.AccessConfig.Type.ONE_TO_ONE_NAT.name |
| 67 | + access.name = "External NAT" |
| 68 | + access.network_tier = access.NetworkTier.PREMIUM.name |
| 69 | + network_interface.access_configs = [access] |
| 70 | + |
| 71 | + template = compute_v1.InstanceTemplate() |
| 72 | + template.name = "test-template-" + uuid.uuid4().hex[:10] |
| 73 | + template.properties = compute_v1.InstanceProperties() |
| 74 | + template.properties.disks = [disk] |
| 75 | + template.properties.machine_type = "e2-standard-16" |
| 76 | + template.properties.network_interfaces = [network_interface] |
| 77 | + |
| 78 | + template.properties.scheduling = compute_v1.Scheduling() |
| 79 | + template.properties.scheduling.on_host_maintenance = compute_v1.Scheduling.OnHostMaintenance.MIGRATE.name |
| 80 | + template.properties.scheduling.provisioning_model = compute_v1.Scheduling.ProvisioningModel.STANDARD.name |
| 81 | + template.properties.scheduling.automatic_restart = True |
| 82 | + |
| 83 | + template.properties.service_accounts = [ |
| 84 | + { |
| 85 | + "email": f"{PROJECT_NUMBER}[email protected]", |
| 86 | + "scopes": [ |
| 87 | + "https://www.googleapis.com/auth/devstorage.read_only", |
| 88 | + "https://www.googleapis.com/auth/logging.write", |
| 89 | + "https://www.googleapis.com/auth/monitoring.write", |
| 90 | + "https://www.googleapis.com/auth/servicecontrol", |
| 91 | + "https://www.googleapis.com/auth/service.management.readonly", |
| 92 | + "https://www.googleapis.com/auth/trace.append" |
| 93 | + ] |
| 94 | + } |
| 95 | + ] |
| 96 | + |
| 97 | + template_client = compute_v1.InstanceTemplatesClient() |
| 98 | + operation_client = compute_v1.GlobalOperationsClient() |
| 99 | + op = template_client.insert_unary( |
| 100 | + project=PROJECT, instance_template_resource=template |
| 101 | + ) |
| 102 | + operation_client.wait(project=PROJECT, operation=op.name) |
| 103 | + |
| 104 | + template = template_client.get(project=PROJECT, instance_template=template.name) |
| 105 | + |
| 106 | + yield template |
| 107 | + |
| 108 | + op = template_client.delete_unary(project=PROJECT, instance_template=template.name) |
| 109 | + operation_client.wait(project=PROJECT, operation=op.name) |
| 110 | + |
| 111 | + |
| 112 | +def test_template_job(job_name, instance_template): |
| 113 | + job = create_script_job_with_template(PROJECT, REGION, job_name, instance_template.self_link) |
| 114 | + _test_body(job) |
0 commit comments