Skip to content

Commit 2d3cd72

Browse files
authored
test(secret-manager): fix ALREADY_EXISTS in delayed destroy tests by using unique UUIDs (GoogleCloudPlatform#4320)
* test(secret-manager): fix ALREADY_EXISTS in delayed destroy tests by using unique UUIDs * test(secret-manager): ensure resource cleanup on assertion failure using try-finally
1 parent 6d11272 commit 2d3cd72

1 file changed

Lines changed: 56 additions & 34 deletions

File tree

secret-manager/test/secretmanager.test.js

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,12 @@ describe('Secret Manager samples', () => {
687687
});
688688

689689
it('disables a secret delayed destroy', async () => {
690+
const customSecretId = `${secretId}-${v4()}`;
691+
const fullSecretName = `projects/${projectId}/secrets/${customSecretId}`;
692+
690693
await client.createSecret({
691694
parent: `projects/${projectId}`,
692-
secretId: `${secretId}-delayedDestroy`,
695+
secretId: customSecretId,
693696
secret: {
694697
replication: {
695698
automatic: {},
@@ -700,21 +703,26 @@ describe('Secret Manager samples', () => {
700703
},
701704
});
702705

703-
const output = execSync(
704-
`node disableSecretDelayedDestroy.js ${secret.name}-delayedDestroy`
705-
);
706-
assert.match(output, new RegExp('Disabled delayed destroy'));
707-
708-
await client.deleteSecret({
709-
name: `${secret.name}-delayedDestroy`,
710-
});
706+
try {
707+
const output = execSync(
708+
`node disableSecretDelayedDestroy.js ${fullSecretName}`
709+
);
710+
assert.match(output, new RegExp('Disabled delayed destroy'));
711+
} finally {
712+
await client.deleteSecret({
713+
name: fullSecretName,
714+
});
715+
}
711716
});
712717

713718
it('updates a secret delayed destroy', async () => {
719+
const customSecretId = `${secretId}-${v4()}`;
720+
const fullSecretName = `projects/${projectId}/secrets/${customSecretId}`;
714721
const updatedTimeToLive = 24 * 60 * 60 * 2;
722+
715723
await client.createSecret({
716724
parent: `projects/${projectId}`,
717-
secretId: `${secretId}-delayedDestroy`,
725+
secretId: customSecretId,
718726
secret: {
719727
replication: {
720728
automatic: {},
@@ -725,13 +733,16 @@ describe('Secret Manager samples', () => {
725733
},
726734
});
727735

728-
const output = execSync(
729-
`node updateSecretWithDelayedDestroy.js ${secret.name}-delayedDestroy ${updatedTimeToLive}`
730-
);
731-
assert.match(output, new RegExp('Updated secret'));
732-
await client.deleteSecret({
733-
name: `${secret.name}-delayedDestroy`,
734-
});
736+
try {
737+
const output = execSync(
738+
`node updateSecretWithDelayedDestroy.js ${fullSecretName} ${updatedTimeToLive}`
739+
);
740+
assert.match(output, new RegExp('Updated secret'));
741+
} finally {
742+
await client.deleteSecret({
743+
name: fullSecretName,
744+
});
745+
}
735746
});
736747

737748
it('creates a regional secret with delayed destroy', async () => {
@@ -743,45 +754,56 @@ describe('Secret Manager samples', () => {
743754
});
744755

745756
it('disables a regional secret delayed destroy', async () => {
757+
const customSecretId = `${secretId}-${v4()}`;
758+
const fullSecretName = `projects/${projectId}/locations/${locationId}/secrets/${customSecretId}`;
759+
746760
await regionalClient.createSecret({
747761
parent: `projects/${projectId}/locations/${locationId}`,
748-
secretId: `${secretId}-delayedDestroy`,
762+
secretId: customSecretId,
749763
secret: {
750764
version_destroy_ttl: {
751765
seconds: 24 * 60 * 60,
752766
},
753767
},
754768
});
755769

756-
const output = execSync(
757-
`node regional_samples/disableRegionalSecretDelayedDestroy.js ${projectId} ${locationId} ${secretId}-delayedDestroy`
758-
);
759-
assert.match(output, new RegExp('Disabled delayed destroy'));
760-
761-
await regionalClient.deleteSecret({
762-
name: `projects/${projectId}/locations/${locationId}/secrets/${secretId}-delayedDestroy`,
763-
});
770+
try {
771+
const output = execSync(
772+
`node regional_samples/disableRegionalSecretDelayedDestroy.js ${projectId} ${locationId} ${customSecretId}`
773+
);
774+
assert.match(output, new RegExp('Disabled delayed destroy'));
775+
} finally {
776+
await regionalClient.deleteSecret({
777+
name: fullSecretName,
778+
});
779+
}
764780
});
765781

766782
it('updates a regional secret delayed destroy', async () => {
783+
const customSecretId = `${secretId}-${v4()}`;
784+
const fullSecretName = `projects/${projectId}/locations/${locationId}/secrets/${customSecretId}`;
785+
767786
const updatedTimeToLive = 24 * 60 * 60 * 2;
768787
await regionalClient.createSecret({
769788
parent: `projects/${projectId}/locations/${locationId}`,
770-
secretId: `${secretId}-delayedDestroy`,
789+
secretId: customSecretId,
771790
secret: {
772791
version_destroy_ttl: {
773792
seconds: 24 * 60 * 60,
774793
},
775794
},
776795
});
777796

778-
const output = execSync(
779-
`node regional_samples/updateRegionalSecretWithDelayedDestroy.js ${projectId} ${locationId} ${secretId}-delayedDestroy ${updatedTimeToLive}`
780-
);
781-
assert.match(output, new RegExp('Updated regional secret'));
782-
await regionalClient.deleteSecret({
783-
name: `projects/${projectId}/locations/${locationId}/secrets/${secretId}-delayedDestroy`,
784-
});
797+
try {
798+
const output = execSync(
799+
`node regional_samples/updateRegionalSecretWithDelayedDestroy.js ${projectId} ${locationId} ${customSecretId} ${updatedTimeToLive}`
800+
);
801+
assert.match(output, new RegExp('Updated regional secret'));
802+
} finally {
803+
await regionalClient.deleteSecret({
804+
name: fullSecretName,
805+
});
806+
}
785807
});
786808

787809
it('creates secret with tags', async () => {

0 commit comments

Comments
 (0)