@@ -14,6 +14,7 @@ package example
1414
1515deny[msg] {
1616 input.apiVersion == "badVersion"
17+ input.extraKey == "extraBadValue"
1718 msg = "Cannot have bad api version"
1819}`
1920
@@ -25,6 +26,18 @@ default allow = true
2526allow = false {
2627 input.apiVersion == "badVersion"
2728}`
29+
30+ goodVersionResourceStr = `
31+ apiVersion: goodVersion
32+ kind: Deployment
33+ metadata:
34+ name: test`
35+
36+ badVersionResourceStr = `
37+ apiVersion: badVersion
38+ kind: Deployment
39+ metadata:
40+ name: test`
2841)
2942
3043func TestPolicyChecker (t * testing.T ) {
@@ -43,8 +56,29 @@ func TestPolicyChecker(t *testing.T) {
4356 Contents : denyPolicyStr ,
4457 Package : "example" ,
4558 Result : "deny" ,
59+ ExtraFields : map [string ]interface {}{
60+ "extraKey" : "extraBadValue" ,
61+ },
62+ },
63+ resource : MakeResource ("test/path" , []byte (goodVersionResourceStr ), 0 ),
64+ expected : CheckResult {
65+ CheckType : CheckTypeOPA ,
66+ CheckName : "testDenyPolicy" ,
67+ Status : StatusValid ,
68+ Message : "Policy returned 0 deny reasons" ,
69+ },
70+ },
71+ {
72+ policyModule : PolicyModule {
73+ Name : "testDenyPolicy" ,
74+ Contents : denyPolicyStr ,
75+ Package : "example" ,
76+ Result : "deny" ,
77+ ExtraFields : map [string ]interface {}{
78+ "extraKey" : "goodValue" ,
79+ },
4680 },
47- resource : MakeResource ("test/path" , []byte ("apiVersion: goodVersion" ), 0 ),
81+ resource : MakeResource ("test/path" , []byte (badVersionResourceStr ), 0 ),
4882 expected : CheckResult {
4983 CheckType : CheckTypeOPA ,
5084 CheckName : "testDenyPolicy" ,
@@ -58,8 +92,11 @@ func TestPolicyChecker(t *testing.T) {
5892 Contents : denyPolicyStr ,
5993 Package : "example" ,
6094 Result : "deny" ,
95+ ExtraFields : map [string ]interface {}{
96+ "extraKey" : "extraBadValue" ,
97+ },
6198 },
62- resource : MakeResource ("test/path" , []byte ("apiVersion: badVersion" ), 0 ),
99+ resource : MakeResource ("test/path" , []byte (badVersionResourceStr ), 0 ),
63100 expected : CheckResult {
64101 CheckType : CheckTypeOPA ,
65102 CheckName : "testDenyPolicy" ,
@@ -74,7 +111,7 @@ func TestPolicyChecker(t *testing.T) {
74111 Package : "example" ,
75112 Result : "allow" ,
76113 },
77- resource : MakeResource ("test/path" , []byte ("apiVersion: goodVersion" ), 0 ),
114+ resource : MakeResource ("test/path" , []byte (goodVersionResourceStr ), 0 ),
78115 expected : CheckResult {
79116 CheckType : CheckTypeOPA ,
80117 CheckName : "testAllowPolicy" ,
@@ -89,14 +126,29 @@ func TestPolicyChecker(t *testing.T) {
89126 Package : "example" ,
90127 Result : "allow" ,
91128 },
92- resource : MakeResource ("test/path" , []byte ("apiVersion: badVersion" ), 0 ),
129+ resource : MakeResource ("test/path" , []byte (badVersionResourceStr ), 0 ),
93130 expected : CheckResult {
94131 CheckType : CheckTypeOPA ,
95132 CheckName : "testAllowPolicy" ,
96133 Status : StatusInvalid ,
97134 Message : "Policy returned allowed = false" ,
98135 },
99136 },
137+ {
138+ policyModule : PolicyModule {
139+ Name : "testAllowPolicy" ,
140+ Contents : allowPolicyStr ,
141+ Package : "example" ,
142+ Result : "allow" ,
143+ },
144+ resource : MakeResource ("test/path" , []byte ("" ), 0 ),
145+ expected : CheckResult {
146+ CheckType : CheckTypeOPA ,
147+ CheckName : "testAllowPolicy" ,
148+ Status : StatusEmpty ,
149+ Message : "No resource content" ,
150+ },
151+ },
100152 }
101153
102154 for _ , testCase := range testCases {
0 commit comments