Skip to content

Commit

Permalink
chore: Update custom bundle.tar.gz in fixtures
Browse files Browse the repository at this point in the history
Co-authored-by: Teodora Sandu <teodora.sandu@snyk.io>
  • Loading branch information
2 people authored and aron committed Jan 4, 2022
1 parent 4cbc544 commit ce9fbe4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
Binary file modified test/fixtures/iac/custom-rules/custom.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion test/jest/acceptance/iac/custom-rules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('iac test --rules', () => {
expect(stdout).toContain('Testing ./iac/terraform/sg_open_ssh.tf');
expect(stdout).toContain('Infrastructure as code issues:');
expect(stdout).toContain('Missing tags');
expect(stdout).toContain('CUSTOM-123');
expect(stdout).toContain('CUSTOM-1');
expect(stdout).toContain(
'introduced by input > resource > aws_security_group[allow_ssh] > tags',
);
Expand Down
65 changes: 43 additions & 22 deletions test/jest/unit/iac-unit-tests/file-scanner.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as mockFs from 'mock-fs';
import * as path from 'path';
import {
scanFiles,
clearPolicyEngineCache,
} from '../../../../src/cli/commands/test/iac-local-execution/file-scanner';
import { LOCAL_POLICY_ENGINE_DIR } from '../../../../src/cli/commands/test/iac-local-execution/local-cache';
import { IacFileParsed } from '../../../../src/cli/commands/test/iac-local-execution/types';
import {
EngineType,
IacFileParsed,
} from '../../../../src/cli/commands/test/iac-local-execution/types';

import {
paresdKubernetesFileStub,
Expand All @@ -15,6 +17,7 @@ import {
expectedViolatedPoliciesForTerraform,
expectedViolatedPoliciesForArm,
} from './file-scanner.fixtures';
import * as localCacheModule from '../../../../src/cli/commands/test/iac-local-execution/local-cache';

describe('scanFiles', () => {
const parsedFiles: Array<IacFileParsed> = [
Expand All @@ -24,23 +27,48 @@ describe('scanFiles', () => {
];

afterEach(() => {
mockFs.restore();
clearPolicyEngineCache();
});

describe('with parsed files', () => {
it('returns the expected violated policies', async () => {
mockFs({
[path.resolve(
__dirname,
path.join('../../../..', LOCAL_POLICY_ENGINE_DIR),
)]: mockFs.load(
path.resolve(
__dirname,
path.join('../../../smoke', LOCAL_POLICY_ENGINE_DIR),
),
),
});
const policyEngineCoreDataPath = path.resolve(
__dirname,
path.join('../../../smoke', LOCAL_POLICY_ENGINE_DIR),
);
const policyEngineMetaDataPath = path.resolve(
__dirname,
path.join('../../../smoke', LOCAL_POLICY_ENGINE_DIR),
);

const spy = jest
.spyOn(localCacheModule, 'getLocalCachePath')
.mockImplementation((engineType: EngineType) => {
switch (engineType) {
case EngineType.Kubernetes:
return [
`${policyEngineCoreDataPath}/k8s_policy.wasm`,
`${policyEngineMetaDataPath}/k8s_data.json`,
];
case EngineType.Terraform:
return [
`${policyEngineCoreDataPath}/tf_policy.wasm`,
`${policyEngineMetaDataPath}/tf_data.json`,
];
case EngineType.CloudFormation:
return [
`${policyEngineCoreDataPath}/cloudformation_policy.wasm`,
`${policyEngineMetaDataPath}/cloudformation_data.json`,
];
case EngineType.ARM:
return [
`${policyEngineCoreDataPath}/arm_policy.wasm`,
`${policyEngineMetaDataPath}/arm_data.json`,
];
default:
return [];
}
});

const scanResults = await scanFiles(parsedFiles);
expect(scanResults[0].violatedPolicies).toEqual(
Expand All @@ -52,20 +80,13 @@ describe('scanFiles', () => {
expect(scanResults[2].violatedPolicies).toEqual(
expectedViolatedPoliciesForArm,
);
spy.mockReset();
});

// TODO: Extract policy engine & the cache mechanism, test them separately.
});

describe('missing policy engine wasm files', () => {
it('throws an error', async () => {
mockFs({
[path.resolve(
__dirname,
path.join('../../../..', LOCAL_POLICY_ENGINE_DIR),
)]: {},
});

await expect(scanFiles(parsedFiles)).rejects.toThrow();
});
});
Expand Down

0 comments on commit ce9fbe4

Please sign in to comment.