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

Add attestation apply test for overlay results #3287

Merged
merged 5 commits into from
Feb 18, 2025
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
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
Loading