Files
archie/internal/app/archied/update_test.go
T
sam db346b159d fix(webui): mark cookies Secure behind a TLS proxy; run gosec in the gate (#1919)
Cookies took Secure from r.TLS, which is nil behind a proxy that terminates
TLS, so sign-in cookies went out without it. Every dashboard cookie now goes
through one builder that reads the scheme the way the origin check does.

The worktree reset and the agent worker's chown walk use os.Root, so a
symlink swapped in mid-walk can't point them outside the worktree.

gosec runs in golangci-lint, tests included. G104, G115, G204 and G304 are
excluded in config; each remaining finding is fixed or suppressed at the site
with its reason.
2026-11-13 03:16:15 +11:00

81 lines
2.6 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 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)
}
}