|
| 1 | +// Copyright 2022 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 compute = require('@google-cloud/compute'); |
| 18 | + |
| 19 | +const {describe, it} = require('mocha'); |
| 20 | +const cp = require('child_process'); |
| 21 | +const {assert} = require('chai'); |
| 22 | + |
| 23 | +const {generateTestId, getStaleVMInstances, deleteInstance} = require('./util'); |
| 24 | + |
| 25 | +const instancesClient = new compute.InstancesClient(); |
| 26 | +const zoneOperationsClient = new compute.ZoneOperationsClient(); |
| 27 | + |
| 28 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 29 | + |
| 30 | +describe('suspend/resume instance tests', () => { |
| 31 | + const instanceName = generateTestId(); |
| 32 | + const zone = 'europe-central2-b'; |
| 33 | + |
| 34 | + const delay = ms => new Promise(res => setTimeout(res, ms)); |
| 35 | + |
| 36 | + const getInstance = async (projectId, zone, instanceName) => { |
| 37 | + const [instance] = await instancesClient.get({ |
| 38 | + project: projectId, |
| 39 | + zone, |
| 40 | + instance: instanceName, |
| 41 | + }); |
| 42 | + return instance; |
| 43 | + }; |
| 44 | + |
| 45 | + after(async () => { |
| 46 | + const instances = await getStaleVMInstances(); |
| 47 | + await Promise.all( |
| 48 | + instances.map(instance => |
| 49 | + deleteInstance(instance.zone, instance.instanceName) |
| 50 | + ) |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should suspend and resume instance', async () => { |
| 55 | + const projectId = await instancesClient.getProjectId(); |
| 56 | + |
| 57 | + const [insertResponse] = await instancesClient.insert({ |
| 58 | + instanceResource: { |
| 59 | + name: instanceName, |
| 60 | + disks: [ |
| 61 | + { |
| 62 | + initializeParams: { |
| 63 | + diskSizeGb: '64', |
| 64 | + sourceImage: |
| 65 | + 'projects/ubuntu-os-cloud/global/images/family/ubuntu-2004-lts', |
| 66 | + }, |
| 67 | + autoDelete: true, |
| 68 | + boot: true, |
| 69 | + }, |
| 70 | + ], |
| 71 | + machineType: `zones/${zone}/machineTypes/n1-standard-1`, |
| 72 | + networkInterfaces: [ |
| 73 | + { |
| 74 | + name: 'global/networks/default', |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + project: projectId, |
| 79 | + zone, |
| 80 | + }); |
| 81 | + let operation = insertResponse.latestResponse; |
| 82 | + |
| 83 | + while (operation.status !== 'DONE') { |
| 84 | + [operation] = await zoneOperationsClient.wait({ |
| 85 | + operation: operation.name, |
| 86 | + project: projectId, |
| 87 | + zone: operation.zone.split('/').pop(), |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + // Once the machine is running, give it some time to fully start all processes |
| 92 | + // before trying to suspend it. |
| 93 | + await delay(45 * 1000); |
| 94 | + |
| 95 | + let output = execSync( |
| 96 | + `node instances/suspend-resume/suspend ${projectId} ${zone} ${instanceName}` |
| 97 | + ); |
| 98 | + assert.match(output, /Instance suspended./); |
| 99 | + |
| 100 | + let instance = await getInstance(projectId, zone, instanceName); |
| 101 | + |
| 102 | + while (instance.status === 'SUSPENDING') { |
| 103 | + [instance] = await instancesClient.get({ |
| 104 | + project: projectId, |
| 105 | + zone, |
| 106 | + instance: instanceName, |
| 107 | + }); |
| 108 | + |
| 109 | + await delay(5 * 1000); |
| 110 | + } |
| 111 | + |
| 112 | + instance = await getInstance(projectId, zone, instanceName); |
| 113 | + |
| 114 | + assert.equal(instance.status, 'SUSPENDED'); |
| 115 | + |
| 116 | + output = execSync( |
| 117 | + `node instances/suspend-resume/resume ${projectId} ${zone} ${instanceName}` |
| 118 | + ); |
| 119 | + assert.match(output, /Instance resumed./); |
| 120 | + |
| 121 | + instance = await getInstance(projectId, zone, instanceName); |
| 122 | + |
| 123 | + assert.equal(instance.status, 'RUNNING'); |
| 124 | + |
| 125 | + execSync(`node deleteInstance ${projectId} ${zone} ${instanceName}`); |
| 126 | + }); |
| 127 | +}); |
0 commit comments