Skip to content

Commit 127cc68

Browse files
committed
test: added test for the controller's update function
This test proves that the shallow clone works as intended and returns a clone of the slice, where the original references can still be used and updated.
1 parent 2a3581b commit 127cc68

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

pkg/api/api_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestAPI_Run(t *testing.T) {
7777
rec := httptest.NewRecorder()
7878
a.router.ServeHTTP(rec, req)
7979

80-
if status := rec.Result().StatusCode; status != tt.want.status { //nolint:bodyclose // closed in defer below
80+
if status := rec.Result().StatusCode; status != tt.want.status {
8181
t.Errorf("Handler for route %s returned wrong status code: got %v want %v", tt.want.path, status, tt.want.status)
8282
}
8383

pkg/sparrow/controller_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,74 @@ func TestChecksController_Reconcile(t *testing.T) {
287287
}
288288
}
289289

290+
// TestChecksController_Reconcile_Update tests the update of the checks
291+
// when the runtime configuration changes.
292+
func TestChecksController_Reconcile_Update(t *testing.T) {
293+
ctx, cancel := logger.NewContextWithLogger(context.Background())
294+
defer cancel()
295+
296+
tests := []struct {
297+
name string
298+
checks []checks.Check
299+
newRuntimeConfig runtime.Config
300+
}{
301+
{
302+
name: "update health check",
303+
checks: []checks.Check{
304+
health.NewCheck(),
305+
},
306+
newRuntimeConfig: runtime.Config{
307+
Health: &health.Config{
308+
Targets: []string{"https://new.com"},
309+
Interval: 200 * time.Millisecond,
310+
Timeout: 1000 * time.Millisecond,
311+
},
312+
},
313+
},
314+
{
315+
name: "update health & latency check",
316+
checks: []checks.Check{
317+
health.NewCheck(),
318+
latency.NewCheck(),
319+
},
320+
newRuntimeConfig: runtime.Config{
321+
Health: &health.Config{
322+
Targets: []string{"https://new.com"},
323+
Interval: 200 * time.Millisecond,
324+
Timeout: 1000 * time.Millisecond,
325+
},
326+
Latency: &latency.Config{
327+
Targets: []string{"https://new.com"},
328+
Interval: 200 * time.Millisecond,
329+
Timeout: 1000 * time.Millisecond,
330+
},
331+
},
332+
},
333+
}
334+
335+
for _, tt := range tests {
336+
t.Run(tt.name, func(t *testing.T) {
337+
cc := NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}))
338+
for _, c := range tt.checks {
339+
cc.checks.Add(c)
340+
}
341+
342+
cc.Reconcile(ctx, tt.newRuntimeConfig)
343+
344+
for _, c := range cc.checks.Iter() {
345+
switch c.GetConfig().For() {
346+
case health.CheckName:
347+
hc := c.(*health.Health)
348+
assert.Equal(t, tt.newRuntimeConfig.Health.Targets, hc.GetConfig().(*health.Config).Targets)
349+
case latency.CheckName:
350+
lc := c.(*latency.Latency)
351+
assert.Equal(t, tt.newRuntimeConfig.Latency.Targets, lc.GetConfig().(*latency.Config).Targets)
352+
}
353+
}
354+
})
355+
}
356+
}
357+
290358
func TestChecksController_RegisterCheck(t *testing.T) {
291359
tests := []struct {
292360
name string

pkg/sparrow/handlers_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestSparrow_handleCheckMetrics(t *testing.T) {
129129
}
130130

131131
s.handleCheckMetrics(w, r)
132-
resp := w.Result() //nolint:bodyclose
132+
resp := w.Result()
133133
body, _ := io.ReadAll(resp.Body)
134134

135135
if tt.wantCode == http.StatusOK {

0 commit comments

Comments
 (0)