2017-07-26 23:23:54 +00:00
|
|
|
// Copyright 2017 clair authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUpsertAncestry(t *testing.T) {
|
|
|
|
store, tx := openSessionForTest(t, "UpsertAncestry", true)
|
|
|
|
defer closeTest(t, store, tx)
|
2018-09-07 15:31:35 +00:00
|
|
|
a1 := database.Ancestry{
|
|
|
|
Name: "a1",
|
2018-09-05 15:34:49 +00:00
|
|
|
Layers: []database.AncestryLayer{
|
|
|
|
{
|
2018-09-11 20:09:08 +00:00
|
|
|
LayerMetadata: database.LayerMetadata{
|
2018-09-05 15:34:49 +00:00
|
|
|
Hash: "layer-N",
|
|
|
|
},
|
|
|
|
},
|
2017-07-26 23:23:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:31:35 +00:00
|
|
|
a2 := database.Ancestry{}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
2018-09-07 15:31:35 +00:00
|
|
|
a3 := database.Ancestry{
|
|
|
|
Name: "a",
|
2018-09-05 15:34:49 +00:00
|
|
|
Layers: []database.AncestryLayer{
|
|
|
|
{
|
2018-09-11 20:09:08 +00:00
|
|
|
LayerMetadata: database.LayerMetadata{
|
2018-09-05 15:34:49 +00:00
|
|
|
Hash: "layer-0",
|
|
|
|
},
|
|
|
|
},
|
2017-07-26 23:23:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:31:35 +00:00
|
|
|
a4 := database.Ancestry{
|
|
|
|
Name: "a",
|
2018-09-05 15:34:49 +00:00
|
|
|
Layers: []database.AncestryLayer{
|
|
|
|
{
|
2018-09-11 20:09:08 +00:00
|
|
|
LayerMetadata: database.LayerMetadata{
|
2018-09-05 15:34:49 +00:00
|
|
|
Hash: "layer-1",
|
|
|
|
},
|
|
|
|
},
|
2017-07-26 23:23:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
f1 := database.Feature{
|
|
|
|
Name: "wechat",
|
|
|
|
Version: "0.5",
|
|
|
|
VersionFormat: "dpkg",
|
|
|
|
}
|
|
|
|
|
|
|
|
// not in database
|
|
|
|
f2 := database.Feature{
|
|
|
|
Name: "wechat",
|
|
|
|
Version: "0.6",
|
|
|
|
VersionFormat: "dpkg",
|
|
|
|
}
|
|
|
|
|
|
|
|
n1 := database.Namespace{
|
|
|
|
Name: "debian:7",
|
|
|
|
VersionFormat: "dpkg",
|
|
|
|
}
|
|
|
|
|
|
|
|
p := database.Processors{
|
|
|
|
Listers: []string{"dpkg", "non-existing"},
|
|
|
|
Detectors: []string{"os-release", "non-existing"},
|
|
|
|
}
|
|
|
|
|
|
|
|
nsf1 := database.NamespacedFeature{
|
|
|
|
Namespace: n1,
|
|
|
|
Feature: f1,
|
|
|
|
}
|
|
|
|
|
|
|
|
// not in database
|
|
|
|
nsf2 := database.NamespacedFeature{
|
|
|
|
Namespace: n1,
|
|
|
|
Feature: f2,
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:34:49 +00:00
|
|
|
a4.ProcessedBy = p
|
2017-07-26 23:23:54 +00:00
|
|
|
// invalid case
|
2018-09-05 15:34:49 +00:00
|
|
|
assert.NotNil(t, tx.UpsertAncestry(a1))
|
|
|
|
assert.NotNil(t, tx.UpsertAncestry(a2))
|
2017-07-26 23:23:54 +00:00
|
|
|
// valid case
|
2018-09-05 15:34:49 +00:00
|
|
|
assert.Nil(t, tx.UpsertAncestry(a3))
|
|
|
|
a4.Layers[0].DetectedFeatures = []database.NamespacedFeature{nsf1, nsf2}
|
2017-07-26 23:23:54 +00:00
|
|
|
// replace invalid case
|
2018-09-05 15:34:49 +00:00
|
|
|
assert.NotNil(t, tx.UpsertAncestry(a4))
|
|
|
|
a4.Layers[0].DetectedFeatures = []database.NamespacedFeature{nsf1}
|
2017-07-26 23:23:54 +00:00
|
|
|
// replace valid case
|
2018-09-05 15:34:49 +00:00
|
|
|
assert.Nil(t, tx.UpsertAncestry(a4))
|
2017-07-26 23:23:54 +00:00
|
|
|
// validate
|
2018-09-07 15:31:35 +00:00
|
|
|
ancestry, ok, err := tx.FindAncestry("a")
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, ok)
|
2018-09-07 15:31:35 +00:00
|
|
|
assertAncestryEqual(t, a4, ancestry)
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertProcessorsEqual(t *testing.T, expected database.Processors, actual database.Processors) bool {
|
|
|
|
sort.Strings(expected.Detectors)
|
|
|
|
sort.Strings(actual.Detectors)
|
|
|
|
sort.Strings(expected.Listers)
|
|
|
|
sort.Strings(actual.Listers)
|
|
|
|
return assert.Equal(t, expected.Detectors, actual.Detectors) && assert.Equal(t, expected.Listers, actual.Listers)
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:31:35 +00:00
|
|
|
func assertAncestryEqual(t *testing.T, expected database.Ancestry, actual database.Ancestry) bool {
|
|
|
|
assert.Equal(t, expected.Name, actual.Name)
|
|
|
|
assertProcessorsEqual(t, expected.ProcessedBy, actual.ProcessedBy)
|
|
|
|
if assert.Equal(t, len(expected.Layers), len(actual.Layers)) {
|
2018-09-05 15:34:49 +00:00
|
|
|
for index, layer := range expected.Layers {
|
|
|
|
if !assertAncestryLayerEqual(t, layer, actual.Layers[index]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
2018-09-05 15:34:49 +00:00
|
|
|
|
|
|
|
func assertAncestryLayerEqual(t *testing.T, expected database.AncestryLayer, actual database.AncestryLayer) bool {
|
2018-09-11 20:09:08 +00:00
|
|
|
return assertLayerEqual(t, expected.LayerMetadata, actual.LayerMetadata) &&
|
2018-09-05 15:34:49 +00:00
|
|
|
assertNamespacedFeatureEqual(t, expected.DetectedFeatures, actual.DetectedFeatures)
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:31:35 +00:00
|
|
|
func TestFindAncestry(t *testing.T) {
|
|
|
|
store, tx := openSessionForTest(t, "FindAncestry", true)
|
2017-07-26 23:23:54 +00:00
|
|
|
defer closeTest(t, store, tx)
|
|
|
|
|
|
|
|
// invalid
|
2018-09-07 15:31:35 +00:00
|
|
|
_, ok, err := tx.FindAncestry("ancestry-non")
|
2017-07-26 23:23:54 +00:00
|
|
|
if assert.Nil(t, err) {
|
|
|
|
assert.False(t, ok)
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:31:35 +00:00
|
|
|
expected := database.Ancestry{
|
|
|
|
Name: "ancestry-2",
|
|
|
|
ProcessedBy: database.Processors{
|
|
|
|
Detectors: []string{"os-release"},
|
|
|
|
Listers: []string{"dpkg"},
|
2017-07-26 23:23:54 +00:00
|
|
|
},
|
2018-09-05 15:34:49 +00:00
|
|
|
Layers: []database.AncestryLayer{
|
2017-07-26 23:23:54 +00:00
|
|
|
{
|
2018-09-11 20:09:08 +00:00
|
|
|
LayerMetadata: database.LayerMetadata{
|
2018-09-05 15:34:49 +00:00
|
|
|
Hash: "layer-0",
|
|
|
|
},
|
|
|
|
DetectedFeatures: []database.NamespacedFeature{
|
|
|
|
{
|
|
|
|
Namespace: database.Namespace{
|
|
|
|
Name: "debian:7",
|
|
|
|
VersionFormat: "dpkg",
|
|
|
|
},
|
|
|
|
Feature: database.Feature{
|
|
|
|
Name: "wechat",
|
|
|
|
Version: "0.5",
|
|
|
|
VersionFormat: "dpkg",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Namespace: database.Namespace{
|
|
|
|
Name: "debian:8",
|
|
|
|
VersionFormat: "dpkg",
|
|
|
|
},
|
|
|
|
Feature: database.Feature{
|
|
|
|
Name: "openssl",
|
|
|
|
Version: "1.0",
|
|
|
|
VersionFormat: "dpkg",
|
|
|
|
},
|
|
|
|
},
|
2017-07-26 23:23:54 +00:00
|
|
|
},
|
2018-09-05 15:34:49 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-11 20:09:08 +00:00
|
|
|
LayerMetadata: database.LayerMetadata{
|
2018-09-05 15:34:49 +00:00
|
|
|
Hash: "layer-1",
|
2017-07-26 23:23:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-09-11 20:09:08 +00:00
|
|
|
LayerMetadata: database.LayerMetadata{
|
2018-09-05 15:34:49 +00:00
|
|
|
Hash: "layer-2",
|
2017-07-26 23:23:54 +00:00
|
|
|
},
|
2018-09-05 15:34:49 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-11 20:09:08 +00:00
|
|
|
LayerMetadata: database.LayerMetadata{
|
2018-09-05 15:34:49 +00:00
|
|
|
Hash: "layer-3b",
|
2017-07-26 23:23:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// valid
|
2018-09-07 15:31:35 +00:00
|
|
|
ancestry, ok, err := tx.FindAncestry("ancestry-2")
|
2017-07-26 23:23:54 +00:00
|
|
|
if assert.Nil(t, err) && assert.True(t, ok) {
|
2018-09-07 15:31:35 +00:00
|
|
|
assertAncestryEqual(t, expected, ancestry)
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
|
|
|
}
|