From e9b7bead769dd3d3055b3a532528a26ca328d2c4 Mon Sep 17 00:00:00 2001 From: sofisl <55454395+sofisl@users.noreply.github.com> Date: Thu, 16 Jun 2022 15:39:26 -0700 Subject: [PATCH] samples: fix compute test, update dependency to V3 (#634) * samples: fix compute test, update dependency to V3 --- .../google-cloud-asset/samples/package.json | 2 +- .../samples/test/sample.test.js | 68 ++++++++++++++++--- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/packages/google-cloud-asset/samples/package.json b/packages/google-cloud-asset/samples/package.json index 4d17638bb18..52b81d34f5a 100644 --- a/packages/google-cloud-asset/samples/package.json +++ b/packages/google-cloud-asset/samples/package.json @@ -17,7 +17,7 @@ "dependencies": { "@google-cloud/asset": "^3.23.1", "@google-cloud/bigquery": "^6.0.0", - "@google-cloud/compute": "^2.0.0", + "@google-cloud/compute": "^3.0.0", "@google-cloud/storage": "^6.0.0", "uuid": "^8.0.0", "yargs": "^16.0.0" diff --git a/packages/google-cloud-asset/samples/test/sample.test.js b/packages/google-cloud-asset/samples/test/sample.test.js index 21b1baf8378..8727a710f81 100644 --- a/packages/google-cloud-asset/samples/test/sample.test.js +++ b/packages/google-cloud-asset/samples/test/sample.test.js @@ -34,11 +34,8 @@ const options = { }; const datasetId = `asset_nodejs_${uuid.v4()}`.replace(/-/gi, '_'); -const Compute = require('@google-cloud/compute'); -const zone = new Compute().zone('us-central1-c'); -const vmName = `asset-nodejs-${uuid.v4()}`; - -let vm; +const compute = require('@google-cloud/compute'); +const instancesClient = new compute.InstancesClient(); // Some of these tests can take an extremely long time, and occasionally // timeout, see: @@ -48,17 +45,68 @@ function sleep(ms) { } describe('quickstart sample tests', () => { + let projectId; + let zone; + let instanceName; + let machineType; + let sourceImage; + let networkName; before(async () => { + zone = 'us-central1-a'; + instanceName = `asset-nodejs-${uuid.v4()}`; + machineType = 'n1-standard-1'; + sourceImage = + 'projects/ubuntu-os-cloud/global/images/family/ubuntu-1804-lts'; + networkName = 'global/networks/default'; + projectId = await instancesClient.getProjectId(); + await bucket.create(); await bigquery.createDataset(datasetId, options); await bigquery.dataset(datasetId).exists(); - [vm] = await zone.createVM(vmName, {os: 'ubuntu'}); + // [vm] = await zone.vm(vmName, {os: 'ubuntu'}); + + const [response] = await instancesClient.insert({ + instanceResource: { + name: instanceName, + disks: [ + { + // Describe the size and source image of the boot disk to attach to the instance. + initializeParams: { + diskSizeGb: '10', + sourceImage, + }, + autoDelete: true, + boot: true, + type: 'PERSISTENT', + }, + ], + machineType: `zones/${zone}/machineTypes/${machineType}`, + networkInterfaces: [ + { + // Use the network interface provided in the networkName argument. + name: networkName, + }, + ], + }, + project: projectId, + zone, + }); + let operation = response.latestResponse; + const operationsClient = new compute.ZoneOperationsClient(); + + // Wait for the create operation to complete. + while (operation.status !== 'DONE') { + [operation] = await operationsClient.wait({ + operation: operation.name, + project: projectId, + zone: operation.zone.split('/').pop(), + }); + } }); after(async () => { await bucket.delete(); await bigquery.dataset(datasetId).delete({force: true}).catch(console.warn); - await vm.delete(); }); it('should export assets to specified path', async () => { @@ -114,10 +162,10 @@ describe('quickstart sample tests', () => { assert.include(stdout, assetName); }); - it.skip('should search all resources successfully', async () => { - const query = `name:${vmName}`; + it('should search all resources successfully', async () => { + const query = `name:${instanceName}`; const stdout = execSync(`node searchAllResources '' ${query}`); - assert.include(stdout, vmName); + assert.include(stdout, instanceName); }); it('should search all iam policies successfully', async () => {