Skip to content

Commit

Permalink
claircore: bump to v0.2.0
Browse files Browse the repository at this point in the history
This commit patches ClairCore to v0.2.0.

The AffectedManifests API is updated to pass back a pointer
AffectedManifest struct.

The DeleteUpdateOperation API is updated to return the number of deleted
UpdateOperations.

All occurences of TestLog are replaced with zlog.Test

Various mocks scattered around tests have been consolidated to their
appropriate service packages and removed.

Signed-off-by: ldelossa <ldelossa@redhat.com>
  • Loading branch information
ldelossa authored and ldelossa committed Feb 1, 2021
1 parent b78f954 commit f183421
Show file tree
Hide file tree
Showing 25 changed files with 176 additions and 245 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ require (
github.com/klauspost/compress v1.10.11
github.com/mattn/go-sqlite3 v1.11.0 // indirect
github.com/prometheus/procfs v0.3.0 // indirect
github.com/quay/claircore v0.1.20
github.com/quay/claircore v0.2.0
github.com/quay/zlog v0.0.0-20210113185248-ce16eed1dcec
github.com/remind101/migrate v0.0.0-20170729031349-52c1edff7319
github.com/rs/zerolog v1.16.0
github.com/rs/zerolog v1.20.0
github.com/streadway/amqp v1.0.0
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80
github.com/urfave/cli/v2 v2.2.0
Expand Down
12 changes: 7 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,12 @@ github.com/prometheus/procfs v0.3.0 h1:Uehi/mxLK0eiUc0H0++5tpMGTexB8wZ598MIgU8Vp
github.com/prometheus/procfs v0.3.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/quay/alas v1.0.1 h1:MuFpGGXyZlDD7+F/hrnMZmzhS8P2bjRzX9DyGmyLA+0=
github.com/quay/alas v1.0.1/go.mod h1:pseepSrG9pwry1joG7RO/RNRFJaWqiqx9qeoomeYwEk=
github.com/quay/claircore v0.1.20 h1:vAghvmmS6dqph7/dYXFYQdC68jo0J9elcMOilQ6ag4M=
github.com/quay/claircore v0.1.20/go.mod h1:Q40RR0jWuYSg5qLglCX7WT5Q9yzmvnr0JkgBNRxPmoo=
github.com/quay/claircore v0.2.0 h1:UwxaWG7Jd0cSpsGfsmtqTkBXdVmbvTIXSpHtmjqlBOQ=
github.com/quay/claircore v0.2.0/go.mod h1:gDvglP5JVrQcH0ErSjcXtcqzub1aIkqUgeOfm8uoPRg=
github.com/quay/goval-parser v0.8.6 h1:h1Xg3SZR/6I7UVa1LcsQZvQft/q7sJbosmFrjzSmdqE=
github.com/quay/goval-parser v0.8.6/go.mod h1:Y0NTNfPYOC7yxsYKzJOrscTWUPq1+QbtHw4XpPXWPMc=
github.com/quay/zlog v0.0.0-20210113185248-ce16eed1dcec h1:v6gbUFTnms8pwArSDyE4rVK1ySLbxy9EQrbQqdOhAyY=
github.com/quay/zlog v0.0.0-20210113185248-ce16eed1dcec/go.mod h1:g+MQjOhV/hDsUIIRvp6JxM3RqCY3vcDgRO/xznpyv1o=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remind101/migrate v0.0.0-20170729031349-52c1edff7319 h1:ukjThsA2ou7AmovpwtMVkNQSuoN/v5U16+JomTz3c7o=
github.com/remind101/migrate v0.0.0-20170729031349-52c1edff7319/go.mod h1:rhSvwcijY9wfmrBYrfCvapX8/xOTV46NAUjBRgUyJqc=
Expand All @@ -634,8 +636,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/rs/zerolog v1.16.0 h1:AaELmZdcJHT8m6oZ5py4213cdFK8XGXkB3dFdAQ+P7Q=
github.com/rs/zerolog v1.16.0/go.mod h1:9nvC1axdVrAHcu/s9taAVfBuIdTZLVQmKQyvrUjF5+I=
github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs=
github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
Expand Down Expand Up @@ -734,6 +736,7 @@ go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.16.0/go.mod h1:P5mQmVnXk429V+RQnY79cRDxP7KeFqEirKBXwQkvfPA=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.16.0 h1:hPbUH5fugPACtUdBWGL5glNqzowwHvnOdnwvQOATWgM=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.16.0/go.mod h1:dNF4PMGeouMEPAWDwgEjsGFlod9hAU8oj0TU0w2J19g=
go.opentelemetry.io/otel v0.15.0/go.mod h1:e4GKElweB8W2gWUqbghw0B8t5MCTccc9212eNHnOHwA=
go.opentelemetry.io/otel v0.16.0 h1:uIWEbdeb4vpKPGITLsRVUS44L5oDbDUCZxn8lkxhmgw=
go.opentelemetry.io/otel v0.16.0/go.mod h1:e4GKElweB8W2gWUqbghw0B8t5MCTccc9212eNHnOHwA=
go.opentelemetry.io/otel/exporters/metric/prometheus v0.16.0 h1:0tB24Dht89vSfsD9JXO5HeEfan/Tb6eQk4U6s9a23qE=
Expand Down Expand Up @@ -1139,7 +1142,6 @@ gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20191010095647-fc94e3f71652/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200506231410-2ff61e1afc86 h1:OfFoIUYv/me30yv7XlMy4F9RJw8DEm8WQ6QG1Ph4bH0=
gopkg.in/yaml.v3 v3.0.0-20200506231410-2ff61e1afc86/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
29 changes: 9 additions & 20 deletions httptransport/affectedmanifesthandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ import (
"github.com/quay/claircore"
)

var _ indexer.Affected = (*affected)(nil)

// affected implements indexer.Affected by calling the func members
type affected struct {
affectedManifests func(context.Context, []claircore.Vulnerability) (claircore.AffectedManifests, error)
}

func (a *affected) AffectedManifests(ctx context.Context, v []claircore.Vulnerability) (claircore.AffectedManifests, error) {
return a.affectedManifests(ctx, v)
}

// TestAffectedManifestHandler is a parallel harness for testing an AffectedManifest handler.
func TestAffectedManifestHandler(t *testing.T) {
t.Run("IndexerOK", testAffectedManifestHandlerIndexerOK)
Expand All @@ -33,9 +22,9 @@ func TestAffectedManifestHandler(t *testing.T) {

func testAffectedManifestHandlerIndexerOK(t *testing.T) {
t.Parallel()
h := AffectedManifestHandler(&affected{
affectedManifests: func(context.Context, []claircore.Vulnerability) (claircore.AffectedManifests, error) {
return claircore.NewAffectedManifests(), nil
h := AffectedManifestHandler(&indexer.Mock{
AffectedManifests_: func(context.Context, []claircore.Vulnerability) (*claircore.AffectedManifests, error) {
return &claircore.AffectedManifests{}, nil
},
})

Expand Down Expand Up @@ -68,9 +57,9 @@ func testAffectedManifestHandlerIndexerOK(t *testing.T) {

func testAffectedManifestHandlerIndexerErr(t *testing.T) {
t.Parallel()
h := AffectedManifestHandler(&affected{
affectedManifests: func(context.Context, []claircore.Vulnerability) (claircore.AffectedManifests, error) {
return claircore.NewAffectedManifests(), fmt.Errorf("error")
h := AffectedManifestHandler(&indexer.Mock{
AffectedManifests_: func(context.Context, []claircore.Vulnerability) (*claircore.AffectedManifests, error) {
return nil, fmt.Errorf("failed")
},
})

Expand Down Expand Up @@ -103,9 +92,9 @@ func testAffectedManifestHandlerIndexerErr(t *testing.T) {

func testAffectedManifestHandlerMethods(t *testing.T) {
t.Parallel()
h := AffectedManifestHandler(&affected{
affectedManifests: func(context.Context, []claircore.Vulnerability) (claircore.AffectedManifests, error) {
return claircore.NewAffectedManifests(), nil
h := AffectedManifestHandler(&indexer.Mock{
AffectedManifests_: func(context.Context, []claircore.Vulnerability) (*claircore.AffectedManifests, error) {
return &claircore.AffectedManifests{}, nil
},
})
srv := httptest.NewServer(h)
Expand Down
14 changes: 7 additions & 7 deletions httptransport/client/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

var _ indexer.Service = (*HTTP)(nil)

func (s *HTTP) AffectedManifests(ctx context.Context, v []claircore.Vulnerability) (claircore.AffectedManifests, error) {
func (s *HTTP) AffectedManifests(ctx context.Context, v []claircore.Vulnerability) (*claircore.AffectedManifests, error) {
var affected claircore.AffectedManifests
buf := bytes.NewBuffer([]byte{})
err := json.NewEncoder(buf).Encode(struct {
Expand All @@ -26,29 +26,29 @@ func (s *HTTP) AffectedManifests(ctx context.Context, v []claircore.Vulnerabilit
v,
})
if err != nil {
return affected, &clairerror.ErrBadVulnerabilities{err}
return nil, &clairerror.ErrBadVulnerabilities{err}
}

u, err := s.addr.Parse(httptransport.AffectedManifestAPIPath)
if err != nil {
return affected, fmt.Errorf("failed to parse api address: %v", err)
return nil, fmt.Errorf("failed to parse api address: %v", err)
}
req, err := http.NewRequestWithContext(ctx, "POST", u.String(), buf)
if err != nil {
return affected, fmt.Errorf("failed to create request: %v", err)
return nil, fmt.Errorf("failed to create request: %v", err)
}
resp, err := s.c.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return affected, &clairerror.ErrRequestFail{Code: resp.StatusCode, Status: resp.Status}
return nil, &clairerror.ErrRequestFail{Code: resp.StatusCode, Status: resp.Status}
}
err = json.NewDecoder(resp.Body).Decode(&affected)
if err != nil {
return affected, &clairerror.ErrBadAffectedManifests{err}
return nil, &clairerror.ErrBadAffectedManifests{err}
}
return affected, nil
return &affected, nil
}

// Index receives a Manifest and returns a IndexReport providing the indexed
Expand Down
10 changes: 6 additions & 4 deletions httptransport/client/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func (c *HTTP) Scan(ctx context.Context, ir *claircore.IndexReport) (*claircore.
}

// DeleteUpdateOperations attempts to delete the referenced update operations.
func (c *HTTP) DeleteUpdateOperations(ctx context.Context, ref ...uuid.UUID) error {
func (c *HTTP) DeleteUpdateOperations(ctx context.Context, ref ...uuid.UUID) (int64, error) {
u, err := c.addr.Parse(httptransport.UpdateOperationAPIPath)
if err != nil {
return err
return 0, err
}

// Spawn a few requests that will write their result into "errs".
Expand Down Expand Up @@ -110,8 +110,10 @@ func (c *HTTP) DeleteUpdateOperations(ctx context.Context, ref ...uuid.UUID) err

var b strings.Builder
var errd bool
var deleted = int64(len(ref))
for _, err := range errs {
if err != nil {
deleted--
if errd {
b.WriteByte('\n')
}
Expand All @@ -121,9 +123,9 @@ func (c *HTTP) DeleteUpdateOperations(ctx context.Context, ref ...uuid.UUID) err
}

if errd {
return errors.New("deletion errors: " + b.String())
return deleted, errors.New("deletion errors: " + b.String())
}
return nil
return deleted, nil
}

// LatestUpdateOperation shouldn't be used by client code and is implemented
Expand Down
6 changes: 5 additions & 1 deletion httptransport/client/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ func TestDiffer(t *testing.T) {
}

// Do the call.
if err := c.DeleteUpdateOperations(ctx, refs...); err != nil {
deleted, err := c.DeleteUpdateOperations(ctx, refs...)
if err != nil {
t.Error(err)
}
if deleted != int64(len(refs)) {
t.Errorf("got: %v, want: %v", deleted, len(refs))
}
})

t.Run("Latest", func(t *testing.T) {
Expand Down
81 changes: 0 additions & 81 deletions httptransport/mock.go

This file was deleted.

57 changes: 20 additions & 37 deletions httptransport/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,45 @@ import (
"testing"

"github.com/google/uuid"
"github.com/quay/clair/v4/indexer"
"github.com/quay/clair/v4/matcher"
"github.com/quay/claircore"
"github.com/quay/claircore/libvuln/driver"
"github.com/quay/claircore/test/log"
"github.com/quay/zlog"
othttp "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
)

type testMatcher struct {
scanner
differ
}

// NewTestMatcher returns a testMatcher with all functions set to non-nil stubs.
func newTestMatcher() *testMatcher {
return &testMatcher{
scanner: scanner{
scan: func(context.Context, *claircore.IndexReport) (*claircore.VulnerabilityReport, error) { return nil, nil },
},
differ: differ{
delete: func(context.Context, ...uuid.UUID) error { return nil },
ops: func(context.Context, ...string) (map[string][]driver.UpdateOperation, error) { return nil, nil },
latestOp: func(context.Context) (uuid.UUID, error) { return uuid.Nil, nil },
latestOps: func(context.Context) (map[string][]driver.UpdateOperation, error) { return nil, nil },
updateDiff: func(context.Context, uuid.UUID, uuid.UUID) (*driver.UpdateDiff, error) { return nil, nil },
},
// TestUpdateEndpoints registers the handlers and tests that they're registered
// at the correct endpoint.
func TestUpdateEndpoints(t *testing.T) {
m := &matcher.Mock{
DeleteUpdateOperations_: func(context.Context, ...uuid.UUID) (int64, error) { return 0, nil },
UpdateOperations_: func(context.Context, ...string) (map[string][]driver.UpdateOperation, error) { return nil, nil },
LatestUpdateOperation_: func(context.Context) (uuid.UUID, error) { return uuid.Nil, nil },
LatestUpdateOperations_: func(context.Context) (map[string][]driver.UpdateOperation, error) { return nil, nil },
UpdateDiff_: func(context.Context, uuid.UUID, uuid.UUID) (*driver.UpdateDiff, error) { return nil, nil },
Scan_: func(context.Context, *claircore.IndexReport) (*claircore.VulnerabilityReport, error) { return nil, nil },
}
}

func newTestIndexer() *indexerMock {
return &indexerMock{
index: func(ctx context.Context, manifest *claircore.Manifest) (*claircore.IndexReport, error) {
i := &indexer.Mock{
Index_: func(ctx context.Context, manifest *claircore.Manifest) (*claircore.IndexReport, error) {
return nil, nil
},
report: func(ctx context.Context, digest claircore.Digest) (*claircore.IndexReport, bool, error) {
IndexReport_: func(ctx context.Context, digest claircore.Digest) (*claircore.IndexReport, bool, error) {
return nil, true, nil
},
state: func(ctx context.Context) (string, error) { return "", nil },
affected: func(ctx context.Context, vulns []claircore.Vulnerability) (claircore.AffectedManifests, error) {
return claircore.NewAffectedManifests(), nil
State_: func(ctx context.Context) (string, error) { return "", nil },
AffectedManifests_: func(ctx context.Context, vulns []claircore.Vulnerability) (*claircore.AffectedManifests, error) {
return nil, nil
},
}
}

// TestUpdateEndpoints registers the handlers and tests that they're registered
// at the correct endpoint.
func TestUpdateEndpoints(t *testing.T) {
m := newTestMatcher()
i := newTestIndexer()
s := &Server{
matcher: m,
indexer: i,
ServeMux: http.NewServeMux(),
traceOpt: othttp.WithTracerProvider(otel.GetTracerProvider()),
}
ctx, done := log.TestLogger(context.Background(), t)
defer done()
ctx := zlog.Test(context.Background(), t)
if err := s.configureMatcherMode(ctx); err != nil {
t.Error(err)
}
Expand Down
Loading

0 comments on commit f183421

Please sign in to comment.