|
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 {before, describe, it} = require('mocha'); |
22 |
| -// const cp = require('child_process'); |
23 |
| -// const {ReservationsClient} = require('@google-cloud/compute').v1; |
24 |
| -// const { |
25 |
| -// getStaleReservations, |
26 |
| -// deleteReservation, |
27 |
| -// getStaleVMInstances, |
28 |
| -// deleteInstance, |
29 |
| -// } = require('./util'); |
30 |
| - |
31 |
| -// const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
32 |
| -// const cwd = path.join(__dirname, '..'); |
33 |
| - |
34 |
| -// describe('Consume reservations', async () => { |
35 |
| -// const zone = 'us-central1-a'; |
36 |
| -// const instancePrefix = 'instance-458a'; |
37 |
| -// const reservationPrefix = 'reservation-'; |
38 |
| -// const reservationsClient = new ReservationsClient(); |
39 |
| -// let projectId; |
40 |
| - |
41 |
| -// before(async () => { |
42 |
| -// projectId = await reservationsClient.getProjectId(); |
43 |
| -// // Cleanup resources |
44 |
| -// const instances = await getStaleVMInstances(instancePrefix); |
45 |
| -// await Promise.all( |
46 |
| -// instances.map(instance => |
47 |
| -// deleteInstance(instance.zone, instance.instanceName) |
48 |
| -// ) |
49 |
| -// ); |
50 |
| -// const reservations = await getStaleReservations(reservationPrefix); |
51 |
| -// await Promise.all( |
52 |
| -// reservations.map(reservation => |
53 |
| -// deleteReservation(reservation.zone, reservation.reservationName) |
54 |
| -// ) |
55 |
| -// ); |
56 |
| -// }); |
57 |
| - |
58 |
| -// it('should create instance that consumes any matching reservation', () => { |
59 |
| -// const reservationName = `${reservationPrefix}${Math.floor(Math.random() * 1000 + 1)}f8a31896`; |
60 |
| -// const instanceName = `${instancePrefix}88aab${Math.floor(Math.random() * 1000 + 1)}f`; |
61 |
| - |
62 |
| -// // Create reservation |
63 |
| -// execSync( |
64 |
| -// `node ./reservations/createReservationFromProperties.js ${reservationName}`, |
65 |
| -// { |
66 |
| -// cwd, |
67 |
| -// } |
68 |
| -// ); |
69 |
| - |
70 |
| -// const response = execSync( |
71 |
| -// `node ./reservations/createInstanceToConsumeAnyReservation.js ${instanceName}`, |
72 |
| -// { |
73 |
| -// cwd, |
74 |
| -// } |
75 |
| -// ); |
76 |
| - |
77 |
| -// assert(response.includes(`Instance ${instanceName} created.`)); |
78 |
| -// // Delete reservation |
79 |
| -// execSync(`node ./reservations/deleteReservation.js ${reservationName}`, { |
80 |
| -// cwd, |
81 |
| -// }); |
82 |
| -// // Delete instance |
83 |
| -// execSync(`node ./deleteInstance.js ${projectId} ${zone} ${instanceName}`, { |
84 |
| -// cwd, |
85 |
| -// }); |
86 |
| -// }); |
87 |
| - |
88 |
| -// it('should create instance that consumes specific single project reservation', () => { |
89 |
| -// const reservationName = `${reservationPrefix}22ab${Math.floor(Math.random() * 1000 + 1)}`; |
90 |
| -// const instanceName = `${instancePrefix}${Math.floor(Math.random() * 1000 + 1)}`; |
91 |
| - |
92 |
| -// // Create reservation |
93 |
| -// execSync( |
94 |
| -// `node ./reservations/createReservationFromProperties.js ${reservationName}`, |
95 |
| -// { |
96 |
| -// cwd, |
97 |
| -// } |
98 |
| -// ); |
99 |
| - |
100 |
| -// const response = execSync( |
101 |
| -// `node ./reservations/createInstanceToConsumeSingleProjectReservation.js ${instanceName} ${reservationName}`, |
102 |
| -// { |
103 |
| -// cwd, |
104 |
| -// } |
105 |
| -// ); |
106 |
| - |
107 |
| -// assert(response.includes(`Instance ${instanceName} created.`)); |
108 |
| -// // Delete reservation |
109 |
| -// execSync(`node ./reservations/deleteReservation.js ${reservationName}`, { |
110 |
| -// cwd, |
111 |
| -// }); |
112 |
| -// // Delete instance |
113 |
| -// execSync(`node ./deleteInstance.js ${projectId} ${zone} ${instanceName}`, { |
114 |
| -// cwd, |
115 |
| -// }); |
116 |
| -// }); |
117 |
| - |
118 |
| -// it('should create instance that should not consume reservations', () => { |
119 |
| -// const instanceName = `instance-458a${Math.floor(Math.random() * 1000 + 1)}`; |
120 |
| - |
121 |
| -// const response = execSync( |
122 |
| -// `node ./reservations/createInstanceToNotConsumeReservation.js ${instanceName}`, |
123 |
| -// { |
124 |
| -// cwd, |
125 |
| -// } |
126 |
| -// ); |
127 |
| - |
128 |
| -// assert(response.includes(`Instance ${instanceName} created.`)); |
129 |
| -// // Delete instance |
130 |
| -// execSync(`node ./deleteInstance.js ${projectId} ${zone} ${instanceName}`, { |
131 |
| -// cwd, |
132 |
| -// }); |
133 |
| -// }); |
134 |
| - |
135 |
| -// it('should create template that should not consume reservations', () => { |
136 |
| -// const templateName = `template-458a${Math.floor(Math.random() * 1000 + 1)}`; |
137 |
| - |
138 |
| -// const response = execSync( |
139 |
| -// `node ./reservations/createTemplateToNotConsumeReservation.js ${templateName}`, |
140 |
| -// { |
141 |
| -// cwd, |
142 |
| -// } |
143 |
| -// ); |
144 |
| - |
145 |
| -// assert(response.includes(`Template ${templateName} created.`)); |
146 |
| -// // Delete template |
147 |
| -// execSync( |
148 |
| -// `node ./create-instance-templates/deleteInstanceTemplate.js ${projectId} ${templateName}`, |
149 |
| -// { |
150 |
| -// cwd, |
151 |
| -// } |
152 |
| -// ); |
153 |
| -// }); |
154 |
| -// }); |
| 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 {before, describe, it} = require('mocha'); |
| 22 | +const cp = require('child_process'); |
| 23 | +const {ReservationsClient} = require('@google-cloud/compute').v1; |
| 24 | +const { |
| 25 | + getStaleReservations, |
| 26 | + deleteReservation, |
| 27 | + getStaleVMInstances, |
| 28 | + deleteInstance, |
| 29 | +} = require('./util'); |
| 30 | + |
| 31 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 32 | +const cwd = path.join(__dirname, '..'); |
| 33 | + |
| 34 | +describe('Consume reservations', async () => { |
| 35 | + const zone = 'us-central1-a'; |
| 36 | + const instancePrefix = 'instance-458a'; |
| 37 | + const reservationPrefix = 'reservation-'; |
| 38 | + const reservationsClient = new ReservationsClient(); |
| 39 | + let projectId; |
| 40 | + |
| 41 | + before(async () => { |
| 42 | + projectId = await reservationsClient.getProjectId(); |
| 43 | + // Cleanup resources |
| 44 | + const instances = await getStaleVMInstances(instancePrefix); |
| 45 | + await Promise.all( |
| 46 | + instances.map(instance => |
| 47 | + deleteInstance(instance.zone, instance.instanceName) |
| 48 | + ) |
| 49 | + ); |
| 50 | + const reservations = await getStaleReservations(reservationPrefix); |
| 51 | + await Promise.all( |
| 52 | + reservations.map(reservation => |
| 53 | + deleteReservation(reservation.zone, reservation.reservationName) |
| 54 | + ) |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should create instance that consumes any matching reservation', () => { |
| 59 | + const reservationName = `${reservationPrefix}${Math.floor(Math.random() * 1000 + 1)}f8a31896`; |
| 60 | + const instanceName = `${instancePrefix}88aab${Math.floor(Math.random() * 1000 + 1)}f`; |
| 61 | + |
| 62 | + // Create reservation |
| 63 | + execSync( |
| 64 | + `node ./reservations/createReservationFromProperties.js ${reservationName}`, |
| 65 | + { |
| 66 | + cwd, |
| 67 | + } |
| 68 | + ); |
| 69 | + |
| 70 | + const response = execSync( |
| 71 | + `node ./reservations/createInstanceToConsumeAnyReservation.js ${instanceName}`, |
| 72 | + { |
| 73 | + cwd, |
| 74 | + } |
| 75 | + ); |
| 76 | + |
| 77 | + assert(response.includes(`Instance ${instanceName} created.`)); |
| 78 | + // Delete reservation |
| 79 | + execSync(`node ./reservations/deleteReservation.js ${reservationName}`, { |
| 80 | + cwd, |
| 81 | + }); |
| 82 | + // Delete instance |
| 83 | + execSync(`node ./deleteInstance.js ${projectId} ${zone} ${instanceName}`, { |
| 84 | + cwd, |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should create instance that consumes specific single project reservation', () => { |
| 89 | + const reservationName = `${reservationPrefix}22ab${Math.floor(Math.random() * 1000 + 1)}`; |
| 90 | + const instanceName = `${instancePrefix}${Math.floor(Math.random() * 1000 + 1)}`; |
| 91 | + |
| 92 | + // Create reservation |
| 93 | + execSync( |
| 94 | + `node ./reservations/createReservationFromProperties.js ${reservationName}`, |
| 95 | + { |
| 96 | + cwd, |
| 97 | + } |
| 98 | + ); |
| 99 | + |
| 100 | + const response = execSync( |
| 101 | + `node ./reservations/createInstanceToConsumeSingleProjectReservation.js ${instanceName} ${reservationName}`, |
| 102 | + { |
| 103 | + cwd, |
| 104 | + } |
| 105 | + ); |
| 106 | + |
| 107 | + assert(response.includes(`Instance ${instanceName} created.`)); |
| 108 | + // Delete reservation |
| 109 | + execSync(`node ./reservations/deleteReservation.js ${reservationName}`, { |
| 110 | + cwd, |
| 111 | + }); |
| 112 | + // Delete instance |
| 113 | + execSync(`node ./deleteInstance.js ${projectId} ${zone} ${instanceName}`, { |
| 114 | + cwd, |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should create instance that should not consume reservations', () => { |
| 119 | + const instanceName = `instance-458a${Math.floor(Math.random() * 1000 + 1)}`; |
| 120 | + |
| 121 | + const response = execSync( |
| 122 | + `node ./reservations/createInstanceToNotConsumeReservation.js ${instanceName}`, |
| 123 | + { |
| 124 | + cwd, |
| 125 | + } |
| 126 | + ); |
| 127 | + |
| 128 | + assert(response.includes(`Instance ${instanceName} created.`)); |
| 129 | + // Delete instance |
| 130 | + execSync(`node ./deleteInstance.js ${projectId} ${zone} ${instanceName}`, { |
| 131 | + cwd, |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should create template that should not consume reservations', () => { |
| 136 | + const templateName = `template-458a${Math.floor(Math.random() * 1000 + 1)}`; |
| 137 | + |
| 138 | + const response = execSync( |
| 139 | + `node ./reservations/createTemplateToNotConsumeReservation.js ${templateName}`, |
| 140 | + { |
| 141 | + cwd, |
| 142 | + } |
| 143 | + ); |
| 144 | + |
| 145 | + assert(response.includes(`Template ${templateName} created.`)); |
| 146 | + // Delete template |
| 147 | + execSync( |
| 148 | + `node ./create-instance-templates/deleteInstanceTemplate.js ${projectId} ${templateName}`, |
| 149 | + { |
| 150 | + cwd, |
| 151 | + } |
| 152 | + ); |
| 153 | + }); |
| 154 | +}); |
0 commit comments