This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
/
util_test.go
107 lines (95 loc) · 2.81 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
package kubeaudit
import (
"bytes"
"encoding/json"
"testing"
"github.com/Shopify/kubeaudit/pkg/k8s"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
type logEntry struct {
AuditResultName string
Foo string
Level string `json:"level"`
ResourceKind string
ResourceApiVersion string
ResourceName string
ResourceNamespace string
}
func TestPrintResults(t *testing.T) {
report := Report{
results: []Result{
&WorkloadResult{
AuditResults: []*AuditResult{
newTestAuditResult(Error),
newTestAuditResult(Warn),
newTestAuditResult(Info),
},
Resource: &kubeResource{
object: k8s.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{
Rule: "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 := k8s.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()
}
}