Files
uptimesebi/internal/check/docker_test.go
Sebastian UnterschützandClaude Opus 5.5 0b7f48a609
ci / build (pull_request) Successful in 5m21s
ci / image (pull_request) Successful in 6m43s
ci / lint (pull_request) Successful in 10m3s
ci / test (pull_request) Successful in 12m5s
feat: Docker container checks
A Docker monitor asks the Docker Engine API for a container's state: up
while it runs and its health check, if any, is not unhealthy; exited,
paused or missing containers are reported as such. The API is reached
over the checker host's Unix socket, allowed only on checkers that may
see private networks, or over plain TCP through the network guard. The
README lists all check types and notification services and explains
mounting the socket.

Closes #97

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 17:03:50 +01:00

91 lines
2.9 KiB
Go

package check
import (
"encoding/json"
"net"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
)
// dockerAPI serves container states like the Docker Engine API, on a Unix
// socket and on TCP.
func dockerAPI(t *testing.T) (socket, tcp string) {
states := map[string]any{
"web": map[string]any{"Status": "running"},
"api": map[string]any{"Status": "running", "Health": map[string]any{"Status": "healthy"}},
"worker": map[string]any{"Status": "running", "Health": map[string]any{"Status": "unhealthy"}},
"migrate": map[string]any{"Status": "exited", "ExitCode": 3},
"paused-1": map[string]any{"Status": "paused"},
}
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/containers/"), "/json")
st, ok := states[name]
if !ok {
w.WriteHeader(http.StatusNotFound)
_ = json.NewEncoder(w).Encode(map[string]string{"message": "No such container: " + name})
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"Name": "/" + name, "State": st})
})
socket = filepath.Join(t.TempDir(), "docker.sock")
l, err := net.Listen("unix", socket)
if err != nil {
t.Fatal(err)
}
srv := httptest.NewUnstartedServer(h)
srv.Listener = l
srv.Start()
t.Cleanup(srv.Close)
ts := httptest.NewServer(h)
t.Cleanup(ts.Close)
return socket, strings.TrimPrefix(ts.URL, "http://")
}
func TestDockerCheck(t *testing.T) {
socket, tcp := dockerAPI(t)
for _, host := range []string{"unix://" + socket, "tcp://" + tcp} {
cfg := `{"host":"` + host + `"}`
for target, want := range map[string]string{
"web": "",
"api": "",
"worker": "container is unhealthy",
"migrate": "container exited with code 3",
"paused-1": "container is paused",
"gone": "no container named gone",
} {
o := runShort(t, Docker, target, cfg)
if want == "" && !o.OK || want != "" && (o.OK || o.Error != want) {
t.Errorf("%s %s: %+v, want %q", host, target, o, want)
}
}
}
// Shared checkers may use neither their socket nor private addresses.
for _, host := range []string{"unix://" + socket, "tcp://" + tcp} {
c, _ := Validate(Docker, "web", []byte(`{"host":"`+host+`"}`))
if o := Run(t.Context(), Spec{Type: Docker, Target: "web", Timeout: 2 * time.Second, Config: c}); o.OK {
t.Errorf("%s reached by a shared checker", host)
}
}
}
func TestDockerValidate(t *testing.T) {
got, err := Validate(Docker, "web", []byte(`{}`))
if err != nil || string(got) != `{"host":"unix:///var/run/docker.sock"}` {
t.Errorf("default = %s, %v", got, err)
}
for _, tc := range []struct{ target, cfg string }{
{"../etc", `{}`},
{"web", `{"host":"http://docker:2375"}`},
{"web", `{"host":"tcp://docker"}`},
{"web", `{"host":"unix://relative.sock"}`},
} {
if _, err := Validate(Docker, tc.target, []byte(tc.cfg)); err == nil {
t.Errorf("Validate(%s, %s) accepted", tc.target, tc.cfg)
}
}
}