From 4c6b8326c5c66256710d29e7c7a560be41154f50 Mon Sep 17 00:00:00 2001 From: pereiramarco011 Date: Wed, 9 Aug 2023 17:30:18 +0100 Subject: [PATCH] add e2e analyzer verifier --- e2e/cli_test.go | 9 +++++++ e2e/fixtures/schemas/result-analyzer.json | 27 ++++++++++++++++++++ e2e/testcases/e2e-cli-066_analyze_command.go | 11 +++----- e2e/testcases/general.go | 13 +++++----- internal/console/analyze.go | 6 +++++ 5 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 e2e/fixtures/schemas/result-analyzer.json diff --git a/e2e/cli_test.go b/e2e/cli_test.go index 09fff247eaa..431f2c00aba 100644 --- a/e2e/cli_test.go +++ b/e2e/cli_test.go @@ -79,6 +79,10 @@ func Test_E2E_CLI(t *testing.T) { checkExpectedOutput(t, &tt, arg) } + if tt.Args.ExpectedAnalyzerResults != nil && arg < len(tt.Args.ExpectedResult) { + checkExpectedAnalyzerResults(t, &tt, arg) + } + if tt.Args.ExpectedPayload != nil { // Check payload file utils.FileCheck(t, tt.Args.ExpectedPayload[arg], tt.Args.ExpectedPayload[arg], "payload") @@ -127,6 +131,11 @@ func Test_E2E_CLI(t *testing.T) { }) } +func checkExpectedAnalyzerResults(t *testing.T, tt *testcases.TestCase, argIndex int) { + jsonFileName := tt.Args.ExpectedResult[argIndex].ResultsFile + ".json" + utils.JSONSchemaValidationFromFile(t, jsonFileName, "AnalyzerResults.json") +} + func checkExpectedOutput(t *testing.T, tt *testcases.TestCase, argIndex int) { jsonFileName := tt.Args.ExpectedResult[argIndex].ResultsFile + ".json" resultsFormats := tt.Args.ExpectedResult[argIndex].ResultsFormats diff --git a/e2e/fixtures/schemas/result-analyzer.json b/e2e/fixtures/schemas/result-analyzer.json new file mode 100644 index 00000000000..35323437f72 --- /dev/null +++ b/e2e/fixtures/schemas/result-analyzer.json @@ -0,0 +1,27 @@ +{ + "type": "object", + "required": [ + "types", + "exc", + "expectedloc" + ], + "properties": { + "types": { + "type": "array", + "items": { + "type": "string" + } + }, + "exc": { + "type": "array", + "items": { + "type": "string" + } + }, + "expectedloc": { + "type": "integer", + "minimum": 0 + } + } + } + \ No newline at end of file diff --git a/e2e/testcases/e2e-cli-066_analyze_command.go b/e2e/testcases/e2e-cli-066_analyze_command.go index c9ba3b7cdb4..f848d7a7c94 100644 --- a/e2e/testcases/e2e-cli-066_analyze_command.go +++ b/e2e/testcases/e2e-cli-066_analyze_command.go @@ -12,15 +12,12 @@ func init() { //nolint "--analyze-path", "/path/e2e/fixtures/samples/swagger", "--analyze-results", "/path/e2e/output/E2E_CLI_066_ANALYZE_RESULTS.json"}, }, - UseMock: []bool{true, true}, - ExpectedResult: []ResultsValidation{ - { - ResultsFile: "E2E_CLI_066_ANALYZE_RESULTS", - ResultsFormats: []string{"json"}, - }, + ExpectedAnalyzerResults: &ResultsValidation{ + ResultsFile: "E2E_CLI_066_ANALYZE_RESULTS", + ResultsFormats: []string{"json"}, }, }, - WantStatus: []int{126}, + WantStatus: []int{0}, } Tests = append(Tests, testSample) } diff --git a/e2e/testcases/general.go b/e2e/testcases/general.go index 083025c9072..c7257a602d0 100644 --- a/e2e/testcases/general.go +++ b/e2e/testcases/general.go @@ -20,12 +20,13 @@ type ResultsValidation struct { } type args struct { - Args []cmdArgs // args to pass to kics binary - ExpectedOut []string // path to file with expected output - ExpectedPayload []string - ExpectedResult []ResultsValidation - ExpectedLog LogValidation - UseMock []bool + Args []cmdArgs // args to pass to kics binary + ExpectedOut []string // path to file with expected output + ExpectedPayload []string + ExpectedResult []ResultsValidation + ExpectedAnalyzerResults *ResultsValidation + ExpectedLog LogValidation + UseMock []bool } type TestTemplates struct { diff --git a/internal/console/analyze.go b/internal/console/analyze.go index f747b5cec9f..00369f2d06d 100644 --- a/internal/console/analyze.go +++ b/internal/console/analyze.go @@ -4,6 +4,7 @@ import ( _ "embed" // Embed kics CLI img and analyze-flags "encoding/json" "os" + "path/filepath" "github.com/Checkmarx/kics/internal/console/flags" sentryReport "github.com/Checkmarx/kics/internal/sentry" @@ -105,6 +106,11 @@ func executeAnalyze(analyzeParams *analyzer.Parameters) error { } func writeToFile(resultsPath string, analyzerResults model.AnalyzedPaths) error { + err := os.MkdirAll(filepath.Dir(resultsPath), 0666) + if err != nil { + return err + } + f, err := os.Create(resultsPath) if err != nil { return err