forked from doitintl/kube-no-trouble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_custom_test.go
61 lines (49 loc) · 1.47 KB
/
rules_custom_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
package test
import (
"testing"
"github.com/doitintl/kube-no-trouble/pkg/collector"
"github.com/doitintl/kube-no-trouble/pkg/judge"
"github.com/doitintl/kube-no-trouble/pkg/rules"
"k8s.io/apimachinery/pkg/runtime/schema"
)
func TestRegoCustom(t *testing.T) {
manifestName := "../fixtures/issuer-v1alpha2.yaml"
expectedKind := "Issuer"
additionalKinds := []schema.GroupVersionKind{
schema.GroupVersionKind{
Group: "cert-manager.io",
Version: "v1alpha2",
Kind: "Issuer",
},
}
c, err := collector.NewFileCollector(
&collector.FileOpts{Filenames: []string{manifestName}},
)
if err != nil {
t.Errorf("expected to succeed for %s, failed: %s", manifestName, err)
}
manifests, err := c.Get()
if err != nil {
t.Errorf("expected to succeed for %s, failed: %s", manifestName, err)
} else if len(manifests) == 0 {
t.Errorf("expected to get some manifests, got %d", len(manifests))
}
loadedRules, err := rules.FetchRegoRules(additionalKinds)
if err != nil {
t.Errorf("failed to load rules: %s", err)
}
judge, err := judge.NewRegoJudge(&judge.RegoOpts{}, loadedRules)
if err != nil {
t.Errorf("failed to initialize judge: %s", err)
}
results, err := judge.Eval(manifests)
if err != nil {
t.Errorf("failed to evaluate rules: %s", err)
}
if len(results) == 0 {
t.Errorf("expected to get some manifests, got %d", len(results))
}
if results[0].Kind != expectedKind {
t.Errorf("expected to get %s result, got %s", expectedKind, results[0].Kind)
}
}