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.
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
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",
|
|
|
|
by: []database.Detector{realDetectors[2]},
|
|
|
|
features: []database.LayerFeature{
|
|
|
|
{realFeatures[1], realDetectors[1]},
|
2018-09-11 20:09:08 +00:00
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
err: "database: parameters are not valid",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "layer with non-existing feature",
|
|
|
|
name: "random-forest",
|
|
|
|
err: "database: associated immutable entities are missing in the database",
|
|
|
|
by: []database.Detector{realDetectors[2]},
|
|
|
|
features: []database.LayerFeature{
|
|
|
|
{fakeFeatures[1], realDetectors[2]},
|
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",
|
|
|
|
err: "database: associated immutable entities are missing in the database",
|
|
|
|
by: []database.Detector{realDetectors[1]},
|
|
|
|
namespaces: []database.LayerNamespace{
|
|
|
|
{fakeNamespaces[1], 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",
|
|
|
|
err: "database: associated immutable entities are missing in the database",
|
|
|
|
by: []database.Detector{fakeDetector[1]},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "valid layer",
|
|
|
|
name: "hamsterhouse",
|
|
|
|
by: []database.Detector{realDetectors[1], realDetectors[2]},
|
|
|
|
features: []database.LayerFeature{
|
|
|
|
{realFeatures[1], realDetectors[2]},
|
|
|
|
{realFeatures[2], realDetectors[2]},
|
|
|
|
},
|
|
|
|
namespaces: []database.LayerNamespace{
|
|
|
|
{realNamespaces[1], realDetectors[1]},
|
|
|
|
},
|
|
|
|
layer: &database.Layer{
|
|
|
|
Hash: "hamsterhouse",
|
|
|
|
By: []database.Detector{realDetectors[1], realDetectors[2]},
|
|
|
|
Features: []database.LayerFeature{
|
|
|
|
{realFeatures[1], realDetectors[2]},
|
|
|
|
{realFeatures[2], realDetectors[2]},
|
|
|
|
},
|
|
|
|
Namespaces: []database.LayerNamespace{
|
|
|
|
{realNamespaces[1], 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",
|
|
|
|
by: []database.Detector{realDetectors[3], realDetectors[4]},
|
|
|
|
features: []database.LayerFeature{
|
|
|
|
{realFeatures[4], realDetectors[3]},
|
2015-12-28 20:03:29 +00:00
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
namespaces: []database.LayerNamespace{
|
|
|
|
{realNamespaces[3], realDetectors[4]},
|
2015-12-28 20:03:29 +00:00
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
layer: &database.Layer{
|
|
|
|
Hash: "layer-1",
|
|
|
|
By: []database.Detector{realDetectors[1], realDetectors[2], realDetectors[3], realDetectors[4]},
|
|
|
|
Features: []database.LayerFeature{
|
|
|
|
{realFeatures[1], realDetectors[2]},
|
|
|
|
{realFeatures[2], realDetectors[2]},
|
|
|
|
{realFeatures[4], realDetectors[3]},
|
|
|
|
},
|
|
|
|
Namespaces: []database.LayerNamespace{
|
|
|
|
{realNamespaces[1], realDetectors[1]},
|
|
|
|
{realNamespaces[3], realDetectors[4]},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-05-12 20:59:17 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func TestPersistLayer(t *testing.T) {
|
|
|
|
datastore, tx := openSessionForTest(t, "PersistLayer", true)
|
|
|
|
defer closeTest(t, datastore, tx)
|
|
|
|
|
|
|
|
for _, test := range persistLayerTests {
|
|
|
|
t.Run(test.title, func(t *testing.T) {
|
|
|
|
err := tx.PersistLayer(test.name, test.features, test.namespaces, test.by)
|
|
|
|
if test.err != "" {
|
|
|
|
assert.EqualError(t, err, test.err, "unexpected error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
if test.layer != nil {
|
|
|
|
layer, ok, err := tx.FindLayer(test.name)
|
|
|
|
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,
|
|
|
|
out: takeLayerPointerFromMap(realLayers, 6),
|
|
|
|
},
|
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) {
|
|
|
|
datastore, tx := openSessionForTest(t, "FindLayer", true)
|
|
|
|
defer closeTest(t, datastore, tx)
|
|
|
|
|
|
|
|
for _, test := range findLayerTests {
|
|
|
|
t.Run(test.title, func(t *testing.T) {
|
|
|
|
layer, ok, err := tx.FindLayer(test.in)
|
|
|
|
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
|
|
|
}
|