57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.cin.su/m/Assistant/adapters/postgres"
|
|
)
|
|
|
|
type fakeWorkGraphStatusReader struct {
|
|
status postgres.WorkGraphRuntimeStatus
|
|
err error
|
|
}
|
|
|
|
func (f fakeWorkGraphStatusReader) RuntimeStatus(context.Context) (postgres.WorkGraphRuntimeStatus, error) {
|
|
return f.status, f.err
|
|
}
|
|
|
|
func TestWorkGraphStatusReportsDurableCounts(t *testing.T) {
|
|
response := httptest.NewRecorder()
|
|
workGraphStatusHandler(fakeWorkGraphStatusReader{status: postgres.WorkGraphRuntimeStatus{
|
|
GraphsByState: map[string]int64{"RUNNING": 2},
|
|
NodesByState: map[string]int64{"LEASED": 3, "SUCCEEDED": 1},
|
|
QueueByState: map[string]int64{"QUEUED": 3},
|
|
ObservedAtUnixMillis: 1700000000000,
|
|
}}).ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/v1/work-graphs/status", nil))
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("status code = %d", response.Code)
|
|
}
|
|
body := response.Body.String()
|
|
for _, expected := range []string{"\"status\":\"ready\"", "\"RUNNING\":2", "\"LEASED\":3", "\"QUEUED\":3", "1700000000000"} {
|
|
if !contains(body, expected) {
|
|
t.Fatalf("response missing %q: %s", expected, body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWorkGraphStatusFailsClosedOnDatabaseError(t *testing.T) {
|
|
response := httptest.NewRecorder()
|
|
workGraphStatusHandler(fakeWorkGraphStatusReader{err: errors.New("database unavailable")}).ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/v1/work-graphs/status", nil))
|
|
if response.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("status code = %d", response.Code)
|
|
}
|
|
}
|
|
|
|
func contains(value, expected string) bool {
|
|
for i := 0; i+len(expected) <= len(value); i++ {
|
|
if value[i:i+len(expected)] == expected {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|