@@ -16,11 +16,11 @@ func Test_PolicyEval(t *testing.T) {
1616 want bool
1717 wantError error
1818 }{
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 ^" )},
2424 }
2525 for _ , tt := range tests {
2626 t .Run (tt .name , func (t * testing.T ) {
@@ -32,42 +32,18 @@ func Test_PolicyEval(t *testing.T) {
3232 if err != nil {
3333 t .Fatal (err )
3434 }
35- got , err := NewPolicyEval ().EvaluatePolicy ([] string { "deny" } , string (policy ), string (data ))
35+ got , err := NewPolicyEval ().EvaluatePolicy (tt . policyRule , string (policy ), string (data ))
3636 if err != nil {
3737 goErr := err .Error ()
3838 if goErr != tt .wantError .Error () {
3939 t .Fatal (err )
4040 }
4141 }
42- if len ( got ) > 0 {
42+ if err == nil {
4343 if got [0 ].Value != tt .want {
4444 t .Errorf ("Test_PolicyEval() = %v, want %v" , got [0 ], tt .want )
4545 }
4646 }
4747 })
4848 }
4949}
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