From 1ab0eef2056e9a201bb0091cc85e0964eb2829ab Mon Sep 17 00:00:00 2001 From: notanatol Date: Tue, 27 Feb 2024 11:47:56 -0600 Subject: [PATCH 1/2] fix: add body to readiness endpoint --- pkg/api/readiness.go | 23 ++++++++++++++++++++--- pkg/api/readiness_test.go | 16 ++++++++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/pkg/api/readiness.go b/pkg/api/readiness.go index d7f0d8dd27a..211e3b4f5d1 100644 --- a/pkg/api/readiness.go +++ b/pkg/api/readiness.go @@ -4,12 +4,29 @@ package api -import "net/http" +import ( + "net/http" + + "github.com/ethersphere/bee" + "github.com/ethersphere/bee/pkg/jsonhttp" +) + +type ReadyStatusResponse healthStatusResponse func (s *Service) readinessHandler(w http.ResponseWriter, _ *http.Request) { if s.probe.Ready() == ProbeStatusOK { - w.WriteHeader(http.StatusOK) + jsonhttp.OK(w, ReadyStatusResponse{ + Status: "ready", + Version: bee.Version, + APIVersion: Version, + DebugAPIVersion: DebugVersion, + }) } else { - w.WriteHeader(http.StatusBadRequest) + jsonhttp.BadRequest(w, ReadyStatusResponse{ + Status: "notReady", + Version: bee.Version, + APIVersion: Version, + DebugAPIVersion: DebugVersion, + }) } } diff --git a/pkg/api/readiness_test.go b/pkg/api/readiness_test.go index 52677214919..c21fcdec62a 100644 --- a/pkg/api/readiness_test.go +++ b/pkg/api/readiness_test.go @@ -37,10 +37,22 @@ func TestReadiness(t *testing.T) { // When we set readiness probe to OK it should indicate that API is ready probe.SetReady(api.ProbeStatusOK) - jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusOK) + jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusOK, + jsonhttptest.WithExpectedJSONResponse(api.ReadyStatusResponse{ + Status: "ready", + Version: "-dev", + APIVersion: "0.0.0", + DebugAPIVersion: "0.0.0", + })) // When we set readiness probe to NOK it should indicate that API is not ready probe.SetReady(api.ProbeStatusNOK) - jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusBadRequest) + jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusBadRequest, + jsonhttptest.WithExpectedJSONResponse(api.ReadyStatusResponse{ + Status: "notReady", + Version: "-dev", + APIVersion: "0.0.0", + DebugAPIVersion: "0.0.0", + })) }) } From 9c436126d16500ad87cfa151cf307dcd100af4a9 Mon Sep 17 00:00:00 2001 From: Acha Bill Date: Wed, 11 Sep 2024 12:30:19 +0100 Subject: [PATCH 2/2] fix: imports --- pkg/api/readiness.go | 18 ++++++++---------- pkg/api/readiness_test.go | 14 ++++++-------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/pkg/api/readiness.go b/pkg/api/readiness.go index 211e3b4f5d1..35685483892 100644 --- a/pkg/api/readiness.go +++ b/pkg/api/readiness.go @@ -7,8 +7,8 @@ package api import ( "net/http" - "github.com/ethersphere/bee" - "github.com/ethersphere/bee/pkg/jsonhttp" + "github.com/ethersphere/bee/v2" + "github.com/ethersphere/bee/v2/pkg/jsonhttp" ) type ReadyStatusResponse healthStatusResponse @@ -16,17 +16,15 @@ type ReadyStatusResponse healthStatusResponse func (s *Service) readinessHandler(w http.ResponseWriter, _ *http.Request) { if s.probe.Ready() == ProbeStatusOK { jsonhttp.OK(w, ReadyStatusResponse{ - Status: "ready", - Version: bee.Version, - APIVersion: Version, - DebugAPIVersion: DebugVersion, + Status: "ready", + Version: bee.Version, + APIVersion: Version, }) } else { jsonhttp.BadRequest(w, ReadyStatusResponse{ - Status: "notReady", - Version: bee.Version, - APIVersion: Version, - DebugAPIVersion: DebugVersion, + Status: "notReady", + Version: bee.Version, + APIVersion: Version, }) } } diff --git a/pkg/api/readiness_test.go b/pkg/api/readiness_test.go index c21fcdec62a..0454f541c2f 100644 --- a/pkg/api/readiness_test.go +++ b/pkg/api/readiness_test.go @@ -39,20 +39,18 @@ func TestReadiness(t *testing.T) { probe.SetReady(api.ProbeStatusOK) jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(api.ReadyStatusResponse{ - Status: "ready", - Version: "-dev", - APIVersion: "0.0.0", - DebugAPIVersion: "0.0.0", + Status: "ready", + Version: "-dev", + APIVersion: "0.0.0", })) // When we set readiness probe to NOK it should indicate that API is not ready probe.SetReady(api.ProbeStatusNOK) jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusBadRequest, jsonhttptest.WithExpectedJSONResponse(api.ReadyStatusResponse{ - Status: "notReady", - Version: "-dev", - APIVersion: "0.0.0", - DebugAPIVersion: "0.0.0", + Status: "notReady", + Version: "-dev", + APIVersion: "0.0.0", })) }) }