Skip to content

Commit 4af3b0a

Browse files
authored
samples: fix compute test, update dependency to V3 (#634)
* samples: fix compute test, update dependency to V3
1 parent a728ecc commit 4af3b0a

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

asset/snippets/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"dependencies": {
1818
"@google-cloud/asset": "^3.23.1",
1919
"@google-cloud/bigquery": "^6.0.0",
20-
"@google-cloud/compute": "^2.0.0",
20+
"@google-cloud/compute": "^3.0.0",
2121
"@google-cloud/storage": "^6.0.0",
2222
"uuid": "^8.0.0",
2323
"yargs": "^16.0.0"

asset/snippets/test/sample.test.js

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ const options = {
3434
};
3535
const datasetId = `asset_nodejs_${uuid.v4()}`.replace(/-/gi, '_');
3636

37-
const Compute = require('@google-cloud/compute');
38-
const zone = new Compute().zone('us-central1-c');
39-
const vmName = `asset-nodejs-${uuid.v4()}`;
40-
41-
let vm;
37+
const compute = require('@google-cloud/compute');
38+
const instancesClient = new compute.InstancesClient();
4239

4340
// Some of these tests can take an extremely long time, and occasionally
4441
// timeout, see:
@@ -48,17 +45,68 @@ function sleep(ms) {
4845
}
4946

5047
describe('quickstart sample tests', () => {
48+
let projectId;
49+
let zone;
50+
let instanceName;
51+
let machineType;
52+
let sourceImage;
53+
let networkName;
5154
before(async () => {
55+
zone = 'us-central1-a';
56+
instanceName = `asset-nodejs-${uuid.v4()}`;
57+
machineType = 'n1-standard-1';
58+
sourceImage =
59+
'projects/ubuntu-os-cloud/global/images/family/ubuntu-1804-lts';
60+
networkName = 'global/networks/default';
61+
projectId = await instancesClient.getProjectId();
62+
5263
await bucket.create();
5364
await bigquery.createDataset(datasetId, options);
5465
await bigquery.dataset(datasetId).exists();
55-
[vm] = await zone.createVM(vmName, {os: 'ubuntu'});
66+
// [vm] = await zone.vm(vmName, {os: 'ubuntu'});
67+
68+
const [response] = await instancesClient.insert({
69+
instanceResource: {
70+
name: instanceName,
71+
disks: [
72+
{
73+
// Describe the size and source image of the boot disk to attach to the instance.
74+
initializeParams: {
75+
diskSizeGb: '10',
76+
sourceImage,
77+
},
78+
autoDelete: true,
79+
boot: true,
80+
type: 'PERSISTENT',
81+
},
82+
],
83+
machineType: `zones/${zone}/machineTypes/${machineType}`,
84+
networkInterfaces: [
85+
{
86+
// Use the network interface provided in the networkName argument.
87+
name: networkName,
88+
},
89+
],
90+
},
91+
project: projectId,
92+
zone,
93+
});
94+
let operation = response.latestResponse;
95+
const operationsClient = new compute.ZoneOperationsClient();
96+
97+
// Wait for the create operation to complete.
98+
while (operation.status !== 'DONE') {
99+
[operation] = await operationsClient.wait({
100+
operation: operation.name,
101+
project: projectId,
102+
zone: operation.zone.split('/').pop(),
103+
});
104+
}
56105
});
57106

58107
after(async () => {
59108
await bucket.delete();
60109
await bigquery.dataset(datasetId).delete({force: true}).catch(console.warn);
61-
await vm.delete();
62110
});
63111

64112
it('should export assets to specified path', async () => {
@@ -114,10 +162,10 @@ describe('quickstart sample tests', () => {
114162
assert.include(stdout, assetName);
115163
});
116164

117-
it.skip('should search all resources successfully', async () => {
118-
const query = `name:${vmName}`;
165+
it('should search all resources successfully', async () => {
166+
const query = `name:${instanceName}`;
119167
const stdout = execSync(`node searchAllResources '' ${query}`);
120-
assert.include(stdout, vmName);
168+
assert.include(stdout, instanceName);
121169
});
122170

123171
it('should search all iam policies successfully', async () => {

0 commit comments

Comments
 (0)