Skip to content

Commit

Permalink
[Cloud Security][FTR]Refactor API FTR to use .to.eql instead of .to.be (
Browse files Browse the repository at this point in the history
#160694)

## Summary

This PR is for refactoring current API FTR to use .to.eql instead of
.to.be for more understandable error message when an error occurs.
Currently when test fail due to unmatched value, the error message can
be quite confusing as we don't know which one is the expected and actual
value from current error message.

With this change from this PR it's easier to see which is the expected
value and which is the actual value
<img width="830" alt="Screenshot 2023-06-27 at 7 49 07 PM"
src="https://github.com/elastic/kibana/assets/8703149/361614ae-4424-4fde-a415-77947f2f8905">

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
animehart and kibanamachine authored Jul 4, 2023
1 parent b128f26 commit cd04cd3
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ export default function ({ getService }: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(500);

expect(body.message).to.be(
'Please provide either benchmarkId or packagePolicyId, but not both'
expect(body.message).to.eql(
'Please provide either benchmarkId or packagePolicyId, but not both',
`expected message to be 'Please provide either benchmarkId or packagePolicyId, but not both' but got ${body.message} instead`
);
});

Expand All @@ -80,8 +81,9 @@ export default function ({ getService }: FtrProviderContext) {
})
.expect(500);

expect(body.message).to.be(
'Please provide either benchmarkId or packagePolicyId, but not both'
expect(body.message).to.eql(
'Please provide either benchmarkId or packagePolicyId, but not both',
`expected message to be 'Please provide either benchmarkId or packagePolicyId, but not both' but got ${body.message} instead`
);
});

Expand All @@ -95,8 +97,14 @@ export default function ({ getService }: FtrProviderContext) {
})
.expect(404);

expect(body.statusCode).to.be(404);
expect(body.error).to.be('Not Found');
expect(body.statusCode).to.eql(
404,
`expected status code to be 404 but got ${body.statusCode} instead`
);
expect(body.error).to.eql(
'Not Found',
`expected error message to be 'Not Found' but got ${body.error} instead`
);
});

it(`Should return 200 status code and filter rules by benchmarkId`, async () => {
Expand Down Expand Up @@ -124,7 +132,10 @@ export default function ({ getService }: FtrProviderContext) {
(rule: CspRuleTemplate) => rule.metadata.benchmark.id === 'cis_k8s'
);

expect(allRulesHaveCorrectBenchmarkId).to.be(true);
expect(allRulesHaveCorrectBenchmarkId).to.eql(
true,
`expected true but got ${allRulesHaveCorrectBenchmarkId} instead`
);
});

it(`Should return 200 status code, and only requested fields in the response`, async () => {
Expand Down Expand Up @@ -157,7 +168,7 @@ export default function ({ getService }: FtrProviderContext) {
);
});

expect(fieldsMatched).to.be(true);
expect(fieldsMatched).to.eql(true, `expected true but got ${fieldsMatched} instead`);
});

it(`Should return 200 status code, items sorted by metadata.section field`, async () => {
Expand Down Expand Up @@ -188,7 +199,8 @@ export default function ({ getService }: FtrProviderContext) {
const isSorted = sections.every(
(section, index) => index === 0 || section >= sections[index - 1]
);
expect(isSorted).to.be(true);

expect(isSorted).to.eql(true, `expected true but got ${isSorted} instead`);
});

it(`Should return 200 status code and paginate rules with a limit of PerPage`, async () => {
Expand All @@ -213,7 +225,10 @@ export default function ({ getService }: FtrProviderContext) {
})
.expect(200);

expect(body.items.length).to.be(perPage);
expect(body.items.length).to.eql(
perPage,
`expected length to be ${perPage} but got ${body.items.length} instead`
);
});
});
}
Expand Down
14 changes: 14 additions & 0 deletions x-pack/test/api_integration/apis/cloud_security_posture/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import type { SuperTest, Test } from 'supertest';
import { Client } from '@elastic/elasticsearch';
import expect from '@kbn/expect';
import type { IndexDetails } from '@kbn/cloud-security-posture-plugin/common/types';
import { SecurityService } from '../../../../../test/common/services/security/security';

export const deleteIndex = (es: Client, indexToBeDeleted: string[]) => {
Expand Down Expand Up @@ -141,3 +143,15 @@ export const deleteRole = async (security: SecurityService, roleName: string) =>
export const deleteUser = async (security: SecurityService, userName: string) => {
await security.user.delete(userName);
};

export const assertIndexStatus = (
indicesDetails: IndexDetails[],
indexName: string,
expectedStatus: string
) => {
const actualValue = indicesDetails.find((idx) => idx.index === indexName)?.status;
expect(actualValue).to.eql(
expectedStatus,
`expected ${indexName} status to be ${expectedStatus} but got ${actualValue} instead`
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.kspm.status).to.be('index-timeout');
expect(res.kspm.status).to.eql(
'index-timeout',
`expected kspm status to be index-timeout but got ${res.kspm.status} instead`
);
});

it(`Should return index-timeout when installed cspm, has findings only on logs-cloud_security_posture.findings-default* and it has been more than 10 minutes since the installation`, async () => {
Expand All @@ -137,7 +140,10 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.cspm.status).to.be('index-timeout');
expect(res.cspm.status).to.eql(
'index-timeout',
`expected cspm status to be index-timeout but got ${res.cspm.status} instead`
);
});

it(`Should return index-timeout when installed cnvm, has findings only on logs-cloud_security_posture.vulnerabilities-default* and it has been more than 4 hours minutes since the installation`, async () => {
Expand All @@ -164,7 +170,10 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.vuln_mgmt.status).to.be('index-timeout');
expect(res.vuln_mgmt.status).to.eql(
'index-timeout',
`expected vuln_mgmt status to be index-timeout but got ${res.vuln_mgmt.status} instead`
);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.kspm.status).to.be('indexed');
expect(res.kspm.status).to.eql(
'indexed',
`expected kspm status to be indexed but got ${res.kspm.status} instead`
);
});

it(`Return cspm status indexed when logs-cloud_security_posture.findings_latest-default contains new cspm documents`, async () => {
Expand All @@ -95,7 +98,10 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.cspm.status).to.be('indexed');
expect(res.cspm.status).to.eql(
'indexed',
`expected cspm status to be indexed but got ${res.cspm.status} instead`
);
});

it(`Return vuln status indexed when logs-cloud_security_posture.vulnerabilities_latest-default contains new documents`, async () => {
Expand All @@ -114,7 +120,10 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.vuln_mgmt.status).to.be('indexed');
expect(res.vuln_mgmt.status).to.eql(
'indexed',
`expected vuln_mgmt status to be indexed but got ${res.vuln_mgmt.status} instead`
);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.kspm.status).to.be('indexing');
expect(res.kspm.status).to.eql(
'indexing',
`expected kspm status to be indexing but got ${res.kspm.status} instead`
);
});

it(`Return cspm status indexing when logs-cloud_security_posture.findings_latest-default doesn't contain new cspm documents, but has newly connected agents `, async () => {
Expand All @@ -94,7 +97,10 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.cspm.status).to.be('indexing');
expect(res.cspm.status).to.eql(
'indexing',
`expected cspm status to be indexing but got ${res.cspm.status} instead`
);
});

it(`Return vuln status indexing when logs-cloud_security_posture.vulnerabilities_latest-default doesn't contain vuln new documents, but has newly connected agents`, async () => {
Expand All @@ -113,7 +119,10 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.vuln_mgmt.status).to.be('indexing');
expect(res.vuln_mgmt.status).to.eql(
'indexing',
`expected vuln_mgmt status to be indexing but got ${res.vuln_mgmt.status} instead`
);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,26 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.kspm.status).to.be('not-deployed');
expect(res.cspm.status).to.be('not-installed');
expect(res.vuln_mgmt.status).to.be('not-installed');
expect(res.kspm.healthyAgents).to.be(0);
expect(res.kspm.installedPackagePolicies).to.be(1);
expect(res.kspm.status).to.eql(
'not-deployed',
`expected kspm status to be not-deployed but got ${res.kspm.status} instead`
);
expect(res.cspm.status).to.eql(
'not-installed',
`expected cspm status to be not-installed but got ${res.cspm.status} instead`
);
expect(res.vuln_mgmt.status).to.eql(
'not-installed',
`expected vuln_mgmt status to be not-installed but got ${res.vuln_mgmt.status} instead`
);
expect(res.kspm.healthyAgents).to.eql(
0,
`expected number of kspm healthy agents to be 0 but got ${res.kspm.healthyAgents} instead`
);
expect(res.kspm.installedPackagePolicies).to.eql(
1,
`expected number of kspm installed package policies to be 1 but got ${res.kspm.installedPackagePolicies} instead`
);
});

it(`Should return not-deployed when installed cspm, no findings on either indices and no healthy agents`, async () => {
Expand All @@ -78,11 +93,26 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.cspm.status).to.be('not-deployed');
expect(res.kspm.status).to.be('not-installed');
expect(res.vuln_mgmt.status).to.be('not-installed');
expect(res.cspm.healthyAgents).to.be(0);
expect(res.cspm.installedPackagePolicies).to.be(1);
expect(res.cspm.status).to.eql(
'not-deployed',
`expected cspm status to be not-deployed but got ${res.cspm.status} instead`
);
expect(res.kspm.status).to.eql(
'not-installed',
`expected kspm status to be not-installed but got ${res.kspm.status} instead`
);
expect(res.vuln_mgmt.status).to.eql(
'not-installed',
`expected vuln_mgmt status to be not-installed but got ${res.vuln_mgmt.status} instead`
);
expect(res.cspm.healthyAgents).to.eql(
0,
`expected number of cspm healthy agents to be 0 but got ${res.cspm.healthyAgents} instead`
);
expect(res.cspm.installedPackagePolicies).to.eql(
1,
`expected number of cspm installed package policies to be 1 but got ${res.cspm.installedPackagePolicies} instead`
);
});

it(`Should return not-deployed when installed cnvm, no findings on either indices and no healthy agents`, async () => {
Expand All @@ -101,11 +131,26 @@ export default function (providerContext: FtrProviderContext) {
.set('kbn-xsrf', 'xxxx')
.expect(200);

expect(res.cspm.status).to.be('not-installed');
expect(res.kspm.status).to.be('not-installed');
expect(res.vuln_mgmt.status).to.be('not-deployed');
expect(res.vuln_mgmt.healthyAgents).to.be(0);
expect(res.vuln_mgmt.installedPackagePolicies).to.be(1);
expect(res.cspm.status).to.eql(
'not-installed',
`expected cspm status to be not-installed but got ${res.cspm.status} instead`
);
expect(res.kspm.status).to.eql(
'not-installed',
`expected kspm status to be not-installed but got ${res.kspm.status} instead`
);
expect(res.vuln_mgmt.status).to.eql(
'not-deployed',
`expected vuln_mgmt status to be not-deployed but got ${res.vuln_mgmt.status} instead`
);
expect(res.vuln_mgmt.healthyAgents).to.eql(
0,
`expected number of vuln_mgmt healthy agents to be 0 but got ${res.vuln_mgmt.healthyAgents} instead`
);
expect(res.vuln_mgmt.installedPackagePolicies).to.eql(
1,
`expected number of vuln_mgmt installed package policies to be 1 but got ${res.vuln_mgmt.installedPackagePolicies} instead`
);
});
});
});
Expand Down
Loading

0 comments on commit cd04cd3

Please sign in to comment.