From 6bb0e4b036927d336d4eb5bb49ff14114face168 Mon Sep 17 00:00:00 2001 From: DmitriyLewen <91113035+DmitriyLewen@users.noreply.github.com> Date: Mon, 1 Aug 2022 17:56:53 +0600 Subject: [PATCH] test(misconf): add tests for misconf handler for dockerfiles (#2621) --- pkg/fanal/handler/misconf/misconf_test.go | 62 ++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/pkg/fanal/handler/misconf/misconf_test.go b/pkg/fanal/handler/misconf/misconf_test.go index f6137617b6f5..c8948a094ed0 100644 --- a/pkg/fanal/handler/misconf/misconf_test.go +++ b/pkg/fanal/handler/misconf/misconf_test.go @@ -1,15 +1,75 @@ package misconf import ( + "context" "fmt" "testing" + "github.com/aquasecurity/trivy/pkg/fanal/analyzer" + misconf "github.com/aquasecurity/trivy/pkg/fanal/analyzer/config" + "github.com/aquasecurity/trivy/pkg/fanal/artifact" + "github.com/aquasecurity/trivy/pkg/fanal/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -func Test_FindingFSTarget(t *testing.T) { +func Test_Handle(t *testing.T) { + tests := []struct { + name string + files map[types.HandlerType][]types.File + filePatterns []string + wantFilePath string + wantFileType string + }{ + { + name: "happy path. Dockerfile", + files: map[types.HandlerType][]types.File{ + types.MisconfPostHandler: { + { + Path: "Dockerfile", + Type: types.Dockerfile, + Content: []byte(`FROM alpine`), + }, + }, + }, + wantFilePath: "Dockerfile", + wantFileType: types.Dockerfile, + }, + { + name: "happy path. Dockerfile with custom file name", + files: map[types.HandlerType][]types.File{ + types.MisconfPostHandler: { + { + Path: "dockerf", + Type: types.Dockerfile, + Content: []byte(`FROM alpine`), + }, + }, + }, + filePatterns: []string{"dockerfile:dockerf"}, + wantFilePath: "dockerf", + wantFileType: types.Dockerfile, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := &analyzer.AnalysisResult{ + Files: tt.files, + } + misconfHandler, err := newMisconfPostHandler(artifact.Option{MisconfScannerOption: misconf.ScannerOption{FilePatterns: tt.filePatterns}}) + assert.NoError(t, err) + blobInfo := &types.BlobInfo{} + err = misconfHandler.Handle(context.Background(), result, blobInfo) + assert.NoError(t, err) + assert.Equal(t, 1, len(blobInfo.Misconfigurations), "wrong number of misconfigurations found") + assert.Equal(t, tt.wantFilePath, blobInfo.Misconfigurations[0].FilePath, "filePaths don't equal") + assert.Equal(t, tt.wantFileType, blobInfo.Misconfigurations[0].FileType, "fileTypes don't equal") + }) + } +} + +func Test_FindingFSTarget(t *testing.T) { tests := []struct { input []string wantTarget string