Skip to content

Commit

Permalink
Add attestation apply test for overlay results (#3287)
Browse files Browse the repository at this point in the history
* updating attestation apply test to include an overlay

Signed-off-by: Emily Rodriguez <[email protected]>

* refactor test file

Signed-off-by: Emily Rodriguez <[email protected]>

---------

Signed-off-by: Emily Rodriguez <[email protected]>
Co-authored-by: Amndeep Singh Mann <[email protected]>
  • Loading branch information
em-c-rod and Amndeep7 authored Feb 18, 2025
1 parent b78c6bb commit 6bca001
Show file tree
Hide file tree
Showing 5 changed files with 2,645 additions and 30 deletions.
122 changes: 92 additions & 30 deletions test/commands/attest/apply.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,103 @@
/* eslint-disable array-bracket-newline */
/* eslint-disable array-element-newline */
import {expect} from 'chai'
import {runCommand} from '@oclif/test'
import tmp from 'tmp'
import path from 'path'
import fs from 'fs'
import {omitHDFChangingFields} from '../utils'
import { expect } from 'chai';
import { runCommand } from '@oclif/test';
import tmp from 'tmp';
import path from 'path';
import fs from 'fs';
import { omitHDFChangingFields } from '../utils';

describe('Test attest apply', () => {
const tmpobj = tmp.dirSync({unsafeCleanup: true})
let tmpobj;

// NOTE: replacing all CR from the files being generated to ensure proper comparison.
it('Successfully applies a JSON attestations file', async () => {
await runCommand<{name: string}>(['attest apply',
'-i', path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus.json'),
path.resolve('./test/sample_data/attestations/attestations_jsonFormat.json'),
'-o', `${tmpobj.name}/rhel8_attestations_jsonOutput.json`,
])
const output = JSON.parse(fs.readFileSync(`${tmpobj.name}/rhel8_attestations_jsonOutput.json`, 'utf8').replaceAll(/\r/gi, ''))
const expected = JSON.parse(fs.readFileSync(path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json'), 'utf8').replaceAll(/\r/gi, ''))
before(() => {
tmpobj = tmp.dirSync({ unsafeCleanup: true });
});

expect(omitHDFChangingFields(output)).to.eql(omitHDFChangingFields(expected))
})
after(() => {
tmpobj.removeCallback();
});

it('Successfully applies an XLSX attestations file', async () => {
await runCommand<{name: string}>(['attest apply', '-i', path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus.json'), path.resolve('./test/sample_data/attestations/attestations_xlsxFormat.xlsx'), '-o', `${tmpobj.name}/rhel8_attestations_xlsxOutput.json`])
const output = JSON.parse(fs.readFileSync(`${tmpobj.name}/rhel8_attestations_xlsxOutput.json`, 'utf8').replaceAll(/\r/gi, ''))
const expected = JSON.parse(fs.readFileSync(path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json'), 'utf8').replaceAll(/\r/gi, ''))
const captureOpts = {
print: true,
stripAnsi: false,
};

const readAndParseJSON = (filePath) => {
return JSON.parse(fs.readFileSync(filePath, 'utf8').replaceAll(/\r/gi, ''));
};

expect(omitHDFChangingFields(output)).to.eql(omitHDFChangingFields(expected))
})
const runAndValidate = async (commandArgs, outputFilePath, expectedFilePath) => {
const { stderr } = await runCommand(commandArgs, undefined, captureOpts);
const output = readAndParseJSON(outputFilePath);
const expected = readAndParseJSON(expectedFilePath);

expect(omitHDFChangingFields(output)).to.eql(omitHDFChangingFields(expected));
expect(stderr).to.be.empty;
};

it('Successfully applies a JSON attestations file', async () => {
await runAndValidate(
[
'attest apply',
'-i', path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus.json'),
path.resolve('./test/sample_data/attestations/attestations_jsonFormat.json'),
'-o', `${tmpobj.name}/rhel8_attestations_jsonOutput.json`,
],
`${tmpobj.name}/rhel8_attestations_jsonOutput.json`,
path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json')
);
});

it('Successfully applies an XLSX attestations file', async () => {
await runAndValidate(
[
'attest apply',
'-i', path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus.json'),
path.resolve('./test/sample_data/attestations/attestations_xlsxFormat.xlsx'),
'-o', `${tmpobj.name}/rhel8_attestations_xlsxOutput.json`,
],
`${tmpobj.name}/rhel8_attestations_xlsxOutput.json`,
path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json')
);
});

it('Successfully applies a YAML attestations file', async () => {
await runCommand<{name: string}>(['attest apply', '-i', path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus.json'), path.resolve('./test/sample_data/attestations/attestations_yamlFormat.yaml'), '-o', `${tmpobj.name}/rhel8_attestations_yamlOutput.json`])
const output = JSON.parse(fs.readFileSync(`${tmpobj.name}/rhel8_attestations_yamlOutput.json`, 'utf8').replaceAll(/\r/gi, ''))
const expected = JSON.parse(fs.readFileSync(path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json'), 'utf8').replaceAll(/\r/gi, ''))
await runAndValidate(
[
'attest apply',
'-i', path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus.json'),
path.resolve('./test/sample_data/attestations/attestations_yamlFormat.yaml'),
'-o', `${tmpobj.name}/rhel8_attestations_yamlOutput.json`,
],
`${tmpobj.name}/rhel8_attestations_yamlOutput.json`,
path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json')
);
});

it('Successfully applies JSON attestations file to an overlay profile', async () => {
await runAndValidate(
[
'attest apply',
'-i', path.resolve('./test/sample_data/HDF/input/triple_overlay_profile_sample.json'),
path.resolve('./test/sample_data/attestations/triple_overlay_example-attestations.json'),
'-o', `${tmpobj.name}/triple_overlay_attested.json`,
],
`${tmpobj.name}/triple_overlay_attested.json`,
path.resolve('./test/sample_data/attestations/triple_overlay_attested.json')
);
});

expect(omitHDFChangingFields(output)).to.eql(omitHDFChangingFields(expected))
})
})
it('Successfully applies a YAML attestations file to an overlay profile', async () => {
await runAndValidate(
[
'attest apply',
'-i', path.resolve('./test/sample_data/HDF/input/triple_overlay_profile_sample.json'),
path.resolve('./test/sample_data/attestations/triple_overlay_example-attestations.yml'),
'-o', `${tmpobj.name}/triple_overlay_attested_with_yml.json`,
],
`${tmpobj.name}/triple_overlay_attested_with_yml.json`,
path.resolve('./test/sample_data/attestations/triple_overlay_attested.json')
);
});
});
Loading

0 comments on commit 6bca001

Please sign in to comment.