forked from Shopify/kubeaudit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
126 lines (111 loc) · 3.6 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package kubeaudit
import (
"bytes"
"encoding/json"
"testing"
"github.com/Shopify/kubeaudit/internal/k8s"
"github.com/Shopify/kubeaudit/k8stypes"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime"
fakeclientset "k8s.io/client-go/kubernetes/fake"
)
type logEntry struct {
AuditResultName string
Foo string
Level string `json:"level"`
ResourceKind string
ResourceApiVersion string
ResourceName string
ResourceNamespace string
}
func TestGetResourcesFromClientset(t *testing.T) {
resources := []runtime.Object{k8stypes.NewDeployment(), k8stypes.NewNamespace()}
expected := make([]KubeResource, 0, len(resources))
for _, resource := range resources {
expected = append(expected, &kubeResource{object: resource})
}
got := getResourcesFromClientset(fakeclientset.NewSimpleClientset(resources...), k8s.ClientOptions{})
assert.Len(t, got, len(expected), "Got an unexpected number of resources from clientset")
for i, resource := range got {
assert.Equal(t, expected[i].Object().GetObjectKind().GroupVersionKind().Kind,
resource.Object().GetObjectKind().GroupVersionKind().Kind)
}
}
func TestPrintResults(t *testing.T) {
report := Report{
results: []Result{
&workloadResult{
AuditResults: []*AuditResult{
newTestAuditResult(Error),
newTestAuditResult(Warn),
newTestAuditResult(Info),
},
Resource: &kubeResource{
object: k8stypes.NewPod(),
},
},
},
}
out := bytes.NewBuffer(nil)
writerOption := WithWriter(out)
formatterOption := WithFormatter(&log.JSONFormatter{})
// Error results only
report.PrintResults(writerOption, WithMinSeverity(Error), formatterOption)
assert.Equal(t, 1, bytes.Count(out.Bytes(), []byte{'\n'}))
out.Reset()
// Error and warn results
report.PrintResults(writerOption, WithMinSeverity(Warn), formatterOption)
assert.Equal(t, 2, bytes.Count(out.Bytes(), []byte{'\n'}))
out.Reset()
// Error, warn, and info results
report.PrintResults(writerOption, WithMinSeverity(Info), formatterOption)
assert.Equal(t, 3, bytes.Count(out.Bytes(), []byte{'\n'}))
}
func newTestAuditResult(severity SeverityLevel) *AuditResult {
return &AuditResult{
Name: "MyAuditResult",
Severity: severity,
Metadata: Metadata{"Foo": "bar"},
}
}
func TestLogAuditResult(t *testing.T) {
for _, severity := range []SeverityLevel{Error, Warn, Info} {
// Send all log output as JSON to this byte buffer
out := bytes.NewBuffer(nil)
resource := k8stypes.NewDeployment()
resource.Name = "mydeployment"
resource.Namespace = "mynamespace"
auditResult := newTestAuditResult(severity)
report := &Report{
results: []Result{
&workloadResult{
AuditResults: []*AuditResult{
auditResult,
},
Resource: &kubeResource{
object: resource,
},
},
},
}
expectedApiVersion, expectedKind := resource.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
expected := logEntry{
AuditResultName: "MyAuditResult",
Level: severity.String(),
Foo: auditResult.Metadata["Foo"],
ResourceKind: expectedKind,
ResourceApiVersion: expectedApiVersion,
ResourceName: resource.GetName(),
ResourceNamespace: resource.GetNamespace(),
}
// This writes the log to the variable out, parses the JSON into the logEntry struct, and checks the struct
printer := NewPrinter(WithWriter(out), WithFormatter(&log.JSONFormatter{}))
printer.PrintReport(report)
got := logEntry{}
err := json.Unmarshal(out.Bytes(), &got)
assert.NoError(t, err)
assert.Equal(t, expected, got)
out.Reset()
}
}