@@ -16,11 +16,11 @@ func Test_PolicyEval(t *testing.T) {
16
16
want bool
17
17
wantError error
18
18
}{
19
- {name : "test validate policy deny pod name json format" , data : "./fixture/pod.json" , pkgName : "example" , policyRule : []string {"deny" }, policy : "./fixture/pod_policy_deny" , want : true , wantError : nil },
20
- {name : "test validate policy deny pod name yaml format" , data : "./fixture/pod.yaml" , pkgName : "example" , policyRule : []string {"deny" }, policy : "./fixture/pod_policy_deny" , want : true , wantError : nil },
21
- {name : "test validate policy allow pod name" , data : "./fixture/allow_pod.json" , pkgName : "example" , policyRule : []string {"deny" }, policy : "./fixture/pod_policy_deny" , want : false , wantError : nil },
22
- {name : "test validate policy bad data" , data : "./fixture/badJson.json" , pkgName : "example" , policyRule : []string {"deny" }, policy : "./fixture/pod_policy_deny" , want : false , wantError : nil },
23
- {name : "test validate policy bad policy" , data : "./fixture/badJson.json" , pkgName : "example" , policyRule : []string {"deny" }, policy : "./fixture/pod_policy_deny_bad" , want : false , wantError : fmt .Errorf ("1 error occurred: .rego:5: rego_parse_error: unexpected } token\n \t }\n \t ^" )},
19
+ {name : "test validate policy deny pod name json format" , data : "./fixture/pod.json" , policyRule : []string {"example. deny" }, policy : "./fixture/pod_policy_deny" , want : true , wantError : nil },
20
+ {name : "test validate policy deny pod name yaml format" , data : "./fixture/pod.yaml" , policyRule : []string {"example. deny" }, policy : "./fixture/pod_policy_deny" , want : true , wantError : nil },
21
+ {name : "test validate policy allow pod name" , data : "./fixture/allow_pod.json" , policyRule : []string {"example. deny" }, policy : "./fixture/pod_policy_deny" , want : false , wantError : nil },
22
+ {name : "test validate policy bad data" , data : "./fixture/badJson.json" , policyRule : []string {"example. deny" }, policy : "./fixture/pod_policy_deny" , want : false , wantError : nil },
23
+ {name : "test validate policy bad policy" , data : "./fixture/badJson.json" , policyRule : []string {"example. deny" }, policy : "./fixture/pod_policy_deny_bad" , want : false , wantError : fmt .Errorf ("1 error occurred: eval .rego:5: rego_parse_error: unexpected } token\n \t }\n \t ^" )},
24
24
}
25
25
for _ , tt := range tests {
26
26
t .Run (tt .name , func (t * testing.T ) {
@@ -32,42 +32,18 @@ func Test_PolicyEval(t *testing.T) {
32
32
if err != nil {
33
33
t .Fatal (err )
34
34
}
35
- got , err := NewPolicyEval ().EvaluatePolicy ([] string { "deny" } , string (policy ), string (data ))
35
+ got , err := NewPolicyEval ().EvaluatePolicy (tt . policyRule , string (policy ), string (data ))
36
36
if err != nil {
37
37
goErr := err .Error ()
38
38
if goErr != tt .wantError .Error () {
39
39
t .Fatal (err )
40
40
}
41
41
}
42
- if len ( got ) > 0 {
42
+ if err == nil {
43
43
if got [0 ].Value != tt .want {
44
44
t .Errorf ("Test_PolicyEval() = %v, want %v" , got [0 ], tt .want )
45
45
}
46
46
}
47
47
})
48
48
}
49
49
}
50
-
51
- func Test_DetectPkgName (t * testing.T ) {
52
- tests := []struct {
53
- name string
54
- policy string
55
- want string
56
- }{
57
- {name : "detect pkg name exist" , policy : "./fixture/pod_policy_deny" , want : "example" },
58
- {name : "detect pkg name not exist" , policy : "./fixture/pod_policy_deny_bad" , want : "" },
59
- }
60
- for _ , tt := range tests {
61
- t .Run (tt .name , func (t * testing.T ) {
62
- policy , err := ioutil .ReadFile (tt .policy )
63
- if err != nil {
64
- t .Fatal (err )
65
- }
66
- pe := & policyEval {}
67
- got := pe .detectPkgName (string (policy ))
68
- if got != tt .want {
69
- t .Errorf ("Test_DetectPkgName() = %v, want %v" , got [0 ], tt .want )
70
- }
71
- })
72
- }
73
- }
0 commit comments