|
1 | 1 | /* eslint-disable array-bracket-newline */
|
2 | 2 | /* eslint-disable array-element-newline */
|
3 |
| -import {expect} from 'chai' |
4 |
| -import {runCommand} from '@oclif/test' |
5 |
| -import tmp from 'tmp' |
6 |
| -import path from 'path' |
7 |
| -import fs from 'fs' |
8 |
| -import {omitHDFChangingFields} from '../utils' |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { runCommand } from '@oclif/test'; |
| 5 | +import tmp from 'tmp'; |
| 6 | +import path from 'path'; |
| 7 | +import fs from 'fs'; |
| 8 | +import { omitHDFChangingFields } from '../utils'; |
9 | 9 |
|
10 | 10 | describe('Test attest apply', () => {
|
11 |
| - const tmpobj = tmp.dirSync({unsafeCleanup: true}) |
| 11 | + let tmpobj; |
12 | 12 |
|
13 |
| - // NOTE: replacing all CR from the files being generated to ensure proper comparison. |
14 |
| - it('Successfully applies a JSON attestations file', async () => { |
15 |
| - await runCommand<{name: string}>(['attest apply', |
16 |
| - '-i', path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus.json'), |
17 |
| - path.resolve('./test/sample_data/attestations/attestations_jsonFormat.json'), |
18 |
| - '-o', `${tmpobj.name}/rhel8_attestations_jsonOutput.json`, |
19 |
| - ]) |
20 |
| - const output = JSON.parse(fs.readFileSync(`${tmpobj.name}/rhel8_attestations_jsonOutput.json`, 'utf8').replaceAll(/\r/gi, '')) |
21 |
| - const expected = JSON.parse(fs.readFileSync(path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json'), 'utf8').replaceAll(/\r/gi, '')) |
| 13 | + before(() => { |
| 14 | + tmpobj = tmp.dirSync({ unsafeCleanup: true }); |
| 15 | + }); |
22 | 16 |
|
23 |
| - expect(omitHDFChangingFields(output)).to.eql(omitHDFChangingFields(expected)) |
24 |
| - }) |
| 17 | + after(() => { |
| 18 | + tmpobj.removeCallback(); |
| 19 | + }); |
25 | 20 |
|
26 |
| - it('Successfully applies an XLSX attestations file', async () => { |
27 |
| - 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`]) |
28 |
| - const output = JSON.parse(fs.readFileSync(`${tmpobj.name}/rhel8_attestations_xlsxOutput.json`, 'utf8').replaceAll(/\r/gi, '')) |
29 |
| - const expected = JSON.parse(fs.readFileSync(path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json'), 'utf8').replaceAll(/\r/gi, '')) |
| 21 | + const captureOpts = { |
| 22 | + print: true, |
| 23 | + stripAnsi: false, |
| 24 | + }; |
| 25 | + |
| 26 | + const readAndParseJSON = (filePath) => { |
| 27 | + return JSON.parse(fs.readFileSync(filePath, 'utf8').replaceAll(/\r/gi, '')); |
| 28 | + }; |
30 | 29 |
|
31 |
| - expect(omitHDFChangingFields(output)).to.eql(omitHDFChangingFields(expected)) |
32 |
| - }) |
| 30 | + const runAndValidate = async (commandArgs, outputFilePath, expectedFilePath) => { |
| 31 | + const { stderr } = await runCommand(commandArgs, undefined, captureOpts); |
| 32 | + const output = readAndParseJSON(outputFilePath); |
| 33 | + const expected = readAndParseJSON(expectedFilePath); |
| 34 | + |
| 35 | + expect(omitHDFChangingFields(output)).to.eql(omitHDFChangingFields(expected)); |
| 36 | + expect(stderr).to.be.empty; |
| 37 | + }; |
| 38 | + |
| 39 | + it('Successfully applies a JSON attestations file', async () => { |
| 40 | + await runAndValidate( |
| 41 | + [ |
| 42 | + 'attest apply', |
| 43 | + '-i', path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus.json'), |
| 44 | + path.resolve('./test/sample_data/attestations/attestations_jsonFormat.json'), |
| 45 | + '-o', `${tmpobj.name}/rhel8_attestations_jsonOutput.json`, |
| 46 | + ], |
| 47 | + `${tmpobj.name}/rhel8_attestations_jsonOutput.json`, |
| 48 | + path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json') |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('Successfully applies an XLSX attestations file', async () => { |
| 53 | + await runAndValidate( |
| 54 | + [ |
| 55 | + 'attest apply', |
| 56 | + '-i', path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus.json'), |
| 57 | + path.resolve('./test/sample_data/attestations/attestations_xlsxFormat.xlsx'), |
| 58 | + '-o', `${tmpobj.name}/rhel8_attestations_xlsxOutput.json`, |
| 59 | + ], |
| 60 | + `${tmpobj.name}/rhel8_attestations_xlsxOutput.json`, |
| 61 | + path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json') |
| 62 | + ); |
| 63 | + }); |
33 | 64 |
|
34 | 65 | it('Successfully applies a YAML attestations file', async () => {
|
35 |
| - 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`]) |
36 |
| - const output = JSON.parse(fs.readFileSync(`${tmpobj.name}/rhel8_attestations_yamlOutput.json`, 'utf8').replaceAll(/\r/gi, '')) |
37 |
| - const expected = JSON.parse(fs.readFileSync(path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json'), 'utf8').replaceAll(/\r/gi, '')) |
| 66 | + await runAndValidate( |
| 67 | + [ |
| 68 | + 'attest apply', |
| 69 | + '-i', path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus.json'), |
| 70 | + path.resolve('./test/sample_data/attestations/attestations_yamlFormat.yaml'), |
| 71 | + '-o', `${tmpobj.name}/rhel8_attestations_yamlOutput.json`, |
| 72 | + ], |
| 73 | + `${tmpobj.name}/rhel8_attestations_yamlOutput.json`, |
| 74 | + path.resolve('./test/sample_data/attestations/rhel8_sample_oneOfEachControlStatus_output.json') |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('Successfully applies JSON attestations file to an overlay profile', async () => { |
| 79 | + await runAndValidate( |
| 80 | + [ |
| 81 | + 'attest apply', |
| 82 | + '-i', path.resolve('./test/sample_data/HDF/input/triple_overlay_profile_sample.json'), |
| 83 | + path.resolve('./test/sample_data/attestations/triple_overlay_example-attestations.json'), |
| 84 | + '-o', `${tmpobj.name}/triple_overlay_attested.json`, |
| 85 | + ], |
| 86 | + `${tmpobj.name}/triple_overlay_attested.json`, |
| 87 | + path.resolve('./test/sample_data/attestations/triple_overlay_attested.json') |
| 88 | + ); |
| 89 | + }); |
38 | 90 |
|
39 |
| - expect(omitHDFChangingFields(output)).to.eql(omitHDFChangingFields(expected)) |
40 |
| - }) |
41 |
| -}) |
| 91 | + it('Successfully applies a YAML attestations file to an overlay profile', async () => { |
| 92 | + await runAndValidate( |
| 93 | + [ |
| 94 | + 'attest apply', |
| 95 | + '-i', path.resolve('./test/sample_data/HDF/input/triple_overlay_profile_sample.json'), |
| 96 | + path.resolve('./test/sample_data/attestations/triple_overlay_example-attestations.yml'), |
| 97 | + '-o', `${tmpobj.name}/triple_overlay_attested_with_yml.json`, |
| 98 | + ], |
| 99 | + `${tmpobj.name}/triple_overlay_attested_with_yml.json`, |
| 100 | + path.resolve('./test/sample_data/attestations/triple_overlay_attested.json') |
| 101 | + ); |
| 102 | + }); |
| 103 | +}); |
0 commit comments