81 lines
2.7 KiB
Go
81 lines
2.7 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"}}
|
|
if err := os.WriteFile(config, []byte("[services.state]\ntarget='localhost:9090'\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
writeScript(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")
|
|
writeScript(t, filepath.Join(root, "systemd-run"), "#!/bin/sh\nprintf '%s\\n' \"$@\" > '"+called+"'\n")
|
|
writeScript(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 func() { _ = 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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// writeScript writes a stand-in for a host command the update runs.
|
|
func writeScript(t *testing.T, path, data string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(path, []byte(data), 0o700); err != nil { //nolint:gosec // G306: a script needs the execute bit.
|
|
t.Fatal(err)
|
|
}
|
|
}
|