github.com/ethersphere/bee/v2@v2.2.0/pkg/api/readiness_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/pkg/api"
    12  	"github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest"
    13  )
    14  
    15  func TestReadiness(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	t.Run("probe not set", func(t *testing.T) {
    19  		t.Parallel()
    20  
    21  		testServer, _, _, _ := newTestServer(t, testServerOptions{})
    22  
    23  		// When probe is not set readiness endpoint should indicate that API is not ready
    24  		jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusBadRequest)
    25  	})
    26  
    27  	t.Run("readiness probe status change", func(t *testing.T) {
    28  		t.Parallel()
    29  
    30  		probe := api.NewProbe()
    31  		testServer, _, _, _ := newTestServer(t, testServerOptions{
    32  			Probe: probe,
    33  		})
    34  
    35  		// Current readiness probe is pending which should indicate that API is not ready
    36  		jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusBadRequest)
    37  
    38  		// When we set readiness probe to OK it should indicate that API is ready
    39  		probe.SetReady(api.ProbeStatusOK)
    40  		jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusOK)
    41  
    42  		// When we set readiness probe to NOK it should indicate that API is not ready
    43  		probe.SetReady(api.ProbeStatusNOK)
    44  		jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusBadRequest)
    45  	})
    46  }