Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Send integrated IaC test analytics [CFG-2169] #3882

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions src/lib/iac/test/v2/analytics/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
import * as createDebugLogger from 'debug';

import { policyEngineReleaseVersion } from '../local-cache/policy-engine/constants';
import { ResourceKind, TestOutput } from '../scan/results';
import * as analytics from '../../../../../lib/analytics';
import { getIacType, IacType } from './iac-type';

const debugLog = createDebugLogger('snyk-iac');

export interface IacAnalytics {
iacType: IacType;
packageManager: ResourceKind[];
'iac-type': IacType;
'iac-issues-count': number;
'iac-ignored-issues-count': number;
'iac-files-count': number;
'iac-resources-count': number;
'iac-test-binary-version': string;
'iac-error-codes': number[];
// 'iac-rules-bundle-version': string; // TODO: Add when we have the rules bundle version
iacIssuesCount: number;
iacIgnoredIssuesCount: number;
iacFilesCount: number;
iacResourcesCount: number;
iacErrorCodes: number[];
iacTestBinaryVersion: string;
// iacRulesBundleVersion: string; // TODO: Add when we have the rules bundle version
}

export function addIacAnalytics(testOutput: TestOutput): void {
const iacAnalytics = computeIacAnalytics(testOutput);

debugLog(iacAnalytics);
analytics.add('iac-type', iacAnalytics.iacType);
analytics.add('packageManager', iacAnalytics.packageManager);
analytics.add('iac-issues-count', iacAnalytics.iacIssuesCount);
analytics.add('iac-ignored-issues-count', iacAnalytics.iacIgnoredIssuesCount);
analytics.add('iac-files-count', iacAnalytics.iacFilesCount);
analytics.add('iac-resources-count', iacAnalytics.iacResourcesCount);
analytics.add('iac-error-codes', iacAnalytics.iacErrorCodes);
analytics.add('iac-test-binary-version', iacAnalytics.iacTestBinaryVersion);
}

export function computeIacAnalytics(testOutput: TestOutput): IacAnalytics {
function computeIacAnalytics(testOutput: TestOutput): IacAnalytics {
const iacType = getIacType(testOutput);

return {
'iac-type': iacType,
iacType,
packageManager: Object.keys(iacType) as ResourceKind[],
'iac-issues-count': testOutput.results?.vulnerabilities?.length || 0,
'iac-ignored-issues-count':
testOutput.results?.scanAnalytics.ignoredCount || 0,
'iac-files-count': Object.values(iacType).reduce(
iacIssuesCount: testOutput.results?.vulnerabilities?.length || 0,
iacIgnoredIssuesCount: testOutput.results?.scanAnalytics.ignoredCount || 0,
iacFilesCount: Object.values(iacType).reduce(
(acc, packageManagerAnalytics) => acc + packageManagerAnalytics!.count,
0,
),
'iac-resources-count': testOutput.results?.resources?.length || 0,
'iac-error-codes':
iacResourcesCount: testOutput.results?.resources?.length || 0,
iacErrorCodes:
[...new Set(testOutput.errors?.map((error) => error.code!))] || [],
'iac-test-binary-version': policyEngineReleaseVersion,
// iacAnalytics['iac-rules-bundle-version'] = ''; // TODO: Add when we have the rules bundle version
iacTestBinaryVersion: policyEngineReleaseVersion,
// iacRulesBundleVersion = ''; // TODO: Add when we have the rules bundle version
};
}
18 changes: 14 additions & 4 deletions test/jest/unit/lib/iac/test/v2/analytics/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as clonedeep from 'lodash.clonedeep';
import * as path from 'path';
import * as analytics from '../../../../../../../../src/lib/analytics';
import * as fs from 'fs';

import { SnykIacTestOutput } from '../../../../../../../../src/lib/iac/test/v2/scan/results';
import {
computeIacAnalytics,
addIacAnalytics,
IacAnalytics,
} from '../../../../../../../../src/lib/iac/test/v2/analytics';

Expand Down Expand Up @@ -44,15 +45,24 @@ describe('computeIacAnalytics', () => {
),
);

it('generates the correct analytics', async () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('sends the expected analytics', async () => {
// Arrange
const addedAnalytics: Record<string, any> = {};
jest.spyOn(analytics, 'add').mockImplementation((key, value) => {
addedAnalytics[key] = value;
});

const testOutput = clonedeep(snykIacTestOutputFixture);
const expectedAnalytics = clonedeep(iacAnalyticsFixture);

// Act
const result = computeIacAnalytics(testOutput);
addIacAnalytics(testOutput);

// Assert
expect(result).toStrictEqual(expectedAnalytics);
expect(addedAnalytics).toStrictEqual(expectedAnalytics);
});
});