Skip to content

Commit

Permalink
add benchmark tests and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Omolola-Akinleye committed Nov 22, 2023
1 parent 58485f1 commit f91fd17
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ export const toBenchmarkDocFieldKey = (benchmarkId: string, benchmarkVersion: st
return `${benchmarkId}${DELIMITER}${benchmarkVersion}`;
};

export const toBenchmarkMappingFieldKey = (benchmarkId: string, benchmarkVersion: string) => {
if (benchmarkVersion.includes(DOC_FIELD_VERSION_DELIMITER))
return `${benchmarkId}${DELIMITER}${benchmarkVersion.replaceAll(
`${DOC_FIELD_VERSION_DELIMITER}`,
MAPPING_VERSION_DELIMITER
)}`;
return `${benchmarkId}${DELIMITER}${benchmarkVersion}`;
export const toBenchmarkMappingFieldKey = (benchmarkVersion: string) => {
return `${benchmarkVersion.replace(
`/${DOC_FIELD_VERSION_DELIMITER}/g`,
MAPPING_VERSION_DELIMITER
)}`;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { BenchmarkBucket, getBenchmarksFromAggs } from './get_benchmarks';

const mockBenchmarkBuckets: BenchmarkBucket[] = [
{
key: 'cis_aws',
doc_count: 12,
aggs_by_benchmark_version: {
buckets: [
{
key: 'v1.5.0',
doc_count: 12,
asset_count: {
value: 1,
},
aggs_by_resource_type: {
buckets: [
{
key: 'foo_type',
doc_count: 6,
passed_findings: {
doc_count: 3,
},
failed_findings: {
doc_count: 3,
},
score: {
value: 0.5,
},
},
{
key: 'boo_type',
doc_count: 6,
passed_findings: {
doc_count: 3,
},
failed_findings: {
doc_count: 3,
},
score: {
value: 0.5,
},
},
],
},
aggs_by_benchmark_name: {
buckets: [
{
key: 'CIS Amazon Web Services Foundations',
doc_count: 12,
},
],
},
passed_findings: {
doc_count: 6,
},
failed_findings: {
doc_count: 6,
},
},
],
},
},
];

describe('getBenchmarksFromAggs', () => {
it('should return value matching ComplianceDashboardData["benchmarks"]', async () => {
const benchmarks = getBenchmarksFromAggs(mockBenchmarkBuckets);
expect(benchmarks).toEqual([
{
meta: {
benchmarkId: 'cis_aws',
benchmarkVersion: 'v1.5.0',
benchmarkName: 'CIS Amazon Web Services Foundations',
assetCount: 1,
},
stats: {
totalFindings: 12,
totalFailed: 6,
totalPassed: 6,
postureScore: 50.0,
},
groupedFindingsEvaluation: [
{
name: 'foo_type',
totalFindings: 6,
totalFailed: 3,
totalPassed: 3,
postureScore: 50.0,
},
{
name: 'boo_type',
totalFindings: 6,
totalFailed: 3,
totalPassed: 3,
postureScore: 50.0,
},
],
},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const getBenchmarksQuery = (
},
});

const getBenchmarksFromAggs = (benchmarks: BenchmarkBucket[]) => {
export const getBenchmarksFromAggs = (benchmarks: BenchmarkBucket[]) => {
return benchmarks.flatMap((benchmarkAggregation: BenchmarkBucket) => {
const benchmarkId = benchmarkAggregation.key;
const versions = benchmarkAggregation.aggs_by_benchmark_version.buckets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ const trendDocs: ScoreTrendDoc[] = [
failed_findings: 15,
},
},
score_by_benchmark_id: {
cis_gcp: {
v2_0_0: {
total_findings: 6,
passed_findings: 3,
failed_findings: 3,
},
},
},
},
{
'@timestamp': '2022-04-06T15:00:00Z',
Expand All @@ -38,22 +47,27 @@ const trendDocs: ScoreTrendDoc[] = [
failed_findings: 5,
},
},
},
{
'@timestamp': '2022-04-05T15:30:00Z',
total_findings: 30,
passed_findings: 25,
failed_findings: 5,
score_by_cluster_id: {
forth_cluster_id: {
total_findings: 25,
passed_findings: 25,
failed_findings: 0,
score_by_benchmark_id: {
cis_gcp: {
v2_0_0: {
total_findings: 6,
passed_findings: 3,
failed_findings: 3,
},
},
fifth_cluster_id: {
total_findings: 5,
passed_findings: 0,
failed_findings: 5,
cis_azure: {
v2_0_0: {
total_findings: 6,
passed_findings: 3,
failed_findings: 3,
},
},
cis_aws: {
v1_5_0: {
total_findings: 6,
passed_findings: 3,
failed_findings: 3,
},
},
},
},
Expand All @@ -79,6 +93,14 @@ describe('getTrendsFromQueryResult', () => {
postureScore: 25.0,
},
},
benchmarks: {
'cis_gcp;v2.0.0': {
totalFailed: 3,
totalFindings: 6,
totalPassed: 3,
postureScore: 50,
},
},
},
{
timestamp: '2022-04-06T15:00:00Z',
Expand All @@ -102,27 +124,24 @@ describe('getTrendsFromQueryResult', () => {
postureScore: 75.0,
},
},
},
{
timestamp: '2022-04-05T15:30:00Z',
summary: {
totalFindings: 30,
totalPassed: 25,
totalFailed: 5,
postureScore: 83.3,
},
clusters: {
forth_cluster_id: {
totalFindings: 25,
totalPassed: 25,
totalFailed: 0,
postureScore: 100.0,
benchmarks: {
'cis_gcp;v2.0.0': {
totalFailed: 3,
totalFindings: 6,
totalPassed: 3,
postureScore: 50.0,
},
fifth_cluster_id: {
totalFindings: 5,
totalPassed: 0,
totalFailed: 5,
postureScore: 0,
'cis_azure;v2.0.0': {
totalFailed: 3,
totalFindings: 6,
totalPassed: 3,
postureScore: 50.0,
},
'cis_aws;v1.5.0': {
totalFailed: 3,
totalFindings: 6,
totalPassed: 3,
postureScore: 50.0,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,9 @@ const getFindingsScoresDocIndexingPromises = (
const benchmarkId = benchmarkIdBucket.key;
const benchmarkVersions = Object.fromEntries(
benchmarkIdBucket.benchmark_versions.buckets.map((benchmarkVersionBucket) => {
const benchmarkIdVersion = toBenchmarkMappingFieldKey(
benchmarkId,
benchmarkVersionBucket.key
);
const benchmarkVersion = toBenchmarkMappingFieldKey(benchmarkVersionBucket.key);
return [
benchmarkIdVersion,
benchmarkVersion,
{
total_findings: benchmarkVersionBucket.total_findings.value,
passed_findings: benchmarkVersionBucket.passed_findings.doc_count,
Expand Down

0 comments on commit f91fd17

Please sign in to comment.