uptimesebi-checker and uptimesebi-statuspage for linux/amd64, arm64, arm/v7 and arm/v6, from a Raspberry Pi Zero to servers. tools/image assembles them from the cross-compiled binaries without Docker or emulation: one layer on an empty base with the binary, a data volume owned by the unprivileged user and a HEALTHCHECK running the new `checker health` / `statuspage health` commands. The binaries embed time zone data and fallback CA roots, so no base image ever needs updating. CI pushes them with the same channel tags as the main image; releases carry tarballs for all four platforms. Refs #128 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
170 lines
5.0 KiB
Go
170 lines
5.0 KiB
Go
package main
|
|
|
|
import (
|
|
"archive/tar"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
"github.com/google/go-containerregistry/pkg/registry"
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
"github.com/google/go-containerregistry/pkg/v1/remote"
|
|
)
|
|
|
|
func fakeDist(t *testing.T, bin string) string {
|
|
t.Helper()
|
|
dist := t.TempDir()
|
|
for _, p := range platforms {
|
|
if err := os.MkdirAll(filepath.Join(dist, p.dir), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dist, p.dir, bin), []byte("binary for "+p.dir), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return dist
|
|
}
|
|
|
|
func TestBuild(t *testing.T) {
|
|
o := options{
|
|
Name: "statuspage", Dist: fakeDist(t, "statuspage"), Version: "v1.2.3", Commit: "abc123",
|
|
Env: []string{"UPTIME_STATUSPAGE_CACHE_DIR=/var/lib/uptimesebi"}, Ports: []string{"8082/tcp"},
|
|
Created: time.Unix(1700000000, 0).UTC(),
|
|
}
|
|
idx, err := build(o)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m, err := idx.IndexManifest()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(m.Manifests) != 4 {
|
|
t.Fatalf("index has %d manifests", len(m.Manifests))
|
|
}
|
|
for i, desc := range m.Manifests {
|
|
want := platforms[i]
|
|
if desc.Platform == nil || !desc.Platform.Equals(want.Platform) {
|
|
t.Errorf("manifest %d platform = %v, want %v", i, desc.Platform, want.Platform)
|
|
}
|
|
img, err := idx.Image(desc.Digest)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cf, err := img.ConfigFile()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cf.Architecture != want.Architecture || cf.Variant != want.Variant || cf.Config.User != "65532:65532" ||
|
|
!slices.Equal(cf.Config.Entrypoint, []string{"/usr/local/bin/statuspage"}) ||
|
|
!slices.Equal(cf.Config.Healthcheck.Test, []string{"CMD", "/usr/local/bin/statuspage", "health"}) ||
|
|
!slices.Contains(cf.Config.Env, "UPTIME_STATUSPAGE_CACHE_DIR=/var/lib/uptimesebi") ||
|
|
cf.Config.Labels["org.opencontainers.image.version"] != "v1.2.3" {
|
|
t.Errorf("%s config = %+v", want.dir, cf.Config)
|
|
}
|
|
files := layerFiles(t, img)
|
|
if got := files["usr/local/bin/statuspage"]; got.Mode != 0o755 || got.Size != int64(len("binary for "+want.dir)) {
|
|
t.Errorf("%s binary = %+v", want.dir, got)
|
|
}
|
|
if got := files["var/lib/uptimesebi/"]; got.Uid != nonroot {
|
|
t.Errorf("data directory owner = %d", got.Uid)
|
|
}
|
|
}
|
|
|
|
// The same input gives the same image.
|
|
again, _ := build(o)
|
|
d1, _ := idx.Digest()
|
|
d2, _ := again.Digest()
|
|
if d1 != d2 {
|
|
t.Errorf("rebuild changed the digest: %s, %s", d1, d2)
|
|
}
|
|
|
|
o.Dist = t.TempDir()
|
|
if _, err := build(o); err == nil {
|
|
t.Error("built without binaries")
|
|
}
|
|
}
|
|
|
|
func layerFiles(t *testing.T, img v1.Image) map[string]*tar.Header {
|
|
t.Helper()
|
|
layers, err := img.Layers()
|
|
if err != nil || len(layers) != 1 {
|
|
t.Fatalf("layers = %d, %v", len(layers), err)
|
|
}
|
|
rc, err := layers[0].Uncompressed()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = rc.Close() }()
|
|
out := map[string]*tar.Header{}
|
|
tr := tar.NewReader(rc)
|
|
for {
|
|
h, err := tr.Next()
|
|
if errors.Is(err, io.EOF) {
|
|
return out
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out[h.Name] = h
|
|
}
|
|
}
|
|
|
|
func TestChannelTags(t *testing.T) {
|
|
created := time.Date(2026, 11, 23, 18, 0, 0, 0, time.UTC)
|
|
for _, c := range []struct {
|
|
ref, suffix string
|
|
want []string
|
|
}{
|
|
{"refs/heads/main", "", []string{"20261123180000-0123456789ab", "edge"}},
|
|
{"refs/tags/v1.4.2", "", []string{"1.4.2", "1.4", "stable", "latest"}},
|
|
{"refs/tags/v1.5.0-beta.3", "", []string{"1.5.0-beta.3", "beta"}},
|
|
{"refs/tags/v1.4.2", "autoupdate", []string{"1.4.2-autoupdate", "1.4-autoupdate", "stable-autoupdate", "latest-autoupdate"}},
|
|
{"refs/tags/nightly", "", nil},
|
|
{"refs/pull/7/merge", "", nil},
|
|
{"refs/heads/feature", "", nil},
|
|
} {
|
|
if got := channelTags(c.ref, "v", "0123456789abcdef", created, c.suffix); !slices.Equal(got, c.want) {
|
|
t.Errorf("%s %s: tags = %v, want %v", c.ref, c.suffix, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPublish(t *testing.T) {
|
|
reg := httptest.NewServer(registry.New())
|
|
defer reg.Close()
|
|
repo := strings.TrimPrefix(reg.URL, "http://") + "/zb/uptimesebi-checker"
|
|
idx, err := build(options{Name: "checker", Dist: fakeDist(t, "checker"), Version: "v1.0.0"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := publish(context.Background(), idx, repo, []string{"1.0.0", "latest"}, credentials{}, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want, _ := idx.Digest()
|
|
for _, tag := range []string{"1.0.0", "latest"} {
|
|
ref, _ := name.NewTag(repo+":"+tag, name.Insecure)
|
|
desc, err := remote.Get(ref)
|
|
if err != nil || desc.Digest != want {
|
|
t.Errorf("%s = %v, %v; want %s", tag, desc, err, want)
|
|
}
|
|
}
|
|
// A Raspberry Pi Zero pulls its own variant.
|
|
ref, _ := name.NewTag(repo+":latest", name.Insecure)
|
|
img, err := remote.Image(ref, remote.WithPlatform(v1.Platform{OS: "linux", Architecture: "arm", Variant: "v6"}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := layerFiles(t, img)["usr/local/bin/checker"]; got.Size != int64(len("binary for linux_arm_v6")) {
|
|
t.Errorf("arm/v6 pulled the wrong binary: %+v", got)
|
|
}
|
|
}
|