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 (
|
2016-05-17 21:30:40 +00:00
|
|
|
"path/filepath"
|
2015-11-13 19:11:28 +00:00
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2016-05-02 22:35:00 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2017-01-03 21:00:20 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
2017-01-13 07:08:52 +00:00
|
|
|
"github.com/coreos/clair/pkg/commonerr"
|
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"
|
|
|
|
_ "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
|
|
|
|
layers map[string]database.Layer
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockDatastore() *mockDatastore {
|
|
|
|
return &mockDatastore{
|
|
|
|
layers: make(map[string]database.Layer),
|
2016-01-19 20:07:19 +00:00
|
|
|
}
|
2016-05-02 22:35:00 +00:00
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2016-05-02 22:35:00 +00:00
|
|
|
func TestProcessWithDistUpgrade(t *testing.T) {
|
2015-11-13 19:11:28 +00:00
|
|
|
_, f, _, _ := runtime.Caller(0)
|
2016-05-17 21:30:40 +00:00
|
|
|
testDataPath := filepath.Join(filepath.Dir(f)) + "/testdata/DistUpgrade/"
|
2016-05-02 22:35:00 +00:00
|
|
|
|
|
|
|
// Create a mock datastore.
|
|
|
|
datastore := newMockDatastore()
|
|
|
|
datastore.FctInsertLayer = func(layer database.Layer) error {
|
|
|
|
datastore.layers[layer.Name] = layer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
datastore.FctFindLayer = func(name string, withFeatures, withVulnerabilities bool) (database.Layer, error) {
|
|
|
|
if layer, exists := datastore.layers[name]; exists {
|
|
|
|
return layer, nil
|
|
|
|
}
|
2017-01-13 07:08:52 +00:00
|
|
|
return database.Layer{}, commonerr.ErrNotFound
|
2016-05-02 22:35:00 +00:00
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2016-05-02 22:35:00 +00:00
|
|
|
// Create the list of FeatureVersions that should not been upgraded from one layer to another.
|
|
|
|
nonUpgradedFeatureVersions := []database.FeatureVersion{
|
2016-12-28 01:45:11 +00:00
|
|
|
{Feature: database.Feature{Name: "libtext-wrapi18n-perl"}, Version: "0.06-7"},
|
|
|
|
{Feature: database.Feature{Name: "libtext-charwidth-perl"}, Version: "0.04-7"},
|
|
|
|
{Feature: database.Feature{Name: "libtext-iconv-perl"}, Version: "1.7-5"},
|
|
|
|
{Feature: database.Feature{Name: "mawk"}, Version: "1.3.3-17"},
|
|
|
|
{Feature: database.Feature{Name: "insserv"}, Version: "1.14.0-5"},
|
|
|
|
{Feature: database.Feature{Name: "db"}, Version: "5.1.29-5"},
|
|
|
|
{Feature: database.Feature{Name: "ustr"}, Version: "1.0.4-3"},
|
|
|
|
{Feature: database.Feature{Name: "xz-utils"}, Version: "5.1.1alpha+20120614-2"},
|
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-01-27 01:14:44 +00:00
|
|
|
assert.Nil(t, ProcessLayer(datastore, "Docker", "blank", "", testDataPath+"blank.tar.gz", nil))
|
|
|
|
assert.Nil(t, ProcessLayer(datastore, "Docker", "wheezy", "blank", testDataPath+"wheezy.tar.gz", nil))
|
|
|
|
assert.Nil(t, ProcessLayer(datastore, "Docker", "jessie", "wheezy", testDataPath+"jessie.tar.gz", nil))
|
2015-12-16 05:01:22 +00:00
|
|
|
|
2016-05-02 22:35:00 +00:00
|
|
|
// Ensure that the 'wheezy' layer has the expected namespace and features.
|
|
|
|
wheezy, ok := datastore.layers["wheezy"]
|
|
|
|
if assert.True(t, ok, "layer 'wheezy' not processed") {
|
2017-05-12 20:59:17 +00:00
|
|
|
if !assert.Len(t, wheezy.Namespaces, 1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, "debian:7", wheezy.Namespaces[0].Name)
|
2016-01-19 20:07:19 +00:00
|
|
|
assert.Len(t, wheezy.Features, 52)
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2016-05-02 22:35:00 +00:00
|
|
|
for _, nufv := range nonUpgradedFeatureVersions {
|
|
|
|
nufv.Feature.Namespace.Name = "debian:7"
|
2017-01-03 21:00:20 +00:00
|
|
|
nufv.Feature.Namespace.VersionFormat = dpkg.ParserName
|
2016-05-02 22:35:00 +00:00
|
|
|
assert.Contains(t, wheezy.Features, nufv)
|
|
|
|
}
|
|
|
|
}
|
2016-01-19 20:07:19 +00:00
|
|
|
|
2016-05-02 22:35:00 +00:00
|
|
|
// Ensure that the 'wheezy' layer has the expected namespace and non-upgraded features.
|
|
|
|
jessie, ok := datastore.layers["jessie"]
|
|
|
|
if assert.True(t, ok, "layer 'jessie' not processed") {
|
2017-05-30 17:45:14 +00:00
|
|
|
if !assert.Len(t, jessie.Namespaces, 2) {
|
2017-05-12 20:59:17 +00:00
|
|
|
return
|
|
|
|
}
|
2017-05-30 17:45:14 +00:00
|
|
|
nsNames := []string{jessie.Namespaces[0].Name, jessie.Namespaces[1].Name}
|
|
|
|
// the old features and namespace should remain here with only the features in new namespaces detected
|
|
|
|
assert.Contains(t, nsNames, "debian:8")
|
|
|
|
assert.Contains(t, nsNames, "debian:7")
|
|
|
|
|
|
|
|
// because the featurefmt detects the features under "debian:8" and "debian:7", therefore the number of features is duplicated
|
|
|
|
assert.Len(t, jessie.Features, 148)
|
2016-01-19 20:07:19 +00:00
|
|
|
|
2016-05-02 22:35:00 +00:00
|
|
|
for _, nufv := range nonUpgradedFeatureVersions {
|
|
|
|
nufv.Feature.Namespace.Name = "debian:7"
|
2017-01-03 21:00:20 +00:00
|
|
|
nufv.Feature.Namespace.VersionFormat = dpkg.ParserName
|
2016-05-02 22:35:00 +00:00
|
|
|
assert.Contains(t, jessie.Features, nufv)
|
|
|
|
}
|
|
|
|
for _, nufv := range nonUpgradedFeatureVersions {
|
|
|
|
nufv.Feature.Namespace.Name = "debian:8"
|
2017-01-03 21:00:20 +00:00
|
|
|
nufv.Feature.Namespace.VersionFormat = dpkg.ParserName
|
2016-05-02 22:35:00 +00:00
|
|
|
assert.NotContains(t, jessie.Features, nufv)
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|