2019-03-06 21:34:16 +00:00
|
|
|
// Copyright 2019 clair authors
|
2017-07-26 23:23:54 +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:34:16 +00:00
|
|
|
package ancestry
|
2017-07-26 23:23:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
2019-03-06 21:34:16 +00:00
|
|
|
"github.com/coreos/clair/database/pgsql/testutil"
|
2017-07-26 23:23:54 +00:00
|
|
|
)
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
var upsertAncestryTests = []struct {
|
|
|
|
in *database.Ancestry
|
|
|
|
err string
|
|
|
|
title string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
title: "ancestry with invalid layer",
|
|
|
|
in: &database.Ancestry{
|
|
|
|
Name: "a1",
|
|
|
|
Layers: []database.AncestryLayer{
|
|
|
|
{
|
|
|
|
Hash: "layer-non-existing",
|
2018-09-05 15:34:49 +00:00
|
|
|
},
|
|
|
|
},
|
2017-07-26 23:23:54 +00:00
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
err: database.ErrMissingEntities.Error(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "ancestry with invalid name",
|
|
|
|
in: &database.Ancestry{},
|
|
|
|
err: database.ErrInvalidParameters.Error(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "new valid ancestry",
|
|
|
|
in: &database.Ancestry{
|
|
|
|
Name: "a",
|
|
|
|
Layers: []database.AncestryLayer{{Hash: "layer-0"}},
|
2017-07-26 23:23:54 +00:00
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "ancestry with invalid feature",
|
|
|
|
in: &database.Ancestry{
|
|
|
|
Name: "a",
|
2019-03-06 21:34:16 +00:00
|
|
|
By: []database.Detector{testutil.RealDetectors[1], testutil.RealDetectors[2]},
|
2018-10-08 15:11:30 +00:00
|
|
|
Layers: []database.AncestryLayer{{Hash: "layer-1", Features: []database.AncestryFeature{
|
2019-03-06 21:34:16 +00:00
|
|
|
{testutil.FakeNamespacedFeatures[1], testutil.FakeDetector[1], testutil.FakeDetector[2]},
|
2018-10-08 15:11:30 +00:00
|
|
|
}}},
|
|
|
|
},
|
|
|
|
err: database.ErrMissingEntities.Error(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "replace old ancestry",
|
|
|
|
in: &database.Ancestry{
|
|
|
|
Name: "a",
|
2019-03-06 21:34:16 +00:00
|
|
|
By: []database.Detector{testutil.RealDetectors[1], testutil.RealDetectors[2]},
|
2018-10-08 15:11:30 +00:00
|
|
|
Layers: []database.AncestryLayer{
|
2019-03-06 21:34:16 +00:00
|
|
|
{"layer-1", []database.AncestryFeature{{testutil.RealNamespacedFeatures[1], testutil.RealDetectors[2], testutil.RealDetectors[1]}}},
|
2018-09-05 15:34:49 +00:00
|
|
|
},
|
2017-07-26 23:23:54 +00:00
|
|
|
},
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func TestUpsertAncestry(t *testing.T) {
|
2019-03-06 21:34:16 +00:00
|
|
|
tx, cleanup := testutil.CreateTestTxWithFixtures(t, "TestUpsertAncestry")
|
|
|
|
defer cleanup()
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
for _, test := range upsertAncestryTests {
|
|
|
|
t.Run(test.title, func(t *testing.T) {
|
2019-03-06 21:34:16 +00:00
|
|
|
err := UpsertAncestry(tx, *test.in)
|
2018-10-08 15:11:30 +00:00
|
|
|
if test.err != "" {
|
|
|
|
assert.EqualError(t, err, test.err, "unexpected error")
|
|
|
|
return
|
2018-09-05 15:34:49 +00:00
|
|
|
}
|
2018-10-08 15:11:30 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-06 21:34:16 +00:00
|
|
|
actual, ok, err := FindAncestry(tx, test.in.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.AssertAncestryEqual(t, test.in, &actual)
|
2018-10-08 15:11:30 +00:00
|
|
|
})
|
2018-09-05 15:34:49 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
2018-09-05 15:34:49 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
var findAncestryTests = []struct {
|
|
|
|
title string
|
|
|
|
in string
|
|
|
|
|
|
|
|
ancestry *database.Ancestry
|
|
|
|
err string
|
|
|
|
ok bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
title: "missing ancestry",
|
|
|
|
in: "ancestry-non",
|
|
|
|
err: "",
|
|
|
|
ancestry: nil,
|
|
|
|
ok: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "valid ancestry",
|
|
|
|
in: "ancestry-2",
|
|
|
|
err: "",
|
|
|
|
ok: true,
|
2019-03-06 21:34:16 +00:00
|
|
|
ancestry: testutil.TakeAncestryPointerFromMap(testutil.RealAncestries, 2),
|
2018-10-08 15:11:30 +00:00
|
|
|
},
|
2018-09-05 15:34:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:31:35 +00:00
|
|
|
func TestFindAncestry(t *testing.T) {
|
2019-03-06 21:34:16 +00:00
|
|
|
tx, cleanup := testutil.CreateTestTxWithFixtures(t, "TestFindAncestry")
|
|
|
|
defer cleanup()
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
for _, test := range findAncestryTests {
|
|
|
|
t.Run(test.title, func(t *testing.T) {
|
2019-03-06 21:34:16 +00:00
|
|
|
ancestry, ok, err := FindAncestry(tx, test.in)
|
2018-10-08 15:11:30 +00:00
|
|
|
if test.err != "" {
|
|
|
|
assert.EqualError(t, err, test.err, "unexpected error")
|
|
|
|
return
|
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, test.ok, ok)
|
|
|
|
if test.ok {
|
2018-10-02 14:50:53 +00:00
|
|
|
database.AssertAncestryEqual(t, test.ancestry, &ancestry)
|
2018-10-08 15:11:30 +00:00
|
|
|
}
|
|
|
|
})
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
|
|
|
}
|