github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/handler/unpackaged/unpackaged_test.go (about) 1 package unpackaged_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/devseccon/trivy/pkg/fanal/analyzer" 11 "github.com/devseccon/trivy/pkg/fanal/artifact" 12 "github.com/devseccon/trivy/pkg/fanal/handler/unpackaged" 13 "github.com/devseccon/trivy/pkg/fanal/types" 14 "github.com/devseccon/trivy/pkg/log" 15 "github.com/devseccon/trivy/pkg/rekortest" 16 ) 17 18 func Test_unpackagedHook_Handle(t *testing.T) { 19 type args struct { 20 res *analyzer.AnalysisResult 21 blob *types.BlobInfo 22 } 23 tests := []struct { 24 name string 25 args args 26 want *types.BlobInfo 27 wantErr string 28 }{ 29 { 30 name: "happy path", 31 args: args{ 32 res: &analyzer.AnalysisResult{ 33 Digests: map[string]string{ 34 "go.mod": "sha256:23f4e10c43c7654e33a3c9570913c8c9c528292762f1a5c4a97253e9e4e4b238", 35 }, 36 }, 37 }, 38 want: &types.BlobInfo{ 39 Applications: []types.Application{ 40 { 41 Type: types.GoModule, 42 FilePath: "go.mod", 43 Libraries: types.Packages{ 44 { 45 Name: "github.com/spf13/cobra", 46 Version: "1.5.0", 47 Ref: "pkg:golang/github.com/spf13/cobra@1.5.0", 48 }, 49 }, 50 }, 51 }, 52 }, 53 }, 54 { 55 name: "404", 56 args: args{ 57 res: &analyzer.AnalysisResult{ 58 Digests: map[string]string{ 59 "go.mod": "sha256:unknown", 60 }, 61 }, 62 }, 63 wantErr: "failed to search", 64 }, 65 } 66 67 require.NoError(t, log.InitLogger(false, true)) 68 for _, tt := range tests { 69 t.Run(tt.name, func(t *testing.T) { 70 ts := rekortest.NewServer(t) 71 defer ts.Close() 72 73 // Set the testing URL 74 opt := artifact.Option{ 75 RekorURL: ts.URL(), 76 } 77 78 got := &types.BlobInfo{} 79 h, err := unpackaged.NewUnpackagedHandler(opt) 80 require.NoError(t, err) 81 82 err = h.Handle(context.Background(), tt.args.res, got) 83 if tt.wantErr != "" { 84 assert.ErrorContains(t, err, tt.wantErr) 85 return 86 } 87 require.NoError(t, err, tt.name) 88 assert.Equal(t, tt.want, got) 89 }) 90 } 91 }