-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add priority class check, some test infra (#342)
* add check for priority-class * add test message * lint
- Loading branch information
Showing
8 changed files
with
144 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
successMessage: Priority class has been set | ||
failureMessage: Priority class should be set | ||
category: Security | ||
target: Pod | ||
schema: | ||
'$schema': http://json-schema.org/draft-07/schema | ||
type: object | ||
required: | ||
- priorityClassName |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: nginx | ||
labels: | ||
env: test | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx | ||
imagePullPolicy: IfNotPresent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: nginx | ||
labels: | ||
env: test | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx | ||
imagePullPolicy: IfNotPresent | ||
priorityClassName: high-priority |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package test | ||
|
||
import ( | ||
"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 { | ||
workload, err := kube.GetWorkloadFromBytes(tc.input) | ||
assert.NoError(t, err) | ||
c, err := config.Parse([]byte("checks:\n " + tc.check + ": danger")) | ||
assert.NoError(t, err) | ||
result, err := validator.ValidateController(&c, *workload) | ||
assert.NoError(t, err) | ||
summary := result.GetSummary() | ||
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) | ||
} | ||
} | ||
} |