This repository was archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathhandler_test.go
109 lines (93 loc) · 2.2 KB
/
handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package handler_test
import (
"context"
"testing"
"github.com/aquasecurity/fanal/analyzer"
"github.com/aquasecurity/fanal/artifact"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/aquasecurity/fanal/handler"
"github.com/aquasecurity/fanal/types"
)
type fakeHook struct{}
func (h fakeHook) Handle(ctx context.Context, result *analyzer.AnalysisResult, info *types.BlobInfo) error {
info.DiffID = "fake"
return nil
}
func (h fakeHook) Priority() int {
return 1
}
func (h fakeHook) Version() int { return 1 }
func (h fakeHook) Type() types.HandlerType { return "fake" }
func fakehookInit(_ artifact.Option) (handler.PostHandler, error) {
return fakeHook{}, nil
}
func TestManager_Versions(t *testing.T) {
tests := []struct {
name string
disable []types.HandlerType
want map[string]int
}{
{
name: "happy path",
want: map[string]int{
"fake": 1,
},
},
{
name: "disable hooks",
disable: []types.HandlerType{"fake"},
want: map[string]int{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler.RegisterPostHandlerInit("fake", fakehookInit)
defer handler.DeregisterPostHandler("fake")
m, err := handler.NewManager(artifact.Option{
DisabledHandlers: tt.disable,
})
require.NoError(t, err)
got := m.Versions()
assert.Equal(t, tt.want, got)
})
}
}
func TestManager_CallHooks(t *testing.T) {
tests := []struct {
name string
disable []types.HandlerType
want types.BlobInfo
}{
{
name: "happy path",
want: types.BlobInfo{
Digest: "digest",
DiffID: "fake",
},
},
{
name: "disable hooks",
disable: []types.HandlerType{"fake"},
want: types.BlobInfo{
Digest: "digest",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler.RegisterPostHandlerInit("fake", fakehookInit)
defer handler.DeregisterPostHandler("fake")
blob := types.BlobInfo{
Digest: "digest",
}
m, err := handler.NewManager(artifact.Option{
DisabledHandlers: tt.disable,
})
require.NoError(t, err)
err = m.PostHandle(context.TODO(), nil, &blob)
require.NoError(t, err)
assert.Equal(t, tt.want, blob)
})
}
}