Skip to content

Commit

Permalink
add e2e analyzer verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
pereiramarco011 committed Aug 9, 2023
1 parent 35ad8bb commit 4c6b832
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
9 changes: 9 additions & 0 deletions e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions e2e/fixtures/schemas/result-analyzer.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}

11 changes: 4 additions & 7 deletions e2e/testcases/e2e-cli-066_analyze_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 7 additions & 6 deletions e2e/testcases/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions internal/console/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Check failure on line 109 in internal/console/analyze.go

View workflow job for this annotation

GitHub Actions / lint

mnd: Magic number: 0666, in <argument> detected (gomnd)

Check failure

Code scanning / gosec

Expect directory permissions to be 0750 or less Error

Expect directory permissions to be 0750 or less
if err != nil {
return err
}

f, err := os.Create(resultsPath)
if err != nil {
return err
Expand Down

0 comments on commit 4c6b832

Please sign in to comment.