Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test fixtures, add a test for controllers #455

Merged
merged 9 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions pkg/kube/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func TestGetResourcesFromPath(t *testing.T) {
assert.Equal(t, 1, len(resources.Namespaces), "Should have a namespace")
assert.Equal(t, "two", resources.Namespaces[0].ObjectMeta.Name)

assert.Equal(t, 8, len(resources.Controllers), "Should have eight controllers")
assert.Equal(t, 9, len(resources.Controllers), "Should have eight controllers")
namespaceCount := map[string]int{}
for _, controller := range resources.Controllers {
namespaceCount[controller.ObjectMeta.GetNamespace()]++
}
assert.Equal(t, 7, namespaceCount[""], "Should have seven controller in default namespace")
assert.Equal(t, 8, namespaceCount[""], "Should have seven controller in default namespace")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should get rid of these messages, both of them are out of sync now.

assert.Equal(t, 1, namespaceCount["two"], "Should have one controller in namespace 'two'")
}

Expand Down Expand Up @@ -87,10 +87,7 @@ func TestAddResourcesFromReader(t *testing.T) {
}

func TestGetResourceFromAPI(t *testing.T) {
k8s, dynamicInterface := test.SetupTestAPI()
k8s = test.SetupAddControllers(context.Background(), k8s, "test")
// TODO find a way to mock out the dynamic client
// and create fake pods in order to find all of the controllers.
k8s, dynamicInterface := test.SetupTestAPI(test.GetMockControllers("test")...)
resources, err := CreateResourceProviderFromAPI(context.Background(), k8s, "test", &dynamicInterface)
assert.Equal(t, nil, err, "Error should be nil")

Expand All @@ -99,7 +96,19 @@ func TestGetResourceFromAPI(t *testing.T) {
assert.IsType(t, time.Now(), resources.CreationTime, "Creation time should be set")

assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")
assert.Equal(t, 1, len(resources.Controllers), "Should have 1 controller")

assert.Equal(t, "", resources.Controllers[0].ObjectMeta.GetName())
assert.Equal(t, 5, len(resources.Controllers), "Should have 5 controllers")

expectedNames := map[string]bool{
"deploy": false,
"job": false,
"cronjob": false,
"statefulset": false,
"daemonset": false,
}
for _, ctrl := range resources.Controllers {
expectedNames[ctrl.ObjectMeta.GetName()] = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this way of handling. I wonder if the assert library we use has something built in already or if we could contribute to it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's something like it, but it expects the same object type

}
for name, val := range expectedNames {
assert.Equal(t, true, val, name)
}
}
20 changes: 20 additions & 0 deletions pkg/kube/test_files/test_1/deployment2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test-deployment-2
spec:
replicas: 2
selector:
matchLabels:
app: test-deployment
template:
metadata:
labels:
app: test-deployment
spec:
containers:
- name: ubuntu
image: ubuntu
ports:
- containerPort: 3000

81 changes: 50 additions & 31 deletions pkg/validator/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package validator

import (
"context"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -59,41 +60,59 @@ func TestValidateController(t *testing.T) {
}

func TestControllerLevelChecks(t *testing.T) {
c := conf.Configuration{
Checks: map[string]conf.Severity{
"multipleReplicasForDeployment": conf.SeverityDanger,
},
}
resources, err := kube.CreateResourceProviderFromPath("../kube/test_files/test_1")

assert.Equal(t, nil, err, "Error should be nil")

assert.Equal(t, 8, len(resources.Controllers), "Should have eight controllers")

expectedSum := CountSummary{
Successes: uint(0),
Warnings: uint(0),
Dangers: uint(1),
}

expectedResults := ResultSet{
"multipleReplicasForDeployment": {ID: "multipleReplicasForDeployment", Message: "Only one replica is scheduled", Success: false, Severity: "danger", Category: "Reliability"},
}

for _, controller := range resources.Controllers {
if controller.Kind == "Deployment" && controller.ObjectMeta.GetName() == "test-deployment" {
actualResult, err := ValidateController(context.Background(), &c, controller)
if err != nil {
panic(err)
testResources := func(res *kube.ResourceProvider) {
c := conf.Configuration{
Checks: map[string]conf.Severity{
"multipleReplicasForDeployment": conf.SeverityDanger,
},
}
expectedResult := ResultMessage{
ID: "multipleReplicasForDeployment",
Severity: "danger",
Category: "Reliability",
}
for _, controller := range res.Controllers {
if controller.Kind == "Deployment" {
actualResult, err := ValidateController(context.Background(), &c, controller)
if err != nil {
panic(err)
}
if controller.ObjectMeta.GetName() == "test-deployment-2" {
expectedResult.Success = true
expectedResult.Message = "Multiple replicas are scheduled"
} else if controller.ObjectMeta.GetName() == "test-deployment" {
expectedResult.Success = false
expectedResult.Message = "Only one replica is scheduled"
}
expectedResults := ResultSet{
"multipleReplicasForDeployment": expectedResult,
}

assert.Equal(t, "Deployment", actualResult.Kind)
assert.Equal(t, 1, len(actualResult.Results), "should be equal")
assert.EqualValues(t, expectedResults, actualResult.Results, controller.ObjectMeta.GetName())
}

assert.Equal(t, "Deployment", actualResult.Kind)
assert.Equal(t, 1, len(actualResult.Results), "should be equal")
assert.EqualValues(t, expectedSum, actualResult.GetSummary())
assert.EqualValues(t, expectedResults, actualResult.Results)
}
}

res, err := kube.CreateResourceProviderFromPath("../kube/test_files/test_1")
assert.Equal(t, nil, err, "Error should be nil")
assert.Equal(t, 9, len(res.Controllers), "Should have eight controllers")
testResources(res)

replicaSpec := map[string]interface{}{"replicas": 2}
b, err := json.Marshal(replicaSpec)
assert.NoError(t, err)
err = json.Unmarshal(b, &replicaSpec)

d1, p1 := test.MockDeploy("test", "test-deployment")
d2, p2 := test.MockDeploy("test", "test-deployment-2")
d2.Object["spec"] = replicaSpec
k8s, dynamicClient := test.SetupTestAPI(&d1, &p1, &d2, &p2)
res, err = kube.CreateResourceProviderFromAPI(context.Background(), k8s, "test", &dynamicClient)
assert.Equal(t, err, nil, "error should be nil")
assert.Equal(t, 2, len(res.Controllers), "Should have two controllers")
testResources(res)
}

func TestSkipHealthChecks(t *testing.T) {
Expand Down
38 changes: 22 additions & 16 deletions pkg/validator/fullaudit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ import (
)

func TestGetTemplateData(t *testing.T) {
k8s, dynamicClient := test.SetupTestAPI()
k8s = test.SetupAddControllers(context.Background(), k8s, "test")
k8s = test.SetupAddExtraControllerVersions(context.Background(), k8s, "test-extra")
// TODO figure out how to mock out dynamic client.
// and add in pods for all controllers to fill out tests.
k8s, dynamicClient := test.SetupTestAPI(test.GetMockControllers("test")...)
resources, err := kube.CreateResourceProviderFromAPI(context.Background(), k8s, "test", &dynamicClient)
assert.Equal(t, err, nil, "error should be nil")
assert.Equal(t, 5, len(resources.Controllers))

c := conf.Configuration{
Checks: map[string]conf.Severity{
Expand All @@ -28,29 +25,38 @@ func TestGetTemplateData(t *testing.T) {

sum := CountSummary{
Successes: uint(0),
Warnings: uint(1),
Dangers: uint(1),
Warnings: uint(3),
Dangers: uint(3),
}

actualAudit, err := RunAudit(context.Background(), c, resources)

assert.Equal(t, err, nil, "error should be nil")

assert.EqualValues(t, sum, actualAudit.GetSummary())
assert.Equal(t, actualAudit.SourceType, "Cluster", "should be from a cluster")
assert.Equal(t, actualAudit.SourceName, "test", "should be from a cluster")

expected := []struct {
expectedResults := []struct {
kind string
results int
}{
{kind: "Pod", results: 2},
{kind: "StatefulSet", results: 2},
{kind: "DaemonSet", results: 2},
{kind: "Deployment", results: 2},
{kind: "Job", results: 0},
{kind: "CronJob", results: 0},
}

assert.Equal(t, len(expected), len(actualAudit.Results))
for idx, result := range actualAudit.Results {
assert.Equal(t, expected[idx].kind, result.Kind)
assert.Equal(t, 1, len(result.PodResult.ContainerResults))
assert.Equal(t, expected[idx].results, len(result.PodResult.ContainerResults[0].Results))
assert.Equal(t, len(expectedResults), len(actualAudit.Results))
for _, result := range actualAudit.Results {
found := false
for _, expected := range expectedResults {
if expected.kind != result.Kind {
continue
}
found = true
assert.Equal(t, 1, len(result.PodResult.ContainerResults))
assert.Equal(t, expected.results, len(result.PodResult.ContainerResults[0].Results))
}
assert.Equal(t, found, true)
}
}
10 changes: 0 additions & 10 deletions pkg/validator/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ func TestValidatePod(t *testing.T) {
},
}

k8s, _ := test.SetupTestAPI()
k8s = test.SetupAddControllers(context.Background(), k8s, "test")
p := test.MockPod()
deployment, err := kube.NewGenericWorkloadFromPod(p, nil)
assert.NoError(t, err)
Expand Down Expand Up @@ -73,8 +71,6 @@ func TestInvalidIPCPod(t *testing.T) {
},
}

k8s, _ := test.SetupTestAPI()
k8s = test.SetupAddControllers(context.Background(), k8s, "test")
p := test.MockPod()
p.Spec.HostIPC = true
workload, err := kube.NewGenericWorkloadFromPod(p, nil)
Expand Down Expand Up @@ -110,8 +106,6 @@ func TestInvalidNeworkPod(t *testing.T) {
},
}

k8s, _ := test.SetupTestAPI()
k8s = test.SetupAddControllers(context.Background(), k8s, "test")
p := test.MockPod()
p.Spec.HostNetwork = true
workload, err := kube.NewGenericWorkloadFromPod(p, nil)
Expand Down Expand Up @@ -148,8 +142,6 @@ func TestInvalidPIDPod(t *testing.T) {
},
}

k8s, _ := test.SetupTestAPI()
k8s = test.SetupAddControllers(context.Background(), k8s, "test")
p := test.MockPod()
p.Spec.HostPID = true
workload, err := kube.NewGenericWorkloadFromPod(p, nil)
Expand Down Expand Up @@ -192,8 +184,6 @@ func TestExemption(t *testing.T) {
},
}

k8s, _ := test.SetupTestAPI()
k8s = test.SetupAddControllers(context.Background(), k8s, "test")
p := test.MockPod()
p.Spec.HostIPC = true
p.ObjectMeta = metav1.ObjectMeta{
Expand Down
Loading