Skip to content

Commit

Permalink
test(misconf): add tests for misconf handler for dockerfiles (#2621)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Aug 1, 2022
1 parent 44d53be commit 6bb0e4b
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion pkg/fanal/handler/misconf/misconf_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 6bb0e4b

Please sign in to comment.