mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-22 23:26:39 -08:00
159 lines
3.1 KiB
Go
159 lines
3.1 KiB
Go
package dockerfile
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
|
|
"github.com/aquasecurity/trivy/pkg/fanal/types"
|
|
)
|
|
|
|
func Test_dockerConfigAnalyzer_Analyze(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
inputFile string
|
|
want *analyzer.AnalysisResult
|
|
wantErr string
|
|
}{
|
|
{
|
|
name: "happy path",
|
|
inputFile: "testdata/Dockerfile.deployment",
|
|
want: &analyzer.AnalysisResult{
|
|
Files: map[types.HandlerType][]types.File{
|
|
types.MisconfPostHandler: {
|
|
{
|
|
Type: types.Dockerfile,
|
|
Path: "testdata/Dockerfile.deployment",
|
|
Content: []byte(`FROM foo
|
|
COPY . /
|
|
RUN echo hello
|
|
`),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "happy path with multi-stage",
|
|
inputFile: "testdata/Dockerfile.multistage",
|
|
want: &analyzer.AnalysisResult{
|
|
Files: map[types.HandlerType][]types.File{
|
|
types.MisconfPostHandler: {
|
|
{
|
|
Type: types.Dockerfile,
|
|
Path: "testdata/Dockerfile.multistage",
|
|
Content: []byte(`FROM foo AS build
|
|
COPY . /
|
|
RUN echo hello
|
|
|
|
FROM scratch
|
|
COPY --from=build /bar /bar
|
|
`),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
f, err := os.Open(tt.inputFile)
|
|
require.NoError(t, err)
|
|
defer f.Close()
|
|
|
|
a := dockerConfigAnalyzer{}
|
|
ctx := context.Background()
|
|
got, err := a.Analyze(ctx, analyzer.AnalysisInput{
|
|
FilePath: tt.inputFile,
|
|
Content: f,
|
|
})
|
|
|
|
if tt.wantErr != "" {
|
|
require.NotNil(t, err)
|
|
assert.Contains(t, err.Error(), tt.wantErr)
|
|
return
|
|
}
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_dockerConfigAnalyzer_Required(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
filePath string
|
|
want bool
|
|
}{
|
|
{
|
|
name: "dockerfile",
|
|
filePath: "dockerfile",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "Dockerfile",
|
|
filePath: "Dockerfile",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "Dockerfile with ext",
|
|
filePath: "Dockerfile.build",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "dockerfile as ext",
|
|
filePath: "build.dockerfile",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "Dockerfile in dir",
|
|
filePath: "docker/Dockerfile",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "Dockerfile as prefix",
|
|
filePath: "Dockerfilebuild",
|
|
want: false,
|
|
},
|
|
{
|
|
name: "Dockerfile as suffix",
|
|
filePath: "buildDockerfile",
|
|
want: false,
|
|
},
|
|
{
|
|
name: "Dockerfile as prefix with ext",
|
|
filePath: "Dockerfilebuild.sh",
|
|
want: false,
|
|
},
|
|
{
|
|
name: "Dockerfile as suffix with ext",
|
|
filePath: "buildDockerfile.sh",
|
|
want: false,
|
|
},
|
|
{
|
|
name: "json",
|
|
filePath: "deployment.json",
|
|
want: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s := dockerConfigAnalyzer{}
|
|
got := s.Required(tt.filePath, nil)
|
|
assert.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_dockerConfigAnalyzer_Type(t *testing.T) {
|
|
s := dockerConfigAnalyzer{}
|
|
want := analyzer.TypeDockerfile
|
|
got := s.Type()
|
|
assert.Equal(t, want, got)
|
|
}
|