github.com/ethersphere/bee/v2@v2.2.0/pkg/api/health_test.go (about) 1 // Copyright 2022 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package api_test 6 7 import ( 8 "net/http" 9 "testing" 10 11 "github.com/ethersphere/bee/v2" 12 "github.com/ethersphere/bee/v2/pkg/api" 13 "github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest" 14 ) 15 16 func TestHealth(t *testing.T) { 17 t.Parallel() 18 19 t.Run("probe not set", func(t *testing.T) { 20 t.Parallel() 21 22 testServer, _, _, _ := newTestServer(t, testServerOptions{}) 23 24 // When probe is not set health endpoint should indicate that node is not healthy 25 jsonhttptest.Request(t, testServer, http.MethodGet, "/health", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(api.HealthStatusResponse{ 26 Status: "nok", 27 Version: bee.Version, 28 APIVersion: api.Version, 29 })) 30 }) 31 32 t.Run("health probe status change", func(t *testing.T) { 33 t.Parallel() 34 35 probe := api.NewProbe() 36 testServer, _, _, _ := newTestServer(t, testServerOptions{ 37 Probe: probe, 38 }) 39 40 // Current health probe is pending which should indicate that API is not healthy 41 jsonhttptest.Request(t, testServer, http.MethodGet, "/health", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(api.HealthStatusResponse{ 42 Status: "nok", 43 Version: bee.Version, 44 APIVersion: api.Version, 45 })) 46 47 // When we set health probe to OK it should indicate that node is healthy 48 probe.SetHealthy(api.ProbeStatusOK) 49 jsonhttptest.Request(t, testServer, http.MethodGet, "/health", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(api.HealthStatusResponse{ 50 Status: "ok", 51 Version: bee.Version, 52 APIVersion: api.Version, 53 })) 54 55 // When we set health probe to NOK it should indicate that node is not healthy 56 probe.SetHealthy(api.ProbeStatusNOK) 57 jsonhttptest.Request(t, testServer, http.MethodGet, "/health", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(api.HealthStatusResponse{ 58 Status: "nok", 59 Version: bee.Version, 60 APIVersion: api.Version, 61 })) 62 }) 63 }