Skip to content

Commit fff465a

Browse files
Joanna Grycziennae
authored andcommitted
Refactor
1 parent 3868b7a commit fff465a

10 files changed

+503
-381
lines changed

compute/test/computeTpu.test.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const path = require('path');
20+
const assert = require('node:assert/strict');
21+
const {before, after, describe, it} = require('mocha');
22+
const cp = require('child_process');
23+
const {NetworksClient, GlobalOperationsClient} =
24+
require('@google-cloud/compute').v1;
25+
const {getStaleNodes, deleteNode} = require('./util');
26+
27+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
28+
const cwd = path.join(__dirname, '..');
29+
30+
async function createNetwork(networkName, region) {
31+
const networksClient = new NetworksClient();
32+
const globalOperationsClient = new GlobalOperationsClient();
33+
const projectId = await networksClient.getProjectId();
34+
35+
// Create the network
36+
const network = {
37+
name: networkName,
38+
autoCreateSubnetworks: true,
39+
routingMode: 'REGIONAL',
40+
};
41+
42+
const [networkResponse] = await networksClient.insert({
43+
project: projectId,
44+
region: region,
45+
networkResource: network,
46+
});
47+
48+
let networkOperation = networkResponse.latestResponse;
49+
50+
// Wait for the create operation to complete.
51+
while (networkOperation.status !== 'DONE') {
52+
[networkOperation] = await globalOperationsClient.wait({
53+
operation: networkOperation.name,
54+
project: projectId,
55+
});
56+
}
57+
58+
console.log(`Network ${networkName} created in region ${region}.`);
59+
}
60+
61+
async function deleteNetwork(networkName) {
62+
const networksClient = new NetworksClient();
63+
const globalOperationsClient = new GlobalOperationsClient();
64+
const projectId = await networksClient.getProjectId();
65+
66+
const [networkResponse] = await networksClient.delete({
67+
project: projectId,
68+
network: networkName,
69+
});
70+
71+
let networkOperation = networkResponse.latestResponse;
72+
73+
// Wait for the operation to complete.
74+
while (networkOperation.status !== 'DONE') {
75+
[networkOperation] = await globalOperationsClient.wait({
76+
operation: networkOperation.name,
77+
project: projectId,
78+
});
79+
}
80+
}
81+
82+
describe('Compute tpu', async () => {
83+
const nodePrefix = 'node-name-2a2b3c';
84+
const nodeName = `${nodePrefix}${Math.floor(Math.random() * 1000 + 1)}`;
85+
const networkName = 'compute-tpu-network';
86+
const region = 'europe-west4';
87+
const zone = `${region}-a`;
88+
const tpuType = 'v2-8';
89+
const tpuSoftwareVersion = 'tpu-vm-tf-2.14.1';
90+
91+
before(async () => {
92+
// Cleanup resources
93+
const nodes = await getStaleNodes(nodePrefix, zone);
94+
await Promise.all(nodes.map(node => deleteNode(zone, node.nodeName)));
95+
// Create network
96+
await createNetwork(networkName, region);
97+
});
98+
99+
after(async () => {
100+
// Delete network
101+
await deleteNetwork(networkName);
102+
});
103+
104+
it('should create a new tpu node', () => {
105+
const response = execSync(
106+
`node ./tpu/vmCreate.js ${nodeName} ${zone} ${tpuType} ${tpuSoftwareVersion}`,
107+
{
108+
cwd,
109+
}
110+
);
111+
assert(response.includes(`TPU VM: ${nodeName} created.`));
112+
});
113+
114+
it('should return tpu node', () => {
115+
const response = execSync(`node ./tpu/vmGet.js ${nodeName} ${zone}`, {
116+
cwd,
117+
});
118+
119+
assert(response.includes(`Node: ${nodeName} retrived.`));
120+
});
121+
122+
it('should return list of tpu nodes', () => {
123+
const response = JSON.parse(
124+
execSync(`node ./tpu/vmList.js ${zone}`, {
125+
cwd,
126+
})
127+
);
128+
129+
assert(Array.isArray(response));
130+
});
131+
132+
it('should delete tpu node', () => {
133+
const response = execSync(`node ./tpu/vmDelete.js ${nodeName} ${zone}`, {
134+
cwd,
135+
});
136+
137+
assert(response.includes(`Node: ${nodeName} deleted.`));
138+
});
139+
});

0 commit comments

Comments
 (0)