Files
archie/internal/releaseupdate/service_test.go
T
sam 369b13b688 feat(releaseupdate): stamp install type at build time, fail closed on unknown
/update could report success at the correct target version while the
archie-agent container running it never actually changed: the update
mechanism trusted the configured installer's exit code and reported
whatever version number the release claimed, with nothing verifying
the two agreed.

The first piece of the fix: a new internal/installtype package
reports how a binary was built for distribution (Type(), defaulting
to "unknown"), stamped once per release artifact via
"-ldflags -X .../installtype.buildType=<value>" rather than guessed
at runtime by probing the environment -- restic and Tailscale both
treat "which install shape is this" as a build-time decision for the
same reason: a probed signal is guessable, a compiled-in one is not.

releaseupdate.Service.Install now refuses to run the configured
Installer at all when InstallType is unset or installtype.Unknown, so
an unstamped or misconfigured build can no longer silently attempt an
update it has no way to know is even the right kind for how this
instance was deployed. Wired through every real archied build path so
the gate doesn't just exist in principle: Taskfile.yml's build task
(binary), Dockerfile.archied (container), and install.sh (binary) all
now stamp it; cmd/archied/telegram_setup.go wires
Service.InstallType from installtype.Type().

Configuration-page display of install type/version and post-update
verification against the actually-running reference (not just the
installer's reported version) remain open -- this lands the
correctness gate, not the full observability surface.

Refs #522
2026-08-15 10:34:46 +10:00

149 lines
4.7 KiB
Go

package releaseupdate
import (
"context"
"path/filepath"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/installtype"
)
func TestServiceDefersOnlyTheVersionsShownToThatRecipient(t *testing.T) {
catalog := &catalogStub{snapshot: Snapshot{Components: []Component{
{ID: "gateway", Label: "THE GATEWAY", Installed: "v0.1.0", Available: "v0.1.1", Changelog: "- Clearer help"},
{ID: "runtime", Label: "THE RUNTIME", Installed: "v0.1.0"},
}}}
service := Service{Catalog: catalog, StatePath: filepath.Join(t.TempDir(), "deferrals.json")}
first, err := service.Check(context.Background(), 42)
if err != nil {
t.Fatal(err)
}
if got := len(first.Available()); got != 1 {
t.Fatalf("available before defer = %d, want 1", got)
}
if err := service.Defer(context.Background(), 42, first); err != nil {
t.Fatal(err)
}
deferred, err := service.Check(context.Background(), 42)
if err != nil {
t.Fatal(err)
}
if got := len(deferred.Available()); got != 0 {
t.Fatalf("available after defer = %d, want 0", got)
}
if !deferred.Deferred {
t.Error("Deferred = false, want true")
}
catalog.snapshot.Components[0].Available = "v0.1.2"
newer, err := service.Check(context.Background(), 42)
if err != nil {
t.Fatal(err)
}
if got := len(newer.Available()); got != 1 {
t.Fatalf("available for newer version = %d, want 1", got)
}
}
func TestServiceInstallForwardsProgressAndFailure(t *testing.T) {
installer := &installerStub{}
service := Service{Installer: installer, InstallType: "binary"}
var progress []string
if err := service.Install(context.Background(), func(message string) { progress = append(progress, message) }); err != nil {
t.Fatal(err)
}
if !installer.called {
t.Error("installer was not called")
}
if len(progress) != 1 || progress[0] != "Pulling release..." {
t.Errorf("progress = %#v", progress)
}
}
// TestServiceInstallRefusesAnUnknownInstallType is the fail-closed guarantee
// that makes InstallType worth having: a binary the release pipeline never
// stamped (or a caller that never wired InstallType at all) must not run
// whatever install command happens to be configured -- there is no way to
// know, at that point, whether it is even the right kind of update for how
// this instance was actually deployed.
func TestServiceInstallRefusesAnUnknownInstallType(t *testing.T) {
tests := []struct {
name string
installType string
}{
{"unset InstallType", ""},
{"explicit unknown", installtype.Unknown},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
installer := &installerStub{}
service := Service{Installer: installer, InstallType: tt.installType}
err := service.Install(context.Background(), func(string) {})
if err == nil {
t.Fatal("Install() returned nil error, want a refusal")
}
if installer.called {
t.Error("installer was called despite an unknown install type")
}
})
}
}
func TestNilServiceReturnsConfigurationErrors(t *testing.T) {
var service *Service
if _, err := service.Check(context.Background(), 0); err == nil {
t.Fatal("nil service check returned nil error")
}
if err := service.Defer(context.Background(), 0, Snapshot{}); err == nil {
t.Fatal("nil service defer returned nil error")
}
if err := service.Install(context.Background(), nil); err == nil {
t.Fatal("nil service install returned nil error")
}
if service.CanInstall() {
t.Fatal("nil service reports that installation is available")
}
}
func TestFormatSnapshotReportsAvailableAndUnchangedComponents(t *testing.T) {
got := FormatSnapshot(Snapshot{Components: []Component{
{Label: "THE GATEWAY", Installed: "v1", Available: "v2", Changelog: "- safer updates"},
{Label: "THE RUNTIME", Installed: "v1"},
}})
for _, want := range []string{"Archie has an update available", "v2 available", "safer updates", "No runtime changes"} {
if !strings.Contains(got, want) {
t.Errorf("formatted snapshot missing %q: %s", want, got)
}
}
}
func TestSameAvailableIgnoresMetadataAndDetectsVersionChanges(t *testing.T) {
left := Snapshot{Components: []Component{{ID: "gateway", Installed: "v1", Available: "v2", Changelog: "old"}}}
right := Snapshot{Components: []Component{{ID: "gateway", Installed: "v1", Available: "v2", Changelog: "new"}}}
if !SameAvailable(left, right) {
t.Fatal("matching available versions should be equal")
}
right.Components[0].Available = "v3"
if SameAvailable(left, right) {
t.Fatal("changed available versions should not be equal")
}
}
type catalogStub struct{ snapshot Snapshot }
func (s *catalogStub) Check(context.Context) (Snapshot, error) { return s.snapshot, nil }
type installerStub struct{ called bool }
func (s *installerStub) Install(_ context.Context, progress func(string)) error {
s.called = true
progress("Pulling release...")
return nil
}