Skip to content

Commit ecef874

Browse files
committed
Refactor saveIdentityInformation to maintain a list of identities
Replace the three scalar World keys (identityNameForScenario, identityTypeForScenario, accountNameForScenario) with a typed SavedIdentity[] array. This makes multiple identities naturally queryable and eliminates manual additionalAccountNames tracking. - Add SavedIdentity interface and getSavedIdentities/getSavedIdentity helpers - saveIdentityInformation now appends to the array - useSavedIdentity uses getSavedIdentity() (defaults to last entry) - Migrate direct getSaved reads in bucket-policies and iam-policies - Simplify "{int} additional accounts" step - Drop additional accounts After cleanup hook Issue: ZENKO-5202
1 parent 77e25de commit ecef874

File tree

6 files changed

+38
-34
lines changed

6 files changed

+38
-34
lines changed

tests/ctst/common/common.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,9 @@ async function getTopicsOffsets(topics: string[], kafkaAdmin: Admin) {
107107
}
108108

109109
Given('{int} additional accounts', async function (this: Zenko, count: number) {
110-
const accountNames: string[] = [];
111110
for (let i = 0; i < count; i++) {
112-
const name = await this.createAccount();
113-
accountNames.push(name);
111+
await this.createAccount();
114112
}
115-
this.addToSaved('additionalAccountNames', accountNames);
116113
});
117114

118115
async function createBucket(world: Zenko, versioning: string, bucketName: string) {

tests/ctst/common/hooks.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,6 @@ After({ tags: '@AzureArchive' }, async function (this: Zenko) {
104104
);
105105
});
106106

107-
After(async function (this: Zenko, results) {
108-
const additionalAccountNames = this.getSaved<string[]>('additionalAccountNames');
109-
if (results.result?.status === 'FAILED' || !additionalAccountNames) {
110-
return;
111-
}
112-
for (const accountName of additionalAccountNames) {
113-
await cleanupAccount(this, accountName);
114-
}
115-
});
116-
117107
After({ tags: '@BP-ASSUME_ROLE_USER_CROSS_ACCOUNT'}, async function (this: Zenko, results) {
118108
const crossAccountName = this.getSaved<string>('crossAccountName');
119109

tests/ctst/steps/bucket-policies/common.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ Given('an {string} IAM Policy that {string} with {string} effect for the current
152152
|| identityType === EntityType.DATA_CONSUMER) {
153153
const result = await IAM.attachRolePolicy({
154154
policyArn,
155-
roleName: this.getSaved<string>('identityNameForScenario'),
155+
roleName: this.getSavedIdentity().identityName,
156156
});
157157
assert.ifError(result.stderr || result.err);
158158
}
159159
if (identityType === EntityType.IAM_USER) {
160160
const result = await IAM.attachUserPolicy({
161161
policyArn,
162-
userName: this.getSaved<string>('identityNameForScenario'),
162+
userName: this.getSavedIdentity().identityName,
163163
});
164164
assert.ifError(result.stderr || result.err);
165165
}
@@ -361,13 +361,13 @@ Given('an environment setup for the API', async function (this: Zenko) {
361361
|| identityType === EntityType.DATA_CONSUMER) {
362362
const result = await IAM.attachRolePolicy({
363363
policyArn,
364-
roleName: this.getSaved<string>('identityNameForScenario'),
364+
roleName: this.getSavedIdentity().identityName,
365365
});
366366
assert.ifError(result.stderr || result.err);
367367
} else if (identityType === EntityType.IAM_USER) { // accounts do not have any policy
368368
const result = await IAM.attachUserPolicy({
369369
policyArn,
370-
userName: this.getSaved<string>('identityNameForScenario'),
370+
userName: this.getSavedIdentity().identityName,
371371
});
372372
assert.ifError(result.stderr || result.err);
373373
}
@@ -434,13 +434,13 @@ Given('an environment setup for the API', async function (this: Zenko) {
434434
|| identityType === EntityType.DATA_CONSUMER) {
435435
const result = await IAM.detachRolePolicy({
436436
policyArn,
437-
roleName: this.getSaved<string>('identityNameForScenario'),
437+
roleName: this.getSavedIdentity().identityName,
438438
});
439439
assert.ifError(result.stderr || result.err);
440440
} else if (identityType === EntityType.IAM_USER) { // accounts do not have any policy
441441
const detachResult = await IAM.detachUserPolicy({
442442
policyArn,
443-
userName: this.getSaved<string>('identityNameForScenario'),
443+
userName: this.getSavedIdentity().identityName,
444444
});
445445
assert.ifError(detachResult.stderr || detachResult.err);
446446
}

tests/ctst/steps/iam-policies/common.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { When, Then } from '@cucumber/cucumber';
22
import { strict as assert } from 'assert';
33
import Zenko from '../../world/Zenko';
4-
import { CacheHelper, ClientOptions, Command, Identity, IdentityEnum, VaultAuth } from 'cli-testing';
4+
import { CacheHelper, ClientOptions, Command, Identity, VaultAuth } from 'cli-testing';
55
import { runActionAgainstBucket } from 'steps/utils/utils';
66

77
When('the user tries to perform {string} on the bucket', async function (this: Zenko, action: string) {
88
await runActionAgainstBucket(this, action);
99
});
1010

1111
When('the user tries to perform vault auth {string}', async function (this: Zenko, action: string) {
12+
const lastIdentity = this.getSavedIdentity();
1213
const userCredentials = Identity.getCredentialsForIdentity(
13-
this.getSaved<IdentityEnum>('identityTypeForScenario'),
14-
this.getSaved<string>('identityNameForScenario'),
15-
this.getSaved<string>('accountNameForScenario'),
14+
lastIdentity.identityType,
15+
lastIdentity.identityName,
16+
lastIdentity.accountName,
1617
);
1718

1819
if (!userCredentials) {
@@ -35,7 +36,7 @@ When('the user tries to perform vault auth {string}', async function (this: Zenk
3536
switch (action) {
3637
case 'GetAccountInfo':
3738
this.setResult(await VaultAuth.getAccounts([
38-
this.getSaved<string>('accountNameForScenario') || this.parameters.AccountName,
39+
lastIdentity.accountName || this.parameters.AccountName,
3940
], null, null, {
4041
// @ts-expect-error accountNames is not generated by CTST yet
4142
accountNames: true,

tests/ctst/steps/reporting/storageUsageReporting.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { When, Then, ITestCaseHookParameter } from '@cucumber/cucumber';
22
import { strict as assert } from 'assert';
33
import Zenko from '../../world/Zenko';
4+
import { IdentityEnum } from 'cli-testing';
45
import { prepareMetricsScenarios } from '../../common/utils';
56

67
interface LocationUsage {
@@ -71,7 +72,9 @@ Then('the storage usage report contains the additional accounts',
7172
async function (this: Zenko) {
7273
const response = this.getSaved<{ statusCode: number; data: ReportingUsageResponse }>(
7374
'reportingResponse');
74-
const accountNames = this.getSaved<string[]>('additionalAccountNames');
75+
const accountNames = this.getSavedIdentities()
76+
.filter(id => id.identityType === IdentityEnum.ACCOUNT)
77+
.map(id => id.accountName);
7578
for (const accountName of accountNames) {
7679
assert.ok(accountName in response.data.accounts,
7780
`Account ${accountName} should be present in the report`);

tests/ctst/world/Zenko.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ interface ServiceUsersCredentials {
3232
}
3333

3434
// Zenko entities
35+
export interface SavedIdentity {
36+
identityName: string;
37+
identityType: IdentityEnum;
38+
accountName: string;
39+
}
40+
3541
export enum EntityType {
3642
ACCOUNT = 'ACCOUNT',
3743
IAM_USER = 'IAM_USER',
@@ -770,17 +776,24 @@ export default class Zenko extends World<ZenkoWorldParameters> {
770776
}
771777

772778
saveIdentityInformation(name: string, identity: IdentityEnum, accountName: string) {
773-
this.addToSaved('identityNameForScenario', name);
774-
this.addToSaved('identityTypeForScenario', identity);
775-
this.addToSaved('accountNameForScenario', accountName);
779+
const identities = this.getSavedIdentities();
780+
identities.push({ identityName: name, identityType: identity, accountName });
781+
this.addToSaved('savedIdentities', identities);
782+
}
783+
784+
getSavedIdentities(): SavedIdentity[] {
785+
return this.getSaved<SavedIdentity[]>('savedIdentities') || [];
786+
}
787+
788+
getSavedIdentity(index = -1): SavedIdentity {
789+
const identities = this.getSavedIdentities();
790+
const i = index < 0 ? identities.length + index : index;
791+
return identities[i];
776792
}
777793

778794
useSavedIdentity() {
779-
Identity.useIdentity(
780-
this.getSaved<IdentityEnum>('identityTypeForScenario'),
781-
this.getSaved<string>('identityNameForScenario'),
782-
this.getSaved<string>('accountNameForScenario'),
783-
);
795+
const last = this.getSavedIdentity();
796+
Identity.useIdentity(last.identityType, last.identityName, last.accountName);
784797
}
785798

786799
/**

0 commit comments

Comments
 (0)