worker: fix dpkg detector and adapt tests
This commit is contained in:
parent
85fa3f9a38
commit
34842fd8f7
@ -60,6 +60,7 @@ func (detector *DpkgFeaturesDetector) Detect(data map[string][]byte) ([]database
|
|||||||
// Defines the name of the package
|
// Defines the name of the package
|
||||||
|
|
||||||
pkg.Feature.Name = strings.TrimSpace(strings.TrimPrefix(line, "Package: "))
|
pkg.Feature.Name = strings.TrimSpace(strings.TrimPrefix(line, "Package: "))
|
||||||
|
pkg.Version = types.Version{}
|
||||||
} else if strings.HasPrefix(line, "Source: ") {
|
} else if strings.HasPrefix(line, "Source: ") {
|
||||||
// Source line (Optionnal)
|
// Source line (Optionnal)
|
||||||
// Gives the name of the source package
|
// Gives the name of the source package
|
||||||
|
@ -143,7 +143,7 @@ func detectContent(name, path, imageFormat string, parent *database.Layer) (name
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if namespace.Name != "" {
|
if namespace != nil {
|
||||||
log.Debugf("layer %s: Namespace is %s.", name, namespace.Name)
|
log.Debugf("layer %s: Namespace is %s.", name, namespace.Name)
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("layer %s: OS is unknown.", name)
|
log.Debugf("layer %s: OS is unknown.", name)
|
||||||
|
@ -5,70 +5,114 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/coreos/clair/config"
|
// Register the required detectors.
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/database/pgsql"
|
||||||
"github.com/coreos/clair/utils/types"
|
"github.com/coreos/clair/utils/types"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
// Register detectors
|
// Register detectors
|
||||||
_ "github.com/coreos/clair/worker/detectors/data"
|
_ "github.com/coreos/clair/worker/detectors/data"
|
||||||
_ "github.com/coreos/clair/worker/detectors/os"
|
_ "github.com/coreos/clair/worker/detectors/feature/dpkg"
|
||||||
_ "github.com/coreos/clair/worker/detectors/packages"
|
_ "github.com/coreos/clair/worker/detectors/namespace/aptsources"
|
||||||
|
_ "github.com/coreos/clair/worker/detectors/namespace/osrelease"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDistUpgrade(t *testing.T) {
|
func TestProcessWithDistUpgrade(t *testing.T) {
|
||||||
database.Open(&config.DatabaseConfig{Type: "memstore"})
|
// TODO(Quentin-M): This should not be bound to a single database implementation.
|
||||||
defer database.Close()
|
datastore, err := pgsql.OpenForTest("ProcessWithDistUpgrade", false)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer datastore.Close()
|
||||||
|
|
||||||
_, f, _, _ := runtime.Caller(0)
|
_, f, _, _ := runtime.Caller(0)
|
||||||
path := path.Join(path.Dir(f)) + "/testdata/DistUpgrade/"
|
path := path.Join(path.Dir(f)) + "/testdata/DistUpgrade/"
|
||||||
|
|
||||||
// blank.tar: MAINTAINER Quentin MACHU <quentin.machu.fr>
|
// blank.tar: MAINTAINER Quentin MACHU <quentin.machu.fr>
|
||||||
// wheezy.tar: FROM debian:wheezy
|
// wheezy.tar: FROM debian:wheezy
|
||||||
// jessie.tar: RUN sed -i "s/precise/trusty/" /etc/apt/sources.list && apt-get update && apt-get -y dist-upgrade
|
// jessie.tar: RUN sed -i "s/precise/trusty/" /etc/apt/sources.list && apt-get update &&
|
||||||
assert.Nil(t, Process("blank", "", path+"blank.tar.gz", "Docker"))
|
// apt-get -y dist-upgrade
|
||||||
assert.Nil(t, Process("wheezy", "blank", path+"wheezy.tar.gz", "Docker"))
|
assert.Nil(t, Process(datastore, "blank", "", path+"blank.tar.gz", "Docker"))
|
||||||
assert.Nil(t, Process("jessie", "wheezy", path+"jessie.tar.gz", "Docker"))
|
assert.Nil(t, Process(datastore, "wheezy", "blank", path+"wheezy.tar.gz", "Docker"))
|
||||||
|
assert.Nil(t, Process(datastore, "jessie", "wheezy", path+"jessie.tar.gz", "Docker"))
|
||||||
|
|
||||||
err := Process("blank", "", path+"blank.tar.gz", "")
|
wheezy, err := datastore.FindLayer("wheezy", true, false)
|
||||||
assert.Error(t, err, "could not process a layer which does not have a specified format")
|
|
||||||
|
|
||||||
err = Process("blank", "", path+"blank.tar.gz", "invalid")
|
|
||||||
assert.Error(t, err, "could not process a layer which does not have a supported format")
|
|
||||||
|
|
||||||
wheezy, err := database.FindOneLayerByID("wheezy", database.FieldLayerAll)
|
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
assert.Equal(t, "debian:7", wheezy.OS)
|
assert.Equal(t, "debian:7", wheezy.Namespace.Name)
|
||||||
assert.Len(t, wheezy.InstalledPackagesNodes, 52)
|
assert.Len(t, wheezy.Features, 52)
|
||||||
assert.Len(t, wheezy.RemovedPackagesNodes, 0)
|
|
||||||
|
|
||||||
jessie, err := database.FindOneLayerByID("jessie", database.FieldLayerAll)
|
jessie, err := datastore.FindLayer("jessie", true, false)
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
assert.Equal(t, "debian:8", jessie.OS)
|
assert.Equal(t, "debian:8", jessie.Namespace.Name)
|
||||||
assert.Len(t, jessie.InstalledPackagesNodes, 66)
|
assert.Len(t, jessie.Features, 74)
|
||||||
assert.Len(t, jessie.RemovedPackagesNodes, 44)
|
|
||||||
|
|
||||||
packageNodes, err := jessie.AllPackages()
|
// These FeatureVersions haven't been upgraded.
|
||||||
if assert.Nil(t, err) {
|
nonUpgradedFeatureVersions := []database.FeatureVersion{
|
||||||
// These packages haven't been upgraded
|
database.FeatureVersion{
|
||||||
nonUpgradedPackages := []database.Package{
|
Feature: database.Feature{Name: "libtext-wrapi18n-perl"},
|
||||||
database.Package{Name: "libtext-wrapi18n-perl", Version: types.NewVersionUnsafe("0.06-7")},
|
Version: types.NewVersionUnsafe("0.06-7"),
|
||||||
database.Package{Name: "libtext-charwidth-perl", Version: types.NewVersionUnsafe("0.04-7")},
|
},
|
||||||
database.Package{Name: "libtext-iconv-perl", Version: types.NewVersionUnsafe("1.7-5")},
|
database.FeatureVersion{
|
||||||
database.Package{Name: "mawk", Version: types.NewVersionUnsafe("1.3.3-17")},
|
Feature: database.Feature{Name: "libtext-charwidth-perl"},
|
||||||
database.Package{Name: "insserv", Version: types.NewVersionUnsafe("1.14.0-5")},
|
Version: types.NewVersionUnsafe("0.04-7"),
|
||||||
database.Package{Name: "db", Version: types.NewVersionUnsafe("5.1.29-5")},
|
},
|
||||||
database.Package{Name: "ustr", Version: types.NewVersionUnsafe("1.0.4-3")},
|
database.FeatureVersion{
|
||||||
database.Package{Name: "xz-utils", Version: types.NewVersionUnsafe("5.1.1alpha+20120614-2")},
|
Feature: database.Feature{Name: "libtext-iconv-perl"},
|
||||||
|
Version: types.NewVersionUnsafe("1.7-5"),
|
||||||
|
},
|
||||||
|
database.FeatureVersion{
|
||||||
|
Feature: database.Feature{Name: "mawk"},
|
||||||
|
Version: types.NewVersionUnsafe("1.3.3-17"),
|
||||||
|
},
|
||||||
|
database.FeatureVersion{
|
||||||
|
Feature: database.Feature{Name: "insserv"},
|
||||||
|
Version: types.NewVersionUnsafe("1.14.0-5"),
|
||||||
|
},
|
||||||
|
database.FeatureVersion{
|
||||||
|
Feature: database.Feature{Name: "db"},
|
||||||
|
Version: types.NewVersionUnsafe("5.1.29-5"),
|
||||||
|
},
|
||||||
|
database.FeatureVersion{
|
||||||
|
Feature: database.Feature{Name: "ustr"},
|
||||||
|
Version: types.NewVersionUnsafe("1.0.4-3"),
|
||||||
|
},
|
||||||
|
database.FeatureVersion{
|
||||||
|
Feature: database.Feature{Name: "xz-utils"},
|
||||||
|
Version: types.NewVersionUnsafe("5.1.1alpha+20120614-2"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, nufv := range nonUpgradedFeatureVersions {
|
||||||
|
nufv.Feature.Namespace.Name = "debian:7"
|
||||||
|
|
||||||
|
found := false
|
||||||
|
for _, fv := range jessie.Features {
|
||||||
|
if fv.Feature.Name == nufv.Feature.Name &&
|
||||||
|
fv.Feature.Namespace.Name == nufv.Feature.Namespace.Name &&
|
||||||
|
fv.Version == nufv.Version {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for _, p := range nonUpgradedPackages {
|
assert.Equal(t, true, found, "Jessie layer doesn't have %#v but it should.", nufv)
|
||||||
p.OS = "debian:7"
|
}
|
||||||
assert.Contains(t, packageNodes, p.GetNode(), "Jessie layer doesn't have %s but it should.", p)
|
|
||||||
}
|
for _, nufv := range nonUpgradedFeatureVersions {
|
||||||
for _, p := range nonUpgradedPackages {
|
nufv.Feature.Namespace.Name = "debian:8"
|
||||||
p.OS = "debian:8"
|
|
||||||
assert.NotContains(t, packageNodes, p.GetNode(), "Jessie layer has %s but it shouldn't.", p)
|
found := false
|
||||||
|
for _, fv := range jessie.Features {
|
||||||
|
if fv.Feature.Name == nufv.Feature.Name &&
|
||||||
|
fv.Feature.Namespace.Name == nufv.Feature.Namespace.Name &&
|
||||||
|
fv.Version == nufv.Version {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
assert.Equal(t, false, found, "Jessie layer has %#v but it shouldn't.", nufv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user