clair/worker/detectors/feature/test.go

57 lines
1.9 KiB
Go
Raw Normal View History

// Copyright 2016 clair authors
2015-11-13 19:11:28 +00:00
//
// 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 feature implements utilities common to implementations of
// FeatureDetector.
2016-01-19 18:36:19 +00:00
package feature
2015-11-13 19:11:28 +00:00
import (
"io/ioutil"
"path/filepath"
2015-11-13 19:11:28 +00:00
"runtime"
"testing"
"github.com/coreos/clair/database"
"github.com/coreos/clair/worker/detectors"
"github.com/stretchr/testify/assert"
)
// TestData represents the data used to test an implementation of
// FeatureDetector.
type TestData struct {
2016-01-19 18:36:19 +00:00
Data map[string][]byte
FeatureVersions []database.FeatureVersion
2015-11-13 19:11:28 +00:00
}
// LoadFileForTest can be used in order to obtain the []byte contents of a file
// that is meant to be used for test data.
2016-01-19 18:36:19 +00:00
func LoadFileForTest(name string) []byte {
2015-11-13 19:11:28 +00:00
_, filename, _, _ := runtime.Caller(0)
d, _ := ioutil.ReadFile(filepath.Join(filepath.Dir(filename)) + "/" + name)
2015-11-13 19:11:28 +00:00
return d
}
// 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 {
2016-01-19 18:36:19 +00:00
assert.Contains(t, featureVersions, expectedFeatureVersion)
2015-11-13 19:11:28 +00:00
}
}
}
}