-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathadvanced_creation.test.js
278 lines (247 loc) · 8.78 KB
/
advanced_creation.test.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2024 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
//
// https://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.
'use strict';
const path = require('path');
const cp = require('child_process');
const {describe, it, before} = require('mocha');
const {BatchServiceClient} = require('@google-cloud/batch').v1;
const {Storage} = require('@google-cloud/storage');
const {ProjectsClient} = require('@google-cloud/resource-manager').v3;
const compute = require('@google-cloud/compute');
// get a short ID for this test run that only contains characters that are valid in UUID
// (a plain UUID won't do because we want the "test-job-js" prefix and that would exceed the length limit)
const {customAlphabet} = require('nanoid');
const allowedChars = 'qwertyuiopasdfghjklzxcvbnm';
const testRunId = customAlphabet(allowedChars, allowedChars.length)();
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const cwd = path.join(__dirname, '..');
const batchClient = new BatchServiceClient();
const storage = new Storage();
const resourcemanagerClient = new ProjectsClient();
const instanceTemplatesClient = new compute.InstanceTemplatesClient();
describe('Create jobs with container, template and bucket', () => {
let projectId;
let bucketName;
let templateName;
before(async () => {
projectId = await batchClient.getProjectId();
bucketName = `batch-js-test-bucket-${testRunId}`;
templateName = `batch-js-test-template-${testRunId}`;
await createBucket(bucketName);
await createTemplate(projectId, templateName);
});
it('creates a job with a container payload', () => {
execSync(
`node create/create_with_container_no_mounting.js ${projectId} us-central1 test-job-js-container-${testRunId}`,
{cwd}
);
});
it('creates a job with a GCS bucket', () => {
execSync(
`node create/create_with_mounted_bucket.js ${projectId} us-central1 test-job-js-bucket-${testRunId} ${bucketName}`,
{cwd}
);
});
it('creates a job with instance template', () => {
execSync(
`node create/create_script_job_with_template.js ${projectId} us-central1 test-job-js-template-${testRunId} ${templateName}`,
{cwd}
);
});
// waiting for jobs to succeed in separate tests lets us create them all on the server and let them run in parallel,
// so the tests complete multiple times faster
it('waits for a job with a GCS bucket to succeed', async () => {
await waitForJobToSucceed(
projectId,
'us-central1',
`test-job-js-bucket-${testRunId}`
);
});
it('waits for a job with a container payload to succeed', async () => {
await waitForJobToSucceed(
projectId,
'us-central1',
`test-job-js-container-${testRunId}`
);
});
it('waits for a job with an instance template to succeed', async () => {
await waitForJobToSucceed(
projectId,
'us-central1',
`test-job-js-template-${testRunId}`
);
});
after(async () => {
await cleanupBucket(bucketName);
await deleteInstanceTemplate(projectId, templateName);
});
});
async function createBucket(bucketName) {
// For default values see: https://cloud.google.com/storage/docs/locations and
// https://cloud.google.com/storage/docs/storage-classes
await storage.createBucket(bucketName, {
location: 'US-CENTRAL1',
storageClass: 'COLDLINE',
});
}
async function cleanupBucket(bucketName) {
// GCS bucket will fail to delete if it is non-empty.
// There is no 'force=true' argument in the Node GCS client,
// so we cannot use force deletion here and have to first delete all files,
// and only then delete the actual bucket.
const promises = [];
for (let i = 0; i <= 3; i++) {
promises.push(deleteFileInBucket(bucketName, `output_task_${i}.txt`));
}
await Promise.all(promises);
await deleteBucket(bucketName);
}
async function deleteBucket(bucketName) {
await storage.bucket(bucketName).delete();
}
async function deleteFileInBucket(bucketName, fileName) {
await storage
.bucket(bucketName)
.file(fileName)
.delete({ignoreNotFound: true});
}
async function waitForJobToSucceed(projectID, region, jobName) {
const maxAttempts = 100;
const pollInterval = 2000; // in milliseconds
let succeeded = false;
for (let i = 0; i < maxAttempts; i++) {
const jobResponse = await getJob(projectID, region, jobName);
const job = jobResponse[0];
if (job.status.state === 'SUCCEEDED') {
succeeded = true;
break;
} else if (job.status.state === 'FAILED') {
throw new Error('Test job failed');
}
await sleep(pollInterval);
}
if (!succeeded) {
throw new Error(
'Timed out waiting for the batch job to succeed after ${maxAttempts} attempts'
);
}
}
async function getJob(projectId, region, jobName) {
const request = {
name: `projects/${projectId}/locations/${region}/jobs/${jobName}`,
};
return await batchClient.getJob(request);
}
function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
async function projectIdToNumber(projectId) {
// Construct request
const request = {
name: `projects/${projectId}`,
};
// Run request
const [response] = await resourcemanagerClient.getProject(request);
const projectNumber = response.name.split('/')[1];
return projectNumber;
}
/**
* Creates a new instance template with the provided name and a specific instance configuration.
* Includes all the stuff neeeded for Batch, such as service accounts.
*
* @param {string} projectId - Project ID or project number of the Cloud project you use.
* @param {string} templateName - Name of the new template to create.
*/
async function createTemplate(projectId, templateName) {
const projectNumber = await projectIdToNumber(projectId);
const serviceAccountAddress = `${projectNumber}[email protected]`;
const [response] = await instanceTemplatesClient.insert({
project: projectId,
instanceTemplateResource: {
name: templateName,
properties: {
disks: [
{
// The template describes the size and source image of the boot disk
// to attach to the instance.
initializeParams: {
diskSizeGb: '25',
sourceImage:
'projects/debian-cloud/global/images/family/debian-11',
},
autoDelete: true,
boot: true,
},
],
machineType: 'e2-standard-4',
// The template connects the instance to the `default` network,
// without specifying a subnetwork.
networkInterfaces: [
{
// Use the network interface provided in the networkName argument.
name: 'global/networks/default',
// The template lets the instance use an external IP address.
accessConfigs: [
{
name: 'External NAT',
type: 'ONE_TO_ONE_NAT',
networkTier: 'PREMIUM',
},
],
},
],
serviceAccounts: [
{
email: serviceAccountAddress,
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',
],
},
],
},
},
});
let operation = response.latestResponse;
const operationsClient = new compute.GlobalOperationsClient();
// Wait for the create operation to complete.
while (operation.status !== 'DONE') {
[operation] = await operationsClient.wait({
operation: operation.name,
project: projectId,
});
}
}
// Delete an instance template.
async function deleteInstanceTemplate(projectId, templateName) {
const [response] = await instanceTemplatesClient.delete({
project: projectId,
instanceTemplate: templateName,
});
let operation = response.latestResponse;
const operationsClient = new compute.GlobalOperationsClient();
// Wait for the create operation to complete.
while (operation.status !== 'DONE') {
[operation] = await operationsClient.wait({
operation: operation.name,
project: projectId,
});
}
}