Files
ctrld/internal/controld/maintenance_test.go
T
Cuong Manh Le 7c1184a799 feat(api): classify Control D API maintenance responses
During the reported incident the API answered "Maintenance is in
progress. Please try again later." after a successful DNS resolution
and TCP connection, and ctrld reported it as API_UNREACHABLE. Nothing
in the client could tell that answer apart from the API refusing the
request, so callers had to guess from an HTTP status that does not
carry the distinction.

Add IsMaintenance. The API answers every public request in hard
maintenance mode with HTTP 503 and error.code 50302
(Application::ERROR_HARD_MAINTENANCE), exported as MaintenanceCode, and
IsMaintenance classifies on that code. It falls back to the message for
deployments that answer without the code, which a test covers so that
path cannot silently drop. The answer's "body" is an empty JSON array;
a test pins that the error decode does not depend on it.

50301 (service unavailable) and 50003 (read-only mode) are deliberately
not maintenance: the first is a failure, and read-only only gates
authenticated controllers, so it never answers /utility or /logs.

Maintenance is temporary and says nothing about the configuration that
was asked for. The HTTP status cannot classify it on its own, because a
maintenance answer can arrive with a client-error status that otherwise
means the same request will be refused again, so callers must ask this
question before they act on a status.
2026-10-07 14:17:30 +07:00

86 lines
3.5 KiB
Go

package controld
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"testing"
)
func maintenanceErr(status, code int, message string) *ErrorResponse {
e := &ErrorResponse{StatusCode: status}
e.ErrorField.Code = code
e.ErrorField.Message = message
return e
}
// The message the API answered with during the reported incident, verbatim.
const reportedMaintenanceMessage = "Maintenance is in progress. Please try again later."
func TestIsMaintenanceByMessage(t *testing.T) {
for _, tc := range []struct {
name string
err error
want bool
}{
{"the reported answer", maintenanceErr(http.StatusServiceUnavailable, 0, reportedMaintenanceMessage), true},
{"carried on a client-error status", maintenanceErr(http.StatusBadRequest, 40003, reportedMaintenanceMessage), true},
{"wrapped", fmt.Errorf("fetching config: %w", maintenanceErr(503, 0, reportedMaintenanceMessage)), true},
{"a device rejection is not maintenance", maintenanceErr(http.StatusNotFound, InvalidConfigCode, "device does not exist"), false},
{"an empty message is not maintenance", maintenanceErr(http.StatusBadGateway, 0, ""), false},
{"a plain error is not maintenance", errors.New("dial tcp: connection refused"), false},
{"no error is not maintenance", nil, false},
} {
t.Run(tc.name, func(t *testing.T) {
if got := IsMaintenance(tc.err); got != tc.want {
t.Fatalf("IsMaintenance() = %v, want %v", got, tc.want)
}
})
}
}
// TestIsMaintenanceByCode covers the dedicated code. It must classify an
// answer that carries no recognizable message, must not stop recognizing the
// message form, which older deployments keep sending, and must not swallow
// the neighbouring codes that mean something else.
func TestIsMaintenanceByCode(t *testing.T) {
for _, tc := range []struct {
name string
err error
want bool
}{
{"hard maintenance code", maintenanceErr(http.StatusServiceUnavailable, 50302, "scheduled downtime"), true},
{"message form without the code", maintenanceErr(http.StatusServiceUnavailable, 0, reportedMaintenanceMessage), true},
{"service unavailable is a failure", maintenanceErr(http.StatusServiceUnavailable, 50301, "Service unavailable"), false},
{"read-only mode", maintenanceErr(http.StatusInternalServerError, 50003,
"Maintenance is in progress, cannot make any modifications. Please try again soon."), false},
{"deleted device", maintenanceErr(http.StatusNotFound, InvalidConfigCode, "device does not exist"), false},
} {
t.Run(tc.name, func(t *testing.T) {
if got := IsMaintenance(tc.err); got != tc.want {
t.Fatalf("IsMaintenance() = %v, want %v", got, tc.want)
}
})
}
}
// The API's hard maintenance answer, byte for byte as api_public/index.php
// writes it. "body" is an empty JSON array rather than an object, so the error
// decode must not depend on it.
func TestIsMaintenanceFromHardMaintenanceResponse(t *testing.T) {
const body = `{"body":[],"success":false,"error":{"date":"Mon, 05 Oct 2026 10:00:00 +0000",` +
`"message":"Maintenance is in progress. Please try again later.","code":50302}}`
errResp, err := apiErrorFromResponse(http.StatusServiceUnavailable, json.NewDecoder(strings.NewReader(body)))
if err != nil {
t.Fatalf("decoding the maintenance answer: %v", err)
}
if errResp.StatusCode != http.StatusServiceUnavailable || errResp.ErrorField.Code != MaintenanceCode {
t.Fatalf("errResp = %+v", errResp)
}
if !IsMaintenance(fmt.Errorf("fetching config: %w", errResp)) {
t.Fatal("the hard maintenance answer was not classified as maintenance")
}
}