|
| 1 | +import { Given, When, Then, ITestCaseHookParameter } from '@cucumber/cucumber'; |
| 2 | +import { strict as assert } from 'assert'; |
| 3 | +import Zenko from '../../world/Zenko'; |
| 4 | +import { SuperAdmin } from 'cli-testing'; |
| 5 | +import { prepareMetricsScenarios } from '../../common/utils'; |
| 6 | + |
| 7 | +interface LocationUsage { |
| 8 | + bytesTotal: number; |
| 9 | + objectsTotal: number; |
| 10 | +} |
| 11 | + |
| 12 | +interface ReportingUsageResponse { |
| 13 | + isTruncated: boolean; |
| 14 | + marker: string; |
| 15 | + accounts: Record<string, Record<string, LocationUsage>>; |
| 16 | +} |
| 17 | + |
| 18 | +export async function prepareStorageUsageReportingScenarios( |
| 19 | + world: Zenko, scenarioConfiguration: ITestCaseHookParameter, |
| 20 | +) { |
| 21 | + await prepareMetricsScenarios(world, scenarioConfiguration, { |
| 22 | + versioning: '', |
| 23 | + jobNamespace: 'storage-usage-reporting-setup', |
| 24 | + jobName: 'end2end-ops-count-items', |
| 25 | + objectSize: 200, |
| 26 | + objectCount: 3, |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +Given('two additional accounts', async function (this: Zenko) { |
| 31 | + const accountNames: string[] = []; |
| 32 | + const accountIds: string[] = []; |
| 33 | + for (let i = 0; i < 2; i++) { |
| 34 | + const name = `reporting-test-${Date.now()}-${i}`; |
| 35 | + await this.createAccount(name, true); |
| 36 | + const account = await SuperAdmin.getAccount({ accountName: name }); |
| 37 | + accountNames.push(name); |
| 38 | + accountIds.push(account.id); |
| 39 | + } |
| 40 | + this.addToSaved('additionalAccountNames', accountNames); |
| 41 | + this.addToSaved('additionalAccountIds', accountIds); |
| 42 | +}); |
| 43 | + |
| 44 | +When('the user retrieves the storage usage report', |
| 45 | + async function (this: Zenko) { |
| 46 | + const result = await this.managementAPIRequest( |
| 47 | + 'GET', |
| 48 | + `/instance/${this.parameters.InstanceID}/reporting/usage`, |
| 49 | + ); |
| 50 | + this.addToSaved('reportingResponse', result); |
| 51 | + }); |
| 52 | + |
| 53 | +When('the user retrieves the storage usage report as a no-rights user', |
| 54 | + async function (this: Zenko) { |
| 55 | + const noRightsUsername = |
| 56 | + `${this.parameters.KeycloakUsername || 'storage_manager'}-norights`; |
| 57 | + const result = await this.managementAPIRequest( |
| 58 | + 'GET', |
| 59 | + `/instance/${this.parameters.InstanceID}/reporting/usage`, |
| 60 | + {}, |
| 61 | + {}, |
| 62 | + noRightsUsername, |
| 63 | + this.parameters.KeycloakTestPassword || '123', |
| 64 | + ); |
| 65 | + this.addToSaved('reportingResponse', result); |
| 66 | + }); |
| 67 | + |
| 68 | +Then('the storage usage report response status is {int}', |
| 69 | + function (this: Zenko, expectedStatus: number) { |
| 70 | + const response = this.getSaved<{ statusCode: number }>('reportingResponse'); |
| 71 | + assert.strictEqual(response.statusCode, expectedStatus, |
| 72 | + `Expected status ${expectedStatus} but got ${response.statusCode}`); |
| 73 | + }); |
| 74 | + |
| 75 | +Then('the storage usage report response has a valid structure', |
| 76 | + function (this: Zenko) { |
| 77 | + const response = this.getSaved<{ statusCode: number; data: ReportingUsageResponse }>( |
| 78 | + 'reportingResponse'); |
| 79 | + const data = response.data; |
| 80 | + assert.strictEqual(typeof data.isTruncated, 'boolean', |
| 81 | + 'isTruncated should be a boolean'); |
| 82 | + assert.strictEqual(typeof data.marker, 'string', |
| 83 | + 'marker should be a string'); |
| 84 | + assert.strictEqual(typeof data.accounts, 'object', |
| 85 | + 'accounts should be an object'); |
| 86 | + }); |
| 87 | + |
| 88 | +Then('the storage usage report contains the additional accounts', |
| 89 | + async function (this: Zenko) { |
| 90 | + const response = this.getSaved<{ statusCode: number; data: ReportingUsageResponse }>( |
| 91 | + 'reportingResponse'); |
| 92 | + const accountIds = this.getSaved<string[]>('additionalAccountIds'); |
| 93 | + for (const accountId of accountIds) { |
| 94 | + assert.ok(accountId in response.data.accounts, |
| 95 | + `Account ${accountId} should be present in the report`); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | +Then('the storage usage report contains the test account with location {string}', |
| 100 | + async function (this: Zenko, locationName: string) { |
| 101 | + const response = this.getSaved<{ statusCode: number; data: ReportingUsageResponse }>( |
| 102 | + 'reportingResponse'); |
| 103 | + const accountName = this.getSaved<string>('accountName'); |
| 104 | + const account = await SuperAdmin.getAccount({ accountName }); |
| 105 | + |
| 106 | + assert.ok(account.id in response.data.accounts, |
| 107 | + `Account ${account.id} (${accountName}) should be present in the report`); |
| 108 | + |
| 109 | + const accountData = response.data.accounts[account.id]; |
| 110 | + assert.ok(locationName in accountData, |
| 111 | + `Location ${locationName} should be present for account ${account.id}`); |
| 112 | + |
| 113 | + this.addToSaved('reportedLocationUsage', accountData[locationName]); |
| 114 | + }); |
| 115 | + |
| 116 | +Then('the location metrics show {int} objects and {int} bytes', |
| 117 | + function (this: Zenko, expectedObjects: number, expectedBytes: number) { |
| 118 | + const usage = this.getSaved<LocationUsage>('reportedLocationUsage'); |
| 119 | + assert.strictEqual(usage.objectsTotal, expectedObjects, |
| 120 | + `Expected ${expectedObjects} objects but got ${usage.objectsTotal}`); |
| 121 | + assert.strictEqual(usage.bytesTotal, expectedBytes, |
| 122 | + `Expected ${expectedBytes} bytes but got ${usage.bytesTotal}`); |
| 123 | + }); |
0 commit comments