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.
|
|
|
|
|
2019-03-06 21:32:46 +00:00
|
|
|
package layer
|
2015-12-28 20:03:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-05-02 22:33:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2019-03-06 21:32:46 +00:00
|
|
|
"github.com/coreos/clair/database/pgsql/testutil"
|
2015-12-28 20:03:29 +00:00
|
|
|
)
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
var persistLayerTests = []struct {
|
|
|
|
title string
|
|
|
|
name string
|
|
|
|
by []database.Detector
|
|
|
|
features []database.LayerFeature
|
|
|
|
namespaces []database.LayerNamespace
|
|
|
|
layer *database.Layer
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
title: "invalid layer name",
|
|
|
|
name: "",
|
|
|
|
err: "expected non-empty layer hash",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "layer with inconsistent feature and detectors",
|
|
|
|
name: "random-forest",
|
2019-03-06 21:32:46 +00:00
|
|
|
by: []database.Detector{testutil.RealDetectors[2]},
|
2018-10-08 15:11:30 +00:00
|
|
|
features: []database.LayerFeature{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealFeatures[1], testutil.RealDetectors[1], database.Namespace{}},
|
2018-09-11 20:09:08 +00:00
|
|
|
},
|
2019-02-19 21:42:00 +00:00
|
|
|
err: "parameters are not valid",
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "layer with non-existing feature",
|
|
|
|
name: "random-forest",
|
2019-02-19 21:42:00 +00:00
|
|
|
err: "associated immutable entities are missing in the database",
|
2019-03-06 21:32:46 +00:00
|
|
|
by: []database.Detector{testutil.RealDetectors[2]},
|
2018-10-08 15:11:30 +00:00
|
|
|
features: []database.LayerFeature{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.FakeFeatures[1], testutil.RealDetectors[2], database.Namespace{}},
|
2018-09-11 20:09:08 +00:00
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "layer with non-existing namespace",
|
|
|
|
name: "random-forest2",
|
2019-02-19 21:42:00 +00:00
|
|
|
err: "associated immutable entities are missing in the database",
|
2019-03-06 21:32:46 +00:00
|
|
|
by: []database.Detector{testutil.RealDetectors[1]},
|
2018-10-08 15:11:30 +00:00
|
|
|
namespaces: []database.LayerNamespace{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.FakeNamespaces[1], testutil.RealDetectors[1]},
|
2018-09-11 20:09:08 +00:00
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "layer with non-existing detector",
|
|
|
|
name: "random-forest3",
|
2019-02-19 21:42:00 +00:00
|
|
|
err: "associated immutable entities are missing in the database",
|
2019-03-06 21:32:46 +00:00
|
|
|
by: []database.Detector{testutil.FakeDetector[1]},
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-06 21:32:46 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
title: "valid layer",
|
|
|
|
name: "hamsterhouse",
|
2019-03-06 21:32:46 +00:00
|
|
|
by: []database.Detector{testutil.RealDetectors[1], testutil.RealDetectors[2]},
|
2018-10-08 15:11:30 +00:00
|
|
|
features: []database.LayerFeature{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealFeatures[1], testutil.RealDetectors[2], database.Namespace{}},
|
|
|
|
{testutil.RealFeatures[2], testutil.RealDetectors[2], database.Namespace{}},
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
|
|
|
namespaces: []database.LayerNamespace{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealNamespaces[1], testutil.RealDetectors[1]},
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
|
|
|
layer: &database.Layer{
|
|
|
|
Hash: "hamsterhouse",
|
2019-03-06 21:32:46 +00:00
|
|
|
By: []database.Detector{testutil.RealDetectors[1], testutil.RealDetectors[2]},
|
2018-10-08 15:11:30 +00:00
|
|
|
Features: []database.LayerFeature{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealFeatures[1], testutil.RealDetectors[2], database.Namespace{}},
|
|
|
|
{testutil.RealFeatures[2], testutil.RealDetectors[2], database.Namespace{}},
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
|
|
|
Namespaces: []database.LayerNamespace{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealNamespaces[1], testutil.RealDetectors[1]},
|
2018-09-05 15:34:49 +00:00
|
|
|
},
|
2015-12-28 20:03:29 +00:00
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "update existing layer",
|
|
|
|
name: "layer-1",
|
2019-03-06 21:32:46 +00:00
|
|
|
by: []database.Detector{testutil.RealDetectors[3], testutil.RealDetectors[4]},
|
2018-10-08 15:11:30 +00:00
|
|
|
features: []database.LayerFeature{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealFeatures[4], testutil.RealDetectors[3], database.Namespace{}},
|
2015-12-28 20:03:29 +00:00
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
namespaces: []database.LayerNamespace{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealNamespaces[3], testutil.RealDetectors[4]},
|
2015-12-28 20:03:29 +00:00
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
layer: &database.Layer{
|
|
|
|
Hash: "layer-1",
|
2019-03-06 21:32:46 +00:00
|
|
|
By: []database.Detector{testutil.RealDetectors[1], testutil.RealDetectors[2], testutil.RealDetectors[3], testutil.RealDetectors[4]},
|
2018-10-08 15:11:30 +00:00
|
|
|
Features: []database.LayerFeature{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealFeatures[1], testutil.RealDetectors[2], database.Namespace{}},
|
|
|
|
{testutil.RealFeatures[2], testutil.RealDetectors[2], database.Namespace{}},
|
|
|
|
{testutil.RealFeatures[4], testutil.RealDetectors[3], database.Namespace{}},
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
|
|
|
Namespaces: []database.LayerNamespace{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealNamespaces[1], testutil.RealDetectors[1]},
|
|
|
|
{testutil.RealNamespaces[3], testutil.RealDetectors[4]},
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-03-07 12:53:58 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
title: "layer with potential namespace",
|
|
|
|
name: "layer-potential-namespace",
|
2019-03-06 21:32:46 +00:00
|
|
|
by: []database.Detector{testutil.RealDetectors[3]},
|
2019-03-07 12:53:58 +00:00
|
|
|
features: []database.LayerFeature{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealFeatures[4], testutil.RealDetectors[3], testutil.RealNamespaces[4]},
|
2019-03-07 12:53:58 +00:00
|
|
|
},
|
|
|
|
namespaces: []database.LayerNamespace{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealNamespaces[3], testutil.RealDetectors[3]},
|
2019-03-07 12:53:58 +00:00
|
|
|
},
|
|
|
|
layer: &database.Layer{
|
|
|
|
Hash: "layer-potential-namespace",
|
2019-03-06 21:32:46 +00:00
|
|
|
By: []database.Detector{testutil.RealDetectors[3]},
|
2019-03-07 12:53:58 +00:00
|
|
|
Features: []database.LayerFeature{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealFeatures[4], testutil.RealDetectors[3], testutil.RealNamespaces[4]},
|
2019-03-07 12:53:58 +00:00
|
|
|
},
|
|
|
|
Namespaces: []database.LayerNamespace{
|
2019-03-06 21:32:46 +00:00
|
|
|
{testutil.RealNamespaces[3], testutil.RealDetectors[3]},
|
2019-03-07 12:53:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
}
|
2017-05-12 20:59:17 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func TestPersistLayer(t *testing.T) {
|
2019-03-06 21:32:46 +00:00
|
|
|
tx, cleanup := testutil.CreateTestTxWithFixtures(t, "PersistLayer")
|
|
|
|
defer cleanup()
|
2018-10-08 15:11:30 +00:00
|
|
|
|
|
|
|
for _, test := range persistLayerTests {
|
|
|
|
t.Run(test.title, func(t *testing.T) {
|
2019-03-06 21:32:46 +00:00
|
|
|
err := PersistLayer(tx, test.name, test.features, test.namespaces, test.by)
|
2018-10-08 15:11:30 +00:00
|
|
|
if test.err != "" {
|
|
|
|
assert.EqualError(t, err, test.err, "unexpected error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
if test.layer != nil {
|
2019-03-06 21:32:46 +00:00
|
|
|
layer, ok, err := FindLayer(tx, test.name)
|
2018-10-08 15:11:30 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, ok)
|
2018-10-02 14:50:53 +00:00
|
|
|
database.AssertLayerEqual(t, test.layer, &layer)
|
2018-10-08 15:11:30 +00:00
|
|
|
}
|
|
|
|
})
|
2017-05-12 20:59:17 +00:00
|
|
|
}
|
2016-01-20 21:04:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
var findLayerTests = []struct {
|
|
|
|
title string
|
|
|
|
in string
|
|
|
|
|
|
|
|
out *database.Layer
|
|
|
|
err string
|
|
|
|
ok bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
title: "invalid layer name",
|
|
|
|
in: "",
|
|
|
|
err: "non empty layer hash is expected.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "non-existing layer",
|
|
|
|
in: "layer-non-existing",
|
|
|
|
ok: false,
|
|
|
|
out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "existing layer",
|
|
|
|
in: "layer-4",
|
|
|
|
ok: true,
|
2019-03-06 21:32:46 +00:00
|
|
|
out: testutil.TakeLayerPointerFromMap(testutil.RealLayers, 6),
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2018-09-05 15:34:49 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func TestFindLayer(t *testing.T) {
|
2019-03-06 21:32:46 +00:00
|
|
|
tx, cleanup := testutil.CreateTestTxWithFixtures(t, "FindLayer")
|
|
|
|
defer cleanup()
|
2018-10-08 15:11:30 +00:00
|
|
|
|
|
|
|
for _, test := range findLayerTests {
|
|
|
|
t.Run(test.title, func(t *testing.T) {
|
2019-03-06 21:32:46 +00:00
|
|
|
layer, ok, err := FindLayer(tx, test.in)
|
2018-10-08 15:11:30 +00:00
|
|
|
if test.err != "" {
|
|
|
|
assert.EqualError(t, err, test.err, "unexpected error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, test.ok, ok)
|
|
|
|
if test.ok {
|
2018-10-02 14:50:53 +00:00
|
|
|
database.AssertLayerEqual(t, test.out, &layer)
|
2018-10-08 15:11:30 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-05 15:34:49 +00:00
|
|
|
}
|