Update from a git repository

router.update.repository names a public repository whose tags vX.Y.Z are
releases. router-update check (daily, and from the Updates page) looks for a
newer one and the Overview says so; router-update --git builds the router
from a tag like a bundle update: the settings are migrated, the new version
must be confirmed, the nixpkgs pin stays. The flake of such a router comes
from the release's config-flake.nix. The global flake registry is off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 08:57:00 +01:00
co-authored by Claude Sonnet 5.5
parent a2dbeb5dea
commit 3fc54e678e
13 changed files with 589 additions and 26 deletions
+102
View File
@@ -0,0 +1,102 @@
package main
import (
"encoding/json"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
)
func TestUpdatesFromGit(t *testing.T) {
a, sys := newTestApp(t)
sys.ReleaseFile = filepath.Join(t.TempDir(), "router-release")
os.WriteFile(sys.ReleaseFile, []byte(`{"version":"1.0.0","serial":1000}`), 0o600)
a.sys = sys
orig := startUnit
var started []string
startUnit = func(unit string) error { started = append(started, unit); return nil }
defer func() { startUnit = orig }()
h := a.routes()
cookie := login(t, h)
token := csrfFrom(t, do(h, "GET", "/update", nil, cookie).Body.String())
post := func(path string, form url.Values) string {
form.Set("csrf", token)
return do(h, "POST", path, form, cookie).Header().Get("Location")
}
// The repository: an address of a public repository, nothing else.
for _, bad := range []string{"git@git.example.org:me/r.git", "https://user:pw@git.example.org/r", "https://git.example.org/r?x=1", "file:///etc", "https://git.example.org/a b"} {
if loc := post("/update/repository", url.Values{"repository": {bad}}); !strings.Contains(loc, "Not+saved") {
t.Errorf("%q was accepted: %s", bad, loc)
}
}
if loc := post("/update/gitcheck", url.Values{}); !strings.Contains(loc, "Set+the+repository") || len(started) != 0 {
t.Fatalf("a check without a repository: %q %v", loc, started)
}
if loc := post("/update/gitinstall", url.Values{"tag": {"v1.1.0"}}); !strings.Contains(loc, "Set+the+repository") {
t.Fatalf("an install without a repository: %q", loc)
}
post("/update/repository", url.Values{"repository": {"https://git.example.org/me/nixos-router/"}})
s, _ := loadSettings(sys.settingsPath())
if got := s.repository(); got != "https://git.example.org/me/nixos-router" {
t.Fatalf("repository: %q", got)
}
// It takes effect when updating, so it is no unapplied change.
if c, err := loadSettings(sys.confirmedPath()); err == nil && c.repository() != "" {
t.Logf("confirmed settings carry it: %q", c.repository())
}
// Looking starts the unit; what it found is shown while it is about this repository and version.
post("/update/gitcheck", url.Values{})
if len(started) != 1 || started[0] != "router-update-check.service" {
t.Fatalf("started %v", started)
}
if body := do(h, "GET", "/update", nil, cookie).Body.String(); !strings.Contains(body, "Not looked yet") {
t.Fatalf("a result before any look:\n%s", body)
}
write := func(repo, installed string, newer bool) {
os.MkdirAll(sys.updateDir(), 0o755)
data, _ := json.Marshal(map[string]any{"repository": repo, "time": "2026-11-26T08:00:00Z", "installed": installed,
"newest": map[string]string{"tag": "v1.1.0", "version": "1.1.0", "rev": "0123456789abcdef0123456789abcdef01234567"}, "newer": newer})
os.WriteFile(filepath.Join(sys.updateDir(), "check.json"), data, 0o644)
}
write("https://git.example.org/someone-else/nixos-router", "1.0.0", true)
if body := do(h, "GET", "/", nil, cookie).Body.String(); strings.Contains(body, "is out") {
t.Fatalf("a notice from another repository:\n%s", body)
}
write("https://git.example.org/me/nixos-router", "0.9.0", true)
if body := do(h, "GET", "/", nil, cookie).Body.String(); strings.Contains(body, "is out") {
t.Fatalf("a notice from before the last update:\n%s", body)
}
write("https://git.example.org/me/nixos-router", "1.0.0", true)
for _, path := range []string{"/", "/update"} {
if body := do(h, "GET", path, nil, cookie).Body.String(); !strings.Contains(body, "1.1.0") || (path == "/" && !strings.Contains(body, "nixos-router 1.1.0 is out")) {
t.Fatalf("%s lacks the release:\n%s", path, body)
}
}
write("https://git.example.org/me/nixos-router", "1.0.0", false)
if body := do(h, "GET", "/", nil, cookie).Body.String(); strings.Contains(body, "nixos-router 1.1.0 is out") {
t.Fatalf("a notice when up to date")
}
// Installing asks the update unit to build the tag, and only a release tag.
if loc := post("/update/gitinstall", url.Values{"tag": {"v1.1.0; reboot"}}); !strings.Contains(loc, "Not+a+release+tag") {
t.Fatalf("a bad tag: %q", loc)
}
started = nil
post("/update/gitinstall", url.Values{"tag": {"v1.1.0"}, "force": {"on"}})
var req struct {
Mode string `json:"mode"`
URL string `json:"url"`
Force bool `json:"force"`
}
data, _ := os.ReadFile(filepath.Join(sys.updateDir(), "request.json"))
if err := json.Unmarshal(data, &req); err != nil || req.Mode != "git" || req.URL != "v1.1.0" || !req.Force {
t.Fatalf("request: %s", data)
}
if len(started) != 1 || started[0] != "router-update.service" {
t.Fatalf("started %v", started)
}
}