The API serves /install.sh with its public address and agent image filled in. The POSIX sh script checks for Linux on a supported CPU, Docker and Compose, writes /opt/uptimesebi/<kind>[-<name>]/compose.yaml and a .env readable only by its owner, starts the agent and waits until it is healthy. Options cover status page hosts with Let's Encrypt, the plain image for manual updates, the Docker socket for checkers and several agents per host; running it again changes the settings. The Compose files in deploy/agent are configured through .env alone, so they also work without the script. Images follow the platform's channel (latest, beta or edge). CI shellchecks the rendered script. Closes #130 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
197 lines
6.3 KiB
Go
197 lines
6.3 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// fakeDocker answers like Docker with Compose for the install script and
|
|
// logs every call.
|
|
const fakeDocker = `#!/bin/sh
|
|
echo "$*" >> "$FAKE_LOG"
|
|
case "$*" in
|
|
"compose ps -q"*) echo c0ffee ;;
|
|
inspect*) echo healthy ;;
|
|
esac
|
|
exit 0
|
|
`
|
|
|
|
type installRun struct {
|
|
dir, log, out string
|
|
err error
|
|
}
|
|
|
|
func runInstall(t *testing.T, script []byte, env []string, args ...string) installRun {
|
|
t.Helper()
|
|
tmp := t.TempDir()
|
|
bin := filepath.Join(tmp, "bin")
|
|
if err := os.Mkdir(bin, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(bin, "docker"), []byte(fakeDocker), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(tmp, "install.sh")
|
|
if err := os.WriteFile(path, script, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := installRun{dir: filepath.Join(tmp, "agent"), log: filepath.Join(tmp, "docker.log")}
|
|
// bash --posix is close to dash and busybox; the script must stay POSIX.
|
|
cmd := exec.Command("bash", append([]string{"--posix", path}, append(args, "--dir", r.dir)...)...)
|
|
cmd.Env = append([]string{"PATH=" + bin + ":/usr/bin:/bin", "FAKE_LOG=" + r.log}, env...)
|
|
out, err := cmd.CombinedOutput()
|
|
r.out, r.err = string(out), err
|
|
return r
|
|
}
|
|
|
|
func read(t *testing.T, path string) string {
|
|
t.Helper()
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func script(t *testing.T) []byte {
|
|
t.Helper()
|
|
s, err := Script(Install{APIURL: "https://uptime.example.com/", Image: "git.example.com/acme/uptimesebi", Tag: "latest"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s
|
|
}
|
|
|
|
func TestInstallChecker(t *testing.T) {
|
|
r := runInstall(t, script(t), nil, "checker", "--token", "usa_secret")
|
|
if r.err != nil {
|
|
t.Fatalf("install failed: %v\n%s", r.err, r.out)
|
|
}
|
|
checker, _ := Compose("checker")
|
|
if got := read(t, filepath.Join(r.dir, "compose.yaml")); got != string(checker) {
|
|
t.Errorf("compose.yaml = %q", got)
|
|
}
|
|
env := read(t, filepath.Join(r.dir, ".env"))
|
|
for _, want := range []string{
|
|
"UPTIME_API_URL=https://uptime.example.com\n", "UPTIME_AGENT_TOKEN=usa_secret\n",
|
|
"UPTIME_AGENT_IMAGE=git.example.com/acme/uptimesebi\n", "UPTIME_AGENT_TAG=latest-autoupdate\n",
|
|
} {
|
|
if !strings.Contains(env, want) {
|
|
t.Errorf(".env lacks %q:\n%s", want, env)
|
|
}
|
|
}
|
|
if st, _ := os.Stat(filepath.Join(r.dir, ".env")); st.Mode().Perm() != 0o600 {
|
|
t.Errorf(".env mode = %v", st.Mode().Perm())
|
|
}
|
|
if log := read(t, r.log); !strings.Contains(log, "compose pull") || !strings.Contains(log, "compose up -d") {
|
|
t.Errorf("docker calls:\n%s", log)
|
|
}
|
|
if !strings.Contains(r.out, "updates itself") {
|
|
t.Errorf("output:\n%s", r.out)
|
|
}
|
|
}
|
|
|
|
func TestInstallStatusPage(t *testing.T) {
|
|
r := runInstall(t, script(t), []string{"UPTIME_AGENT_TOKEN=usa_fromenv"}, "statuspage",
|
|
"--host", "status.example.com", "--host", "status.example.org", "--tls", "--email", "ops@example.com",
|
|
"--manual-updates", "--http-port", "8080", "--no-start")
|
|
if r.err != nil {
|
|
t.Fatalf("install failed: %v\n%s", r.err, r.out)
|
|
}
|
|
env := read(t, filepath.Join(r.dir, ".env"))
|
|
for _, want := range []string{
|
|
"UPTIME_AGENT_TOKEN=usa_fromenv\n", "UPTIME_AGENT_TAG=latest\n",
|
|
"UPTIME_STATUSPAGE_HOSTS=status.example.com,status.example.org\n", "UPTIME_STATUSPAGE_TLS=true\n",
|
|
"UPTIME_STATUSPAGE_TLS_LISTEN=:8443\n", "UPTIME_ACME_EMAIL=ops@example.com\n", "UPTIME_HTTP_PORT=8080\n",
|
|
} {
|
|
if !strings.Contains(env, want) {
|
|
t.Errorf(".env lacks %q:\n%s", want, env)
|
|
}
|
|
}
|
|
if log, _ := os.ReadFile(r.log); strings.Contains(string(log), "compose up") {
|
|
t.Error("--no-start started the agent")
|
|
}
|
|
|
|
// Running it again keeps the old settings as a backup.
|
|
again := runInstall(t, script(t), nil, "statuspage", "--token", "usa_new", "--no-start")
|
|
if again.err != nil {
|
|
t.Fatal(again.out)
|
|
}
|
|
}
|
|
|
|
func TestInstallSocket(t *testing.T) {
|
|
if _, err := os.Stat("/var/run/docker.sock"); err != nil {
|
|
t.Skip("no Docker socket on this host")
|
|
}
|
|
r := runInstall(t, script(t), nil, "checker", "--token", "usa_x", "--docker-socket", "--no-start")
|
|
if r.err != nil {
|
|
t.Fatalf("install failed: %v\n%s", r.err, r.out)
|
|
}
|
|
socket, _ := Compose("docker-socket")
|
|
if read(t, filepath.Join(r.dir, "compose.override.yaml")) != string(socket) || !strings.Contains(read(t, filepath.Join(r.dir, ".env")), "DOCKER_GID=") {
|
|
t.Error("the Docker socket is not set up")
|
|
}
|
|
}
|
|
|
|
func TestInstallRefuses(t *testing.T) {
|
|
for name, args := range map[string][]string{
|
|
"no token": {"checker"},
|
|
"user token": {"checker", "--token", "ust_abc"},
|
|
"unknown kind": {"database", "--token", "usa_x"},
|
|
"unknown option": {"checker", "--token", "usa_x", "--force"},
|
|
"tls without host": {"statuspage", "--token", "usa_x", "--tls", "--email", "a@b.c"},
|
|
"host for a checker": {"checker", "--token", "usa_x", "--host", "x.example.com"},
|
|
"socket for pages": {"statuspage", "--token", "usa_x", "--docker-socket"},
|
|
"bad name": {"checker", "--token", "usa_x", "--name", "../etc"},
|
|
} {
|
|
r := runInstall(t, script(t), nil, args...)
|
|
if r.err == nil {
|
|
t.Errorf("%s: installed:\n%s", name, r.out)
|
|
}
|
|
if _, err := os.Stat(r.dir); err == nil {
|
|
t.Errorf("%s: wrote files", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestScript(t *testing.T) {
|
|
for _, bad := range []Install{
|
|
{APIURL: "ftp://x", Image: "a", Tag: "latest"},
|
|
{APIURL: "https://x.example/'$(reboot)'", Image: "a", Tag: "latest"},
|
|
{APIURL: "https://x.example", Image: "a b", Tag: "latest"},
|
|
{APIURL: "https://x.example", Image: "a", Tag: "$(id)"},
|
|
} {
|
|
if _, err := Script(bad); err == nil {
|
|
t.Errorf("Script(%+v) accepted", bad)
|
|
}
|
|
}
|
|
path := filepath.Join(t.TempDir(), "install.sh")
|
|
if err := os.WriteFile(path, script(t), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if out, err := exec.Command("bash", "--posix", "-n", path).CombinedOutput(); err != nil {
|
|
t.Fatalf("syntax: %v %s", err, out)
|
|
}
|
|
// shellcheck runs in CI; locally only when installed.
|
|
sc, err := exec.LookPath("shellcheck")
|
|
if err != nil {
|
|
t.Skip("shellcheck is not installed")
|
|
}
|
|
if out, err := exec.Command(sc, "--shell=sh", path).CombinedOutput(); err != nil {
|
|
t.Errorf("shellcheck:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestChannel(t *testing.T) {
|
|
for v, want := range map[string]string{
|
|
"v1.4.2": "latest", "v1.5.0-beta.2": "beta", "20261123180000-abc": "edge", "dev": "edge", "v1.4": "edge",
|
|
} {
|
|
if got := Channel(v); got != want {
|
|
t.Errorf("Channel(%s) = %s, want %s", v, got, want)
|
|
}
|
|
}
|
|
}
|