detectors/namespace: support pointers in tests
This change adjusts some names of types being exported and adds some documentation.
This commit is contained in:
parent
5396396ff7
commit
0b2a9ab12b
@ -1,4 +1,4 @@
|
||||
// Copyright 2015 clair authors
|
||||
// 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.
|
||||
@ -21,9 +21,10 @@ import (
|
||||
"github.com/coreos/clair/worker/detectors/namespace"
|
||||
)
|
||||
|
||||
var aptSourcesOSTests = []namespace.NamespaceTest{
|
||||
func TestAptSourcesNamespaceDetector(t *testing.T) {
|
||||
testData := []namespace.TestData{
|
||||
{
|
||||
ExpectedNamespace: database.Namespace{Name: "debian:unstable"},
|
||||
ExpectedNamespace: &database.Namespace{Name: "debian:unstable"},
|
||||
Data: map[string][]byte{
|
||||
"etc/os-release": []byte(
|
||||
`PRETTY_NAME="Debian GNU/Linux stretch/sid"
|
||||
@ -35,8 +36,7 @@ BUG_REPORT_URL="https://bugs.debian.org/"`),
|
||||
"etc/apt/sources.list": []byte(`deb http://httpredir.debian.org/debian unstable main`),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestAptSourcesNamespaceDetector(t *testing.T) {
|
||||
namespace.TestNamespaceDetector(t, &AptSourcesNamespaceDetector{}, aptSourcesOSTests)
|
||||
namespace.TestDetector(t, &AptSourcesNamespaceDetector{}, testData)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2015 clair authors
|
||||
// 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.
|
||||
@ -21,9 +21,10 @@ import (
|
||||
"github.com/coreos/clair/worker/detectors/namespace"
|
||||
)
|
||||
|
||||
var lsbReleaseOSTests = []namespace.NamespaceTest{
|
||||
func TestLsbReleaseNamespaceDetector(t *testing.T) {
|
||||
testData := []namespace.TestData{
|
||||
{
|
||||
ExpectedNamespace: database.Namespace{Name: "ubuntu:12.04"},
|
||||
ExpectedNamespace: &database.Namespace{Name: "ubuntu:12.04"},
|
||||
Data: map[string][]byte{
|
||||
"etc/lsb-release": []byte(
|
||||
`DISTRIB_ID=Ubuntu
|
||||
@ -33,7 +34,7 @@ DISTRIB_DESCRIPTION="Ubuntu 12.04 LTS"`),
|
||||
},
|
||||
},
|
||||
{ // We don't care about the minor version of Debian
|
||||
ExpectedNamespace: database.Namespace{Name: "debian:7"},
|
||||
ExpectedNamespace: &database.Namespace{Name: "debian:7"},
|
||||
Data: map[string][]byte{
|
||||
"etc/lsb-release": []byte(
|
||||
`DISTRIB_ID=Debian
|
||||
@ -42,8 +43,7 @@ DISTRIB_CODENAME=wheezy
|
||||
DISTRIB_DESCRIPTION="Debian 7.1"`),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestLsbReleaseNamespaceDetector(t *testing.T) {
|
||||
namespace.TestNamespaceDetector(t, &LsbReleaseNamespaceDetector{}, lsbReleaseOSTests)
|
||||
namespace.TestDetector(t, &LsbReleaseNamespaceDetector{}, testData)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2015 clair authors
|
||||
// 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.
|
||||
@ -21,9 +21,10 @@ import (
|
||||
"github.com/coreos/clair/worker/detectors/namespace"
|
||||
)
|
||||
|
||||
var osReleaseOSTests = []namespace.NamespaceTest{
|
||||
func TestOsReleaseNamespaceDetector(t *testing.T) {
|
||||
testData := []namespace.TestData{
|
||||
{
|
||||
ExpectedNamespace: database.Namespace{Name: "debian:8"},
|
||||
ExpectedNamespace: &database.Namespace{Name: "debian:8"},
|
||||
Data: map[string][]byte{
|
||||
"etc/os-release": []byte(
|
||||
`PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
|
||||
@ -37,7 +38,7 @@ BUG_REPORT_URL="https://bugs.debian.org/"`),
|
||||
},
|
||||
},
|
||||
{
|
||||
ExpectedNamespace: database.Namespace{Name: "ubuntu:15.10"},
|
||||
ExpectedNamespace: &database.Namespace{Name: "ubuntu:15.10"},
|
||||
Data: map[string][]byte{
|
||||
"etc/os-release": []byte(
|
||||
`NAME="Ubuntu"
|
||||
@ -52,7 +53,7 @@ BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`),
|
||||
},
|
||||
},
|
||||
{ // Doesn't have quotes around VERSION_ID
|
||||
ExpectedNamespace: database.Namespace{Name: "fedora:20"},
|
||||
ExpectedNamespace: &database.Namespace{Name: "fedora:20"},
|
||||
Data: map[string][]byte{
|
||||
"etc/os-release": []byte(
|
||||
`NAME=Fedora
|
||||
@ -70,8 +71,7 @@ REDHAT_SUPPORT_PRODUCT="Fedora"
|
||||
REDHAT_SUPPORT_PRODUCT_VERSION=20`),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestOsReleaseNamespaceDetector(t *testing.T) {
|
||||
namespace.TestNamespaceDetector(t, &OsReleaseNamespaceDetector{}, osReleaseOSTests)
|
||||
namespace.TestDetector(t, &OsReleaseNamespaceDetector{}, testData)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2015 clair authors
|
||||
// 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.
|
||||
@ -21,21 +21,21 @@ import (
|
||||
"github.com/coreos/clair/worker/detectors/namespace"
|
||||
)
|
||||
|
||||
var redhatReleaseTests = []namespace.NamespaceTest{
|
||||
func TestRedhatReleaseNamespaceDetector(t *testing.T) {
|
||||
testData := []namespace.TestData{
|
||||
{
|
||||
ExpectedNamespace: database.Namespace{Name: "centos:6"},
|
||||
ExpectedNamespace: &database.Namespace{Name: "centos:6"},
|
||||
Data: map[string][]byte{
|
||||
"etc/centos-release": []byte(`CentOS release 6.6 (Final)`),
|
||||
},
|
||||
},
|
||||
{
|
||||
ExpectedNamespace: database.Namespace{Name: "centos:7"},
|
||||
ExpectedNamespace: &database.Namespace{Name: "centos:7"},
|
||||
Data: map[string][]byte{
|
||||
"etc/system-release": []byte(`CentOS Linux release 7.1.1503 (Core)`),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedhatReleaseNamespaceDetector(t *testing.T) {
|
||||
namespace.TestNamespaceDetector(t, &RedhatReleaseNamespaceDetector{}, redhatReleaseTests)
|
||||
namespace.TestDetector(t, &RedhatReleaseNamespaceDetector{}, testData)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2015 clair authors
|
||||
// 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.
|
||||
@ -12,6 +12,8 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package namespace implements utilities common to implementations of
|
||||
// NamespaceDetector.
|
||||
package namespace
|
||||
|
||||
import (
|
||||
@ -22,13 +24,22 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type NamespaceTest struct {
|
||||
// TestData represents the data used to test an implementation of
|
||||
// NameSpaceDetector.
|
||||
type TestData struct {
|
||||
Data map[string][]byte
|
||||
ExpectedNamespace database.Namespace
|
||||
ExpectedNamespace *database.Namespace
|
||||
}
|
||||
|
||||
func TestNamespaceDetector(t *testing.T, detector detectors.NamespaceDetector, tests []NamespaceTest) {
|
||||
for _, test := range tests {
|
||||
assert.Equal(t, test.ExpectedNamespace, *detector.Detect(test.Data))
|
||||
// TestDetector runs a detector on each provided instance of TestData and
|
||||
// asserts the output to be equal to the expected output.
|
||||
func TestDetector(t *testing.T, detector detectors.NamespaceDetector, testData []TestData) {
|
||||
for _, td := range testData {
|
||||
detectedNamespace := detector.Detect(td.Data)
|
||||
if detectedNamespace == nil {
|
||||
assert.Equal(t, td.ExpectedNamespace, detectedNamespace)
|
||||
} else {
|
||||
assert.Equal(t, td.ExpectedNamespace.Name, detectedNamespace.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user