Files
sablier/main_test.go
Alexis Couvreur ebd24562b1 feat: add healthcheck lookup before redirect (#12)
Docker classic IsUp will return false when the container defines a healthcheck and is not healthy, otherwise as soon as it's started it's up.
Docker swarm will check that the number of required tasks is higher than 1, and that the number of running tasks matches the number of desired tasks.
A task is not running when it defines a healthcheck and is not healthy.
2021-09-28 12:44:27 +02:00

72 lines
1.6 KiB
Go

package main
import (
"net/http"
"net/http/httptest"
"testing"
)
type ScalerMock struct {
isUp bool
}
func (s ScalerMock) IsUp(name string) bool {
return s.isUp
}
func (ScalerMock) ScaleUp(name string) error { return nil }
func (ScalerMock) ScaleDown(name string) error { return nil }
func TestOndemand_ServeHTTP(t *testing.T) {
testCases := []struct {
desc string
scaler ScalerMock
status string
statusCode int
contentType string
}{
{
desc: "service is starting",
status: "starting",
scaler: ScalerMock{isUp: false},
statusCode: http.StatusAccepted,
contentType: "text/plain",
},
{
desc: "service is started",
status: "started",
scaler: ScalerMock{isUp: true},
statusCode: http.StatusCreated,
contentType: "text/plain",
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Logf("IsUp: %t", test.scaler.isUp)
request := httptest.NewRequest("GET", "/?name=whoami&timeout=5m", nil)
responseRecorder := httptest.NewRecorder()
onDemandHandler := onDemand(test.scaler)
onDemandHandler(responseRecorder, request)
body := responseRecorder.Body.String()
if responseRecorder.Code != test.statusCode {
t.Errorf("Want status '%d', got '%d'", test.statusCode, responseRecorder.Code)
}
if responseRecorder.Body.String() != test.status {
t.Errorf("Want body '%s', got '%s'", test.status, body)
}
if responseRecorder.Header().Get("Content-Type") != test.contentType {
t.Errorf("Want content type '%s', got '%s'", test.contentType, responseRecorder.Header().Get("Content-Type"))
}
})
}
}