2015-02-26 02:15:07 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-10-27 19:44:25 +00:00
|
|
|
"context"
|
2015-02-26 02:15:07 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/health"
|
2015-02-26 02:15:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestGETDownHandlerDoesNotChangeStatus ensures that calling the endpoint
|
|
|
|
// /debug/health/down with METHOD GET returns a 404
|
|
|
|
func TestGETDownHandlerDoesNotChangeStatus(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "https://fakeurl.com/debug/health/down", nil)
|
2015-02-26 02:15:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create request.")
|
|
|
|
}
|
|
|
|
|
|
|
|
DownHandler(recorder, req)
|
|
|
|
|
|
|
|
if recorder.Code != 404 {
|
|
|
|
t.Errorf("Did not get a 404.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestGETUpHandlerDoesNotChangeStatus ensures that calling the endpoint
|
|
|
|
// /debug/health/down with METHOD GET returns a 404
|
|
|
|
func TestGETUpHandlerDoesNotChangeStatus(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "https://fakeurl.com/debug/health/up", nil)
|
2015-02-26 02:15:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create request.")
|
|
|
|
}
|
|
|
|
|
|
|
|
DownHandler(recorder, req)
|
|
|
|
|
|
|
|
if recorder.Code != 404 {
|
|
|
|
t.Errorf("Did not get a 404.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPOSTDownHandlerChangeStatus ensures the endpoint /debug/health/down changes
|
|
|
|
// the status code of the response to 503
|
|
|
|
// This test is order dependent, and should come before TestPOSTUpHandlerChangeStatus
|
|
|
|
func TestPOSTDownHandlerChangeStatus(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, "https://fakeurl.com/debug/health/down", nil)
|
2015-02-26 02:15:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create request.")
|
|
|
|
}
|
|
|
|
|
|
|
|
DownHandler(recorder, req)
|
|
|
|
|
|
|
|
if recorder.Code != 200 {
|
|
|
|
t.Errorf("Did not get a 200.")
|
|
|
|
}
|
|
|
|
|
2023-10-27 19:44:25 +00:00
|
|
|
if len(health.CheckStatus(context.Background())) != 1 {
|
2015-02-26 02:15:07 +00:00
|
|
|
t.Errorf("DownHandler didn't add an error check.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPOSTUpHandlerChangeStatus ensures the endpoint /debug/health/up changes
|
|
|
|
// the status code of the response to 200
|
|
|
|
func TestPOSTUpHandlerChangeStatus(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, "https://fakeurl.com/debug/health/up", nil)
|
2015-02-26 02:15:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create request.")
|
|
|
|
}
|
|
|
|
|
|
|
|
UpHandler(recorder, req)
|
|
|
|
|
|
|
|
if recorder.Code != 200 {
|
|
|
|
t.Errorf("Did not get a 200.")
|
|
|
|
}
|
|
|
|
|
2023-10-27 19:44:25 +00:00
|
|
|
if len(health.CheckStatus(context.Background())) != 0 {
|
2015-02-26 02:15:07 +00:00
|
|
|
t.Errorf("UpHandler didn't remove the error check.")
|
|
|
|
}
|
|
|
|
}
|