Skip to content

Commit dbaecb6

Browse files
Joanna Gryczgryczj
authored andcommitted
Use mocked TPUClient in tests
1 parent d3b82bd commit dbaecb6

File tree

8 files changed

+254
-173
lines changed

8 files changed

+254
-173
lines changed

tpu/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"test": "c8 mocha -p -j 2 test --timeout 1200000"
1515
},
1616
"dependencies": {
17-
"@google-cloud/tpu": "^3.5.0"
17+
"@google-cloud/tpu": "^3.5.0",
18+
"sinon": "^19.0.2"
1819
},
1920
"devDependencies": {
2021
"c8": "^10.0.0",

tpu/queuedResources/createQueuedResource.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,20 @@
1616

1717
'use strict';
1818

19-
async function main(
20-
nodeName,
21-
queuedResourceName,
22-
zone,
23-
tpuType,
24-
tpuSoftwareVersion
25-
) {
19+
async function main(tpuClient) {
2620
// [START tpu_queued_resources_create]
27-
// Import the TPU library
28-
const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
21+
// Import the TPUClient
22+
// TODO(developer): Uncomment below line before running the sample.
23+
// const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
2924
const {Node, NetworkConfig, QueuedResource} =
3025
require('@google-cloud/tpu').protos.google.cloud.tpu.v2alpha1;
3126

3227
// Instantiate a tpuClient
33-
const tpuClient = new TpuClient();
28+
// TODO(developer): Uncomment below line before running the sample.
29+
// tpuClient = new TpuClient();
3430

3531
/**
36-
* TODO(developer): Update/uncomment these variables before running the sample.
32+
* TODO(developer): Update these variables before running the sample.
3733
*/
3834
// Project ID or project number of the Google Cloud project, where you want to create queued resource.
3935
const projectId = await tpuClient.getProjectId();
@@ -45,24 +41,24 @@ async function main(
4541
const region = 'europe-west4';
4642

4743
// The name for your queued resource.
48-
// queuedResourceName = 'queued-resource-1';
44+
const queuedResourceName = 'queued-resource-1';
4945

5046
// The name for your node.
51-
// nodeName = 'node-name-1';
47+
const nodeName = 'node-name-1';
5248

5349
// The zone in which to create the node.
5450
// For more information about supported TPU types for specific zones,
5551
// see https://cloud.google.com/tpu/docs/regions-zones
56-
// zone = 'europe-west4-a';
52+
const zone = 'europe-west4-a';
5753

5854
// The accelerator type that specifies the version and size of the node you want to create.
5955
// For more information about supported accelerator types for each TPU version,
6056
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
61-
// tpuType = 'v2-8';
57+
const tpuType = 'v2-8';
6258

6359
// Software version that specifies the version of the node runtime to install. For more information,
6460
// see https://cloud.google.com/tpu/docs/runtimes
65-
// tpuSoftwareVersion = 'tpu-vm-tf-2.14.1';
61+
const tpuSoftwareVersion = 'tpu-vm-tf-2.14.1';
6662

6763
async function callCreateQueuedResource() {
6864
// Create a node
@@ -108,17 +104,21 @@ async function main(
108104
const [operation] = await tpuClient.createQueuedResource(request);
109105

110106
// Wait for the create operation to complete.
111-
await operation.promise();
107+
const [response] = await operation.promise();
112108

113109
// You can wait until TPU Node is READY,
114-
// and check its status using getTpuVm() from `tpu_vm_get` sample.
110+
// and check its status using callGetTpuVm() from `tpu_vm_get` sample.
115111
console.log(`Queued resource ${queuedResourceName} created.`);
112+
return response;
116113
}
117-
await callCreateQueuedResource();
114+
return await callCreateQueuedResource();
118115
// [END tpu_queued_resources_create]
119116
}
120117

121-
main(...process.argv.slice(2)).catch(err => {
122-
console.error(err);
123-
process.exitCode = 1;
124-
});
118+
module.exports = main;
119+
120+
// TODO(developer): Uncomment below lines before running the sample.
121+
// main(...process.argv.slice(2)).catch(err => {
122+
// console.error(err);
123+
// process.exitCode = 1;
124+
// });

tpu/queuedResources/deleteQueuedResource.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,27 @@
1616

1717
'use strict';
1818

19-
async function main(queuedResourceName, zone) {
19+
async function main(tpuClient) {
2020
// [START tpu_queued_resources_delete]
21-
// Import the TPU library
22-
const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
21+
// Import the TPUClient
22+
// TODO(developer): Uncomment below line before running the sample.
23+
// const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
2324

2425
// Instantiate a tpuClient
25-
const tpuClient = new TpuClient();
26+
// TODO(developer): Uncomment below line before running the sample.
27+
// tpuClient = new TpuClient();
2628

2729
/**
28-
* TODO(developer): Update/uncomment these variables before running the sample.
30+
* TODO(developer): Update these variables before running the sample.
2931
*/
3032
// Project ID or project number of the Google Cloud project, where you want to delete node.
3133
const projectId = await tpuClient.getProjectId();
3234

3335
// The name of queued resource.
34-
// queuedResourceName = 'queued-resource-1';
36+
const queuedResourceName = 'queued-resource-1';
3537

3638
// The zone of your queued resource.
37-
// zone = 'europe-west4-a';
39+
const zone = 'europe-west4-a';
3840

3941
async function callDeleteTpuVM(nodeName) {
4042
const request = {
@@ -55,24 +57,28 @@ async function main(queuedResourceName, zone) {
5557
};
5658

5759
// Retrive node name
58-
const [response] = await tpuClient.getQueuedResource(request);
59-
const nodeName = response.tpu.nodeSpec[0].nodeId;
60+
const [queuedResource] = await tpuClient.getQueuedResource(request);
61+
const nodeName = queuedResource.tpu.nodeSpec[0].nodeId;
6062

6163
// Before deleting the queued resource it is required to delete the TPU VM.
6264
await callDeleteTpuVM(nodeName);
6365

6466
const [operation] = await tpuClient.deleteQueuedResource(request);
6567

6668
// Wait for the delete operation to complete.
67-
await operation.promise();
69+
const [response] = await operation.promise();
6870

6971
console.log(`Queued resource ${queuedResourceName} deleted.`);
72+
return response;
7073
}
71-
await callDeleteQueuedResource();
74+
return await callDeleteQueuedResource();
7275
// [END tpu_queued_resources_delete]
7376
}
7477

75-
main(...process.argv.slice(2)).catch(err => {
76-
console.error(err);
77-
process.exitCode = 1;
78-
});
78+
module.exports = main;
79+
80+
// TODO(developer): Uncomment below lines before running the sample.
81+
// main(...process.argv.slice(2)).catch(err => {
82+
// console.error(err);
83+
// process.exitCode = 1;
84+
// });

tpu/queuedResources/forceDeleteQueuedResource.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,27 @@
1616

1717
'use strict';
1818

19-
async function main(queuedResourceName, zone) {
19+
async function main(tpuClient) {
2020
// [START tpu_queued_resources_delete_force]
21-
// Import the TPU library
22-
const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
21+
// Import the TPUClient
22+
// TODO(developer): Uncomment below line before running the sample.
23+
// const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
2324

2425
// Instantiate a tpuClient
25-
const tpuClient = new TpuClient();
26+
// TODO(developer): Uncomment below line before running the sample.
27+
// tpuClient = new TpuClient();
2628

2729
/**
28-
* TODO(developer): Update/uncomment these variables before running the sample.
30+
* TODO(developer): Update these variables before running the sample.
2931
*/
3032
// Project ID or project number of the Google Cloud project, where you want to delete node.
3133
const projectId = await tpuClient.getProjectId();
3234

3335
// The name of queued resource.
34-
// queuedResourceName = 'queued-resource-1';
36+
const queuedResourceName = 'queued-resource-1';
3537

3638
// The zone of your queued resource.
37-
// zone = 'europe-west4-a';
39+
const zone = 'europe-west4-a';
3840

3941
async function callForceDeleteQueuedResource() {
4042
const request = {
@@ -45,15 +47,19 @@ async function main(queuedResourceName, zone) {
4547
const [operation] = await tpuClient.deleteQueuedResource(request);
4648

4749
// Wait for the delete operation to complete.
48-
await operation.promise();
50+
const [response] = await operation.promise();
4951

5052
console.log(`Queued resource ${queuedResourceName} deletion forced.`);
53+
return response;
5154
}
52-
await callForceDeleteQueuedResource();
55+
return await callForceDeleteQueuedResource();
5356
// [END tpu_queued_resources_delete_force]
5457
}
5558

56-
main(...process.argv.slice(2)).catch(err => {
57-
console.error(err);
58-
process.exitCode = 1;
59-
});
59+
module.exports = main;
60+
61+
// TODO(developer): Uncomment below lines before running the sample.
62+
// main(...process.argv.slice(2)).catch(err => {
63+
// console.error(err);
64+
// process.exitCode = 1;
65+
// });

tpu/queuedResources/getQueuedResource.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,27 @@
1616

1717
'use strict';
1818

19-
async function main(queuedResourceName, zone) {
19+
async function main(tpuClient) {
2020
// [START tpu_queued_resources_get]
21-
// Import the TPU library
22-
const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
21+
// Import the TPUClient
22+
// TODO(developer): Uncomment below line before running the sample.
23+
// const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
2324

2425
// Instantiate a tpuClient
25-
const tpuClient = new TpuClient();
26+
// TODO(developer): Uncomment below line before running the sample.
27+
// tpuClient = new TpuClient();
2628

2729
/**
28-
* TODO(developer): Update/uncomment these variables before running the sample.
30+
* TODO(developer): Update these variables before running the sample.
2931
*/
3032
// Project ID or project number of the Google Cloud project, where you want to retrive node.
3133
const projectId = await tpuClient.getProjectId();
3234

3335
// The name of queued resource.
34-
// queuedResourceName = 'queued-resource-1';
36+
const queuedResourceName = 'queued-resource-1';
3537

3638
// The zone of your queued resource.
37-
// zone = 'europe-west4-a';
39+
const zone = 'europe-west4-a';
3840

3941
async function callGetQueuedResource() {
4042
const request = {
@@ -43,14 +45,17 @@ async function main(queuedResourceName, zone) {
4345

4446
const [response] = await tpuClient.getQueuedResource(request);
4547

46-
console.log(JSON.stringify(response));
4748
console.log(`Queued resource ${queuedResourceName} retrived.`);
49+
return response;
4850
}
49-
await callGetQueuedResource();
51+
return await callGetQueuedResource();
5052
// [END tpu_queued_resources_get]
5153
}
5254

53-
main(...process.argv.slice(2)).catch(err => {
54-
console.error(err);
55-
process.exitCode = 1;
56-
});
55+
module.exports = main;
56+
57+
// TODO(developer): Uncomment below lines before running the sample.
58+
// main(...process.argv.slice(2)).catch(err => {
59+
// console.error(err);
60+
// process.exitCode = 1;
61+
// });

tpu/queuedResources/getQueuedResourcesList.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
'use strict';
1818

19-
async function main(zone) {
19+
async function main(tpuClient) {
2020
// [START tpu_queued_resources_list]
21-
// Import the TPU library
22-
const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
21+
// Import the TPUClient
22+
// TODO(developer): Uncomment below line before running the sample.
23+
// const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
2324

2425
// Instantiate a tpuClient
25-
const tpuClient = new TpuClient();
26+
// TODO(developer): Uncomment below line before running the sample.
27+
// tpuClient = new TpuClient();
2628

2729
/**
2830
* TODO(developer): Update/uncomment these variables before running the sample.
@@ -31,7 +33,7 @@ async function main(zone) {
3133
const projectId = await tpuClient.getProjectId();
3234

3335
// The zone from which the Queued Resources are retrived.
34-
// zone = 'europe-west4-a';
36+
const zone = 'europe-west4-a';
3537

3638
async function callGetQueuedResourcesList() {
3739
const request = {
@@ -40,13 +42,16 @@ async function main(zone) {
4042

4143
const [response] = await tpuClient.listQueuedResources(request);
4244

43-
console.log(JSON.stringify(response));
45+
return response;
4446
}
45-
await callGetQueuedResourcesList();
47+
return await callGetQueuedResourcesList();
4648
// [END tpu_queued_resources_list]
4749
}
4850

49-
main(...process.argv.slice(2)).catch(err => {
50-
console.error(err);
51-
process.exitCode = 1;
52-
});
51+
module.exports = main;
52+
53+
// TODO(developer): Uncomment below lines before running the sample.
54+
// main(...process.argv.slice(2)).catch(err => {
55+
// console.error(err);
56+
// process.exitCode = 1;
57+
// });

0 commit comments

Comments
 (0)