Files
polaris/test/checks_test.go
Jordan Doig 63fd576d3e Add support for arbitrary Kinds (#505)
* Add basic flow

* Add arbitrary validator

* Pipe config through to resource provider

* Set arbitraries on resource provider

* Add arbitrary validation to fullaudit

* Add conf argument

* Fix resource setting from string

* PR updates

* Fix nil map error

* Delete lingering print, add pdb check, start implementing validator test

* move ingress to arbitrary

* fix compile

* refactor a bunch

* add tls tests

* tests passing

* resource provider helper

* refactor tests

* fix exemptions

* fix check test

* fix up resource creation from API

* fix init containers

* fix cronjob test

* fix pod tests

* combine controllers and-noncontrollers in resource provider

* delint

* add ingress backward compat

* fix tests

* reenable test

* rename a fn

* remove unused fn

* remove if

Co-authored-by: Robert Brennan <contact@rbren.io>
2021-03-26 08:29:59 -04:00

81 lines
1.9 KiB
Go

package test
import (
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/fairwindsops/polaris/pkg/config"
"github.com/fairwindsops/polaris/pkg/kube"
"github.com/fairwindsops/polaris/pkg/validator"
)
var testCases = []testCase{}
type testCase struct {
check string
input []byte
failure bool
}
func init() {
_, baseDir, _, _ := runtime.Caller(0)
baseDir = filepath.Dir(baseDir) + "/checks"
dirs, err := ioutil.ReadDir(baseDir)
if err != nil {
panic(err)
}
for _, dir := range dirs {
check := dir.Name()
checkDir := baseDir + "/" + check
cases, err := ioutil.ReadDir(checkDir)
if err != nil {
panic(err)
}
for _, tc := range cases {
body, err := ioutil.ReadFile(checkDir + "/" + tc.Name())
if err != nil {
panic(err)
}
testCases = append(testCases, testCase{
check: check,
input: body,
failure: strings.Contains(tc.Name(), "failure"),
})
}
}
}
func TestChecks(t *testing.T) {
for _, tc := range testCases {
res, err := kube.NewGenericResourceFromBytes(tc.input)
if err != nil {
fmt.Println("error parsing", string(tc.input))
panic(err)
}
c, err := config.Parse([]byte("checks:\n " + tc.check + ": danger"))
assert.NoError(t, err)
result, err := validator.ApplyAllSchemaChecks(&c, res)
assert.NoError(t, err)
summary := result.GetSummary()
total := summary.Successes + summary.Dangers
msg := fmt.Sprintf("Check %s ran %d times instead of 1", tc.check, total)
if assert.Equal(t, uint(1), total, msg) {
if tc.failure {
message := "Check " + tc.check + " passed unexpectedly"
assert.Equal(t, uint(0), summary.Successes, message)
assert.Equal(t, uint(1), summary.Dangers, message)
} else {
message := "Check " + tc.check + " failed unexpectedly"
assert.Equal(t, uint(1), summary.Successes, message)
assert.Equal(t, uint(0), summary.Dangers, message)
}
}
}
}