forked from Shopify/kubeaudit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubeaudit_test.go
85 lines (64 loc) · 2.11 KB
/
kubeaudit_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
package kubeaudit_test
import (
"os"
"testing"
"github.com/Shopify/kubeaudit"
"github.com/Shopify/kubeaudit/auditors/all"
"github.com/Shopify/kubeaudit/config"
"github.com/Shopify/kubeaudit/internal/k8sinternal"
"github.com/Shopify/kubeaudit/internal/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
require := require.New(t)
allAuditors, err := all.Auditors(config.KubeauditConfig{})
require.NoError(err)
auditor, err := kubeaudit.New(allAuditors)
require.NoError(err)
assert.NotNil(t, auditor)
_, err = kubeaudit.New(nil)
require.NotNil(err)
}
func TestAuditLocal(t *testing.T) {
if os.Getenv("USE_KIND") == "false" {
return
}
require := require.New(t)
allAuditors, err := all.Auditors(config.KubeauditConfig{})
require.NoError(err)
auditor, err := kubeaudit.New(allAuditors)
require.NoError(err)
_, err = auditor.AuditLocal("", "", k8sinternal.ClientOptions{})
require.NoError(err)
_, err = auditor.AuditLocal("invalid_path", "", k8sinternal.ClientOptions{})
require.NotNil(err)
_, err = auditor.AuditLocal("", "invalid_context", k8sinternal.ClientOptions{})
require.NotNil(err)
}
func TestAuditCluster(t *testing.T) {
require := require.New(t)
allAuditors, err := all.Auditors(config.KubeauditConfig{})
require.NoError(err)
auditor, err := kubeaudit.New(allAuditors)
require.NoError(err)
_, err = auditor.AuditCluster(k8sinternal.ClientOptions{})
require.NotNil(err)
}
func TestUnknownResource(t *testing.T) {
// Make sure we produce only warning results for resources kubeaudit doesn't know how to audit
files := []string{"unknown_resource_type.yml", "custom_resource_definition.yml"}
allAuditors, err := all.Auditors(config.KubeauditConfig{})
require.NoError(t, err)
for _, file := range files {
t.Run(file, func(t *testing.T) {
_, report := test.FixSetupMultiple(t, "internal/test/fixtures", file, allAuditors)
require.NotNil(t, report)
for _, result := range report.Results() {
for _, auditResult := range result.GetAuditResults() {
assert.Equal(t, kubeaudit.Warn, auditResult.Severity)
}
}
})
}
}