Merge pull request #394 from KeyboardNerd/multiplens
added support for detecting multiple namespaces in a layerpull/318/merge
commit
abd7d2e013
@ -0,0 +1,44 @@
|
||||
// Copyright 2016 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 migrations
|
||||
|
||||
import "github.com/remind101/migrate"
|
||||
|
||||
func init() {
|
||||
RegisterMigration(migrate.Migration{
|
||||
ID: 8,
|
||||
Up: migrate.Queries([]string{
|
||||
// set on deletion, remove the corresponding rows in database
|
||||
`CREATE TABLE IF NOT EXISTS Layer_Namespace(
|
||||
id SERIAL PRIMARY KEY,
|
||||
layer_id INT REFERENCES Layer(id) ON DELETE CASCADE,
|
||||
namespace_id INT REFERENCES Namespace(id) ON DELETE CASCADE,
|
||||
unique(layer_id, namespace_id)
|
||||
);`,
|
||||
`CREATE INDEX ON Layer_Namespace (namespace_id);`,
|
||||
`CREATE INDEX ON Layer_Namespace (layer_id);`,
|
||||
// move the namespace_id to the table
|
||||
`INSERT INTO Layer_Namespace (layer_id, namespace_id) SELECT id, namespace_id FROM Layer;`,
|
||||
// alter the Layer table to remove the column
|
||||
`ALTER TABLE IF EXISTS Layer DROP namespace_id;`,
|
||||
}),
|
||||
Down: migrate.Queries([]string{
|
||||
`ALTER TABLE IF EXISTS Layer ADD namespace_id INT NULL REFERENCES Namespace;`,
|
||||
`CREATE INDEX ON Layer (namespace_id);`,
|
||||
`UPDATE IF EXISTS Layer SET namespace_id = (SELECT lns.namespace_id FROM Layer_Namespace lns WHERE Layer.id = lns.layer_id LIMIT 1);`,
|
||||
`DROP TABLE IF EXISTS Layer_Namespace;`,
|
||||
}),
|
||||
})
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package featurens_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/coreos/clair/database"
|
||||
"github.com/coreos/clair/ext/featurens"
|
||||
"github.com/coreos/clair/pkg/tarutil"
|
||||
|
||||
_ "github.com/coreos/clair/ext/featurens/alpinerelease"
|
||||
_ "github.com/coreos/clair/ext/featurens/aptsources"
|
||||
_ "github.com/coreos/clair/ext/featurens/lsbrelease"
|
||||
_ "github.com/coreos/clair/ext/featurens/osrelease"
|
||||
_ "github.com/coreos/clair/ext/featurens/redhatrelease"
|
||||
)
|
||||
|
||||
type MultipleNamespaceTestData struct {
|
||||
Files tarutil.FilesMap
|
||||
ExpectedNamespaces []database.Namespace
|
||||
}
|
||||
|
||||
func assertnsNameEqual(t *testing.T, nslist_expected, nslist []database.Namespace) {
|
||||
assert.Equal(t, len(nslist_expected), len(nslist))
|
||||
expected := map[string]struct{}{}
|
||||
input := map[string]struct{}{}
|
||||
// compare the two sets
|
||||
for i := range nslist_expected {
|
||||
expected[nslist_expected[i].Name] = struct{}{}
|
||||
input[nslist[i].Name] = struct{}{}
|
||||
}
|
||||
assert.Equal(t, expected, input)
|
||||
}
|
||||
|
||||
func testMultipleNamespace(t *testing.T, testData []MultipleNamespaceTestData) {
|
||||
for _, td := range testData {
|
||||
nslist, err := featurens.Detect(td.Files)
|
||||
assert.Nil(t, err)
|
||||
assertnsNameEqual(t, td.ExpectedNamespaces, nslist)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultipleNamespaceDetector(t *testing.T) {
|
||||
testData := []MultipleNamespaceTestData{
|
||||
{
|
||||
ExpectedNamespaces: []database.Namespace{
|
||||
database.Namespace{Name: "debian:8", VersionFormat: "dpkg"},
|
||||
database.Namespace{Name: "alpine:v3.3", VersionFormat: "dpkg"},
|
||||
},
|
||||
Files: tarutil.FilesMap{
|
||||
"etc/os-release": []byte(`
|
||||
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
|
||||
NAME="Debian GNU/Linux"
|
||||
VERSION_ID="8"
|
||||
VERSION="8 (jessie)"
|
||||
ID=debian
|
||||
HOME_URL="http://www.debian.org/"
|
||||
SUPPORT_URL="http://www.debian.org/support/"
|
||||
BUG_REPORT_URL="https://bugs.debian.org/"`),
|
||||
"etc/alpine-release": []byte(`3.3.4`),
|
||||
},
|
||||
},
|
||||
}
|
||||
testMultipleNamespace(t, testData)
|
||||
}
|
Loading…
Reference in new issue