database: move dbutil and testutil to database from pkg
Move dbutil and testutil to database from pkg Rename all "result"
This commit is contained in:
parent
0c1b80b2ed
commit
e657d26313
@ -12,47 +12,45 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package dbutil
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/deckarep/golang-set"
|
"github.com/deckarep/golang-set"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeduplicateNamespaces deduplicates a list of namespaces.
|
// DeduplicateNamespaces deduplicates a list of namespaces.
|
||||||
func DeduplicateNamespaces(namespaces ...database.Namespace) []database.Namespace {
|
func DeduplicateNamespaces(namespaces ...Namespace) []Namespace {
|
||||||
nsSet := mapset.NewSet()
|
nsSet := mapset.NewSet()
|
||||||
for _, ns := range namespaces {
|
for _, ns := range namespaces {
|
||||||
nsSet.Add(ns)
|
nsSet.Add(ns)
|
||||||
}
|
}
|
||||||
|
|
||||||
result := make([]database.Namespace, 0, nsSet.Cardinality())
|
uniqueNamespaces := make([]Namespace, 0, nsSet.Cardinality())
|
||||||
for ns := range nsSet.Iter() {
|
for ns := range nsSet.Iter() {
|
||||||
result = append(result, ns.(database.Namespace))
|
uniqueNamespaces = append(uniqueNamespaces, ns.(Namespace))
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return uniqueNamespaces
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeduplicateFeatures deduplicates a list of list of features.
|
// DeduplicateFeatures deduplicates a list of list of features.
|
||||||
func DeduplicateFeatures(features ...database.Feature) []database.Feature {
|
func DeduplicateFeatures(features ...Feature) []Feature {
|
||||||
fSet := mapset.NewSet()
|
fSet := mapset.NewSet()
|
||||||
for _, f := range features {
|
for _, f := range features {
|
||||||
fSet.Add(f)
|
fSet.Add(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
result := make([]database.Feature, 0, fSet.Cardinality())
|
uniqueFeatures := make([]Feature, 0, fSet.Cardinality())
|
||||||
for f := range fSet.Iter() {
|
for f := range fSet.Iter() {
|
||||||
result = append(result, f.(database.Feature))
|
uniqueFeatures = append(uniqueFeatures, f.(Feature))
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return uniqueFeatures
|
||||||
}
|
}
|
||||||
|
|
||||||
// PersistPartialLayer wraps session PersistLayer function with begin and
|
// PersistPartialLayer wraps session PersistLayer function with begin and
|
||||||
// commit.
|
// commit.
|
||||||
func PersistPartialLayer(datastore database.Datastore, layer *database.Layer) error {
|
func PersistPartialLayer(datastore Datastore, layer *Layer) error {
|
||||||
tx, err := datastore.Begin()
|
tx, err := datastore.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -67,7 +65,7 @@ func PersistPartialLayer(datastore database.Datastore, layer *database.Layer) er
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PersistFeatures wraps session PersistFeatures function with begin and commit.
|
// PersistFeatures wraps session PersistFeatures function with begin and commit.
|
||||||
func PersistFeatures(datastore database.Datastore, features []database.Feature) error {
|
func PersistFeatures(datastore Datastore, features []Feature) error {
|
||||||
tx, err := datastore.Begin()
|
tx, err := datastore.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -82,7 +80,7 @@ func PersistFeatures(datastore database.Datastore, features []database.Feature)
|
|||||||
|
|
||||||
// PersistNamespaces wraps session PersistNamespaces function with begin and
|
// PersistNamespaces wraps session PersistNamespaces function with begin and
|
||||||
// commit.
|
// commit.
|
||||||
func PersistNamespaces(datastore database.Datastore, namespaces []database.Namespace) error {
|
func PersistNamespaces(datastore Datastore, namespaces []Namespace) error {
|
||||||
tx, err := datastore.Begin()
|
tx, err := datastore.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -97,20 +95,20 @@ func PersistNamespaces(datastore database.Datastore, namespaces []database.Names
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindAncestry wraps session FindAncestry function with begin and rollback.
|
// FindAncestry wraps session FindAncestry function with begin and rollback.
|
||||||
func FindAncestry(datastore database.Datastore, name string) (database.Ancestry, bool, error) {
|
func FindAncestry(datastore Datastore, name string) (Ancestry, bool, error) {
|
||||||
tx, err := datastore.Begin()
|
tx, err := datastore.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return database.Ancestry{}, false, err
|
return Ancestry{}, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return tx.FindAncestry(name)
|
return tx.FindAncestry(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindLayer wraps session FindLayer function with begin and rollback.
|
// FindLayer wraps session FindLayer function with begin and rollback.
|
||||||
func FindLayer(datastore database.Datastore, hash string) (layer database.Layer, ok bool, err error) {
|
func FindLayer(datastore Datastore, hash string) (layer Layer, ok bool, err error) {
|
||||||
var tx database.Session
|
var tx Session
|
||||||
if tx, err = datastore.Begin(); err != nil {
|
if tx, err = datastore.Begin(); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -122,24 +120,24 @@ func FindLayer(datastore database.Datastore, hash string) (layer database.Layer,
|
|||||||
|
|
||||||
// DeduplicateNamespacedFeatures returns a copy of all unique features in the
|
// DeduplicateNamespacedFeatures returns a copy of all unique features in the
|
||||||
// input.
|
// input.
|
||||||
func DeduplicateNamespacedFeatures(features []database.NamespacedFeature) []database.NamespacedFeature {
|
func DeduplicateNamespacedFeatures(features []NamespacedFeature) []NamespacedFeature {
|
||||||
nsSet := mapset.NewSet()
|
nsSet := mapset.NewSet()
|
||||||
for _, ns := range features {
|
for _, ns := range features {
|
||||||
nsSet.Add(ns)
|
nsSet.Add(ns)
|
||||||
}
|
}
|
||||||
|
|
||||||
result := make([]database.NamespacedFeature, 0, nsSet.Cardinality())
|
uniqueFeatures := make([]NamespacedFeature, 0, nsSet.Cardinality())
|
||||||
for ns := range nsSet.Iter() {
|
for ns := range nsSet.Iter() {
|
||||||
result = append(result, ns.(database.NamespacedFeature))
|
uniqueFeatures = append(uniqueFeatures, ns.(NamespacedFeature))
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return uniqueFeatures
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAncestryFeatures returns a list of unique namespaced features in the
|
// GetAncestryFeatures returns a list of unique namespaced features in the
|
||||||
// ancestry.
|
// ancestry.
|
||||||
func GetAncestryFeatures(ancestry database.Ancestry) []database.NamespacedFeature {
|
func GetAncestryFeatures(ancestry Ancestry) []NamespacedFeature {
|
||||||
features := []database.NamespacedFeature{}
|
features := []NamespacedFeature{}
|
||||||
for _, layer := range ancestry.Layers {
|
for _, layer := range ancestry.Layers {
|
||||||
features = append(features, layer.GetFeatures()...)
|
features = append(features, layer.GetFeatures()...)
|
||||||
}
|
}
|
||||||
@ -148,7 +146,7 @@ func GetAncestryFeatures(ancestry database.Ancestry) []database.NamespacedFeatur
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpsertAncestry wraps session UpsertAncestry function with begin and commit.
|
// UpsertAncestry wraps session UpsertAncestry function with begin and commit.
|
||||||
func UpsertAncestry(datastore database.Datastore, ancestry database.Ancestry) error {
|
func UpsertAncestry(datastore Datastore, ancestry Ancestry) error {
|
||||||
tx, err := datastore.Begin()
|
tx, err := datastore.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -168,7 +166,7 @@ func UpsertAncestry(datastore database.Datastore, ancestry database.Ancestry) er
|
|||||||
|
|
||||||
// PersistNamespacedFeatures wraps session PersistNamespacedFeatures function
|
// PersistNamespacedFeatures wraps session PersistNamespacedFeatures function
|
||||||
// with begin and commit.
|
// with begin and commit.
|
||||||
func PersistNamespacedFeatures(datastore database.Datastore, features []database.NamespacedFeature) error {
|
func PersistNamespacedFeatures(datastore Datastore, features []NamespacedFeature) error {
|
||||||
tx, err := datastore.Begin()
|
tx, err := datastore.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -188,7 +186,7 @@ func PersistNamespacedFeatures(datastore database.Datastore, features []database
|
|||||||
|
|
||||||
// CacheRelatedVulnerability wraps session CacheAffectedNamespacedFeatures
|
// CacheRelatedVulnerability wraps session CacheAffectedNamespacedFeatures
|
||||||
// function with begin and commit.
|
// function with begin and commit.
|
||||||
func CacheRelatedVulnerability(datastore database.Datastore, features []database.NamespacedFeature) error {
|
func CacheRelatedVulnerability(datastore Datastore, features []NamespacedFeature) error {
|
||||||
tx, err := datastore.Begin()
|
tx, err := datastore.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -203,7 +201,7 @@ func CacheRelatedVulnerability(datastore database.Datastore, features []database
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IntersectDetectors returns the detectors in both d1 and d2.
|
// IntersectDetectors returns the detectors in both d1 and d2.
|
||||||
func IntersectDetectors(d1 []database.Detector, d2 []database.Detector) []database.Detector {
|
func IntersectDetectors(d1 []Detector, d2 []Detector) []Detector {
|
||||||
d1Set := mapset.NewSet()
|
d1Set := mapset.NewSet()
|
||||||
for _, d := range d1 {
|
for _, d := range d1 {
|
||||||
d1Set.Add(d)
|
d1Set.Add(d)
|
||||||
@ -215,16 +213,16 @@ func IntersectDetectors(d1 []database.Detector, d2 []database.Detector) []databa
|
|||||||
}
|
}
|
||||||
|
|
||||||
inter := d1Set.Intersect(d2Set)
|
inter := d1Set.Intersect(d2Set)
|
||||||
result := make([]database.Detector, 0, inter.Cardinality())
|
detectors := make([]Detector, 0, inter.Cardinality())
|
||||||
for d := range inter.Iter() {
|
for d := range inter.Iter() {
|
||||||
result = append(result, d.(database.Detector))
|
detectors = append(detectors, d.(Detector))
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return detectors
|
||||||
}
|
}
|
||||||
|
|
||||||
// DiffDetectors returns the detectors belongs to d1 but not d2
|
// DiffDetectors returns the detectors belongs to d1 but not d2
|
||||||
func DiffDetectors(d1 []database.Detector, d2 []database.Detector) []database.Detector {
|
func DiffDetectors(d1 []Detector, d2 []Detector) []Detector {
|
||||||
d1Set := mapset.NewSet()
|
d1Set := mapset.NewSet()
|
||||||
for _, d := range d1 {
|
for _, d := range d1 {
|
||||||
d1Set.Add(d)
|
d1Set.Add(d)
|
||||||
@ -236,17 +234,17 @@ func DiffDetectors(d1 []database.Detector, d2 []database.Detector) []database.De
|
|||||||
}
|
}
|
||||||
|
|
||||||
diff := d1Set.Difference(d2Set)
|
diff := d1Set.Difference(d2Set)
|
||||||
result := make([]database.Detector, 0, diff.Cardinality())
|
detectors := make([]Detector, 0, diff.Cardinality())
|
||||||
for d := range diff.Iter() {
|
for d := range diff.Iter() {
|
||||||
result = append(result, d.(database.Detector))
|
detectors = append(detectors, d.(Detector))
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return detectors
|
||||||
}
|
}
|
||||||
|
|
||||||
// MergeLayers merges all content in new layer to l, where the content is
|
// MergeLayers merges all content in new layer to l, where the content is
|
||||||
// updated.
|
// updated.
|
||||||
func MergeLayers(l *database.Layer, new *database.Layer) *database.Layer {
|
func MergeLayers(l *Layer, new *Layer) *Layer {
|
||||||
featureSet := mapset.NewSet()
|
featureSet := mapset.NewSet()
|
||||||
namespaceSet := mapset.NewSet()
|
namespaceSet := mapset.NewSet()
|
||||||
bySet := mapset.NewSet()
|
bySet := mapset.NewSet()
|
@ -20,7 +20,6 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
"github.com/coreos/clair/pkg/testutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var upsertAncestryTests = []struct {
|
var upsertAncestryTests = []struct {
|
||||||
@ -89,7 +88,7 @@ func TestUpsertAncestry(t *testing.T) {
|
|||||||
actual, ok, err := tx.FindAncestry(test.in.Name)
|
actual, ok, err := tx.FindAncestry(test.in.Name)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
testutil.AssertAncestryEqual(t, test.in, &actual)
|
database.AssertAncestryEqual(t, test.in, &actual)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -132,7 +131,7 @@ func TestFindAncestry(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, test.ok, ok)
|
assert.Equal(t, test.ok, ok)
|
||||||
if test.ok {
|
if test.ok {
|
||||||
testutil.AssertAncestryEqual(t, test.ancestry, &ancestry)
|
database.AssertAncestryEqual(t, test.ancestry, &ancestry)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
"github.com/coreos/clair/pkg/testutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var persistLayerTests = []struct {
|
var persistLayerTests = []struct {
|
||||||
@ -136,7 +135,7 @@ func TestPersistLayer(t *testing.T) {
|
|||||||
layer, ok, err := tx.FindLayer(test.name)
|
layer, ok, err := tx.FindLayer(test.name)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
testutil.AssertLayerEqual(t, test.layer, &layer)
|
database.AssertLayerEqual(t, test.layer, &layer)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -184,7 +183,7 @@ func TestFindLayer(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, test.ok, ok)
|
assert.Equal(t, test.ok, ok)
|
||||||
if test.ok {
|
if test.ok {
|
||||||
testutil.AssertLayerEqual(t, test.out, &layer)
|
database.AssertLayerEqual(t, test.out, &layer)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,6 @@ import (
|
|||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
"github.com/coreos/clair/pkg/pagination"
|
"github.com/coreos/clair/pkg/pagination"
|
||||||
"github.com/coreos/clair/pkg/testutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// int keys must be the consistent with the database ID.
|
// int keys must be the consistent with the database ID.
|
||||||
@ -232,12 +231,12 @@ func AssertPagedVulnerableAncestriesEqual(t *testing.T, key pagination.Key, expe
|
|||||||
return assert.Equal(t, expected, actual)
|
return assert.Equal(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
return testutil.AssertVulnerabilityEqual(t, &expected.Vulnerability, &actual.Vulnerability) &&
|
return database.AssertVulnerabilityEqual(t, &expected.Vulnerability, &actual.Vulnerability) &&
|
||||||
assert.Equal(t, expected.Limit, actual.Limit) &&
|
assert.Equal(t, expected.Limit, actual.Limit) &&
|
||||||
assert.Equal(t, mustUnmarshalToken(key, expected.Current), mustUnmarshalToken(key, actual.Current)) &&
|
assert.Equal(t, mustUnmarshalToken(key, expected.Current), mustUnmarshalToken(key, actual.Current)) &&
|
||||||
assert.Equal(t, mustUnmarshalToken(key, expected.Next), mustUnmarshalToken(key, actual.Next)) &&
|
assert.Equal(t, mustUnmarshalToken(key, expected.Next), mustUnmarshalToken(key, actual.Next)) &&
|
||||||
assert.Equal(t, expected.End, actual.End) &&
|
assert.Equal(t, expected.End, actual.End) &&
|
||||||
testutil.AssertIntStringMapEqual(t, expected.Affected, actual.Affected)
|
database.AssertIntStringMapEqual(t, expected.Affected, actual.Affected)
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustUnmarshalToken(key pagination.Key, token pagination.Token) Page {
|
func mustUnmarshalToken(key pagination.Key, token pagination.Token) Page {
|
||||||
|
@ -1,4 +1,18 @@
|
|||||||
package testutil
|
// Copyright 2018 clair authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -7,13 +21,11 @@ import (
|
|||||||
|
|
||||||
"github.com/deckarep/golang-set"
|
"github.com/deckarep/golang-set"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// AssertDetectorsEqual asserts actual detectors are content wise equal to
|
// AssertDetectorsEqual asserts actual detectors are content wise equal to
|
||||||
// expected detectors regardless of the ordering.
|
// expected detectors regardless of the ordering.
|
||||||
func AssertDetectorsEqual(t *testing.T, expected, actual []database.Detector) bool {
|
func AssertDetectorsEqual(t *testing.T, expected, actual []Detector) bool {
|
||||||
if len(expected) != len(actual) {
|
if len(expected) != len(actual) {
|
||||||
return assert.Fail(t, "detectors are not equal", "expected: '%v', actual: '%v'", expected, actual)
|
return assert.Fail(t, "detectors are not equal", "expected: '%v', actual: '%v'", expected, actual)
|
||||||
}
|
}
|
||||||
@ -37,7 +49,7 @@ func AssertDetectorsEqual(t *testing.T, expected, actual []database.Detector) bo
|
|||||||
|
|
||||||
// AssertAncestryEqual asserts actual ancestry equals to expected ancestry
|
// AssertAncestryEqual asserts actual ancestry equals to expected ancestry
|
||||||
// content wise.
|
// content wise.
|
||||||
func AssertAncestryEqual(t *testing.T, expected, actual *database.Ancestry) bool {
|
func AssertAncestryEqual(t *testing.T, expected, actual *Ancestry) bool {
|
||||||
if expected == actual {
|
if expected == actual {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -63,7 +75,7 @@ func AssertAncestryEqual(t *testing.T, expected, actual *database.Ancestry) bool
|
|||||||
|
|
||||||
// AssertAncestryLayerEqual asserts actual ancestry layer equals to expected
|
// AssertAncestryLayerEqual asserts actual ancestry layer equals to expected
|
||||||
// ancestry layer content wise.
|
// ancestry layer content wise.
|
||||||
func AssertAncestryLayerEqual(t *testing.T, expected, actual *database.AncestryLayer) bool {
|
func AssertAncestryLayerEqual(t *testing.T, expected, actual *AncestryLayer) bool {
|
||||||
if !assert.Equal(t, expected.Hash, actual.Hash) {
|
if !assert.Equal(t, expected.Hash, actual.Hash) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -76,7 +88,7 @@ func AssertAncestryLayerEqual(t *testing.T, expected, actual *database.AncestryL
|
|||||||
}
|
}
|
||||||
|
|
||||||
// feature -> is in actual layer
|
// feature -> is in actual layer
|
||||||
hitCounter := map[database.AncestryFeature]bool{}
|
hitCounter := map[AncestryFeature]bool{}
|
||||||
for _, f := range expected.Features {
|
for _, f := range expected.Features {
|
||||||
hitCounter[f] = false
|
hitCounter[f] = false
|
||||||
}
|
}
|
||||||
@ -131,9 +143,9 @@ func AssertElementsEqual(t *testing.T, expected, actual []interface{}) bool {
|
|||||||
|
|
||||||
// AssertFeaturesEqual asserts content in actual equals content in expected
|
// AssertFeaturesEqual asserts content in actual equals content in expected
|
||||||
// regardless of ordering.
|
// regardless of ordering.
|
||||||
func AssertFeaturesEqual(t *testing.T, expected, actual []database.Feature) bool {
|
func AssertFeaturesEqual(t *testing.T, expected, actual []Feature) bool {
|
||||||
if assert.Len(t, actual, len(expected)) {
|
if assert.Len(t, actual, len(expected)) {
|
||||||
has := map[database.Feature]bool{}
|
has := map[Feature]bool{}
|
||||||
for _, nf := range expected {
|
for _, nf := range expected {
|
||||||
has[nf] = false
|
has[nf] = false
|
||||||
}
|
}
|
||||||
@ -154,7 +166,7 @@ func AssertFeaturesEqual(t *testing.T, expected, actual []database.Feature) bool
|
|||||||
|
|
||||||
// AssertLayerFeaturesEqual asserts content in actual equals to content in
|
// AssertLayerFeaturesEqual asserts content in actual equals to content in
|
||||||
// expected regardless of ordering.
|
// expected regardless of ordering.
|
||||||
func AssertLayerFeaturesEqual(t *testing.T, expected, actual []database.LayerFeature) bool {
|
func AssertLayerFeaturesEqual(t *testing.T, expected, actual []LayerFeature) bool {
|
||||||
if !assert.Len(t, actual, len(expected)) {
|
if !assert.Len(t, actual, len(expected)) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -174,7 +186,7 @@ func AssertLayerFeaturesEqual(t *testing.T, expected, actual []database.LayerFea
|
|||||||
|
|
||||||
// AssertNamespacesEqual asserts content in actual equals to content in
|
// AssertNamespacesEqual asserts content in actual equals to content in
|
||||||
// expected regardless of ordering.
|
// expected regardless of ordering.
|
||||||
func AssertNamespacesEqual(t *testing.T, expected, actual []database.Namespace) bool {
|
func AssertNamespacesEqual(t *testing.T, expected, actual []Namespace) bool {
|
||||||
expectedInterfaces := []interface{}{}
|
expectedInterfaces := []interface{}{}
|
||||||
for _, e := range expected {
|
for _, e := range expected {
|
||||||
expectedInterfaces = append(expectedInterfaces, e)
|
expectedInterfaces = append(expectedInterfaces, e)
|
||||||
@ -190,7 +202,7 @@ func AssertNamespacesEqual(t *testing.T, expected, actual []database.Namespace)
|
|||||||
|
|
||||||
// AssertLayerNamespacesEqual asserts content in actual equals to content in
|
// AssertLayerNamespacesEqual asserts content in actual equals to content in
|
||||||
// expected regardless of ordering.
|
// expected regardless of ordering.
|
||||||
func AssertLayerNamespacesEqual(t *testing.T, expected, actual []database.LayerNamespace) bool {
|
func AssertLayerNamespacesEqual(t *testing.T, expected, actual []LayerNamespace) bool {
|
||||||
expectedInterfaces := []interface{}{}
|
expectedInterfaces := []interface{}{}
|
||||||
for _, e := range expected {
|
for _, e := range expected {
|
||||||
expectedInterfaces = append(expectedInterfaces, e)
|
expectedInterfaces = append(expectedInterfaces, e)
|
||||||
@ -205,7 +217,7 @@ func AssertLayerNamespacesEqual(t *testing.T, expected, actual []database.LayerN
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AssertLayerEqual asserts actual layer equals to expected layer content wise.
|
// AssertLayerEqual asserts actual layer equals to expected layer content wise.
|
||||||
func AssertLayerEqual(t *testing.T, expected, actual *database.Layer) bool {
|
func AssertLayerEqual(t *testing.T, expected, actual *Layer) bool {
|
||||||
if expected == actual {
|
if expected == actual {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -239,7 +251,7 @@ func AssertIntStringMapEqual(t *testing.T, expected, actual map[int]string) bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AssertVulnerabilityEqual asserts two vulnerabilities are equal.
|
// AssertVulnerabilityEqual asserts two vulnerabilities are equal.
|
||||||
func AssertVulnerabilityEqual(t *testing.T, expected, actual *database.Vulnerability) bool {
|
func AssertVulnerabilityEqual(t *testing.T, expected, actual *Vulnerability) bool {
|
||||||
return assert.Equal(t, expected.Name, actual.Name) &&
|
return assert.Equal(t, expected.Name, actual.Name) &&
|
||||||
assert.Equal(t, expected.Link, actual.Link) &&
|
assert.Equal(t, expected.Link, actual.Link) &&
|
||||||
assert.Equal(t, expected.Description, actual.Description) &&
|
assert.Equal(t, expected.Description, actual.Description) &&
|
||||||
@ -248,7 +260,7 @@ func AssertVulnerabilityEqual(t *testing.T, expected, actual *database.Vulnerabi
|
|||||||
AssertMetadataMapEqual(t, expected.Metadata, actual.Metadata)
|
AssertMetadataMapEqual(t, expected.Metadata, actual.Metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
func castMetadataMapToInterface(metadata database.MetadataMap) map[string]interface{} {
|
func castMetadataMapToInterface(metadata MetadataMap) map[string]interface{} {
|
||||||
content, err := json.Marshal(metadata)
|
content, err := json.Marshal(metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -263,7 +275,7 @@ func castMetadataMapToInterface(metadata database.MetadataMap) map[string]interf
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AssertMetadataMapEqual asserts two metadata maps are equal.
|
// AssertMetadataMapEqual asserts two metadata maps are equal.
|
||||||
func AssertMetadataMapEqual(t *testing.T, expected, actual database.MetadataMap) bool {
|
func AssertMetadataMapEqual(t *testing.T, expected, actual MetadataMap) bool {
|
||||||
expectedMap := castMetadataMapToInterface(expected)
|
expectedMap := castMetadataMapToInterface(expected)
|
||||||
actualMap := castMetadataMapToInterface(actual)
|
actualMap := castMetadataMapToInterface(actual)
|
||||||
checked := mapset.NewSet()
|
checked := mapset.NewSet()
|
@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
"github.com/coreos/clair/ext/featurens"
|
"github.com/coreos/clair/ext/featurens"
|
||||||
"github.com/coreos/clair/pkg/tarutil"
|
"github.com/coreos/clair/pkg/tarutil"
|
||||||
"github.com/coreos/clair/pkg/testutil"
|
|
||||||
|
|
||||||
_ "github.com/coreos/clair/ext/featurens/alpinerelease"
|
_ "github.com/coreos/clair/ext/featurens/alpinerelease"
|
||||||
_ "github.com/coreos/clair/ext/featurens/aptsources"
|
_ "github.com/coreos/clair/ext/featurens/aptsources"
|
||||||
@ -50,6 +49,6 @@ func TestNamespaceDetector(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
testutil.AssertLayerNamespacesEqual(t, test.out, out)
|
database.AssertLayerNamespacesEqual(t, test.out, out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/clair/pkg/dbutil"
|
|
||||||
|
|
||||||
"github.com/pborman/uuid"
|
"github.com/pborman/uuid"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@ -210,7 +208,7 @@ func update(datastore database.Datastore, firstUpdate bool) {
|
|||||||
namespaces = append(namespaces, ns)
|
namespaces = append(namespaces, ns)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dbutil.PersistNamespaces(datastore, namespaces); err != nil {
|
if err := database.PersistNamespaces(datastore, namespaces); err != nil {
|
||||||
log.WithError(err).Error("Unable to insert namespaces")
|
log.WithError(err).Error("Unable to insert namespaces")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
29
worker.go
29
worker.go
@ -26,7 +26,6 @@ import (
|
|||||||
"github.com/coreos/clair/ext/featurens"
|
"github.com/coreos/clair/ext/featurens"
|
||||||
"github.com/coreos/clair/ext/imagefmt"
|
"github.com/coreos/clair/ext/imagefmt"
|
||||||
"github.com/coreos/clair/pkg/commonerr"
|
"github.com/coreos/clair/pkg/commonerr"
|
||||||
"github.com/coreos/clair/pkg/dbutil"
|
|
||||||
"github.com/coreos/clair/pkg/strutil"
|
"github.com/coreos/clair/pkg/strutil"
|
||||||
"github.com/coreos/clair/pkg/tarutil"
|
"github.com/coreos/clair/pkg/tarutil"
|
||||||
)
|
)
|
||||||
@ -102,7 +101,7 @@ func processRequests(imageFormat string, toDetect map[string]*processRequest) (m
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getProcessRequest(datastore database.Datastore, req LayerRequest) (preq *processRequest, err error) {
|
func getProcessRequest(datastore database.Datastore, req LayerRequest) (preq *processRequest, err error) {
|
||||||
layer, ok, err := dbutil.FindLayer(datastore, req.Hash)
|
layer, ok, err := database.FindLayer(datastore, req.Hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -125,7 +124,7 @@ func getProcessRequest(datastore database.Datastore, req LayerRequest) (preq *pr
|
|||||||
preq = &processRequest{
|
preq = &processRequest{
|
||||||
LayerRequest: req,
|
LayerRequest: req,
|
||||||
existingLayer: &layer,
|
existingLayer: &layer,
|
||||||
detectors: dbutil.DiffDetectors(EnabledDetectors, layer.By),
|
detectors: database.DiffDetectors(EnabledDetectors, layer.By),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,18 +139,18 @@ func persistProcessResult(datastore database.Datastore, results map[string]*proc
|
|||||||
namespaces = append(namespaces, r.newLayerContent.GetNamespaces()...)
|
namespaces = append(namespaces, r.newLayerContent.GetNamespaces()...)
|
||||||
}
|
}
|
||||||
|
|
||||||
features = dbutil.DeduplicateFeatures(features...)
|
features = database.DeduplicateFeatures(features...)
|
||||||
namespaces = dbutil.DeduplicateNamespaces(namespaces...)
|
namespaces = database.DeduplicateNamespaces(namespaces...)
|
||||||
if err := dbutil.PersistNamespaces(datastore, namespaces); err != nil {
|
if err := database.PersistNamespaces(datastore, namespaces); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dbutil.PersistFeatures(datastore, features); err != nil {
|
if err := database.PersistFeatures(datastore, features); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, layer := range results {
|
for _, layer := range results {
|
||||||
if err := dbutil.PersistPartialLayer(datastore, layer.newLayerContent); err != nil {
|
if err := database.PersistPartialLayer(datastore, layer.newLayerContent); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -196,19 +195,19 @@ func processLayers(datastore database.Datastore, imageFormat string, requests []
|
|||||||
func getProcessResultLayers(results map[string]*processResult) map[string]database.Layer {
|
func getProcessResultLayers(results map[string]*processResult) map[string]database.Layer {
|
||||||
layers := map[string]database.Layer{}
|
layers := map[string]database.Layer{}
|
||||||
for name, r := range results {
|
for name, r := range results {
|
||||||
layers[name] = *dbutil.MergeLayers(r.existingLayer, r.newLayerContent)
|
layers[name] = *database.MergeLayers(r.existingLayer, r.newLayerContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
return layers
|
return layers
|
||||||
}
|
}
|
||||||
|
|
||||||
func isAncestryProcessed(datastore database.Datastore, name string) (bool, error) {
|
func isAncestryProcessed(datastore database.Datastore, name string) (bool, error) {
|
||||||
ancestry, ok, err := dbutil.FindAncestry(datastore, name)
|
ancestry, ok, err := database.FindAncestry(datastore, name)
|
||||||
if err != nil || !ok {
|
if err != nil || !ok {
|
||||||
return ok, err
|
return ok, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return len(dbutil.DiffDetectors(EnabledDetectors, ancestry.By)) == 0, nil
|
return len(database.DiffDetectors(EnabledDetectors, ancestry.By)) == 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessAncestry downloads and scans an ancestry if it's not scanned by all
|
// ProcessAncestry downloads and scans an ancestry if it's not scanned by all
|
||||||
@ -255,7 +254,7 @@ func processAncestry(datastore database.Datastore, name string, layers []databas
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ancestryFeatures := dbutil.GetAncestryFeatures(ancestry)
|
ancestryFeatures := database.GetAncestryFeatures(ancestry)
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"ancestry": name,
|
"ancestry": name,
|
||||||
"processed by": EnabledDetectors,
|
"processed by": EnabledDetectors,
|
||||||
@ -263,17 +262,17 @@ func processAncestry(datastore database.Datastore, name string, layers []databas
|
|||||||
"layer count": len(ancestry.Layers),
|
"layer count": len(ancestry.Layers),
|
||||||
}).Debug("compute ancestry features")
|
}).Debug("compute ancestry features")
|
||||||
|
|
||||||
if err := dbutil.PersistNamespacedFeatures(datastore, ancestryFeatures); err != nil {
|
if err := database.PersistNamespacedFeatures(datastore, ancestryFeatures); err != nil {
|
||||||
log.WithField("ancestry", name).WithError(err).Error("could not persist namespaced features for ancestry")
|
log.WithField("ancestry", name).WithError(err).Error("could not persist namespaced features for ancestry")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dbutil.CacheRelatedVulnerability(datastore, ancestryFeatures); err != nil {
|
if err := database.CacheRelatedVulnerability(datastore, ancestryFeatures); err != nil {
|
||||||
log.WithField("ancestry", name).WithError(err).Error("failed to cache feature related vulnerability")
|
log.WithField("ancestry", name).WithError(err).Error("failed to cache feature related vulnerability")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dbutil.UpsertAncestry(datastore, ancestry); err != nil {
|
if err := database.UpsertAncestry(datastore, ancestry); err != nil {
|
||||||
log.WithField("ancestry", name).WithError(err).Error("could not upsert ancestry")
|
log.WithField("ancestry", name).WithError(err).Error("could not upsert ancestry")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -28,8 +28,6 @@ import (
|
|||||||
"github.com/coreos/clair/ext/featurefmt"
|
"github.com/coreos/clair/ext/featurefmt"
|
||||||
"github.com/coreos/clair/ext/featurens"
|
"github.com/coreos/clair/ext/featurens"
|
||||||
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
||||||
"github.com/coreos/clair/pkg/dbutil"
|
|
||||||
"github.com/coreos/clair/pkg/testutil"
|
|
||||||
|
|
||||||
// Register the required detectors.
|
// Register the required detectors.
|
||||||
_ "github.com/coreos/clair/ext/featurefmt/dpkg"
|
_ "github.com/coreos/clair/ext/featurefmt/dpkg"
|
||||||
@ -203,7 +201,7 @@ func newMockDatastore() *mockDatastore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
layer, _ := session.copy.layers[hash]
|
layer, _ := session.copy.layers[hash]
|
||||||
dbutil.MergeLayers(&layer, &database.Layer{
|
database.MergeLayers(&layer, &database.Layer{
|
||||||
Hash: hash,
|
Hash: hash,
|
||||||
By: by,
|
By: by,
|
||||||
Namespaces: namespaces,
|
Namespaces: namespaces,
|
||||||
@ -359,7 +357,7 @@ func TestProcessLayers(t *testing.T) {
|
|||||||
|
|
||||||
// Ensure each layer has expected namespaces and features detected
|
// Ensure each layer has expected namespaces and features detected
|
||||||
if blank, ok := datastore.layers["blank"]; ok {
|
if blank, ok := datastore.layers["blank"]; ok {
|
||||||
testutil.AssertDetectorsEqual(t, EnabledDetectors, blank.By)
|
database.AssertDetectorsEqual(t, EnabledDetectors, blank.By)
|
||||||
assert.Len(t, blank.Namespaces, 0)
|
assert.Len(t, blank.Namespaces, 0)
|
||||||
assert.Len(t, blank.Features, 0)
|
assert.Len(t, blank.Features, 0)
|
||||||
} else {
|
} else {
|
||||||
@ -368,7 +366,7 @@ func TestProcessLayers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if wheezy, ok := datastore.layers["wheezy"]; ok {
|
if wheezy, ok := datastore.layers["wheezy"]; ok {
|
||||||
testutil.AssertDetectorsEqual(t, EnabledDetectors, wheezy.By)
|
database.AssertDetectorsEqual(t, EnabledDetectors, wheezy.By)
|
||||||
assert.Equal(t, []database.LayerNamespace{
|
assert.Equal(t, []database.LayerNamespace{
|
||||||
{database.Namespace{"debian:7", dpkg.ParserName}, database.NewNamespaceDetector("os-release", "1.0")},
|
{database.Namespace{"debian:7", dpkg.ParserName}, database.NewNamespaceDetector("os-release", "1.0")},
|
||||||
}, wheezy.Namespaces)
|
}, wheezy.Namespaces)
|
||||||
@ -380,7 +378,7 @@ func TestProcessLayers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if jessie, ok := datastore.layers["jessie"]; ok {
|
if jessie, ok := datastore.layers["jessie"]; ok {
|
||||||
testutil.AssertDetectorsEqual(t, EnabledDetectors, jessie.By)
|
database.AssertDetectorsEqual(t, EnabledDetectors, jessie.By)
|
||||||
assert.Equal(t, []database.LayerNamespace{
|
assert.Equal(t, []database.LayerNamespace{
|
||||||
{database.Namespace{"debian:8", dpkg.ParserName}, database.NewNamespaceDetector("os-release", "1.0")},
|
{database.Namespace{"debian:8", dpkg.ParserName}, database.NewNamespaceDetector("os-release", "1.0")},
|
||||||
}, jessie.Namespaces)
|
}, jessie.Namespaces)
|
||||||
@ -578,8 +576,8 @@ func TestComputeAncestryFeatures(t *testing.T) {
|
|||||||
ancestryLayers, detectors, err := computeAncestryLayers(layers)
|
ancestryLayers, detectors, err := computeAncestryLayers(layers)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
testutil.AssertDetectorsEqual(t, expectedDetectors, detectors)
|
database.AssertDetectorsEqual(t, expectedDetectors, detectors)
|
||||||
for i := range expected {
|
for i := range expected {
|
||||||
testutil.AssertAncestryLayerEqual(t, &expected[i], &ancestryLayers[i])
|
database.AssertAncestryLayerEqual(t, &expected[i], &ancestryLayers[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user