Skip to content

Commit

Permalink
test: added test for the controller's update function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
puffitos committed Jan 22, 2025
1 parent 2a3581b commit 127cc68
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestAPI_Run(t *testing.T) {
rec := httptest.NewRecorder()
a.router.ServeHTTP(rec, req)

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

Expand Down
68 changes: 68 additions & 0 deletions pkg/sparrow/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,74 @@ func TestChecksController_Reconcile(t *testing.T) {
}
}

// TestChecksController_Reconcile_Update tests the update of the checks
// when the runtime configuration changes.
func TestChecksController_Reconcile_Update(t *testing.T) {
ctx, cancel := logger.NewContextWithLogger(context.Background())
defer cancel()

tests := []struct {
name string
checks []checks.Check
newRuntimeConfig runtime.Config
}{
{
name: "update health check",
checks: []checks.Check{
health.NewCheck(),
},
newRuntimeConfig: runtime.Config{
Health: &health.Config{
Targets: []string{"https://new.com"},
Interval: 200 * time.Millisecond,
Timeout: 1000 * time.Millisecond,
},
},
},
{
name: "update health & latency check",
checks: []checks.Check{
health.NewCheck(),
latency.NewCheck(),
},
newRuntimeConfig: runtime.Config{
Health: &health.Config{
Targets: []string{"https://new.com"},
Interval: 200 * time.Millisecond,
Timeout: 1000 * time.Millisecond,
},
Latency: &latency.Config{
Targets: []string{"https://new.com"},
Interval: 200 * time.Millisecond,
Timeout: 1000 * time.Millisecond,
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cc := NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}))
for _, c := range tt.checks {
cc.checks.Add(c)
}

cc.Reconcile(ctx, tt.newRuntimeConfig)

for _, c := range cc.checks.Iter() {
switch c.GetConfig().For() {
case health.CheckName:
hc := c.(*health.Health)
assert.Equal(t, tt.newRuntimeConfig.Health.Targets, hc.GetConfig().(*health.Config).Targets)
case latency.CheckName:
lc := c.(*latency.Latency)
assert.Equal(t, tt.newRuntimeConfig.Latency.Targets, lc.GetConfig().(*latency.Config).Targets)
}
}
})
}
}

func TestChecksController_RegisterCheck(t *testing.T) {
tests := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion pkg/sparrow/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestSparrow_handleCheckMetrics(t *testing.T) {
}

s.handleCheckMetrics(w, r)
resp := w.Result() //nolint:bodyclose
resp := w.Result()
body, _ := io.ReadAll(resp.Body)

if tt.wantCode == http.StatusOK {
Expand Down

0 comments on commit 127cc68

Please sign in to comment.