From e4b5930f7769083767005d8e1730be81bf9eab8f Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Fri, 11 Nov 2016 15:14:37 -0500 Subject: [PATCH] detectors/feature: consistent naming and godoc --- worker/detectors/feature/dpkg/dpkg_test.go | 44 +++++++++++----------- worker/detectors/feature/rpm/rpm_test.go | 40 ++++++++++---------- worker/detectors/feature/test.go | 24 ++++++++---- 3 files changed, 58 insertions(+), 50 deletions(-) diff --git a/worker/detectors/feature/dpkg/dpkg_test.go b/worker/detectors/feature/dpkg/dpkg_test.go index 94fd34d4..d6f26d8c 100644 --- a/worker/detectors/feature/dpkg/dpkg_test.go +++ b/worker/detectors/feature/dpkg/dpkg_test.go @@ -22,30 +22,30 @@ import ( "github.com/coreos/clair/worker/detectors/feature" ) -var dpkgPackagesTests = []feature.FeatureVersionTest{ - // Test an Ubuntu dpkg status file - { - FeatureVersions: []database.FeatureVersion{ - // Two packages from this source are installed, it should only appear one time - { - Feature: database.Feature{Name: "pam"}, - Version: types.NewVersionUnsafe("1.1.8-3.1ubuntu3"), +func TestDpkgFeatureDetection(t *testing.T) { + testData := []feature.TestData{ + // Test an Ubuntu dpkg status file + { + FeatureVersions: []database.FeatureVersion{ + // Two packages from this source are installed, it should only appear one time + { + Feature: database.Feature{Name: "pam"}, + Version: types.NewVersionUnsafe("1.1.8-3.1ubuntu3"), + }, + { + Feature: database.Feature{Name: "makedev"}, // The source name and the package name are equals + Version: types.NewVersionUnsafe("2.3.1-93ubuntu1"), // The version comes from the "Version:" line + }, + { + Feature: database.Feature{Name: "gcc-5"}, + Version: types.NewVersionUnsafe("5.1.1-12ubuntu1"), // The version comes from the "Source:" line + }, }, - { - Feature: database.Feature{Name: "makedev"}, // The source name and the package name are equals - Version: types.NewVersionUnsafe("2.3.1-93ubuntu1"), // The version comes from the "Version:" line - }, - { - Feature: database.Feature{Name: "gcc-5"}, - Version: types.NewVersionUnsafe("5.1.1-12ubuntu1"), // The version comes from the "Source:" line + Data: map[string][]byte{ + "var/lib/dpkg/status": feature.LoadFileForTest("dpkg/testdata/status"), }, }, - Data: map[string][]byte{ - "var/lib/dpkg/status": feature.LoadFileForTest("dpkg/testdata/status"), - }, - }, -} + } -func TestDpkgFeaturesDetector(t *testing.T) { - feature.TestFeaturesDetector(t, &DpkgFeaturesDetector{}, dpkgPackagesTests) + feature.TestDetector(t, &DpkgFeaturesDetector{}, testData) } diff --git a/worker/detectors/feature/rpm/rpm_test.go b/worker/detectors/feature/rpm/rpm_test.go index adb5adb4..5e359aba 100644 --- a/worker/detectors/feature/rpm/rpm_test.go +++ b/worker/detectors/feature/rpm/rpm_test.go @@ -22,28 +22,28 @@ import ( "github.com/coreos/clair/worker/detectors/feature" ) -var rpmPackagesTests = []feature.FeatureVersionTest{ - // 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 - { - FeatureVersions: []database.FeatureVersion{ - // Two packages from this source are installed, it should only appear once - { - Feature: database.Feature{Name: "centos-release"}, - Version: types.NewVersionUnsafe("7-1.1503.el7.centos.2.8"), +func TestRpmFeatureDetection(t *testing.T) { + testData := []feature.TestData{ + // 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 + { + FeatureVersions: []database.FeatureVersion{ + // Two packages from this source are installed, it should only appear once + { + Feature: database.Feature{Name: "centos-release"}, + Version: types.NewVersionUnsafe("7-1.1503.el7.centos.2.8"), + }, + // Two packages from this source are installed, it should only appear once + { + Feature: database.Feature{Name: "filesystem"}, + Version: types.NewVersionUnsafe("3.2-18.el7"), + }, }, - // Two packages from this source are installed, it should only appear once - { - Feature: database.Feature{Name: "filesystem"}, - Version: types.NewVersionUnsafe("3.2-18.el7"), + Data: map[string][]byte{ + "var/lib/rpm/Packages": feature.LoadFileForTest("rpm/testdata/Packages"), }, }, - Data: map[string][]byte{ - "var/lib/rpm/Packages": feature.LoadFileForTest("rpm/testdata/Packages"), - }, - }, -} + } -func TestRpmFeaturesDetector(t *testing.T) { - feature.TestFeaturesDetector(t, &RpmFeaturesDetector{}, rpmPackagesTests) + feature.TestDetector(t, &RpmFeaturesDetector{}, testData) } diff --git a/worker/detectors/feature/test.go b/worker/detectors/feature/test.go index fe9f3d55..45335d7d 100644 --- a/worker/detectors/feature/test.go +++ b/worker/detectors/feature/test.go @@ -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 feature implements utilities common to implementations of +// FeatureDetector. package feature import ( @@ -25,22 +27,28 @@ import ( "github.com/stretchr/testify/assert" ) -type FeatureVersionTest struct { - FeatureVersions []database.FeatureVersion +// TestData represents the data used to test an implementation of +// FeatureDetector. +type TestData struct { 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 { _, filename, _, _ := runtime.Caller(0) d, _ := ioutil.ReadFile(filepath.Join(filepath.Dir(filename)) + "/" + name) return d } -func TestFeaturesDetector(t *testing.T, detector detectors.FeaturesDetector, tests []FeatureVersionTest) { - for _, test := range tests { - featureVersions, err := detector.Detect(test.Data) - if assert.Nil(t, err) && assert.Len(t, featureVersions, len(test.FeatureVersions)) { - for _, expectedFeatureVersion := range test.FeatureVersions { +// TestDetector runs a detector on each provided instance of TestData and +// asserts the ouput to be equal to the expected output. +func TestDetector(t *testing.T, detector detectors.FeaturesDetector, testData []TestData) { + for _, td := range testData { + 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) } }