|
| 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('chai'); |
| 21 | +const {after, before, describe, it} = require('mocha'); |
| 22 | +const cp = require('child_process'); |
| 23 | +const {DisksClient, StoragePoolsClient} = require('@google-cloud/compute').v1; |
| 24 | + |
| 25 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 26 | +const cwd = path.join(__dirname, '..'); |
| 27 | + |
| 28 | +describe('Create compute hyperdisk from pool', async () => { |
| 29 | + const diskName = 'disk-name-from-pool'; |
| 30 | + const zone = 'europe-central2-b'; |
| 31 | + const storagePoolName = 'storage-pool-name-hyperdisk'; |
| 32 | + const disksClient = new DisksClient(); |
| 33 | + const storagePoolsClient = new StoragePoolsClient(); |
| 34 | + let projectId; |
| 35 | + |
| 36 | + before(async () => { |
| 37 | + projectId = await disksClient.getProjectId(); |
| 38 | + |
| 39 | + // Ensure resources are deleted before attempting to recreate them |
| 40 | + try { |
| 41 | + await disksClient.delete({ |
| 42 | + project: projectId, |
| 43 | + disk: diskName, |
| 44 | + zone, |
| 45 | + }); |
| 46 | + } catch (err) { |
| 47 | + // Should be ok to ignore (resource doesn't exist) |
| 48 | + console.error(err); |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + await storagePoolsClient.delete({ |
| 53 | + project: projectId, |
| 54 | + storagePool: storagePoolName, |
| 55 | + zone, |
| 56 | + }); |
| 57 | + } catch (err) { |
| 58 | + // Should be ok to ignore (resource doesn't exist) |
| 59 | + console.error(err); |
| 60 | + } |
| 61 | + |
| 62 | + await storagePoolsClient.insert({ |
| 63 | + project: projectId, |
| 64 | + storagePoolResource: { |
| 65 | + name: storagePoolName, |
| 66 | + poolProvisionedCapacityGb: 10240, |
| 67 | + poolProvisionedIops: 10000, |
| 68 | + poolProvisionedThroughput: 1024, |
| 69 | + storagePoolType: `projects/${projectId}/zones/${zone}/storagePoolTypes/hyperdisk-balanced`, |
| 70 | + capacityProvisioningType: 'advanced', |
| 71 | + zone, |
| 72 | + }, |
| 73 | + zone, |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + after(async () => { |
| 78 | + // Trying to delete the disk too quickly seems to fail |
| 79 | + const deleteDisk = async () => { |
| 80 | + setTimeout(async () => { |
| 81 | + await disksClient.delete({ |
| 82 | + project: projectId, |
| 83 | + disk: diskName, |
| 84 | + zone, |
| 85 | + }); |
| 86 | + }, 120 * 1000); // wait two minutes |
| 87 | + }; |
| 88 | + |
| 89 | + try { |
| 90 | + await deleteDisk(); |
| 91 | + } catch { |
| 92 | + // Try one more time after repeating the delay |
| 93 | + await deleteDisk(); |
| 94 | + } |
| 95 | + |
| 96 | + // Need enough time after removing the disk before removing the pool |
| 97 | + const deletePool = async () => { |
| 98 | + setTimeout(async () => { |
| 99 | + await storagePoolsClient.delete({ |
| 100 | + project: projectId, |
| 101 | + storagePool: storagePoolName, |
| 102 | + zone, |
| 103 | + }); |
| 104 | + }, 120 * 1000); // wait two minutes |
| 105 | + }; |
| 106 | + |
| 107 | + try { |
| 108 | + await deletePool(); |
| 109 | + } catch { |
| 110 | + // Try one more time after repeating the delay |
| 111 | + await deletePool(); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it('should create a new hyperdisk from pool', () => { |
| 116 | + const response = JSON.parse( |
| 117 | + execSync('node ./disks/createComputeHyperdiskFromPool.js', { |
| 118 | + cwd, |
| 119 | + }) |
| 120 | + ); |
| 121 | + |
| 122 | + assert.equal(response.name, diskName); |
| 123 | + assert.equal( |
| 124 | + response.storagePool, |
| 125 | + `https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/storagePools/${storagePoolName}` |
| 126 | + ); |
| 127 | + }); |
| 128 | +}); |
0 commit comments