forked from quay/claircore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofflineimport_test.go
More file actions
120 lines (106 loc) · 2.87 KB
/
Copy pathofflineimport_test.go
File metadata and controls
120 lines (106 loc) · 2.87 KB
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
110
111
112
113
114
115
116
117
118
119
120
package libvuln
import (
"context"
"flag"
"fmt"
"io"
"log/slog"
"os"
"testing"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/klauspost/compress/zstd"
"github.com/quay/claircore/datastore/postgres"
"github.com/quay/claircore/libvuln/driver"
"github.com/quay/claircore/libvuln/jsonblob"
"github.com/quay/claircore/test"
"github.com/quay/claircore/test/integration"
pgtest "github.com/quay/claircore/test/postgres"
)
var importFile *string
func init() {
flag.Func(`load-file`, "run the integration test reading from `FILE` (must be zstd compressed)", func(v string) error {
importFile = &v
return nil
})
}
func TestMain(m *testing.M) {
var c int
defer func() { os.Exit(c) }()
defer integration.DBSetup()()
c = m.Run()
}
// TestLiveOfflineImport is meant to be used for profiling and testing the
// system on a "real" export as produced by `clairctl`.
func TestOfflineImport(t *testing.T) {
if importFile == nil {
t.Skip(`needed flag "-load-file" not provided`)
}
integration.NeedDB(t)
t.Run("Old", testOneOfflineImport(oldOfflineImport))
t.Run("New", testOneOfflineImport(OfflineImport))
}
func testOneOfflineImport(inner func(context.Context, *pgxpool.Pool, io.Reader) error) func(*testing.T) {
return func(t *testing.T) {
ctx := test.Logging(t)
f, err := os.Open(*importFile)
if err != nil {
t.Fatal(err)
}
defer f.Close()
zr, err := zstd.NewReader(f)
if err != nil {
t.Fatal(err)
}
defer zr.Close()
pool := pgtest.TestMatcherDB(ctx, t)
if err := inner(ctx, pool, zr); err != nil {
t.Error(err)
}
}
}
// OldOfflineImport is a copy of the previous implementation of [OfflineImport],
// kept here for the above test.
func oldOfflineImport(ctx context.Context, pool *pgxpool.Pool, in io.Reader) error {
s := postgres.NewMatcherStore(pool)
l, err := jsonblob.NewLoader(ctx, in)
if err != nil {
return err
}
ops, err := s.GetUpdateOperations(ctx, driver.VulnerabilityKind)
if err != nil {
return err
}
Update:
for l.Next() {
e := l.Entry()
log := slog.With("updater", e.Updater)
for _, op := range ops[e.Updater] {
// This only helps if updaters don't keep something that
// changes in the fingerprint.
if op.Fingerprint == e.Fingerprint {
log.InfoContext(ctx, "fingerprint match, skipping")
continue Update
}
}
var ref uuid.UUID
if e.Enrichment != nil {
if ref, err = s.UpdateEnrichments(ctx, e.Updater, e.Fingerprint, e.Enrichment); err != nil {
return fmt.Errorf("updating enrichements: %w", err)
}
}
if e.Vuln != nil {
if ref, err = s.UpdateVulnerabilities(ctx, e.Updater, e.Fingerprint, e.Vuln); err != nil {
return fmt.Errorf("updating vulnerabilities: %w", err)
}
}
log.InfoContext(ctx, "update imported",
"ref", ref,
"vuln_count", len(e.Vuln),
"enrichment_count", len(e.Enrichment))
}
if err := l.Err(); err != nil {
return err
}
return nil
}