|
| 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 uuid = require('uuid'); |
| 22 | +const {after, before, describe, it} = require('mocha'); |
| 23 | +const cp = require('child_process'); |
| 24 | +const computeLib = require('@google-cloud/compute'); |
| 25 | +const {getStaleVMInstances, deleteInstance} = require('./util'); |
| 26 | + |
| 27 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 28 | +const cwd = path.join(__dirname, '..'); |
| 29 | + |
| 30 | +const disksClient = new computeLib.DisksClient(); |
| 31 | + |
| 32 | +async function createDisk(projectId, zone, diskName) { |
| 33 | + const [response] = await disksClient.insert({ |
| 34 | + project: projectId, |
| 35 | + zone, |
| 36 | + diskResource: { |
| 37 | + name: diskName, |
| 38 | + }, |
| 39 | + }); |
| 40 | + let operation = response.latestResponse; |
| 41 | + const operationsClient = new computeLib.ZoneOperationsClient(); |
| 42 | + |
| 43 | + // Wait for the create operation to complete. |
| 44 | + while (operation.status !== 'DONE') { |
| 45 | + [operation] = await operationsClient.wait({ |
| 46 | + operation: operation.name, |
| 47 | + project: projectId, |
| 48 | + zone: operation.zone.split('/').pop(), |
| 49 | + }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +async function deleteDisk(projectId, zone, diskName) { |
| 54 | + const [response] = await disksClient.delete({ |
| 55 | + project: projectId, |
| 56 | + zone, |
| 57 | + disk: diskName, |
| 58 | + }); |
| 59 | + let operation = response.latestResponse; |
| 60 | + const operationsClient = new computeLib.ZoneOperationsClient(); |
| 61 | + |
| 62 | + // Wait for the delete operation to complete. |
| 63 | + while (operation.status !== 'DONE') { |
| 64 | + [operation] = await operationsClient.wait({ |
| 65 | + operation: operation.name, |
| 66 | + project: projectId, |
| 67 | + zone: operation.zone.split('/').pop(), |
| 68 | + }); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +async function createDiskSnapshot(projectId, zone, diskName, snapshotName) { |
| 73 | + const [response] = await disksClient.createSnapshot({ |
| 74 | + project: projectId, |
| 75 | + zone, |
| 76 | + disk: diskName, |
| 77 | + snapshotResource: { |
| 78 | + name: snapshotName, |
| 79 | + }, |
| 80 | + }); |
| 81 | + let operation = response.latestResponse; |
| 82 | + const operationsClient = new computeLib.ZoneOperationsClient(); |
| 83 | + |
| 84 | + // Wait for the create operation to complete. |
| 85 | + while (operation.status !== 'DONE') { |
| 86 | + [operation] = await operationsClient.wait({ |
| 87 | + operation: operation.name, |
| 88 | + project: projectId, |
| 89 | + zone: operation.zone.split('/').pop(), |
| 90 | + }); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +async function deleteDiskSnapshot(projectId, snapshotName) { |
| 95 | + const snapshotsClient = new computeLib.SnapshotsClient(); |
| 96 | + const [response] = await snapshotsClient.delete({ |
| 97 | + project: projectId, |
| 98 | + snapshot: snapshotName, |
| 99 | + }); |
| 100 | + let operation = response.latestResponse; |
| 101 | + const operationsClient = new computeLib.GlobalOperationsClient(); |
| 102 | + |
| 103 | + // Wait for the delete operation to complete. |
| 104 | + while (operation.status !== 'DONE') { |
| 105 | + [operation] = await operationsClient.wait({ |
| 106 | + operation: operation.name, |
| 107 | + project: projectId, |
| 108 | + }); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +describe('Create compute instance with replicated boot disk', async () => { |
| 113 | + const vmName = `instance-replicated-disk-${uuid.v4()}`; |
| 114 | + const snapshotName = `snapshot-${uuid.v4()}`; |
| 115 | + const diskName = `snapshot-disk-${uuid.v4()}`; |
| 116 | + const zone1 = 'us-central1-a'; |
| 117 | + const zone2 = 'us-central1-b'; |
| 118 | + let projectId; |
| 119 | + let diskSnapshotLink; |
| 120 | + |
| 121 | + before(async () => { |
| 122 | + const instancesClient = new computeLib.InstancesClient(); |
| 123 | + projectId = await instancesClient.getProjectId(); |
| 124 | + diskSnapshotLink = `projects/${projectId}/global/snapshots/${snapshotName}`; |
| 125 | + |
| 126 | + await createDisk(projectId, zone1, diskName); |
| 127 | + await createDiskSnapshot(projectId, zone1, diskName, snapshotName); |
| 128 | + }); |
| 129 | + |
| 130 | + after(async () => { |
| 131 | + // Cleanup resources |
| 132 | + const instances = await getStaleVMInstances(); |
| 133 | + await Promise.all( |
| 134 | + instances.map(instance => |
| 135 | + deleteInstance(instance.zone, instance.instanceName) |
| 136 | + ) |
| 137 | + ); |
| 138 | + await deleteDiskSnapshot(projectId, snapshotName); |
| 139 | + await deleteDisk(projectId, zone1, diskName); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should create an instance with replicated boot disk', () => { |
| 143 | + const response = execSync( |
| 144 | + `node ./instances/create-start-instance/createInstanceReplicatedBootDisk.js ${zone1} ${zone2} ${vmName} ${diskSnapshotLink}`, |
| 145 | + { |
| 146 | + cwd, |
| 147 | + } |
| 148 | + ); |
| 149 | + |
| 150 | + assert( |
| 151 | + response.includes( |
| 152 | + `Instance: ${vmName} with replicated boot disk created.` |
| 153 | + ) |
| 154 | + ); |
| 155 | + }); |
| 156 | +}); |
0 commit comments