fix(update): make daemon rollback schema-safe (#704)
This commit is contained in:
1 parent
c75ed44721
commit
bf7115fcb5
5 files changed
+198
-37
No files matched your search
@@ -370,7 +370,6 @@ func (b *boot) setupObservability() {
|
||||
// doesn't expose it. See docs/prds/payload-field-mapping.md.
|
||||
if ms, ok := b.st.(store.MappingStore); ok {
|
||||
b.web.Mappings = ms
|
||||
b.d.Mappings = ms
|
||||
} else {
|
||||
log.Warn("mapping storage unavailable: task store does not implement MappingStore")
|
||||
}
|
||||
@@ -383,21 +382,14 @@ func (b *boot) setupObservability() {
|
||||
// See docs/prds/webhook-intake-security.md.
|
||||
if bs, ok := b.st.(store.BindingStore); ok {
|
||||
b.web.Bindings = bs
|
||||
b.d.Bindings = bs
|
||||
} else {
|
||||
log.Warn("binding storage unavailable: task store does not implement BindingStore")
|
||||
}
|
||||
if bd, ok := b.st.(store.BindingDispatcher); ok {
|
||||
b.web.BindingDispatcher = bd
|
||||
b.d.BindingDispatcher = bd
|
||||
} else {
|
||||
log.Warn("binding dispatcher unavailable: task store does not implement BindingDispatcher")
|
||||
}
|
||||
if btc, ok := b.st.(store.BindingTaskCreator); ok {
|
||||
b.d.BindingTaskCreator = btc
|
||||
} else {
|
||||
log.Warn("binding task creator unavailable: task store does not implement BindingTaskCreator")
|
||||
}
|
||||
sink := bus.Subscribe(256)
|
||||
go persistAndBroadcastEvents(sink, b.st, b.web, log)
|
||||
}
|
||||
@@ -411,6 +403,9 @@ func (b *boot) connectNATS(ctx context.Context) error {
|
||||
url := cfg.NATS.URL
|
||||
var natsToken string
|
||||
if cfg.NATS.Mode == config.NATSModeExternal {
|
||||
if url == "" {
|
||||
return fmt.Errorf("nats.url is required when nats.mode is external")
|
||||
}
|
||||
var err error
|
||||
natsToken, err = configuredNATSToken(cfg.NATS, os.Getenv)
|
||||
if err != nil {
|
||||
@@ -1242,6 +1237,18 @@ func (b *boot) buildDaemon() {
|
||||
// this point /api/config and the daemon can never disagree about the
|
||||
// published config.
|
||||
b.web.Cfg = b.d.Cfg
|
||||
if ms, ok := b.st.(store.MappingStore); ok {
|
||||
b.d.Mappings = ms
|
||||
}
|
||||
if bs, ok := b.st.(store.BindingStore); ok {
|
||||
b.d.Bindings = bs
|
||||
}
|
||||
if bd, ok := b.st.(store.BindingDispatcher); ok {
|
||||
b.d.BindingDispatcher = bd
|
||||
}
|
||||
if btc, ok := b.st.(store.BindingTaskCreator); ok {
|
||||
b.d.BindingTaskCreator = btc
|
||||
}
|
||||
b.setupForgeWebhook()
|
||||
}
|
||||
|
||||
|
||||
@@ -150,3 +150,40 @@ func writeDockerResponse(t *testing.T, w http.ResponseWriter, value any) {
|
||||
t.Errorf("encode Docker response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectExternalNATSEmptyURLReturnsConfigurationError(t *testing.T) {
|
||||
b := newBootstrap()
|
||||
b.cfg.NATS = config.NATSConfig{Mode: config.NATSModeExternal}
|
||||
|
||||
err := b.connectNATS(t.Context())
|
||||
if err == nil || !strings.Contains(err.Error(), "nats.url is required") {
|
||||
t.Fatalf("connectNATS() error = %v, want useful missing-url error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupObservabilityDoesNotTouchDaemonBeforeBuild(t *testing.T) {
|
||||
fileset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fileset, "bootstrap.go", nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
setup := methodBody(t, file, "setupObservability")
|
||||
var daemonAssignments int
|
||||
ast.Inspect(setup, func(node ast.Node) bool {
|
||||
assign, ok := node.(*ast.AssignStmt)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
for _, lhs := range assign.Lhs {
|
||||
if selector, ok := lhs.(*ast.SelectorExpr); ok {
|
||||
if receiver, ok := selector.X.(*ast.SelectorExpr); ok && receiver.Sel.Name == "d" {
|
||||
daemonAssignments++
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
if daemonAssignments != 0 {
|
||||
t.Fatalf("setupObservability assigns daemon fields %d times; daemon is built later", daemonAssignments)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package releaseupdate
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -121,6 +122,73 @@ func TestUpdateWatchdogRollsBackManagedWorkerImageAndReportsOnlyChangedComponent
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateWatchdogRestoresTaskDatabaseOnDaemonRollback(t *testing.T) {
|
||||
root, err := filepath.Abs(filepath.Join("..", ".."))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
work := t.TempDir()
|
||||
binDir, fakeDir := filepath.Join(work, "bin"), filepath.Join(work, "fake-bin")
|
||||
reportPath := filepath.Join(work, "report.json")
|
||||
dbPath, backupPath := filepath.Join(work, "tasks.sqlite"), filepath.Join(work, "tasks.prev.sqlite")
|
||||
if err := os.MkdirAll(binDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, content := range map[string]string{"archied": "new", "archied.prev": "old"} {
|
||||
if err := os.WriteFile(filepath.Join(binDir, name), []byte(content), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for path, version := range map[string]int{dbPath: 2, backupPath: 1} {
|
||||
if output, err := exec.Command("sqlite3", path, fmt.Sprintf("PRAGMA user_version=%d; CREATE TABLE tasks (id INTEGER);", version)).CombinedOutput(); err != nil {
|
||||
t.Fatalf("create database %s: %v: %s", path, err, output)
|
||||
}
|
||||
}
|
||||
callsPath := filepath.Join(work, "calls")
|
||||
writeFakeCommand(t, fakeDir, "systemctl", `printf '%s\n' "systemctl $*" >> "$ARCHIE_TEST_CALLS"`)
|
||||
writeFakeCommand(t, fakeDir, "cp", `printf '%s\n' "cp $*" >> "$ARCHIE_TEST_CALLS"; /usr/bin/cp "$@"`)
|
||||
writeFakeCommand(t, fakeDir, "curl", `exit 1`)
|
||||
cmd := exec.CommandContext(t.Context(), filepath.Join(root, "scripts", "archie-update-watchdog"))
|
||||
cmd.Env = append(os.Environ(), "PATH="+fakeDir+":"+os.Getenv("PATH"), "ARCHIE_BIN_DIR="+binDir,
|
||||
"ARCHIE_TEST_CALLS="+callsPath,
|
||||
"ARCHIE_UPDATE_REPORT_PATH="+reportPath, "ARCHIE_UPDATE_HEALTH_TIMEOUT=0", "ARCHIE_UPDATE_COMPONENTS=daemon",
|
||||
"ARCHIE_UPDATE_PREVIOUS_GATEWAY=1.19.10", "ARCHIE_UPDATE_INSTALLED_GATEWAY=1.20.0",
|
||||
"ARCHIE_TASK_DB_PATH="+dbPath, "ARCHIE_TASK_DB_BACKUP="+backupPath)
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("watchdog failed: %v\n%s", err, output)
|
||||
}
|
||||
output, err := exec.Command("sqlite3", dbPath, "PRAGMA user_version;").Output()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.TrimSpace(string(output)) != "1" {
|
||||
t.Fatalf("restored schema version = %q, want 1", output)
|
||||
}
|
||||
calls, err := os.ReadFile(callsPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
callLines := strings.Split(strings.TrimSpace(string(calls)), "\n")
|
||||
stopIndex, copyIndex, startIndex := -1, -1, -1
|
||||
for i, line := range callLines {
|
||||
switch line {
|
||||
case "systemctl --user stop archied.service":
|
||||
stopIndex = i
|
||||
case "systemctl --user restart archied.service":
|
||||
startIndex = i
|
||||
}
|
||||
if strings.HasPrefix(line, "cp ") && strings.Contains(line, "tasks.prev.sqlite") {
|
||||
copyIndex = i
|
||||
}
|
||||
}
|
||||
if stopIndex < 0 || copyIndex < 0 || stopIndex > copyIndex {
|
||||
t.Fatalf("rollback must stop service before restoring database; calls = %q", calls)
|
||||
}
|
||||
if startIndex < 0 || startIndex <= copyIndex {
|
||||
t.Fatalf("rollback must restart service after restoring database; calls = %q", calls)
|
||||
}
|
||||
}
|
||||
|
||||
func runUpdateInstallScript(t *testing.T, environment map[string]string) (Result, []string) {
|
||||
t.Helper()
|
||||
ctx := t.Context()
|
||||
|
||||
Reference in new issue
Block a user