|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const {assert} = require('chai'); |
| 18 | +const {after, before, describe, it} = require('mocha'); |
| 19 | +const uuid = require('uuid'); |
| 20 | +const cp = require('child_process'); |
| 21 | +const {Storage} = require('@google-cloud/storage'); |
| 22 | + |
| 23 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 24 | + |
| 25 | +const storage = new Storage(); |
| 26 | +const bucketName = `asset-nodejs-${uuid.v4()}`; |
| 27 | +const bucket = storage.bucket(bucketName); |
| 28 | +const fileSuffix = `${uuid.v4()}`; |
| 29 | + |
| 30 | +const {BigQuery} = require('@google-cloud/bigquery'); |
| 31 | +const bigquery = new BigQuery(); |
| 32 | +const options = { |
| 33 | + location: 'US', |
| 34 | +}; |
| 35 | +const datasetId = `asset_nodejs_${uuid.v4()}`.replace(/-/gi, '_'); |
| 36 | + |
| 37 | +const compute = require('@google-cloud/compute'); |
| 38 | +const instancesClient = new compute.InstancesClient(); |
| 39 | + |
| 40 | +// Some of these tests can take an extremely long time, and occasionally |
| 41 | +// timeout, see: |
| 42 | +// "Timeout of 180000ms exceeded. For async tests and hooks". |
| 43 | +function sleep(ms) { |
| 44 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 45 | +} |
| 46 | + |
| 47 | +describe('quickstart sample tests', () => { |
| 48 | + let projectId; |
| 49 | + let zone; |
| 50 | + let instanceName; |
| 51 | + let machineType; |
| 52 | + let sourceImage; |
| 53 | + let networkName; |
| 54 | + 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 | + |
| 63 | + await bucket.create(); |
| 64 | + await bigquery.createDataset(datasetId, options); |
| 65 | + await bigquery.dataset(datasetId).exists(); |
| 66 | + |
| 67 | + const [response] = await instancesClient.insert({ |
| 68 | + instanceResource: { |
| 69 | + name: instanceName, |
| 70 | + disks: [ |
| 71 | + { |
| 72 | + // Describe the size and source image of the boot disk to attach to the instance. |
| 73 | + initializeParams: { |
| 74 | + diskSizeGb: '10', |
| 75 | + sourceImage, |
| 76 | + }, |
| 77 | + autoDelete: true, |
| 78 | + boot: true, |
| 79 | + type: 'PERSISTENT', |
| 80 | + }, |
| 81 | + ], |
| 82 | + machineType: `zones/${zone}/machineTypes/${machineType}`, |
| 83 | + networkInterfaces: [ |
| 84 | + { |
| 85 | + // Use the network interface provided in the networkName argument. |
| 86 | + name: networkName, |
| 87 | + }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + project: projectId, |
| 91 | + zone, |
| 92 | + }); |
| 93 | + let operation = response.latestResponse; |
| 94 | + const operationsClient = new compute.ZoneOperationsClient(); |
| 95 | + |
| 96 | + // Wait for the create operation to complete. |
| 97 | + while (operation.status !== 'DONE') { |
| 98 | + [operation] = await operationsClient.wait({ |
| 99 | + operation: operation.name, |
| 100 | + project: projectId, |
| 101 | + zone: operation.zone.split('/').pop(), |
| 102 | + }); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + after(async () => { |
| 107 | + await bucket.delete(); |
| 108 | + await bigquery.dataset(datasetId).delete({force: true}).catch(console.warn); |
| 109 | + const [response] = await instancesClient.delete({ |
| 110 | + instance: instanceName, |
| 111 | + project: projectId, |
| 112 | + zone, |
| 113 | + }); |
| 114 | + let operation = response.latestResponse; |
| 115 | + const operationsClient = new compute.ZoneOperationsClient(); |
| 116 | + |
| 117 | + // Wait for the delete operation to complete. |
| 118 | + while (operation.status !== 'DONE') { |
| 119 | + [operation] = await operationsClient.wait({ |
| 120 | + operation: operation.name, |
| 121 | + project: projectId, |
| 122 | + zone: operation.zone.split('/').pop(), |
| 123 | + }); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + it('should export assets to specified path', async () => { |
| 128 | + const dumpFilePath = `gs://${bucketName}/my-assets-${fileSuffix}.txt`; |
| 129 | + execSync(`node exportAssets ${dumpFilePath}`); |
| 130 | + let waitMs = 4000; |
| 131 | + let exists = false; |
| 132 | + let file; |
| 133 | + for (let retry = 0; retry < 3 && !exists; ++retry) { |
| 134 | + await sleep((waitMs *= 2)); |
| 135 | + file = await bucket.file(`my-assets-${fileSuffix}.txt`); |
| 136 | + exists = await file.exists(); |
| 137 | + } |
| 138 | + assert.ok(exists); |
| 139 | + await file.delete(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should export asset relationships to specified path', async () => { |
| 143 | + const dumpFilePath = `gs://${bucketName}/my-relationships-${fileSuffix}.txt`; |
| 144 | + const contentType = 'RELATIONSHIP'; |
| 145 | + execSync(`node exportAssets ${dumpFilePath} ${contentType}`); |
| 146 | + let waitMs = 4000; |
| 147 | + let exists = false; |
| 148 | + let file; |
| 149 | + for (let retry = 0; retry < 3 && !exists; ++retry) { |
| 150 | + await sleep((waitMs *= 2)); |
| 151 | + file = await bucket.file(`my-relationships-${fileSuffix}.txt`); |
| 152 | + exists = await file.exists(); |
| 153 | + } |
| 154 | + assert.ok(exists); |
| 155 | + await file.delete(); |
| 156 | + }); |
| 157 | + |
| 158 | + // The assets returned within 'readTimeWindow' frequently do not include |
| 159 | + // the newly created bucket: |
| 160 | + it('should get assets history successfully', async () => { |
| 161 | + const assetName = `//storage.googleapis.com/${bucketName}`; |
| 162 | + let waitMs = 1000; |
| 163 | + let included = false; |
| 164 | + for (let retry = 0; retry < 3 && !included; ++retry) { |
| 165 | + await sleep((waitMs *= 2)); |
| 166 | + const stdout = execSync( |
| 167 | + `node getBatchAssetHistory ${assetName} 'RESOURCE'` |
| 168 | + ); |
| 169 | + included = stdout.includes(assetName); |
| 170 | + } |
| 171 | + assert.ok(included); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should run the quickstart', async () => { |
| 175 | + const assetName = `//storage.googleapis.com/${bucketName}`; |
| 176 | + const stdout = execSync(`node quickstart ${assetName}`); |
| 177 | + assert.include(stdout, assetName); |
| 178 | + }); |
| 179 | + |
| 180 | + // https://github.com/googleapis/nodejs-asset/issues/650 |
| 181 | + it.skip('should search all resources successfully', async () => { |
| 182 | + const query = `name:${instanceName}`; |
| 183 | + const stdout = execSync(`node searchAllResources '' ${query}`); |
| 184 | + assert.include(stdout, instanceName); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should search all iam policies successfully', async () => { |
| 188 | + const query = 'policy:roles/owner'; |
| 189 | + const stdout = execSync(`node searchAllIamPolicies '' ${query}`); |
| 190 | + assert.include(stdout, 'roles/owner'); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should list assets successfully', async () => { |
| 194 | + const assetType = 'storage.googleapis.com/Bucket'; |
| 195 | + const stdout = execSync(`node listAssets ${assetType} 'RESOURCE'`); |
| 196 | + assert.include(stdout, assetType); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should list asset relationship successfully', async () => { |
| 200 | + const stdout = execSync("node listAssets '' 'RELATIONSHIP'"); |
| 201 | + assert.include(stdout, 'relatedAsset'); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should get effective iam policies successfully', async () => { |
| 205 | + const assetName = `//storage.googleapis.com/${bucketName}`; |
| 206 | + const stdout = execSync(`node getBatchEffectiveIamPolicies ${assetName}`); |
| 207 | + assert.include(stdout, assetName); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should analyze iam policy successfully', async () => { |
| 211 | + const stdout = execSync('node analyzeIamPolicy'); |
| 212 | + assert.include(stdout, '//cloudresourcemanager.googleapis.com/projects'); |
| 213 | + }); |
| 214 | + |
| 215 | + it('should analyze iam policy and write analysis results to gcs successfully', async () => { |
| 216 | + const uri = `gs://${bucketName}/my-analysis.json`; |
| 217 | + execSync(`node analyzeIamPolicyLongrunningGcs ${uri}`); |
| 218 | + let waitMs = 1000; |
| 219 | + let exists = false; |
| 220 | + let file; |
| 221 | + for (let retry = 0; retry < 3 && !exists; ++retry) { |
| 222 | + await sleep((waitMs *= 2)); |
| 223 | + file = await bucket.file('my-analysis.json'); |
| 224 | + exists = await file.exists(); |
| 225 | + } |
| 226 | + assert.ok(exists); |
| 227 | + await file.delete(); |
| 228 | + }); |
| 229 | + |
| 230 | + it('should analyze iam policy and write analysis results to bigquery successfully', async () => { |
| 231 | + const tablePrefix = 'analysis_nodejs'; |
| 232 | + execSync( |
| 233 | + `node analyzeIamPolicyLongrunningBigquery ${datasetId} ${tablePrefix}` |
| 234 | + ); |
| 235 | + let waitMs = 4000; |
| 236 | + let metadataTable; |
| 237 | + let metadataTable_exists = false; |
| 238 | + let resultsTable; |
| 239 | + let resultsTable_exists = false; |
| 240 | + |
| 241 | + for ( |
| 242 | + let retry = 0; |
| 243 | + retry < 3 && !(metadataTable_exists || resultsTable_exists); |
| 244 | + ++retry |
| 245 | + ) { |
| 246 | + await sleep((waitMs *= 2)); |
| 247 | + metadataTable = await bigquery |
| 248 | + .dataset(datasetId) |
| 249 | + .table('analysis_nodejs_analysis'); |
| 250 | + metadataTable_exists = await metadataTable.exists(); |
| 251 | + resultsTable = await bigquery |
| 252 | + .dataset(datasetId) |
| 253 | + .table('analysis_nodejs_analysis_result'); |
| 254 | + resultsTable_exists = await resultsTable.exists(); |
| 255 | + } |
| 256 | + |
| 257 | + assert.ok(metadataTable_exists); |
| 258 | + assert.ok(resultsTable_exists); |
| 259 | + await metadataTable.delete(); |
| 260 | + await resultsTable.delete(); |
| 261 | + }); |
| 262 | +}); |
0 commit comments