detectors/feature: consistent naming and godoc

This commit is contained in:
Jimmy Zelinskie 2016-11-11 15:14:37 -05:00
parent 1d5a9ddd3c
commit e4b5930f77
3 changed files with 58 additions and 50 deletions

View File

@ -22,7 +22,8 @@ import (
"github.com/coreos/clair/worker/detectors/feature" "github.com/coreos/clair/worker/detectors/feature"
) )
var dpkgPackagesTests = []feature.FeatureVersionTest{ func TestDpkgFeatureDetection(t *testing.T) {
testData := []feature.TestData{
// Test an Ubuntu dpkg status file // Test an Ubuntu dpkg status file
{ {
FeatureVersions: []database.FeatureVersion{ FeatureVersions: []database.FeatureVersion{
@ -46,6 +47,5 @@ var dpkgPackagesTests = []feature.FeatureVersionTest{
}, },
} }
func TestDpkgFeaturesDetector(t *testing.T) { feature.TestDetector(t, &DpkgFeaturesDetector{}, testData)
feature.TestFeaturesDetector(t, &DpkgFeaturesDetector{}, dpkgPackagesTests)
} }

View File

@ -22,7 +22,8 @@ import (
"github.com/coreos/clair/worker/detectors/feature" "github.com/coreos/clair/worker/detectors/feature"
) )
var rpmPackagesTests = []feature.FeatureVersionTest{ func TestRpmFeatureDetection(t *testing.T) {
testData := []feature.TestData{
// Test a CentOS 7 RPM database // Test a CentOS 7 RPM database
// Memo: Use the following command on a RPM-based system to shrink a database: rpm -qa --qf "%{NAME}\n" |tail -n +3| xargs rpm -e --justdb // Memo: Use the following command on a RPM-based system to shrink a database: rpm -qa --qf "%{NAME}\n" |tail -n +3| xargs rpm -e --justdb
{ {
@ -44,6 +45,5 @@ var rpmPackagesTests = []feature.FeatureVersionTest{
}, },
} }
func TestRpmFeaturesDetector(t *testing.T) { feature.TestDetector(t, &RpmFeaturesDetector{}, testData)
feature.TestFeaturesDetector(t, &RpmFeaturesDetector{}, rpmPackagesTests)
} }

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 feature implements utilities common to implementations of
// FeatureDetector.
package feature package feature
import ( import (
@ -25,22 +27,28 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
type FeatureVersionTest struct { // TestData represents the data used to test an implementation of
FeatureVersions []database.FeatureVersion // FeatureDetector.
type TestData struct {
Data map[string][]byte Data map[string][]byte
FeatureVersions []database.FeatureVersion
} }
// LoadFileForTest can be used in order to obtain the []byte contents of a file
// that is meant to be used for test data.
func LoadFileForTest(name string) []byte { func LoadFileForTest(name string) []byte {
_, filename, _, _ := runtime.Caller(0) _, filename, _, _ := runtime.Caller(0)
d, _ := ioutil.ReadFile(filepath.Join(filepath.Dir(filename)) + "/" + name) d, _ := ioutil.ReadFile(filepath.Join(filepath.Dir(filename)) + "/" + name)
return d return d
} }
func TestFeaturesDetector(t *testing.T, detector detectors.FeaturesDetector, tests []FeatureVersionTest) { // TestDetector runs a detector on each provided instance of TestData and
for _, test := range tests { // asserts the ouput to be equal to the expected output.
featureVersions, err := detector.Detect(test.Data) func TestDetector(t *testing.T, detector detectors.FeaturesDetector, testData []TestData) {
if assert.Nil(t, err) && assert.Len(t, featureVersions, len(test.FeatureVersions)) { for _, td := range testData {
for _, expectedFeatureVersion := range test.FeatureVersions { featureVersions, err := detector.Detect(td.Data)
if assert.Nil(t, err) && assert.Len(t, featureVersions, len(td.FeatureVersions)) {
for _, expectedFeatureVersion := range td.FeatureVersions {
assert.Contains(t, featureVersions, expectedFeatureVersion) assert.Contains(t, featureVersions, expectedFeatureVersion)
} }
} }