diff --git a/compute/disks/createComputeHyperdisk.js b/compute/disks/createComputeHyperdisk.js index 016bdb1dcd..76b231eded 100644 --- a/compute/disks/createComputeHyperdisk.js +++ b/compute/disks/createComputeHyperdisk.js @@ -16,7 +16,7 @@ 'use strict'; -async function main(diskName) { +async function main() { // [START compute_hyperdisk_create] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -28,14 +28,14 @@ async function main(diskName) { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update/uncomment these variables before running the sample. + * TODO(developer): Update these variables before running the sample. */ // Project ID or project number of the Google Cloud project you want to use. const projectId = await disksClient.getProjectId(); // The zone where your VM and new disk are located. const zone = 'europe-central2-b'; // The name of the new disk - // const diskName = 'disk-name'; + const diskName = 'disk-name'; // The type of disk. This value uses the following format: // "zones/{zone}/diskTypes/(hyperdisk-balanced|hyperdisk-extreme|hyperdisk-ml|hyperdisk-throughput)". // For example: "zones/us-west3-b/diskTypes/hyperdisk-balanced" @@ -78,14 +78,22 @@ async function main(diskName) { }); } - console.log(`Disk: ${diskName} created.`); + const hyperdisk = ( + await disksClient.get({ + project: projectId, + zone, + disk: diskName, + }) + )[0]; + + console.log(JSON.stringify(hyperdisk)); } await callCreateComputeHyperdisk(); // [END compute_hyperdisk_create] } -main(...process.argv.slice(2)).catch(err => { +main().catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/disks/createComputeHyperdiskFromPool.js b/compute/disks/createComputeHyperdiskFromPool.js index 369a13a830..3a2c34d09d 100644 --- a/compute/disks/createComputeHyperdiskFromPool.js +++ b/compute/disks/createComputeHyperdiskFromPool.js @@ -16,7 +16,7 @@ 'use strict'; -async function main(diskName, storagePoolName) { +async function main() { // [START compute_hyperdisk_create_from_pool] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -28,16 +28,16 @@ async function main(diskName, storagePoolName) { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update/uncomment these variables before running the sample. + * TODO(developer): Update these variables before running the sample. */ // Project ID or project number of the Google Cloud project you want to use. const projectId = await disksClient.getProjectId(); // The zone where your VM and new disk are located. const zone = 'us-central1-a'; // The name of the new disk - // const diskName = 'disk-from-pool-name'; + const diskName = 'disk-from-pool-name'; // The name of the storage pool - // const storagePoolName = 'storage-pool-name'; + const storagePoolName = 'storage-pool-name'; // Link to the storagePool you want to use. Use format: // https://www.googleapis.com/compute/v1/projects/{projectId}/zones/{zone}/storagePools/{storagePoolName} const storagePool = `https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/storagePools/${storagePoolName}`; @@ -84,14 +84,22 @@ async function main(diskName, storagePoolName) { }); } - console.log(`Disk: ${diskName} created.`); + const hyperdisk = ( + await disksClient.get({ + project: projectId, + zone, + disk: diskName, + }) + )[0]; + + console.log(JSON.stringify(hyperdisk)); } await callCreateComputeHyperdiskFromPool(); // [END compute_hyperdisk_create_from_pool] } -main(...process.argv.slice(2)).catch(err => { +main().catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/disks/createComputeHyperdiskPool.js b/compute/disks/createComputeHyperdiskPool.js index 845b92a250..d8ae457e6b 100644 --- a/compute/disks/createComputeHyperdiskPool.js +++ b/compute/disks/createComputeHyperdiskPool.js @@ -16,7 +16,7 @@ 'use strict'; -async function main(storagePoolName) { +async function main() { // [START compute_hyperdisk_pool_create] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -28,14 +28,14 @@ async function main(storagePoolName) { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update/uncomment these variables before running the sample. + * TODO(developer): Update these variables before running the sample. */ // Project ID or project number of the Google Cloud project you want to use. const projectId = await storagePoolClient.getProjectId(); // Name of the zone in which you want to create the storagePool. const zone = 'us-central1-a'; // Name of the storagePool you want to create. - // const storagePoolName = 'storage-pool-name'; + const storagePoolName = 'storage-pool-name'; // The type of disk you want to create. This value uses the following format: // "projects/{projectId}/zones/{zone}/storagePoolTypes/(hyperdisk-throughput|hyperdisk-balanced)" const storagePoolType = `projects/${projectId}/zones/${zone}/storagePoolTypes/hyperdisk-balanced`; @@ -79,14 +79,22 @@ async function main(storagePoolName) { }); } - console.log(`Storage pool: ${storagePoolName} created.`); + const createdStoragePool = ( + await storagePoolClient.get({ + project: projectId, + zone, + storagePool: storagePoolName, + }) + )[0]; + + console.log(JSON.stringify(createdStoragePool)); } await callCreateComputeHyperdiskPool(); // [END compute_hyperdisk_pool_create] } -main(...process.argv.slice(2)).catch(err => { +main().catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/reservations/createReservationFromProperties.js b/compute/reservations/createReservationFromProperties.js index 05b38361ca..42e23755e8 100644 --- a/compute/reservations/createReservationFromProperties.js +++ b/compute/reservations/createReservationFromProperties.js @@ -16,7 +16,7 @@ 'use strict'; -async function main(reservationName) { +async function main() { // [START compute_reservation_create] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -28,14 +28,14 @@ async function main(reservationName) { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update/uncomment these variables before running the sample. + * TODO(developer): Update these variables before running the sample. */ // The ID of the project where you want to reserve resources. const projectId = await reservationsClient.getProjectId(); // The zone in which to reserve resources. const zone = 'us-central1-a'; // The name of the reservation to create. - // const reservationName = 'reservation-01'; + const reservationName = 'reservation-01'; // The number of VMs to reserve. const vmsNumber = 3; // Machine type to use for each VM. @@ -96,14 +96,22 @@ async function main(reservationName) { }); } - console.log(`Reservation: ${reservationName} created.`); + const createdReservation = ( + await reservationsClient.get({ + project: projectId, + zone, + reservation: reservationName, + }) + )[0]; + + console.log(JSON.stringify(createdReservation)); } await callCreateComputeReservationFromProperties(); // [END compute_reservation_create] } -main(...process.argv.slice(2)).catch(err => { +main().catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/reservations/createReservationInstanceTemplate.js b/compute/reservations/createReservationInstanceTemplate.js index 94e2a5ea98..796bbc6064 100644 --- a/compute/reservations/createReservationInstanceTemplate.js +++ b/compute/reservations/createReservationInstanceTemplate.js @@ -16,7 +16,7 @@ 'use strict'; -async function main(reservationName, location, instanceTemplateName) { +async function main(location, instanceTemplateName) { // [START compute_reservation_create_template] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -28,14 +28,14 @@ async function main(reservationName, location, instanceTemplateName) { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update/uncomment these variables before running the sample. + * TODO(developer): Update these variables before running the sample. */ // The ID of the project where you want to reserve resources and where the instance template exists. const projectId = await reservationsClient.getProjectId(); // The zone in which to reserve resources. const zone = 'us-central1-a'; // The name of the reservation to create. - // const reservationName = 'reservation-01'; + const reservationName = 'reservation-01'; // The number of VMs to reserve. const vmsNumber = 3; @@ -87,7 +87,15 @@ async function main(reservationName, location, instanceTemplateName) { }); } - console.log(`Reservation: ${reservationName} created.`); + const createdReservation = ( + await reservationsClient.get({ + project: projectId, + zone, + reservation: reservationName, + }) + )[0]; + + console.log(JSON.stringify(createdReservation)); } await callCreateComputeReservationInstanceTemplate(); diff --git a/compute/reservations/createSharedReservation.js b/compute/reservations/createSharedReservation.js index ec584e2e0f..84cce08ffa 100644 --- a/compute/reservations/createSharedReservation.js +++ b/compute/reservations/createSharedReservation.js @@ -16,29 +16,33 @@ 'use strict'; -async function main(reservationName, instanceTemplateName) { +async function main(reservationsClient, zoneOperationsClient) { // [START compute_reservation_create_shared] // Import the Compute library const computeLib = require('@google-cloud/compute'); const compute = computeLib.protos.google.cloud.compute.v1; + /** + * TODO(developer): Uncomment reservationsClient and zoneOperationsClient before running the sample. + */ // Instantiate a reservationsClient - const reservationsClient = new computeLib.ReservationsClient(); + // reservationsClient = new computeLib.ReservationsClient(); // Instantiate a zoneOperationsClient - const zoneOperationsClient = new computeLib.ZoneOperationsClient(); + // zoneOperationsClient = new computeLib.ZoneOperationsClient(); + /** - * TODO(developer): Update/uncomment these variables before running the sample. + * TODO(developer): Update these variables before running the sample. */ // The ID of the project where you want to reserve resources and where the instance template exists. const projectId = await reservationsClient.getProjectId(); // The zone in which to reserve resources. const zone = 'us-central1-a'; // The name of the reservation to create. - // const reservationName = 'reservation-01'; + const reservationName = 'reservation-01'; // The number of VMs to reserve. const vmsNumber = 3; // The name of an existing instance template. - // const instanceTemplateName = 'global-instance-template-name'; + const instanceTemplateName = 'global-instance-template-name'; // The location of the instance template. const location = 'global'; @@ -56,10 +60,9 @@ async function main(reservationName, instanceTemplateName) { // The IDs of projects that can consume this reservation. You can include up to 100 consumer projects. // These projects must be in the same organization as the owner project. // Don't include the owner project. By default, it is already allowed to consume the reservation. - // TODO(developer): Uncomment and update with your consumerId before running the sample. - // consumerId: { - // projectId: 'consumerId', - // }, + consumer_project_id: { + projectId: 'consumer_project_id', + }, }, }); @@ -95,7 +98,10 @@ async function main(reservationName, instanceTemplateName) { // [END compute_reservation_create_shared] } -main(...process.argv.slice(2)).catch(err => { - console.error(err); - process.exitCode = 1; -}); +module.exports = main; + +// TODO(developer): Uncomment below lines before running the sample. +// main(...process.argv.slice(2)).catch(err => { +// console.error(err); +// process.exitCode = 1; +// }); diff --git a/compute/reservations/deleteReservation.js b/compute/reservations/deleteReservation.js index 6c948a8019..0c13b49f11 100644 --- a/compute/reservations/deleteReservation.js +++ b/compute/reservations/deleteReservation.js @@ -14,7 +14,7 @@ * limitations under the License. */ 'use strict'; -async function main(reservationName) { +async function main() { // [START compute_reservation_delete] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -23,14 +23,14 @@ async function main(reservationName) { // Instantiate a zoneOperationsClient const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update/uncomment these variables before running the sample. + * TODO(developer): Update these variables before running the sample. */ // The ID of the project where your reservation is located. const projectId = await reservationsClient.getProjectId(); // The zone where your reservation is located. const zone = 'us-central1-a'; // The name of the reservation to delete. - // const reservationName = 'reservation-01'; + const reservationName = 'reservation-01'; async function callDeleteReservation() { // Delete the reservation @@ -50,13 +50,11 @@ async function main(reservationName) { zone: operation.zone.split('/').pop(), }); } - - console.log(`Reservation: ${reservationName} deleted.`); } await callDeleteReservation(); // [END compute_reservation_delete] } -main(...process.argv.slice(2)).catch(err => { +main().catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/reservations/getReservation.js b/compute/reservations/getReservation.js index bc4ecd2525..733090ae6d 100644 --- a/compute/reservations/getReservation.js +++ b/compute/reservations/getReservation.js @@ -16,7 +16,7 @@ 'use strict'; -async function main(reservationName) { +async function main() { // [START compute_reservation_get] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -25,14 +25,14 @@ async function main(reservationName) { const reservationsClient = new computeLib.ReservationsClient(); /** - * TODO(developer): Update/uncomment these variables before running the sample. + * TODO(developer): Update these variables before running the sample. */ // The ID of the project where your reservation is located. const projectId = await reservationsClient.getProjectId(); // The zone where your reservation is located. const zone = 'us-central1-a'; // The name of the reservation to return. - // const reservationName = 'reservation-01'; + const reservationName = 'reservation-01'; async function callGetReservation() { const requestedReservation = ( @@ -50,7 +50,7 @@ async function main(reservationName) { // [END compute_reservation_get] } -main(...process.argv.slice(2)).catch(err => { +main().catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/reservations/reservationVmsUpdate.js b/compute/reservations/reservationVmsUpdate.js index 23a7387921..24305367f7 100644 --- a/compute/reservations/reservationVmsUpdate.js +++ b/compute/reservations/reservationVmsUpdate.js @@ -16,7 +16,7 @@ 'use strict'; -async function main(reservationName) { +async function main() { // [START compute_reservation_vms_update] // Import the Compute library const computeLib = require('@google-cloud/compute'); @@ -27,14 +27,14 @@ async function main(reservationName) { const zoneOperationsClient = new computeLib.ZoneOperationsClient(); /** - * TODO(developer): Update/uncomment these variables before running the sample. + * TODO(developer): Update these variables before running the sample. */ // The ID of the project where the reservation is located. const projectId = await reservationsClient.getProjectId(); // The zone where the reservation is located. const zone = 'us-central1-a'; // The name of an existing reservation. - // const reservationName = 'reservation-01'; + const reservationName = 'reservation-01'; // The new number of VMs to reserve(increase or decrease the number). Before modifying the number of VMs, // ensure that all required conditions are met. See: https://cloud.google.com/compute/docs/instances/reservations-modify#resizing_a_reservation. const vmsNumber = 1; @@ -61,14 +61,22 @@ async function main(reservationName) { }); } - console.log(`Reservation: ${reservationName} updated.`); + const updatedReservation = ( + await reservationsClient.get({ + project: projectId, + zone, + reservation: reservationName, + }) + )[0]; + + console.log(JSON.stringify(updatedReservation)); } await callComputeReservationVmsUpdate(); // [END compute_reservation_vms_update] } -main(...process.argv.slice(2)).catch(err => { +main().catch(err => { console.error(err); process.exitCode = 1; }); diff --git a/compute/reservations/sharedReservationConsumerProjectsUpdate.js b/compute/reservations/sharedReservationConsumerProjectsUpdate.js index 2a787b98c7..41c90fc5d5 100644 --- a/compute/reservations/sharedReservationConsumerProjectsUpdate.js +++ b/compute/reservations/sharedReservationConsumerProjectsUpdate.js @@ -18,27 +18,30 @@ 'use strict'; -// [START compute_reservation_consumer_projects_update] +async function main(reservationsClient, zoneOperationsClient) { + // [START compute_reservation_consumer_projects_update] -// Import the Compute library -const computeLib = require('@google-cloud/compute'); -// Instantiate a reservationsClient -const reservationsClient = new computeLib.ReservationsClient(); -// Instantiate a zoneOperationsClient -const zoneOperationsClient = new computeLib.ZoneOperationsClient(); + /** + * TODO(developer): Uncomment computeLib, reservationsClient and zoneOperationsClient before running the sample. + */ + // Import the Compute library + // const computeLib = require('@google-cloud/compute'); + // Instantiate a reservationsClient + // reservationsClient = new computeLib.ReservationsClient(); + // Instantiate a zoneOperationsClient + // zoneOperationsClient = new computeLib.ZoneOperationsClient(); -async function main(reservationName) { /** - * TODO(developer): Update/uncomment these variables before running the sample. + * TODO(developer): Update these variables before running the sample. */ // The ID of the owner project, which is the project used to create the shared reservation. const projectId = await reservationsClient.getProjectId(); // The zone where the shared reservation is located. - // const zone = 'us-central1-a'; + const zone = 'us-central1-a'; // The name of an existing shared reservation. - // const reservationName = 'reservation-01'; + const reservationName = 'reservation-01'; // The ID of project to share the reservation with. - // const consumerId = 'newConsumerId'; + const consumerId = 'newConsumerId'; async function callComputeSharedReservationConsumerProjectsUpdate() { // Update the reservation. @@ -46,25 +49,24 @@ async function main(reservationName) { project: projectId, reservation: reservationName, /** - * TODO(developer): Uncomment `paths, zone, reservationResource` variables before running the sample. * Define `paths` field for each consumer, that you want to allow/not allow to consume a shared resevation, e.g. * to allow 2 projects to consume a shared reservation, you need to specify 2 paths: * paths: 'shareSettings.projectMap.{consumerId1}', * paths: 'shareSettings.projectMap.{consumerId2}' * */ - // paths: `shareSettings.projectMap.${consumerId}`, - // zone, - // reservationResource: { - // name: reservationName, - // // To stop allowing one or more projects to consume a shared reservation, comment shareSettings object. - // shareSettings: { - // projectMap: { - // newConsumerId: { - // projectId: `${consumerId}`, - // }, - // }, - // }, - // }, + paths: `shareSettings.projectMap.${consumerId}`, + zone, + reservationResource: { + name: reservationName, + // To stop allowing one or more projects to consume a shared reservation, comment shareSettings object. + shareSettings: { + projectMap: { + newConsumerId: { + projectId: `${consumerId}`, + }, + }, + }, + }, }); let operation = response.latestResponse; @@ -85,7 +87,10 @@ async function main(reservationName) { // [END compute_reservation_consumer_projects_update] } -main(...process.argv.slice(2)).catch(err => { - console.error(err); - process.exitCode = 1; -}); +module.exports = main; + +// TODO(developer): Uncomment below lines before running the sample. +// main(...process.argv.slice(2)).catch(err => { +// console.error(err); +// process.exitCode = 1; +// }); diff --git a/compute/test/createComputeHyperdisk.test.js b/compute/test/createComputeHyperdisk.test.js index dfcaa43e07..ac4721f764 100644 --- a/compute/test/createComputeHyperdisk.test.js +++ b/compute/test/createComputeHyperdisk.test.js @@ -26,13 +26,23 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cwd = path.join(__dirname, '..'); describe('Create compute hyperdisk', async () => { - const diskName = `hyperdisk-name-941ad2d${Math.floor(Math.random() * 1000 + 1)}`; + const diskName = 'disk-name'; const zone = 'europe-central2-b'; const disksClient = new DisksClient(); let projectId; before(async () => { projectId = await disksClient.getProjectId(); + try { + // Ensure resource is deleted attempting to recreate it + await disksClient.delete({ + project: projectId, + disk: diskName, + zone, + }); + } catch { + // ok to ignore (resource doesn't exist) + } }); after(async () => { @@ -44,13 +54,12 @@ describe('Create compute hyperdisk', async () => { }); it('should create a new hyperdisk', () => { - const response = execSync( - `node ./disks/createComputeHyperdisk.js ${diskName}`, - { + const response = JSON.parse( + execSync('node ./disks/createComputeHyperdisk.js', { cwd, - } + }) ); - assert.include(response, `Disk: ${diskName} created.`); + assert.equal(response.name, diskName); }); }); diff --git a/compute/test/createComputeHyperdiskFromPool.test.js b/compute/test/createComputeHyperdiskFromPool.test.js index 35dabcec43..5813a2158c 100644 --- a/compute/test/createComputeHyperdiskFromPool.test.js +++ b/compute/test/createComputeHyperdiskFromPool.test.js @@ -66,14 +66,22 @@ async function cleanupResources(projectId, zone, diskName, storagePoolName) { } describe('Create compute hyperdisk from pool', async () => { - const diskName = `disk-from-pool-name-745d98${Math.floor(Math.random() * 1000 + 1)}f`; + const diskName = 'disk-from-pool-name'; const zone = 'us-central1-a'; - const storagePoolName = `storage-pool-name-745d9${Math.floor(Math.random() * 1000 + 1)}5f`; + const storagePoolName = 'storage-pool-name'; const disksClient = new DisksClient(); let projectId; before(async () => { projectId = await disksClient.getProjectId(); + + // Ensure resources are deleted before attempting to recreate them + try { + await cleanupResources(projectId, zone, diskName, storagePoolName); + } catch (err) { + // Should be ok to ignore (resources do not exist) + console.error(err); + } }); after(async () => { @@ -81,24 +89,26 @@ describe('Create compute hyperdisk from pool', async () => { }); it('should create a new storage pool', () => { - const response = execSync( - `node ./disks/createComputeHyperdiskPool.js ${storagePoolName}`, - { + const response = JSON.parse( + execSync('node ./disks/createComputeHyperdiskPool.js', { cwd, - } + }) ); - assert.include(response, `Storage pool: ${storagePoolName} created.`); + assert.equal(response.name, storagePoolName); }); it('should create a new hyperdisk from pool', () => { - const response = execSync( - `node ./disks/createComputeHyperdiskFromPool.js ${diskName} ${storagePoolName}`, - { + const response = JSON.parse( + execSync('node ./disks/createComputeHyperdiskFromPool.js', { cwd, - } + }) ); - assert.include(response, `Disk: ${diskName} created.`); + assert.equal(response.name, diskName); + assert.equal( + response.storagePool, + `https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/storagePools/${storagePoolName}` + ); }); }); diff --git a/compute/test/createReservationGlobalInstanceTemplate.test.js b/compute/test/createReservationGlobalInstanceTemplate.test.js index 1f962ecfe4..df34b1ad56 100644 --- a/compute/test/createReservationGlobalInstanceTemplate.test.js +++ b/compute/test/createReservationGlobalInstanceTemplate.test.js @@ -17,7 +17,7 @@ 'use strict'; const path = require('path'); -const {assert} = require('chai'); +const assert = require('node:assert/strict'); const {after, before, describe, it} = require('mocha'); const cp = require('child_process'); const {ReservationsClient} = require('@google-cloud/compute').v1; @@ -26,8 +26,8 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cwd = path.join(__dirname, '..'); describe('Create compute reservation using global instance template', async () => { - const reservationName = `global-reservation-68ef06a${Math.floor(Math.random() * 1000 + 1)}`; - const instanceTemplateName = `pernament-global-template-68ef06a${Math.floor(Math.random() * 1000 + 1)}`; + const reservationName = 'reservation-01'; + const instanceTemplateName = 'pernament-global-template-name'; const location = 'global'; const reservationsClient = new ReservationsClient(); let projectId; @@ -45,7 +45,7 @@ describe('Create compute reservation using global instance template', async () = after(() => { // Delete reservation - execSync(`node ./reservations/deleteReservation.js ${reservationName}`, { + execSync('node ./reservations/deleteReservation.js', { cwd, }); // Delete template @@ -58,13 +58,20 @@ describe('Create compute reservation using global instance template', async () = }); it('should create a new reservation', () => { - const response = execSync( - `node ./reservations/createReservationInstanceTemplate.js ${reservationName} ${location} ${instanceTemplateName}`, - { - cwd, - } + const response = JSON.parse( + execSync( + `node ./reservations/createReservationInstanceTemplate.js ${location} ${instanceTemplateName}`, + { + cwd, + } + ) ); - assert.include(response, `Reservation: ${reservationName} created.`); + assert.equal(response.name, reservationName); + assert.equal(response.specificReservation.count, '3'); + assert.equal( + response.specificReservation.sourceInstanceTemplate, + `https://www.googleapis.com/compute/v1/projects/${projectId}/${location}/instanceTemplates/${instanceTemplateName}` + ); }); }); diff --git a/compute/test/createReservationRegionalInstanceTemplate.test.js b/compute/test/createReservationRegionalInstanceTemplate.test.js index b909e0fd7e..76522e3e2b 100644 --- a/compute/test/createReservationRegionalInstanceTemplate.test.js +++ b/compute/test/createReservationRegionalInstanceTemplate.test.js @@ -17,33 +17,47 @@ 'use strict'; const path = require('path'); -const {assert} = require('chai'); -const {after, describe, it} = require('mocha'); +const assert = require('node:assert/strict'); +const {after, before, describe, it} = require('mocha'); const cp = require('child_process'); +const {ReservationsClient} = require('@google-cloud/compute').v1; const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cwd = path.join(__dirname, '..'); describe('Create compute reservation using regional instance template', async () => { - const reservationName = `regional-reservation-04bf4ed${Math.floor(Math.random() * 1000 + 1)}`; + const reservationName = 'reservation-01'; const instanceTemplateName = 'pernament-region-template-name'; const location = 'regions/us-central1'; + const reservationsClient = new ReservationsClient(); + let projectId; + + before(async () => { + projectId = await reservationsClient.getProjectId(); + }); after(() => { // Delete reservation - execSync(`node ./reservations/deleteReservation.js ${reservationName}`, { + execSync('node ./reservations/deleteReservation.js', { cwd, }); }); it('should create a new reservation', () => { - const response = execSync( - `node ./reservations/createReservationInstanceTemplate.js ${reservationName} ${location} ${instanceTemplateName}`, - { - cwd, - } + const response = JSON.parse( + execSync( + `node ./reservations/createReservationInstanceTemplate.js ${location} ${instanceTemplateName}`, + { + cwd, + } + ) ); - assert.include(response, `Reservation: ${reservationName} created.`); + assert.equal(response.name, reservationName); + assert.equal(response.specificReservation.count, '3'); + assert.equal( + response.specificReservation.sourceInstanceTemplate, + `https://www.googleapis.com/compute/v1/projects/${projectId}/${location}/instanceTemplates/${instanceTemplateName}` + ); }); }); diff --git a/compute/test/reservations.test.js b/compute/test/reservations.test.js index 846ad99364..75715f020f 100644 --- a/compute/test/reservations.test.js +++ b/compute/test/reservations.test.js @@ -17,7 +17,7 @@ 'use strict'; const path = require('path'); -const {assert} = require('chai'); +const assert = require('node:assert/strict'); const {describe, it} = require('mocha'); const cp = require('child_process'); const {ReservationsClient} = require('@google-cloud/compute').v1; @@ -26,34 +26,62 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cwd = path.join(__dirname, '..'); describe('Compute reservation', async () => { - const reservationName = `reservation-1a0bb69e-${Math.floor(Math.random() * 1000 + 1)}ec`; + const reservationName = 'reservation-01'; const zone = 'us-central1-a'; const reservationsClient = new ReservationsClient(); let projectId; + let reservation; before(async () => { projectId = await reservationsClient.getProjectId(); }); it('should create a new reservation', () => { - const response = execSync( - `node ./reservations/createReservationFromProperties.js ${reservationName}`, - { + const instanceProperties = { + _machineType: 'machineType', + _minCpuPlatform: 'minCpuPlatform', + guestAccelerators: [ + { + _acceleratorCount: 'acceleratorCount', + _acceleratorType: 'acceleratorType', + acceleratorCount: 1, + acceleratorType: 'nvidia-tesla-t4', + }, + ], + localSsds: [ + { + diskSizeGb: '375', + interface: 'NVME', + _diskSizeGb: 'diskSizeGb', + _interface: 'interface', + }, + ], + machineType: 'n1-standard-4', + minCpuPlatform: 'Intel Skylake', + }; + + reservation = JSON.parse( + execSync('node ./reservations/createReservationFromProperties.js', { cwd, - } + }) ); - assert.include(response, `Reservation: ${reservationName} created.`); + assert.equal(reservation.name, reservationName); + assert.equal(reservation.specificReservation.count, '3'); + assert.deepEqual( + reservation.specificReservation.instanceProperties, + instanceProperties + ); }); it('should return reservation', () => { const response = JSON.parse( - execSync(`node ./reservations/getReservation.js ${reservationName}`, { + execSync('node ./reservations/getReservation.js', { cwd, }) ); - assert.equal(response.name, reservationName); + assert.deepEqual(response, reservation); }); it('should return list of reservations', () => { @@ -63,11 +91,11 @@ describe('Compute reservation', async () => { }) ); - assert.equal(response.length, 1); + assert.deepEqual(response, [reservation]); }); it('should delete reservation', async () => { - execSync(`node ./reservations/deleteReservation.js ${reservationName}`, { + execSync('node ./reservations/deleteReservation.js', { cwd, }); diff --git a/compute/test/sharedReservation.test.js b/compute/test/sharedReservation.test.js index 5a97866364..2692cba9e8 100644 --- a/compute/test/sharedReservation.test.js +++ b/compute/test/sharedReservation.test.js @@ -16,68 +16,90 @@ 'use strict'; -const {before, after, describe, it} = require('mocha'); -const path = require('path'); -const {assert} = require('chai'); -const cp = require('child_process'); -const {ReservationsClient} = require('@google-cloud/compute').v1; +const {beforeEach, afterEach, describe, it} = require('mocha'); +const {expect} = require('chai'); +const sinon = require('sinon'); +const createSharedReservation = require('../reservations/createSharedReservation.js'); +const sharedReservationConsumerProjectsUpdate = require('../reservations/sharedReservationConsumerProjectsUpdate.js'); -const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); -const cwd = path.join(__dirname, '..'); +describe('Create compute shared reservation using global instance template', async () => { + const reservationName = 'reservation-01'; + const reservation = { + name: reservationName, + shareSettings: { + shareType: 'SPECIFIC_PROJECTS', + projectMap: { + consumer_project_id: { + projectId: 'consumer_project_id', + }, + }, + }, + }; + let reservationsClientMock; + let zoneOperationsClientMock; -describe('Compute shared reservation', async () => { - const reservationName = `shared-reservation-e2${Math.floor(Math.random() * 1000 + 1)}f`; - const instanceTemplateName = `global-instance-template-e2${Math.floor(Math.random() * 1000 + 1)}f`; - const reservationsClient = new ReservationsClient(); - - let projectId; - - before(async () => { - projectId = await reservationsClient.getProjectId(); - // Create template - execSync( - `node ./create-instance-templates/createTemplate.js ${projectId} ${instanceTemplateName}`, - { - cwd, - } - ); + beforeEach(() => { + sinon.stub(console, 'log'); + reservationsClientMock = { + getProjectId: sinon.stub().resolves('project_id'), + insert: sinon.stub().resolves([ + { + latestResponse: { + status: 'DONE', + name: 'operation-1234567890', + zone: { + value: 'us-central1-a', + }, + }, + }, + ]), + update: sinon.stub().resolves([ + { + latestResponse: { + status: 'DONE', + name: 'operation-1234567890', + zone: { + value: 'us-central1-a', + }, + }, + }, + ]), + get: sinon.stub().resolves([reservation]), + }; + zoneOperationsClientMock = { + wait: sinon.stub().resolves([ + { + latestResponse: { + status: 'DONE', + }, + }, + ]), + }; }); - after(() => { - // Delete reservation - execSync(`node ./reservations/deleteReservation.js ${reservationName}`, { - cwd, - }); - // Delete template - execSync( - `node ./create-instance-templates/deleteInstanceTemplate.js ${projectId} ${instanceTemplateName}`, - { - cwd, - } - ); + afterEach(() => { + sinon.restore(); }); - it('should create new shared reservation', async () => { - const response = execSync( - `node ./reservations/createSharedReservation.js ${reservationName} ${instanceTemplateName}`, - { - cwd, - } + it('should create shared reservation', async () => { + await createSharedReservation( + reservationsClientMock, + zoneOperationsClientMock ); - assert.include(response, `Reservation: ${reservationName} created.`); + expect(console.log.calledOnce).to.be.true; + expect(console.log.calledWith(`Reservation: ${reservationName} created.`)) + .to.be.true; }); - it('should update consumer projects in shared reservation', async () => { - const response = JSON.parse( - execSync( - `node ./reservations/sharedReservationConsumerProjectsUpdate.js ${reservationName}`, - { - cwd, - } - ) + it('should update shared reservation', async () => { + await sharedReservationConsumerProjectsUpdate( + reservationsClientMock, + zoneOperationsClientMock ); - assert.include(response, `Reservation: ${reservationName} updated.`); + expect(console.log.calledOnce).to.be.true; + expect(console.log.calledWith(`Reservation: ${reservationName} updated.`)) + .to.be.true; }); });