archied update install verifies the release zip, checks every binary reports the release, pulls the agent image by digest, backs up the database in-process and stages the binaries before handing off to archied update watchdog in its own process. The watchdog restarts units in release order, requires archied healthy for 120s and otherwise rolls back binaries and image policy, restoring the database only when a migration ran. Boot recovery resumes the frozen watchdog. Deletes scripts/archie-update-install, scripts/archie-update-watchdog, the updater's --from-source and --auto, CommandInstaller, ScriptInstaller and the update-image and update-topology subcommands.
78 lines
2.5 KiB
Go
78 lines
2.5 KiB
Go
package archied
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
|
)
|
|
|
|
func TestUpdateTopologyRefusal(t *testing.T) {
|
|
root := t.TempDir()
|
|
config := filepath.Join(root, "config.toml")
|
|
required := releaseupdate.Topology{Units: []string{"archie-state-store", "archie-gateway", "archied"}, ConfigSections: []string{"services.state", "services.gateway"}}
|
|
writeUpdateFixture(t, config, "[services.state]\ntarget='localhost:9090'\n")
|
|
writeUpdateFixture(t, filepath.Join(root, "systemctl"), "#!/bin/sh\nprintf 'archied.service enabled enabled\\n'\n")
|
|
t.Setenv("PATH", root+":"+os.Getenv("PATH"))
|
|
var output bytes.Buffer
|
|
if err := checkUpdateTopology(t.Context(), config, required, true, &output); err == nil {
|
|
t.Fatal("partial topology accepted")
|
|
}
|
|
for _, missing := range []string{"configure [services.gateway]", "install and enable archie-state-store.service", "install and enable archie-gateway.service"} {
|
|
if !strings.Contains(output.String(), missing) {
|
|
t.Fatalf("missing step %q: %s", missing, output.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestResumeUpdate(t *testing.T) {
|
|
for _, active := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "interrupted", true: "live"}[active], func(t *testing.T) {
|
|
root := t.TempDir()
|
|
t.Setenv("ARCHIE_UPDATE_STATE_DIR", root)
|
|
if err := os.Mkdir(filepath.Join(root, "transaction"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
called := filepath.Join(root, "called")
|
|
writeUpdateFixture(t, filepath.Join(root, "systemd-run"), "#!/bin/sh\nprintf '%s\\n' \"$@\" > '"+called+"'\n")
|
|
writeUpdateFixture(t, filepath.Join(root, "systemctl"), "#!/bin/sh\n")
|
|
t.Setenv("PATH", root+":"+os.Getenv("PATH"))
|
|
lock, err := os.OpenFile(filepath.Join(root, "lock"), os.O_CREATE|os.O_RDWR, 0o600)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer lock.Close()
|
|
if active {
|
|
if err := unix.Flock(int(lock.Fd()), unix.LOCK_EX); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
resumed, err := resumeUpdate(t.Context(), filepath.Join(root, "config.toml"))
|
|
if err != nil || resumed == active {
|
|
t.Fatalf("resumed=%v err=%v", resumed, err)
|
|
}
|
|
if !active {
|
|
data, err := os.ReadFile(called)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(data), "-recover") {
|
|
t.Fatalf("not a rollback: %s", data)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func writeUpdateFixture(t *testing.T, path, data string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(path, []byte(data), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|