forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_template.py
More file actions
127 lines (101 loc) · 4.15 KB
/
test_template.py
File metadata and controls
127 lines (101 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import uuid
from flaky import flaky
import google.auth
from google.cloud import batch_v1
from google.cloud import compute_v1
from google.cloud import resourcemanager_v3
import pytest
from .test_basics import _test_body
from ..create.create_with_template import create_script_job_with_template
PROJECT = google.auth.default()[1]
PROJECT_NUMBER = (
resourcemanager_v3.ProjectsClient()
.get_project(name=f"projects/{PROJECT}")
.name.split("/")[1]
)
REGION = "europe-north1"
TIMEOUT = 600 # 10 minutes
WAIT_STATES = {
batch_v1.JobStatus.State.STATE_UNSPECIFIED,
batch_v1.JobStatus.State.QUEUED,
batch_v1.JobStatus.State.RUNNING,
batch_v1.JobStatus.State.SCHEDULED,
}
@pytest.fixture
def job_name():
return f"test-job-{uuid.uuid4().hex[:10]}"
@pytest.fixture
def instance_template():
disk = compute_v1.AttachedDisk()
initialize_params = compute_v1.AttachedDiskInitializeParams()
initialize_params.source_image = (
"projects/ubuntu-os-cloud/global/images/family/ubuntu-2204-lts"
)
initialize_params.disk_size_gb = 25
initialize_params.disk_type = "pd-balanced"
disk.initialize_params = initialize_params
disk.auto_delete = True
disk.boot = True
network_interface = compute_v1.NetworkInterface()
network_interface.name = "global/networks/default"
access = compute_v1.AccessConfig()
access.type_ = compute_v1.AccessConfig.Type.ONE_TO_ONE_NAT.name
access.name = "External NAT"
access.network_tier = access.NetworkTier.PREMIUM.name
network_interface.access_configs = [access]
template = compute_v1.InstanceTemplate()
template.name = "test-template-" + uuid.uuid4().hex[:10]
template.properties = compute_v1.InstanceProperties()
template.properties.disks = [disk]
template.properties.machine_type = "e2-standard-16"
template.properties.network_interfaces = [network_interface]
template.properties.scheduling = compute_v1.Scheduling()
template.properties.scheduling.on_host_maintenance = (
compute_v1.Scheduling.OnHostMaintenance.MIGRATE.name
)
template.properties.scheduling.provisioning_model = (
compute_v1.Scheduling.ProvisioningModel.STANDARD.name
)
template.properties.scheduling.automatic_restart = True
template.properties.service_accounts = [
{
"email": f"{PROJECT_NUMBER}-compute@developer.gserviceaccount.com",
"scopes": [
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring.write",
"https://www.googleapis.com/auth/servicecontrol",
"https://www.googleapis.com/auth/service.management.readonly",
"https://www.googleapis.com/auth/trace.append",
],
}
]
template_client = compute_v1.InstanceTemplatesClient()
operation_client = compute_v1.GlobalOperationsClient()
op = template_client.insert_unary(
project=PROJECT, instance_template_resource=template
)
operation_client.wait(project=PROJECT, operation=op.name)
template = template_client.get(project=PROJECT, instance_template=template.name)
yield template
op = template_client.delete_unary(project=PROJECT, instance_template=template.name)
operation_client.wait(project=PROJECT, operation=op.name)
@flaky(max_runs=3, min_passes=1)
def test_template_job(job_name, instance_template):
job = create_script_job_with_template(
PROJECT, REGION, job_name, instance_template.self_link
)
_test_body(job, region=REGION)