From 48daaf8f4723a83b46fde86880034f7fee3cebb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Carpintero?= Date: Wed, 18 Dec 2024 16:45:08 +0100 Subject: [PATCH] Test healtz --- internal/http/check_test.go | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/internal/http/check_test.go b/internal/http/check_test.go index 7d95d3b..afd2fcd 100644 --- a/internal/http/check_test.go +++ b/internal/http/check_test.go @@ -14,6 +14,7 @@ import ( "net/http" "strconv" "sync" + "syscall" "testing" "time" @@ -245,13 +246,33 @@ func TestIntegrationHttpMode(t *testing.T) { resp agent.State } - for { - res, err := http.Get(fmt.Sprintf("http://localhost:%d/healthcheck", *tt.args.config.Port)) - if err == nil && res.StatusCode == 200 { + i := 0 + for i < 5 { + i++ + res, err := http.Get(fmt.Sprintf("http://localhost:%d/healthz", *tt.args.config.Port)) + if err != nil { + if errors.Is(err, syscall.ECONNREFUSED) { + l.Info("Connection refused - waiting...") + time.Sleep(1 * time.Second) + continue + } + + t.Errorf("staring server: %v", err) break } - l.Info("Waiting for check to be ready") - time.Sleep(1 * time.Second) + + if res.StatusCode != 200 { + t.Errorf("unexpected status for healthz: %v", res.StatusCode) + } + defer res.Body.Close() + body, _ := io.ReadAll(res.Body) + if string(body) != `"OK"` { + t.Errorf("unexpected body for healthz: %v", string(body)) + } + break + } + if i == 5 { + t.Error("Unable to start the server") } // ch will receive the results of the concurrent job executions @@ -317,7 +338,7 @@ func TestIntegrationHttpMode(t *testing.T) { close(ch) c.Shutdown() - time.Sleep(1 * time.Second) + time.Sleep(5 * time.Second) results := map[string]agent.State{} for x := range ch { results[x.check] = x.resp