detectors/namespace: support pointers in tests

This change adjusts some names of types being exported and adds some
documentation.
This commit is contained in:
Jimmy Zelinskie 2016-11-09 15:51:28 -05:00
parent 5396396ff7
commit 0b2a9ab12b
5 changed files with 86 additions and 75 deletions

View File

@ -1,4 +1,4 @@
// Copyright 2015 clair authors // Copyright 2016 clair authors
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -21,22 +21,22 @@ import (
"github.com/coreos/clair/worker/detectors/namespace" "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"}, {
Data: map[string][]byte{ ExpectedNamespace: &database.Namespace{Name: "debian:unstable"},
"etc/os-release": []byte( Data: map[string][]byte{
`PRETTY_NAME="Debian GNU/Linux stretch/sid" "etc/os-release": []byte(
`PRETTY_NAME="Debian GNU/Linux stretch/sid"
NAME="Debian GNU/Linux" NAME="Debian GNU/Linux"
ID=debian ID=debian
HOME_URL="https://www.debian.org/" HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support/" SUPPORT_URL="https://www.debian.org/support/"
BUG_REPORT_URL="https://bugs.debian.org/"`), BUG_REPORT_URL="https://bugs.debian.org/"`),
"etc/apt/sources.list": []byte(`deb http://httpredir.debian.org/debian unstable main`), "etc/apt/sources.list": []byte(`deb http://httpredir.debian.org/debian unstable main`),
},
}, },
}, }
}
func TestAptSourcesNamespaceDetector(t *testing.T) { namespace.TestDetector(t, &AptSourcesNamespaceDetector{}, testData)
namespace.TestNamespaceDetector(t, &AptSourcesNamespaceDetector{}, aptSourcesOSTests)
} }

View File

@ -1,4 +1,4 @@
// Copyright 2015 clair authors // Copyright 2016 clair authors
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -21,29 +21,29 @@ import (
"github.com/coreos/clair/worker/detectors/namespace" "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"}, {
Data: map[string][]byte{ ExpectedNamespace: &database.Namespace{Name: "ubuntu:12.04"},
"etc/lsb-release": []byte( Data: map[string][]byte{
`DISTRIB_ID=Ubuntu "etc/lsb-release": []byte(
`DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04 DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04 LTS"`), DISTRIB_DESCRIPTION="Ubuntu 12.04 LTS"`),
},
}, },
}, { // We don't care about the minor version of Debian
{ // 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{
Data: map[string][]byte{ "etc/lsb-release": []byte(
"etc/lsb-release": []byte( `DISTRIB_ID=Debian
`DISTRIB_ID=Debian
DISTRIB_RELEASE=7.1 DISTRIB_RELEASE=7.1
DISTRIB_CODENAME=wheezy DISTRIB_CODENAME=wheezy
DISTRIB_DESCRIPTION="Debian 7.1"`), DISTRIB_DESCRIPTION="Debian 7.1"`),
},
}, },
}, }
}
func TestLsbReleaseNamespaceDetector(t *testing.T) { namespace.TestDetector(t, &LsbReleaseNamespaceDetector{}, testData)
namespace.TestNamespaceDetector(t, &LsbReleaseNamespaceDetector{}, lsbReleaseOSTests)
} }

View File

@ -1,4 +1,4 @@
// Copyright 2015 clair authors // Copyright 2016 clair authors
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -21,12 +21,13 @@ import (
"github.com/coreos/clair/worker/detectors/namespace" "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"}, {
Data: map[string][]byte{ ExpectedNamespace: &database.Namespace{Name: "debian:8"},
"etc/os-release": []byte( Data: map[string][]byte{
`PRETTY_NAME="Debian GNU/Linux 8 (jessie)" "etc/os-release": []byte(
`PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux" NAME="Debian GNU/Linux"
VERSION_ID="8" VERSION_ID="8"
VERSION="8 (jessie)" VERSION="8 (jessie)"
@ -34,13 +35,13 @@ ID=debian
HOME_URL="http://www.debian.org/" HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support/" SUPPORT_URL="http://www.debian.org/support/"
BUG_REPORT_URL="https://bugs.debian.org/"`), 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{
Data: map[string][]byte{ "etc/os-release": []byte(
"etc/os-release": []byte( `NAME="Ubuntu"
`NAME="Ubuntu"
VERSION="15.10 (Wily Werewolf)" VERSION="15.10 (Wily Werewolf)"
ID=ubuntu ID=ubuntu
ID_LIKE=debian ID_LIKE=debian
@ -49,13 +50,13 @@ VERSION_ID="15.10"
HOME_URL="http://www.ubuntu.com/" HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`), BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`),
},
}, },
}, { // Doesn't have quotes around VERSION_ID
{ // Doesn't have quotes around VERSION_ID ExpectedNamespace: &database.Namespace{Name: "fedora:20"},
ExpectedNamespace: database.Namespace{Name: "fedora:20"}, Data: map[string][]byte{
Data: map[string][]byte{ "etc/os-release": []byte(
"etc/os-release": []byte( `NAME=Fedora
`NAME=Fedora
VERSION="20 (Heisenbug)" VERSION="20 (Heisenbug)"
ID=fedora ID=fedora
VERSION_ID=20 VERSION_ID=20
@ -68,10 +69,9 @@ REDHAT_BUGZILLA_PRODUCT="Fedora"
REDHAT_BUGZILLA_PRODUCT_VERSION=20 REDHAT_BUGZILLA_PRODUCT_VERSION=20
REDHAT_SUPPORT_PRODUCT="Fedora" REDHAT_SUPPORT_PRODUCT="Fedora"
REDHAT_SUPPORT_PRODUCT_VERSION=20`), REDHAT_SUPPORT_PRODUCT_VERSION=20`),
},
}, },
}, }
}
func TestOsReleaseNamespaceDetector(t *testing.T) { namespace.TestDetector(t, &OsReleaseNamespaceDetector{}, testData)
namespace.TestNamespaceDetector(t, &OsReleaseNamespaceDetector{}, osReleaseOSTests)
} }

View File

@ -1,4 +1,4 @@
// Copyright 2015 clair authors // Copyright 2016 clair authors
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with 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" "github.com/coreos/clair/worker/detectors/namespace"
) )
var redhatReleaseTests = []namespace.NamespaceTest{
{
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"},
Data: map[string][]byte{
"etc/system-release": []byte(`CentOS Linux release 7.1.1503 (Core)`),
},
},
}
func TestRedhatReleaseNamespaceDetector(t *testing.T) { func TestRedhatReleaseNamespaceDetector(t *testing.T) {
namespace.TestNamespaceDetector(t, &RedhatReleaseNamespaceDetector{}, redhatReleaseTests) testData := []namespace.TestData{
{
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"},
Data: map[string][]byte{
"etc/system-release": []byte(`CentOS Linux release 7.1.1503 (Core)`),
},
},
}
namespace.TestDetector(t, &RedhatReleaseNamespaceDetector{}, testData)
} }

View File

@ -1,4 +1,4 @@
// Copyright 2015 clair authors // Copyright 2016 clair authors
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with 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 // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Package namespace implements utilities common to implementations of
// NamespaceDetector.
package namespace package namespace
import ( import (
@ -22,13 +24,22 @@ import (
"github.com/stretchr/testify/assert" "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 Data map[string][]byte
ExpectedNamespace database.Namespace ExpectedNamespace *database.Namespace
} }
func TestNamespaceDetector(t *testing.T, detector detectors.NamespaceDetector, tests []NamespaceTest) { // TestDetector runs a detector on each provided instance of TestData and
for _, test := range tests { // asserts the output to be equal to the expected output.
assert.Equal(t, test.ExpectedNamespace, *detector.Detect(test.Data)) 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)
}
} }
} }