forked from quay/claircore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher_integration_test.go
94 lines (81 loc) · 2.17 KB
/
matcher_integration_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
package ruby
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/quay/zlog"
"github.com/quay/claircore"
"github.com/quay/claircore/datastore/postgres"
internalMatcher "github.com/quay/claircore/internal/matcher"
"github.com/quay/claircore/libvuln/driver"
"github.com/quay/claircore/libvuln/updates"
"github.com/quay/claircore/pkg/ctxlock"
"github.com/quay/claircore/test/integration"
pgtest "github.com/quay/claircore/test/postgres"
"github.com/quay/claircore/updater/osv"
)
func TestMain(m *testing.M) {
var c int
defer func() { os.Exit(c) }()
defer integration.DBSetup()()
c = m.Run()
}
func TestMatcherIntegration(t *testing.T) {
integration.NeedDB(t)
ctx := zlog.Test(context.Background(), t)
pool := pgtest.TestMatcherDB(ctx, t)
store := postgres.NewMatcherStore(pool)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTeapot)
}))
defer srv.Close()
m := &Matcher{}
locks, err := ctxlock.New(ctx, pool)
if err != nil {
t.Fatalf("%v", err)
}
defer locks.Close(ctx)
cfg := map[string]driver.ConfigUnmarshaler{
"osv": func(v interface{}) error {
cfg := v.(*osv.FactoryConfig)
cfg.URL = osv.DefaultURL
return nil
},
}
facs := map[string]driver.UpdaterSetFactory{
"osv": new(osv.Factory),
}
mgr, err := updates.NewManager(ctx, store, locks, srv.Client(),
updates.WithFactories(facs), updates.WithConfigs(cfg))
if err != nil {
t.Fatalf("%v", err)
}
// force update
if err := mgr.Run(ctx); err != nil {
t.Fatalf("%v", err)
}
path := filepath.Join("testdata", "indexreport-bullseye-ruby.json")
f, err := os.Open(path)
if err != nil {
t.Fatalf("%v", err)
}
defer f.Close()
var ir claircore.IndexReport
err = json.NewDecoder(f).Decode(&ir)
if err != nil {
t.Fatalf("failed to decode IndexReport: %v", err)
}
vr, err := internalMatcher.Match(ctx, &ir, []driver.Matcher{m}, store)
if err != nil {
t.Fatalf("expected error to be nil but got %v", err)
}
vulns := vr.Vulnerabilities
t.Logf("Number of Vulnerabilities found: %d", len(vulns))
if len(vulns) < 1 {
t.Fatalf("failed to match vulns: %v", err)
}
}