Skip to content

Commit

Permalink
feat: refactor unit tests to handle mode, configure json to only show…
Browse files Browse the repository at this point in the history
… up for vm mode
  • Loading branch information
IgorEulalio committed Apr 8, 2024
1 parent 3b6e3e6 commit 4d95d50
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
11 changes: 8 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function parseActionInputs() {
groupByPackage: core.getInput('group-by-package') == 'true',
extraParameters: core.getInput('extra-parameters'),
mode: core.getInput('mode') || vmMode,
recursive: core.getInput('recursive') || false,
recursive: core.getInput('recursive') == 'true',
minimumSeverity: core.getInput('minimum-severity'),
iacScanPath: core.getInput('iac-scan-path') || './'
}
Expand Down Expand Up @@ -200,6 +200,7 @@ function composeFlags(opts) {
}

if (opts.mode && opts.mode == vmMode) {
flags += ` --json-scan-result=${cliScannerResult}`
flags += ` ${opts.imageTag}`;
}

Expand All @@ -221,14 +222,17 @@ function writeReport(reportData) {
function validateInput(opts) {
if (!opts.standalone && !opts.sysdigSecureToken) {
core.setFailed("Sysdig Secure Token is required for standard execution, please set your token or remove the standalone input.");
throw new Error("Sysdig Secure Token is required for standard execution, please set your token or remove the standalone input.");
}

if (opts.mode && opts.mode == vmMode && !opts.imageTag) {
core.setFailed("Image Tag is required for VM mode.");
core.setFailed("image-tag is required for VM mode.");
throw new Error("image-tag is required for VM mode.");
}

if (opts.mode && opts.mode == iacMode && opts.iacScanPath == "") {
core.setFailed("IaC Scan Path can't be empty, please specify the path you want to scan your manifest resources.");
core.setFailed("iac-scan-path can't be empty, please specify the path you want to scan your manifest resources.");
throw new Error("iac-scan-path can't be empty, please specify the path you want to scan your manifest resources.");
}
}

Expand Down Expand Up @@ -855,6 +859,7 @@ module.exports = {
executeScan,
processScanResult,
run,
validateInput,
cliScannerName,
cliScannerResult,
cliScannerVersion,
Expand Down
29 changes: 24 additions & 5 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ describe("input parsing", () => {

it("raises error if no image tag provided", () => {
process.env['INPUT_SYSDIG-SECURE-TOKEN'] = "token";
expect(() => index.parseActionInputs()).toThrow("Input required and not supplied: image-tag");
let opts = index.parseActionInputs()
expect(() => index.validateInput(opts)).toThrow("image-tag is required for VM mode.");
})

it("sets default for inputs", () => {
Expand All @@ -65,7 +66,11 @@ describe("input parsing", () => {
"sysdigSkipTLS": false,
"severityAtLeast": "any",
"groupByPackage": false,
"extraParameters": ""
"extraParameters": "",
"iacScanPath": "./",
"recursive": false,
"minimumSeverity": "",
"mode": "vm"
})
})

Expand All @@ -89,6 +94,10 @@ describe("input parsing", () => {
process.env['INPUT_SEVERITY-AT-LEAST'] = "medium";
process.env['INPUT_GROUP-BY-PACKAGE'] = 'true';
process.env['INPUT_EXTRA-PARAMETERS'] = "--extra-param";
process.env['INPUT_IAC-SCAN-PATH'] = "./";
process.env['INPUT_RECURSIVE'] = "true";
process.env['INPUT_MINIMUM-SEVERITY'] = "high";
process.env['INPUT_MODE'] = "vm";
let opts = index.parseActionInputs()

expect(opts).toEqual({
Expand All @@ -110,20 +119,30 @@ describe("input parsing", () => {
"sysdigSkipTLS": true,
"severityAtLeast": "medium",
"groupByPackage": true,
"extraParameters": "--extra-param"
"extraParameters": "--extra-param",
"iacScanPath": "./",
"recursive": true,
"minimumSeverity": "high",
"mode": "vm"
})
})

})

describe("execution flags", () => {

it("uses default flags", () => {
let flags = index.composeFlags({ sysdigSecureToken: "foo-token", imageTag: "image:tag" });
it("uses default flags for VM mode", () => {
let flags = index.composeFlags({ sysdigSecureToken: "foo-token", imageTag: "image:tag", mode: "vm" });
expect(flags.envvars.SECURE_API_TOKEN).toMatch("foo-token");
expect(flags.flags).toMatch(/(^| )image:tag($| )/);
})

it("uses default flags for IaC mode", () => {
let flags = index.composeFlags({ sysdigSecureToken: "foo-token", mode: "iac", iacScanPath: "/my-special-path" });
expect(flags.envvars.SECURE_API_TOKEN).toMatch("foo-token");
expect(flags.flags).toMatch(/(^| )--iac \/my-special-path($| )/);
})

it("adds secure URL flag", () => {
let flags = index.composeFlags({
sysdigSecureToken: "foo-token",
Expand Down

0 comments on commit 4d95d50

Please sign in to comment.