2017-01-13 07:08:52 +00:00
|
|
|
// Copyright 2017 clair authors
|
2016-01-19 20:16:45 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-01-26 23:24:04 +00:00
|
|
|
package clair
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
import (
|
2017-07-26 23:22:29 +00:00
|
|
|
"errors"
|
2016-05-17 21:30:40 +00:00
|
|
|
"path/filepath"
|
2015-11-13 19:11:28 +00:00
|
|
|
"runtime"
|
2017-07-26 23:22:29 +00:00
|
|
|
"strings"
|
2015-11-13 19:11:28 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-05-02 22:35:00 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-20 19:57:06 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-05-02 22:35:00 +00:00
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2017-07-26 23:22:29 +00:00
|
|
|
"github.com/coreos/clair/ext/featurefmt"
|
|
|
|
"github.com/coreos/clair/ext/featurens"
|
2017-01-03 21:00:20 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
2016-01-25 21:20:47 +00:00
|
|
|
|
2016-01-19 20:16:45 +00:00
|
|
|
// Register the required detectors.
|
2017-01-13 23:49:02 +00:00
|
|
|
_ "github.com/coreos/clair/ext/featurefmt/dpkg"
|
2017-07-26 23:22:29 +00:00
|
|
|
_ "github.com/coreos/clair/ext/featurefmt/rpm"
|
2017-01-13 23:49:02 +00:00
|
|
|
_ "github.com/coreos/clair/ext/featurens/aptsources"
|
|
|
|
_ "github.com/coreos/clair/ext/featurens/osrelease"
|
|
|
|
_ "github.com/coreos/clair/ext/imagefmt/docker"
|
2015-11-13 19:11:28 +00:00
|
|
|
)
|
|
|
|
|
2016-05-02 22:35:00 +00:00
|
|
|
type mockDatastore struct {
|
|
|
|
database.MockDatastore
|
2017-07-26 23:22:29 +00:00
|
|
|
|
2018-09-11 20:09:08 +00:00
|
|
|
layers map[string]database.Layer
|
2018-09-07 15:31:35 +00:00
|
|
|
ancestry map[string]database.Ancestry
|
2017-07-26 23:22:29 +00:00
|
|
|
namespaces map[string]database.Namespace
|
|
|
|
features map[string]database.Feature
|
|
|
|
namespacedFeatures map[string]database.NamespacedFeature
|
2016-05-02 22:35:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
type mockSession struct {
|
|
|
|
database.MockSession
|
|
|
|
|
|
|
|
store *mockDatastore
|
|
|
|
copy mockDatastore
|
|
|
|
terminated bool
|
2016-05-02 22:35:00 +00:00
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
func copyDatastore(md *mockDatastore) mockDatastore {
|
2018-09-11 20:09:08 +00:00
|
|
|
layers := map[string]database.Layer{}
|
2017-07-26 23:22:29 +00:00
|
|
|
for k, l := range md.layers {
|
2018-09-11 20:09:08 +00:00
|
|
|
layers[k] = database.Layer{
|
2018-09-20 19:57:06 +00:00
|
|
|
Hash: l.Hash,
|
|
|
|
By: append([]database.Detector{}, l.By...),
|
|
|
|
Features: append([]database.LayerFeature{}, l.Features...),
|
|
|
|
Namespaces: append([]database.LayerNamespace{}, l.Namespaces...),
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-02 22:35:00 +00:00
|
|
|
|
2018-09-07 15:31:35 +00:00
|
|
|
ancestry := map[string]database.Ancestry{}
|
2017-07-26 23:22:29 +00:00
|
|
|
for k, a := range md.ancestry {
|
2018-09-05 15:34:49 +00:00
|
|
|
ancestryLayers := []database.AncestryLayer{}
|
|
|
|
for _, layer := range a.Layers {
|
|
|
|
ancestryLayers = append(ancestryLayers, database.AncestryLayer{
|
2018-09-20 19:57:06 +00:00
|
|
|
Hash: layer.Hash,
|
|
|
|
Features: append([]database.AncestryFeature{}, layer.Features...),
|
2018-09-05 15:34:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:31:35 +00:00
|
|
|
ancestry[k] = database.Ancestry{
|
2018-09-20 19:57:06 +00:00
|
|
|
Name: a.Name,
|
|
|
|
By: append([]database.Detector{}, a.By...),
|
2018-09-05 15:34:49 +00:00
|
|
|
Layers: ancestryLayers,
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespaces := map[string]database.Namespace{}
|
|
|
|
for k, n := range md.namespaces {
|
|
|
|
namespaces[k] = n
|
|
|
|
}
|
|
|
|
|
|
|
|
features := map[string]database.Feature{}
|
|
|
|
for k, f := range md.features {
|
|
|
|
features[k] = f
|
|
|
|
}
|
|
|
|
|
|
|
|
namespacedFeatures := map[string]database.NamespacedFeature{}
|
|
|
|
for k, f := range md.namespacedFeatures {
|
|
|
|
namespacedFeatures[k] = f
|
|
|
|
}
|
2018-09-20 19:57:06 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
return mockDatastore{
|
|
|
|
layers: layers,
|
|
|
|
ancestry: ancestry,
|
|
|
|
namespaces: namespaces,
|
|
|
|
namespacedFeatures: namespacedFeatures,
|
|
|
|
features: features,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockDatastore() *mockDatastore {
|
|
|
|
errSessionDone := errors.New("Session Done")
|
|
|
|
md := &mockDatastore{
|
2018-09-11 20:09:08 +00:00
|
|
|
layers: make(map[string]database.Layer),
|
2018-09-07 15:31:35 +00:00
|
|
|
ancestry: make(map[string]database.Ancestry),
|
2017-07-26 23:22:29 +00:00
|
|
|
namespaces: make(map[string]database.Namespace),
|
|
|
|
features: make(map[string]database.Feature),
|
|
|
|
namespacedFeatures: make(map[string]database.NamespacedFeature),
|
2016-05-02 22:35:00 +00:00
|
|
|
}
|
2017-07-26 23:22:29 +00:00
|
|
|
|
|
|
|
md.FctBegin = func() (database.Session, error) {
|
|
|
|
session := &mockSession{
|
|
|
|
store: md,
|
|
|
|
copy: copyDatastore(md),
|
|
|
|
terminated: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
session.FctCommit = func() error {
|
|
|
|
if session.terminated {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
session.store.layers = session.copy.layers
|
|
|
|
session.store.ancestry = session.copy.ancestry
|
|
|
|
session.store.namespaces = session.copy.namespaces
|
|
|
|
session.store.features = session.copy.features
|
|
|
|
session.store.namespacedFeatures = session.copy.namespacedFeatures
|
|
|
|
session.terminated = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
session.FctRollback = func() error {
|
|
|
|
if session.terminated {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
session.terminated = true
|
|
|
|
session.copy = mockDatastore{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:34:49 +00:00
|
|
|
session.FctFindAncestry = func(name string) (database.Ancestry, bool, error) {
|
2017-07-26 23:22:29 +00:00
|
|
|
if session.terminated {
|
2018-09-05 15:34:49 +00:00
|
|
|
return database.Ancestry{}, false, errSessionDone
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
ancestry, ok := session.copy.ancestry[name]
|
2018-09-07 15:31:35 +00:00
|
|
|
return ancestry, ok, nil
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 15:34:49 +00:00
|
|
|
session.FctFindLayer = func(name string) (database.Layer, bool, error) {
|
2017-07-26 23:22:29 +00:00
|
|
|
if session.terminated {
|
2018-09-05 15:34:49 +00:00
|
|
|
return database.Layer{}, false, errSessionDone
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
layer, ok := session.copy.layers[name]
|
|
|
|
return layer, ok, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
session.FctPersistNamespaces = func(ns []database.Namespace) error {
|
|
|
|
if session.terminated {
|
|
|
|
return errSessionDone
|
|
|
|
}
|
|
|
|
for _, n := range ns {
|
2018-09-20 19:57:06 +00:00
|
|
|
session.copy.namespaces[NamespaceKey(&n)] = n
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
session.FctPersistFeatures = func(fs []database.Feature) error {
|
|
|
|
if session.terminated {
|
|
|
|
return errSessionDone
|
|
|
|
}
|
|
|
|
for _, f := range fs {
|
2018-09-20 19:57:06 +00:00
|
|
|
session.copy.features[FeatureKey(&f)] = f
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
2018-09-20 19:57:06 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
return nil
|
2016-05-02 22:35:00 +00:00
|
|
|
}
|
2017-07-26 23:22:29 +00:00
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
session.FctPersistLayer = func(hash string, features []database.LayerFeature, namespaces []database.LayerNamespace, by []database.Detector) error {
|
2017-07-26 23:22:29 +00:00
|
|
|
if session.terminated {
|
|
|
|
return errSessionDone
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ns := range namespaces {
|
2018-09-20 19:57:06 +00:00
|
|
|
if _, ok := session.copy.namespaces[NamespaceKey(&ns.Namespace)]; !ok {
|
|
|
|
panic("")
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range features {
|
2018-09-20 19:57:06 +00:00
|
|
|
if _, ok := session.copy.features[FeatureKey(&f.Feature)]; !ok {
|
|
|
|
panic("")
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
layer, _ := session.copy.layers[hash]
|
2018-10-02 14:50:53 +00:00
|
|
|
database.MergeLayers(&layer, &database.Layer{
|
2018-09-20 19:57:06 +00:00
|
|
|
Hash: hash,
|
|
|
|
By: by,
|
|
|
|
Namespaces: namespaces,
|
|
|
|
Features: features,
|
|
|
|
})
|
2017-07-26 23:22:29 +00:00
|
|
|
|
|
|
|
session.copy.layers[hash] = layer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:31:35 +00:00
|
|
|
session.FctUpsertAncestry = func(ancestry database.Ancestry) error {
|
2017-07-26 23:22:29 +00:00
|
|
|
if session.terminated {
|
|
|
|
return errSessionDone
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
// ensure the namespaces features are in the code base
|
|
|
|
for _, l := range ancestry.Layers {
|
|
|
|
for _, f := range l.GetFeatures() {
|
|
|
|
if _, ok := session.copy.namespacedFeatures[NamespacedFeatureKey(&f)]; !ok {
|
|
|
|
panic("")
|
|
|
|
}
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:34:49 +00:00
|
|
|
session.copy.ancestry[ancestry.Name] = ancestry
|
2017-07-26 23:22:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
session.FctPersistNamespacedFeatures = func(namespacedFeatures []database.NamespacedFeature) error {
|
|
|
|
for i, f := range namespacedFeatures {
|
2018-09-20 19:57:06 +00:00
|
|
|
if _, ok := session.copy.features[FeatureKey(&f.Feature)]; !ok {
|
|
|
|
panic("")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := session.copy.namespaces[NamespaceKey(&f.Namespace)]; !ok {
|
|
|
|
panic("")
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
session.copy.namespacedFeatures[NamespacedFeatureKey(&f)] = namespacedFeatures[i]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
session.FctCacheAffectedNamespacedFeatures = func(namespacedFeatures []database.NamespacedFeature) error {
|
|
|
|
// The function does nothing because we don't care about the vulnerability cache in worker_test.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return session, nil
|
2016-05-02 22:35:00 +00:00
|
|
|
}
|
2017-07-26 23:22:29 +00:00
|
|
|
return md
|
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2018-09-20 19:57:06 +00:00
|
|
|
EnabledDetectors = append(featurefmt.ListListers(), featurens.ListDetectors()...)
|
2017-07-26 23:22:29 +00:00
|
|
|
m.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func FeatureKey(f *database.Feature) string {
|
|
|
|
return strings.Join([]string{f.Name, f.VersionFormat, f.Version}, "__")
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
func NamespaceKey(ns *database.Namespace) string {
|
|
|
|
return strings.Join([]string{ns.Name, ns.VersionFormat}, "__")
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
func NamespacedFeatureKey(f *database.NamespacedFeature) string {
|
|
|
|
return strings.Join([]string{f.Name, f.Namespace.Name}, "__")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessAncestryWithDistUpgrade(t *testing.T) {
|
2018-09-20 19:57:06 +00:00
|
|
|
// TODO(sidac): Change to use table driven tests.
|
2017-07-26 23:22:29 +00:00
|
|
|
// Create the list of Features that should not been upgraded from one layer to another.
|
|
|
|
nonUpgradedFeatures := []database.Feature{
|
|
|
|
{Name: "libtext-wrapi18n-perl", Version: "0.06-7"},
|
|
|
|
{Name: "libtext-charwidth-perl", Version: "0.04-7"},
|
|
|
|
{Name: "libtext-iconv-perl", Version: "1.7-5"},
|
|
|
|
{Name: "mawk", Version: "1.3.3-17"},
|
|
|
|
{Name: "insserv", Version: "1.14.0-5"},
|
|
|
|
{Name: "db", Version: "5.1.29-5"},
|
|
|
|
{Name: "ustr", Version: "1.0.4-3"},
|
|
|
|
{Name: "xz-utils", Version: "5.1.1alpha+20120614-2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
nonUpgradedMap := map[database.Feature]struct{}{}
|
|
|
|
for _, f := range nonUpgradedFeatures {
|
|
|
|
f.VersionFormat = "dpkg"
|
|
|
|
nonUpgradedMap[f] = struct{}{}
|
2016-05-02 22:35:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process test layers.
|
|
|
|
//
|
2015-11-13 19:11:28 +00:00
|
|
|
// blank.tar: MAINTAINER Quentin MACHU <quentin.machu.fr>
|
|
|
|
// wheezy.tar: FROM debian:wheezy
|
2016-01-19 20:07:19 +00:00
|
|
|
// jessie.tar: RUN sed -i "s/precise/trusty/" /etc/apt/sources.list && apt-get update &&
|
|
|
|
// apt-get -y dist-upgrade
|
2017-07-26 23:22:29 +00:00
|
|
|
_, f, _, _ := runtime.Caller(0)
|
|
|
|
testDataPath := filepath.Join(filepath.Dir(f)) + "/testdata/DistUpgrade/"
|
2015-12-16 05:01:22 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
datastore := newMockDatastore()
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
layers := []LayerRequest{
|
|
|
|
{Hash: "blank", Path: testDataPath + "blank.tar.gz"},
|
|
|
|
{Hash: "wheezy", Path: testDataPath + "wheezy.tar.gz"},
|
|
|
|
{Hash: "jessie", Path: testDataPath + "jessie.tar.gz"},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Nil(t, ProcessAncestry(datastore, "Docker", "Mock", layers))
|
2018-09-05 15:34:49 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
// check the ancestry features
|
2018-09-20 19:57:06 +00:00
|
|
|
features := []database.AncestryFeature{}
|
|
|
|
for i, l := range datastore.ancestry["Mock"].Layers {
|
|
|
|
assert.Equal(t, layers[i].Hash, l.Hash)
|
|
|
|
features = append(features, l.Features...)
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:34:49 +00:00
|
|
|
assert.Len(t, features, 74)
|
|
|
|
for _, f := range features {
|
2017-07-26 23:22:29 +00:00
|
|
|
if _, ok := nonUpgradedMap[f.Feature]; ok {
|
|
|
|
assert.Equal(t, "debian:7", f.Namespace.Name)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "debian:8", f.Namespace.Name)
|
2016-05-02 22:35:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessLayers(t *testing.T) {
|
|
|
|
_, f, _, _ := runtime.Caller(0)
|
|
|
|
testDataPath := filepath.Join(filepath.Dir(f)) + "/testdata/DistUpgrade/"
|
|
|
|
|
|
|
|
datastore := newMockDatastore()
|
|
|
|
|
|
|
|
layers := []LayerRequest{
|
|
|
|
{Hash: "blank", Path: testDataPath + "blank.tar.gz"},
|
|
|
|
{Hash: "wheezy", Path: testDataPath + "wheezy.tar.gz"},
|
|
|
|
{Hash: "jessie", Path: testDataPath + "jessie.tar.gz"},
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:34:49 +00:00
|
|
|
LayerWithContents, err := processLayers(datastore, "Docker", layers)
|
2017-07-26 23:22:29 +00:00
|
|
|
assert.Nil(t, err)
|
2018-09-05 15:34:49 +00:00
|
|
|
assert.Len(t, LayerWithContents, 3)
|
2017-07-26 23:22:29 +00:00
|
|
|
// ensure resubmit won't break the stuff
|
2018-09-05 15:34:49 +00:00
|
|
|
LayerWithContents, err = processLayers(datastore, "Docker", layers)
|
2017-07-26 23:22:29 +00:00
|
|
|
assert.Nil(t, err)
|
2018-09-05 15:34:49 +00:00
|
|
|
assert.Len(t, LayerWithContents, 3)
|
2017-07-26 23:22:29 +00:00
|
|
|
// Ensure each processed layer is correct
|
2018-09-05 15:34:49 +00:00
|
|
|
assert.Len(t, LayerWithContents[0].Namespaces, 0)
|
|
|
|
assert.Len(t, LayerWithContents[1].Namespaces, 1)
|
|
|
|
assert.Len(t, LayerWithContents[2].Namespaces, 1)
|
|
|
|
assert.Len(t, LayerWithContents[0].Features, 0)
|
|
|
|
assert.Len(t, LayerWithContents[1].Features, 52)
|
|
|
|
assert.Len(t, LayerWithContents[2].Features, 74)
|
2017-07-26 23:22:29 +00:00
|
|
|
|
|
|
|
// Ensure each layer has expected namespaces and features detected
|
|
|
|
if blank, ok := datastore.layers["blank"]; ok {
|
2018-10-02 14:50:53 +00:00
|
|
|
database.AssertDetectorsEqual(t, EnabledDetectors, blank.By)
|
2017-07-26 23:22:29 +00:00
|
|
|
assert.Len(t, blank.Namespaces, 0)
|
|
|
|
assert.Len(t, blank.Features, 0)
|
|
|
|
} else {
|
|
|
|
assert.Fail(t, "blank is not stored")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if wheezy, ok := datastore.layers["wheezy"]; ok {
|
2018-10-02 14:50:53 +00:00
|
|
|
database.AssertDetectorsEqual(t, EnabledDetectors, wheezy.By)
|
2018-09-20 19:57:06 +00:00
|
|
|
assert.Equal(t, []database.LayerNamespace{
|
|
|
|
{database.Namespace{"debian:7", dpkg.ParserName}, database.NewNamespaceDetector("os-release", "1.0")},
|
|
|
|
}, wheezy.Namespaces)
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
assert.Len(t, wheezy.Features, 52)
|
|
|
|
} else {
|
|
|
|
assert.Fail(t, "wheezy is not stored")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if jessie, ok := datastore.layers["jessie"]; ok {
|
2018-10-02 14:50:53 +00:00
|
|
|
database.AssertDetectorsEqual(t, EnabledDetectors, jessie.By)
|
2018-09-20 19:57:06 +00:00
|
|
|
assert.Equal(t, []database.LayerNamespace{
|
|
|
|
{database.Namespace{"debian:8", dpkg.ParserName}, database.NewNamespaceDetector("os-release", "1.0")},
|
|
|
|
}, jessie.Namespaces)
|
2017-06-22 18:01:41 +00:00
|
|
|
assert.Len(t, jessie.Features, 74)
|
2017-07-26 23:22:29 +00:00
|
|
|
} else {
|
|
|
|
assert.Fail(t, "jessie is not stored")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
func getFeatures(a database.Ancestry) []database.AncestryFeature {
|
|
|
|
features := []database.AncestryFeature{}
|
|
|
|
for _, l := range a.Layers {
|
|
|
|
features = append(features, l.Features...)
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
return features
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestComputeAncestryFeatures(t *testing.T) {
|
|
|
|
vf1 := "format 1"
|
|
|
|
vf2 := "format 2"
|
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
nd1 := database.NewNamespaceDetector("apk", "1.0")
|
|
|
|
fd1 := database.NewFeatureDetector("fd1", "1.0")
|
|
|
|
// this detector only scans one layer with one extra feature, this one
|
|
|
|
// should be omitted.
|
|
|
|
fd2 := database.NewFeatureDetector("fd2", "1.0")
|
|
|
|
|
|
|
|
ns1a := database.LayerNamespace{
|
|
|
|
database.Namespace{
|
|
|
|
Name: "namespace 1:a",
|
|
|
|
VersionFormat: vf1,
|
|
|
|
}, nd1,
|
|
|
|
}
|
|
|
|
|
|
|
|
ns1b := database.LayerNamespace{
|
|
|
|
database.Namespace{
|
|
|
|
Name: "namespace 1:b",
|
|
|
|
VersionFormat: vf1,
|
|
|
|
}, nd1}
|
|
|
|
|
|
|
|
ns2a := database.LayerNamespace{
|
|
|
|
database.Namespace{
|
|
|
|
Name: "namespace 2:a",
|
|
|
|
VersionFormat: vf2,
|
|
|
|
}, nd1}
|
|
|
|
|
|
|
|
ns2b := database.LayerNamespace{
|
|
|
|
database.Namespace{
|
|
|
|
Name: "namespace 2:b",
|
|
|
|
VersionFormat: vf2,
|
|
|
|
}, nd1}
|
|
|
|
|
|
|
|
f1 := database.LayerFeature{
|
|
|
|
database.Feature{
|
|
|
|
Name: "feature 1",
|
|
|
|
Version: "0.1",
|
|
|
|
VersionFormat: vf1,
|
|
|
|
}, fd1}
|
|
|
|
|
|
|
|
f2 := database.LayerFeature{database.Feature{
|
2017-07-26 23:22:29 +00:00
|
|
|
Name: "feature 2",
|
|
|
|
Version: "0.2",
|
|
|
|
VersionFormat: vf1,
|
2018-09-20 19:57:06 +00:00
|
|
|
}, fd2}
|
|
|
|
|
|
|
|
f3 := database.LayerFeature{
|
|
|
|
database.Feature{
|
|
|
|
Name: "feature 1",
|
|
|
|
Version: "0.3",
|
|
|
|
VersionFormat: vf2,
|
|
|
|
}, fd1}
|
|
|
|
|
|
|
|
f4 := database.LayerFeature{
|
|
|
|
database.Feature{
|
|
|
|
Name: "feature 2",
|
|
|
|
Version: "0.3",
|
|
|
|
VersionFormat: vf2,
|
|
|
|
}, fd1}
|
|
|
|
|
|
|
|
f5 := database.LayerFeature{
|
|
|
|
database.Feature{
|
|
|
|
Name: "feature 3",
|
|
|
|
Version: "0.3",
|
|
|
|
VersionFormat: vf2,
|
|
|
|
},
|
|
|
|
fd2,
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Suppose Clair is watching two files for namespaces one containing ns1
|
|
|
|
// changes e.g. os-release and the other one containing ns2 changes e.g.
|
|
|
|
// node.
|
2018-09-20 19:57:06 +00:00
|
|
|
blank := database.Layer{
|
|
|
|
Hash: "blank",
|
|
|
|
By: []database.Detector{nd1, fd1, fd1},
|
|
|
|
}
|
2018-09-11 20:09:08 +00:00
|
|
|
initNS1a := database.Layer{
|
2018-09-20 19:57:06 +00:00
|
|
|
Hash: "initNS1a",
|
|
|
|
By: []database.Detector{nd1, fd1, fd1},
|
|
|
|
Namespaces: []database.LayerNamespace{ns1a},
|
|
|
|
Features: []database.LayerFeature{f1, f2},
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 20:09:08 +00:00
|
|
|
upgradeNS2b := database.Layer{
|
2018-09-20 19:57:06 +00:00
|
|
|
Hash: "upgradeNS2b",
|
|
|
|
By: []database.Detector{nd1, fd1, fd1},
|
|
|
|
Namespaces: []database.LayerNamespace{ns2b},
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 20:09:08 +00:00
|
|
|
upgradeNS1b := database.Layer{
|
2018-09-20 19:57:06 +00:00
|
|
|
Hash: "upgradeNS1b",
|
|
|
|
By: []database.Detector{nd1, fd1, fd1, fd2},
|
|
|
|
Namespaces: []database.LayerNamespace{ns1b},
|
|
|
|
Features: []database.LayerFeature{f1, f2, f5},
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 20:09:08 +00:00
|
|
|
initNS2a := database.Layer{
|
2018-09-20 19:57:06 +00:00
|
|
|
Hash: "initNS2a",
|
|
|
|
By: []database.Detector{nd1, fd1, fd1},
|
|
|
|
Namespaces: []database.LayerNamespace{ns2a},
|
|
|
|
Features: []database.LayerFeature{f3, f4},
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 20:09:08 +00:00
|
|
|
removeF2 := database.Layer{
|
2018-09-20 19:57:06 +00:00
|
|
|
Hash: "removeF2",
|
|
|
|
By: []database.Detector{nd1, fd1, fd1},
|
|
|
|
Features: []database.LayerFeature{f1},
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// blank -> ns1:a, f1 f2 (init)
|
|
|
|
// -> f1 (feature change)
|
|
|
|
// -> ns2:a, f3, f4 (init ns2a)
|
|
|
|
// -> ns2:b (ns2 upgrade without changing features)
|
|
|
|
// -> blank (empty)
|
|
|
|
// -> ns1:b, f1 f2 (ns1 upgrade and add f2)
|
|
|
|
// -> f1 (remove f2)
|
|
|
|
// -> blank (empty)
|
|
|
|
|
2018-09-11 20:09:08 +00:00
|
|
|
layers := []database.Layer{
|
2018-09-20 19:57:06 +00:00
|
|
|
blank, // empty
|
|
|
|
initNS1a, // namespace: NS1a, features: f1, f2
|
|
|
|
removeF2, // namespace: , features: f1
|
|
|
|
initNS2a, // namespace: NS2a, features: f3, f4 ( under NS2a )
|
|
|
|
upgradeNS2b, // namespace: NS2b, ( f3, f4 are now under NS2b )
|
|
|
|
blank, // empty
|
|
|
|
upgradeNS1b, // namespace: NS1b, ( f1, f2 are now under NS1b, and they are introduced in this layer. )
|
|
|
|
removeF2, // namespace: , features: f1
|
2017-07-26 23:22:29 +00:00
|
|
|
blank,
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
expected := []database.AncestryLayer{
|
|
|
|
{
|
|
|
|
"blank",
|
|
|
|
[]database.AncestryFeature{},
|
|
|
|
},
|
2017-07-26 23:22:29 +00:00
|
|
|
{
|
2018-09-20 19:57:06 +00:00
|
|
|
"initNS1a",
|
|
|
|
[]database.AncestryFeature{{database.NamespacedFeature{f1.Feature, ns1a.Namespace}, f1.By, ns1a.By}},
|
|
|
|
},
|
2017-07-26 23:22:29 +00:00
|
|
|
{
|
2018-09-20 19:57:06 +00:00
|
|
|
"removeF2",
|
|
|
|
[]database.AncestryFeature{},
|
|
|
|
},
|
2017-07-26 23:22:29 +00:00
|
|
|
{
|
2018-09-20 19:57:06 +00:00
|
|
|
"initNS2a",
|
|
|
|
[]database.AncestryFeature{
|
|
|
|
{database.NamespacedFeature{f3.Feature, ns2a.Namespace}, f3.By, ns2a.By},
|
|
|
|
{database.NamespacedFeature{f4.Feature, ns2a.Namespace}, f4.By, ns2a.By},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"upgradeNS2b",
|
|
|
|
[]database.AncestryFeature{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"blank",
|
|
|
|
[]database.AncestryFeature{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"upgradeNS1b",
|
|
|
|
[]database.AncestryFeature{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"removeF2",
|
|
|
|
[]database.AncestryFeature{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"blank",
|
|
|
|
[]database.AncestryFeature{},
|
|
|
|
},
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 19:57:06 +00:00
|
|
|
expectedDetectors := []database.Detector{nd1, fd1}
|
|
|
|
ancestryLayers, detectors, err := computeAncestryLayers(layers)
|
|
|
|
require.Nil(t, err)
|
2017-07-26 23:22:29 +00:00
|
|
|
|
2018-10-02 14:50:53 +00:00
|
|
|
database.AssertDetectorsEqual(t, expectedDetectors, detectors)
|
2018-09-20 19:57:06 +00:00
|
|
|
for i := range expected {
|
2018-10-02 14:50:53 +00:00
|
|
|
database.AssertAncestryLayerEqual(t, &expected[i], &ancestryLayers[i])
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|