@@ -57,13 +57,41 @@ func TestK8sEventSink(t *testing.T) {
57
57
Labels : map [string ]string {},
58
58
}
59
59
60
+ fluxHelmViolatingEntity := domain.Entity {
61
+ ID : uuid .NewV4 ().String (),
62
+ APIVersion : "v1" ,
63
+ Kind : "Deployment" ,
64
+ Name : "my-helm-violating-entity" ,
65
+ Namespace : "default" ,
66
+ Manifest : map [string ]interface {}{},
67
+ ResourceVersion : "1" ,
68
+ Labels : map [string ]string {
69
+ "helm.toolkit.fluxcd.io/name" : "my-helm-app-name" ,
70
+ "helm.toolkit.fluxcd.io/namespace" : "my-helm-app-namespace" ,
71
+ },
72
+ }
73
+
74
+ fluxKustomizeViolatingEntity := domain.Entity {
75
+ ID : uuid .NewV4 ().String (),
76
+ APIVersion : "v1" ,
77
+ Kind : "Deployment" ,
78
+ Name : "my-kustomize-violating-entity" ,
79
+ Namespace : "default" ,
80
+ Manifest : map [string ]interface {}{},
81
+ ResourceVersion : "1" ,
82
+ Labels : map [string ]string {
83
+ "kustomize.toolkit.fluxcd.io/name" : "my-kustomize-app-name" ,
84
+ "kustomize.toolkit.fluxcd.io/namespace" : "my-kustomize-app-namespace" ,
85
+ },
86
+ }
87
+
60
88
results := []domain.PolicyValidation {
61
89
{
62
90
ID : uuid .NewV4 ().String (),
63
91
Policy : policy ,
64
92
Entity : violatingEntity ,
65
93
Status : domain .PolicyValidationStatusViolating ,
66
- Message : "message " ,
94
+ Message : "violating-entity " ,
67
95
Type : "Admission" ,
68
96
Trigger : "Admission" ,
69
97
CreatedAt : time .Now (),
@@ -73,7 +101,27 @@ func TestK8sEventSink(t *testing.T) {
73
101
Policy : policy ,
74
102
Entity : compliantEntity ,
75
103
Status : domain .PolicyValidationStatusCompliant ,
76
- Message : "message" ,
104
+ Message : "compliant-entity" ,
105
+ Type : "Admission" ,
106
+ Trigger : "Admission" ,
107
+ CreatedAt : time .Now (),
108
+ },
109
+ {
110
+ ID : uuid .NewV4 ().String (),
111
+ Policy : policy ,
112
+ Entity : fluxHelmViolatingEntity ,
113
+ Status : domain .PolicyValidationStatusViolating ,
114
+ Message : "flux-helm-entity" ,
115
+ Type : "Admission" ,
116
+ Trigger : "Admission" ,
117
+ CreatedAt : time .Now (),
118
+ },
119
+ {
120
+ ID : uuid .NewV4 ().String (),
121
+ Policy : policy ,
122
+ Entity : fluxKustomizeViolatingEntity ,
123
+ Status : domain .PolicyValidationStatusViolating ,
124
+ Message : "flux-kustomize-entity" ,
77
125
Type : "Admission" ,
78
126
Trigger : "Admission" ,
79
127
CreatedAt : time .Now (),
@@ -93,35 +141,49 @@ func TestK8sEventSink(t *testing.T) {
93
141
t .Error (err )
94
142
}
95
143
96
- time .Sleep (2 * time .Second )
144
+ time .Sleep (4 * time .Second )
97
145
98
146
events , err := sink .kubeClient .CoreV1 ().Events ("" ).List (ctx , metav1.ListOptions {})
99
147
if err != nil {
100
148
t .Error (err )
101
149
}
102
150
103
- assert .Equal (t , len (events .Items ), 2 , "did not receive expected events" )
151
+ assert .Equal (t , len (events .Items ), 4 , "did not receive expected events" )
104
152
105
153
for _ , event := range events .Items {
106
- if event .Type == v1 . EventTypeWarning {
154
+ if event .Message == "violating-entity" {
107
155
assert .Equal (t , event .Reason , domain .EventReasonPolicyViolation )
108
156
assert .Equal (t , event .Action , domain .EventActionRejected )
109
-
110
157
// verify involved object holds entity info
111
158
assert .Equal (t , event .InvolvedObject .APIVersion , violatingEntity .APIVersion )
112
159
assert .Equal (t , event .InvolvedObject .Kind , violatingEntity .Kind )
113
160
assert .Equal (t , event .InvolvedObject .Name , violatingEntity .Name )
114
161
assert .Equal (t , event .InvolvedObject .Namespace , violatingEntity .Namespace )
115
162
116
- } else if event .Type == v1 . EventTypeNormal {
163
+ } else if event .Message == "compliant-entity" {
117
164
assert .Equal (t , event .Reason , domain .EventReasonPolicyCompliance )
118
165
assert .Equal (t , event .Action , domain .EventActionAllowed )
119
-
120
166
// verify involved object holds entity info
121
167
assert .Equal (t , event .InvolvedObject .APIVersion , compliantEntity .APIVersion )
122
168
assert .Equal (t , event .InvolvedObject .Kind , compliantEntity .Kind )
123
169
assert .Equal (t , event .InvolvedObject .Name , compliantEntity .Name )
124
170
assert .Equal (t , event .InvolvedObject .Namespace , compliantEntity .Namespace )
171
+ } else if event .Message == "flux-helm-entity" {
172
+ assert .Equal (t , event .Reason , domain .EventReasonPolicyViolation )
173
+ assert .Equal (t , event .Action , domain .EventActionRejected )
174
+ // verify involved object holds entity info
175
+ assert .Equal (t , event .InvolvedObject .APIVersion , "helm.toolkit.fluxcd.io" )
176
+ assert .Equal (t , event .InvolvedObject .Kind , "HelmRelease" )
177
+ assert .Equal (t , event .InvolvedObject .Name , "my-helm-app-name" )
178
+ assert .Equal (t , event .InvolvedObject .Namespace , "my-helm-app-namespace" )
179
+ } else if event .Message == "compliant-entity" {
180
+ assert .Equal (t , event .Reason , domain .EventReasonPolicyViolation )
181
+ assert .Equal (t , event .Action , domain .EventActionRejected )
182
+ // verify involved object holds entity info
183
+ assert .Equal (t , event .InvolvedObject .APIVersion , "kustomize.toolkit.fluxcd.io" )
184
+ assert .Equal (t , event .InvolvedObject .Kind , "Kustomization" )
185
+ assert .Equal (t , event .InvolvedObject .Name , "my-kustomize-app-name" )
186
+ assert .Equal (t , event .InvolvedObject .Namespace , "my-kustomize-app-namespace" )
125
187
}
126
188
127
189
// verify involved object holds entity info
0 commit comments