@@ -20,29 +20,39 @@ import (
20
20
"bytes"
21
21
"encoding/json"
22
22
"fmt"
23
+ "io"
23
24
"net/http"
24
25
"net/http/httptest"
25
26
"testing"
26
27
"time"
27
28
28
- "github.com/onsi/gomega"
29
+ . "github.com/onsi/gomega"
29
30
"github.com/sethvargo/go-limiter/httplimit"
30
31
"github.com/sethvargo/go-limiter/memorystore"
32
+ "github.com/sethvargo/go-limiter/noopstore"
33
+ "github.com/slok/go-http-metrics/middleware"
31
34
corev1 "k8s.io/api/core/v1"
35
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36
+ utilruntime "k8s.io/apimachinery/pkg/util/runtime"
37
+ "k8s.io/kubectl/pkg/scheme"
38
+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
39
+ logf "sigs.k8s.io/controller-runtime/pkg/log"
32
40
41
+ notifyv1 "github.com/fluxcd/notification-controller/api/v1beta1"
42
+ "github.com/fluxcd/pkg/apis/meta"
33
43
"github.com/fluxcd/pkg/runtime/events"
34
44
)
35
45
36
46
func TestEventKeyFunc (t * testing.T ) {
37
- g := gomega . NewGomegaWithT (t )
47
+ g := NewWithT (t )
38
48
39
49
// Setup middleware
40
50
store , err := memorystore .New (& memorystore.Config {
41
51
Interval : 10 * time .Minute ,
42
52
})
43
- g .Expect (err ).ShouldNot (gomega . HaveOccurred ())
53
+ g .Expect (err ).ShouldNot (HaveOccurred ())
44
54
middleware , err := httplimit .NewMiddleware (store , eventKeyFunc )
45
- g .Expect (err ).ShouldNot (gomega . HaveOccurred ())
55
+ g .Expect (err ).ShouldNot (HaveOccurred ())
46
56
handler := middleware .Handle (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
47
57
w .WriteHeader (http .StatusOK )
48
58
}))
@@ -129,19 +139,135 @@ func TestEventKeyFunc(t *testing.T) {
129
139
Message : tt .message ,
130
140
}
131
141
eventData , err := json .Marshal (event )
132
- g .Expect (err ).ShouldNot (gomega . HaveOccurred ())
142
+ g .Expect (err ).ShouldNot (HaveOccurred ())
133
143
134
144
req := httptest .NewRequest ("POST" , "/" , bytes .NewBuffer (eventData ))
135
- g .Expect (err ).ShouldNot (gomega . HaveOccurred ())
145
+ g .Expect (err ).ShouldNot (HaveOccurred ())
136
146
res := httptest .NewRecorder ()
137
147
handler .ServeHTTP (res , req )
138
148
139
149
if tt .rateLimit {
140
- g .Expect (res .Code ).Should (gomega . Equal (429 ))
141
- g .Expect (res .Header ().Get ("X-Ratelimit-Remaining" )).Should (gomega . Equal ("0" ))
150
+ g .Expect (res .Code ).Should (Equal (http . StatusTooManyRequests ))
151
+ g .Expect (res .Header ().Get ("X-Ratelimit-Remaining" )).Should (Equal ("0" ))
142
152
} else {
143
- g .Expect (res .Code ).Should (gomega . Equal (200 ))
153
+ g .Expect (res .Code ).Should (Equal (http . StatusOK ))
144
154
}
145
155
})
146
156
}
147
157
}
158
+
159
+ func TestBlockInsecureHTTP (t * testing.T ) {
160
+ g := NewWithT (t )
161
+
162
+ var requestsReceived int
163
+ rcvServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
164
+ requestsReceived ++
165
+ io .Copy (io .Discard , r .Body )
166
+ w .WriteHeader (http .StatusOK )
167
+ }))
168
+ defer rcvServer .Close ()
169
+
170
+ utilruntime .Must (notifyv1 .AddToScheme (scheme .Scheme ))
171
+
172
+ testNamespace := "test-ns"
173
+ providerKey := "provider"
174
+ client := fake .NewFakeClientWithScheme (scheme .Scheme ,
175
+ & notifyv1.Provider {
176
+ ObjectMeta : metav1.ObjectMeta {
177
+ Name : providerKey ,
178
+ Namespace : testNamespace ,
179
+ },
180
+ Spec : notifyv1.ProviderSpec {
181
+ Type : "generic" ,
182
+ Address : rcvServer .URL ,
183
+ },
184
+ },
185
+ & notifyv1.Alert {
186
+ ObjectMeta : metav1.ObjectMeta {
187
+ Name : "some-alert-name" ,
188
+ Namespace : testNamespace ,
189
+ },
190
+ Spec : notifyv1.AlertSpec {
191
+ ProviderRef : meta.LocalObjectReference {
192
+ Name : providerKey ,
193
+ },
194
+ EventSeverity : "info" ,
195
+ EventSources : []notifyv1.CrossNamespaceObjectReference {
196
+ {
197
+ Kind : "Bucket" ,
198
+ Name : "hyacinth" ,
199
+ Namespace : testNamespace ,
200
+ },
201
+ },
202
+ },
203
+ Status : notifyv1.AlertStatus {
204
+ Conditions : []metav1.Condition {
205
+ {Type : meta .ReadyCondition , Status : metav1 .ConditionTrue },
206
+ },
207
+ },
208
+ },
209
+ )
210
+
211
+ eventMdlw := middleware .New (middleware.Config {})
212
+
213
+ store , err := noopstore .New ()
214
+ g .Expect (err ).ToNot (HaveOccurred ())
215
+
216
+ serverEndpoint := "127.0.0.1:56789"
217
+ eventServer := NewEventServer (serverEndpoint , logf .Log , client , true , true )
218
+ stopCh := make (chan struct {})
219
+ go eventServer .ListenAndServe (stopCh , eventMdlw , store )
220
+ defer close (stopCh )
221
+
222
+ event := events.Event {
223
+ InvolvedObject : corev1.ObjectReference {
224
+ Kind : "Bucket" ,
225
+ Name : "hyacinth" ,
226
+ Namespace : testNamespace ,
227
+ },
228
+ Severity : "info" ,
229
+ Timestamp : metav1 .Now (),
230
+ Message : "well that happened" ,
231
+ Reason : "event-happened" ,
232
+ ReportingController : "source-controller" ,
233
+ }
234
+
235
+ eventServerTests := []struct {
236
+ name string
237
+ isHttpEnabled bool
238
+ url string
239
+ wantRequest int
240
+ }{
241
+ {
242
+ name : "http scheme is disabled" ,
243
+ isHttpEnabled : false ,
244
+ wantRequest : 0 ,
245
+ },
246
+ {
247
+ name : "http scheme is enabled" ,
248
+ isHttpEnabled : true ,
249
+ wantRequest : 1 ,
250
+ },
251
+ }
252
+ for _ , tt := range eventServerTests {
253
+ t .Run (tt .name , func (t * testing.T ) {
254
+ g := NewWithT (t )
255
+ requestsReceived = 0 // reset counter
256
+
257
+ // Change the internal state instead of creating a new server.
258
+ eventServer .supportHttpScheme = tt .isHttpEnabled
259
+
260
+ buf := & bytes.Buffer {}
261
+ g .Expect (json .NewEncoder (buf ).Encode (& event )).To (Succeed ())
262
+ res , err := http .Post ("http://" + serverEndpoint , "application/json" , buf )
263
+
264
+ g .Expect (err ).ToNot (HaveOccurred ())
265
+ g .Expect (res .StatusCode ).To (Equal (http .StatusAccepted ))
266
+
267
+ // Requests happens async, so should the assertion.
268
+ g .Eventually (func () bool {
269
+ return requestsReceived == tt .wantRequest
270
+ }, 5 * time .Second ).Should (BeTrue ())
271
+ })
272
+ }
273
+ }
0 commit comments