diff --git a/plugin.go b/plugin.go index e9ec423..04bf434 100644 --- a/plugin.go +++ b/plugin.go @@ -94,9 +94,14 @@ func (c *Plugin) Init(cfg Configurer, log Logger) error { func (c *Plugin) Serve() chan error { errCh := make(chan error, 1) + healthHandler := NewHealthHandler(c.statusRegistry, &c.shutdownInitiated, c.log, c.cfg.UnavailableStatusCode) + readyHandler := NewReadyHandler(c.readyRegistry, &c.shutdownInitiated, c.log, c.cfg.UnavailableStatusCode) + mux := http.NewServeMux() - mux.Handle("/health", NewHealthHandler(c.statusRegistry, &c.shutdownInitiated, c.log, c.cfg.UnavailableStatusCode)) - mux.Handle("/ready", NewReadyHandler(c.readyRegistry, &c.shutdownInitiated, c.log, c.cfg.UnavailableStatusCode)) + mux.Handle("/health", healthHandler) + mux.Handle("/livez", healthHandler) + mux.Handle("/ready", readyHandler) + mux.Handle("/readyz", readyHandler) mux.Handle("/jobs", NewJobsHandler(c.statusJobsRegistry, &c.shutdownInitiated, c.log, c.cfg.UnavailableStatusCode)) c.mu.Lock() diff --git a/tests/endpoints_test.go b/tests/endpoints_test.go index c150267..67ed50f 100644 --- a/tests/endpoints_test.go +++ b/tests/endpoints_test.go @@ -33,10 +33,8 @@ const ( readyInitRPC = "127.0.0.1:6006" ) -// TestStatusEndpoints drives /health, /ready and /jobs against a container -// running the http plugin. The rpc plugin is deliberately left out: the -// ?plugin=http&plugin=rpc queries then prove that a name missing from the -// registry is skipped instead of reported. +// TestStatusEndpoints checks health, readiness, their aliases, and jobs. +// The missing rpc plugin checks that unknown query names are skipped. func TestStatusEndpoints(t *testing.T) { helpers.Start(t, statusInitCfg, []any{ &server.Plugin{}, @@ -48,45 +46,29 @@ func TestStatusEndpoints(t *testing.T) { // what makes its status and readiness meaningful helpers.WaitListener(t, "tcp", sharedHTTPAddr) - t.Run("HealthFiltered", func(t *testing.T) { - code, reports := helpers.GetReports(t, statusInitURL+"/health?plugin=http&plugin=rpc") - assert.Equal(t, http.StatusOK, code) - - require.Len(t, reports, 1) - assert.Equal(t, "http", reports[0].PluginName) - assert.Empty(t, reports[0].ErrorMessage) - assert.Equal(t, http.StatusOK, reports[0].StatusCode) - }) - - t.Run("HealthAll", func(t *testing.T) { - code, reports := helpers.GetReports(t, statusInitURL+"/health") - assert.Equal(t, http.StatusOK, code) - - require.Len(t, reports, 1) - assert.Equal(t, "http", reports[0].PluginName) - assert.Empty(t, reports[0].ErrorMessage) - assert.Equal(t, http.StatusOK, reports[0].StatusCode) - }) - - t.Run("ReadyFiltered", func(t *testing.T) { - code, reports := helpers.GetReports(t, statusInitURL+"/ready?plugin=http&plugin=rpc") - assert.Equal(t, http.StatusOK, code) - - require.Len(t, reports, 1) - assert.Equal(t, "http", reports[0].PluginName) - assert.Empty(t, reports[0].ErrorMessage) - assert.Equal(t, http.StatusOK, reports[0].StatusCode) - }) - - t.Run("ReadyAll", func(t *testing.T) { - code, reports := helpers.GetReports(t, statusInitURL+"/ready") - assert.Equal(t, http.StatusOK, code) - - require.Len(t, reports, 1) - assert.Equal(t, "http", reports[0].PluginName) - assert.Empty(t, reports[0].ErrorMessage) - assert.Equal(t, http.StatusOK, reports[0].StatusCode) - }) + for _, tt := range []struct { + name string + path string + }{ + {name: "HealthFiltered", path: "/health?plugin=http&plugin=rpc"}, + {name: "HealthAll", path: "/health"}, + {name: "LivezFiltered", path: "/livez?plugin=http&plugin=rpc"}, + {name: "LivezAll", path: "/livez"}, + {name: "ReadyFiltered", path: "/ready?plugin=http&plugin=rpc"}, + {name: "ReadyAll", path: "/ready"}, + {name: "ReadyzFiltered", path: "/readyz?plugin=http&plugin=rpc"}, + {name: "ReadyzAll", path: "/readyz"}, + } { + t.Run(tt.name, func(t *testing.T) { + code, reports := helpers.GetReports(t, statusInitURL+tt.path) + assert.Equal(t, http.StatusOK, code) + + require.Len(t, reports, 1) + assert.Equal(t, "http", reports[0].PluginName) + assert.Empty(t, reports[0].ErrorMessage) + assert.Equal(t, http.StatusOK, reports[0].StatusCode) + }) + } t.Run("JobsWithoutJobsPlugin", func(t *testing.T) { code, body := helpers.GetBody(t, statusInitURL+"/jobs") @@ -116,13 +98,27 @@ func TestReadinessWorkerBusy(t *testing.T) { return statusCode(t.Context(), readyURL) == http.StatusServiceUnavailable }, time.Second*15, time.Millisecond*20, "the pool kept a ready worker") - code, reports := helpers.GetReports(t, readyURL) - assert.Equal(t, http.StatusServiceUnavailable, code) - - require.Len(t, reports, 1) - assert.Equal(t, "http", reports[0].PluginName) - assert.Equal(t, "internal server error, see logs", reports[0].ErrorMessage) - assert.Equal(t, http.StatusServiceUnavailable, reports[0].StatusCode) + for _, tt := range []struct { + name string + path string + wantError string + wantCode int + }{ + {name: "Health", path: "/health?plugin=http&plugin=rpc", wantCode: http.StatusOK}, + {name: "Livez", path: "/livez?plugin=http&plugin=rpc", wantCode: http.StatusOK}, + {name: "Ready", path: "/ready?plugin=http&plugin=rpc", wantCode: http.StatusServiceUnavailable, wantError: "internal server error, see logs"}, + {name: "Readyz", path: "/readyz?plugin=http&plugin=rpc", wantCode: http.StatusServiceUnavailable, wantError: "internal server error, see logs"}, + } { + t.Run(tt.name, func(t *testing.T) { + code, reports := helpers.GetReports(t, readyInitURL+tt.path) + assert.Equal(t, tt.wantCode, code) + + require.Len(t, reports, 1) + assert.Equal(t, "http", reports[0].PluginName) + assert.Equal(t, tt.wantError, reports[0].ErrorMessage) + assert.Equal(t, tt.wantCode, reports[0].StatusCode) + }) + } rsp := &statusV1.Response{} require.NoError(t, helpers.RPC(t, readyInitRPC).Call("status.Ready", &statusV1.Request{Plugin: "http"}, rsp)) diff --git a/tests/shutdown_test.go b/tests/shutdown_test.go index 99d2ecd..9699513 100644 --- a/tests/shutdown_test.go +++ b/tests/shutdown_test.go @@ -38,16 +38,21 @@ func TestShutdown503(t *testing.T) { // returns once the container has been stopped stop() - // liveness stays 200 so the orchestrator does not kill the draining process - code, body := helpers.GetBody(t, shutdownURL+"/health") - assert.Equal(t, http.StatusOK, code) - assert.Contains(t, body, "service is shutting down") - - code, body = helpers.GetBody(t, shutdownURL+"/ready") - assert.Equal(t, http.StatusServiceUnavailable, code) - assert.Contains(t, body, "service is shutting down") - - code, body = helpers.GetBody(t, shutdownURL+"/jobs") - assert.Equal(t, http.StatusServiceUnavailable, code) - assert.Contains(t, body, "service is shutting down") + for _, tt := range []struct { + name string + path string + wantCode int + }{ + {name: "Health", path: "/health", wantCode: http.StatusOK}, + {name: "Livez", path: "/livez", wantCode: http.StatusOK}, + {name: "Ready", path: "/ready", wantCode: http.StatusServiceUnavailable}, + {name: "Readyz", path: "/readyz", wantCode: http.StatusServiceUnavailable}, + {name: "Jobs", path: "/jobs", wantCode: http.StatusServiceUnavailable}, + } { + t.Run(tt.name, func(t *testing.T) { + code, body := helpers.GetBody(t, shutdownURL+tt.path) + assert.Equal(t, tt.wantCode, code) + assert.Contains(t, body, "service is shutting down") + }) + } }