Files
magpie/internal/cli/update_find_test.go
T
yetone 1315ac7d99 update: the first update finds a magpie without gateway.json, and a restart waits a bounded time (ljlwlcz on X)
Two edges of v0.1.3687's gateway.json:

1. The first update from a magpie older than the record found no gateway.json,
   asked 3425, said "isn't running" and left a service on MAGPIE_ADDR=0.0.0.0:10100
   on the old version. With no record, no MAGPIE_ADDR and no answer at the usual
   address, `magpie update` now tries the installed service's MAGPIE_ADDR (read
   from its unit or launch agent, daemon.Addr), the port in Settings and 3425,
   and takes the first that answers as magpie. --addr and MAGPIE_ADDR stay the
   user's word.
2. A restart asked by `magpie update` waited on "N requests" with no end (Windows
   hands nothing over, long streams never finish). The app and magpie serve now
   wait at most 10 minutes for that ask, then restart; the output says so
   ("or in 10 min at the latest, which cuts what is still running"). `magpie
   update --force` restarts at once (gateway.SetForceRestarter), and says if the
   running magpie is too old to know --force. The app's own look at the disk
   (followReplaced) and autonomous serve follow still never cut a request.

Run (mote refused SSH; win-run on xiaomi-gpu): go vet cli/gui/gateway/daemon
(windows), linux nogui vet; go test -run TestUpdate|TestServeFollow|
TestTerminalUpdate|TestRestart over cli, gui, gateway, daemon: ok except
TestUpdateThatWontStart, red on origin/main on the same box too (#6059).
Broken by hand: drain bound off, record fallback off, serve bound off: the new
tests TestTerminalUpdateWaitIsBoundedAndCanBeForced, TestUpdateFindsAMagpieThatWroteNoRecord,
TestServeFollowDrainIsBounded go red.
Not tried: a real systemd service upgrade; darwin cross vet (cgo) and the full
suite (started after the tag if mote is back).
2026-11-11 13:01:11 +08:00

77 lines
3.1 KiB
Go

package cli
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/gateway"
)
// ljlwlcz on X: the first update from a magpie older than gateway.json
// finds no record, looked at 3425 and said "isn't running" while the
// service ran on 10100. Without a record it looks where such a magpie runs.
func TestUpdateFindsAMagpieThatWroteNoRecord(t *testing.T) {
fakeServing(t, 4242) // sandbox HOME, no record, MAGPIE_ADDR unset
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, `{"name":"magpie","version":"v0.1.3600"}`)
}))
defer ts.Close()
oldProbe, oldCands := updateProbe, updateCandidates
t.Cleanup(func() { updateProbe, updateCandidates = oldProbe, oldCands })
updateProbe = func(u string) bool { return gateway.ServedAt(u).Running }
updateCandidates = func() []string { return []string{"http://127.0.0.1:1", ts.URL} }
if got := updateBase(""); got != ts.URL {
t.Errorf("no record, the service on another port: %q, want %q", got, ts.URL)
}
if got := updateBase("http://127.0.0.1:7"); got != "http://127.0.0.1:7" {
t.Errorf("--addr is the user's word: %q", got)
}
t.Setenv("MAGPIE_ADDR", "127.0.0.1:9")
if got := updateBase(""); got != gateway.URL() {
t.Errorf("MAGPIE_ADDR is the user's word: %q", got)
}
// nothing answers anywhere: the address the command knew
t.Setenv("MAGPIE_ADDR", "")
updateCandidates = func() []string { return []string{"http://127.0.0.1:1"} }
if got := updateBase(""); got != gateway.ClientURL() {
t.Errorf("nothing running: %q", got)
}
}
// --force reaches the running magpie, and its answer is told plainly: a
// forced restart, a bounded wait with its minutes, and an old magpie that
// ignored --force.
func TestUpdateForceAndTheBoundedWaitAreSaid(t *testing.T) {
var reply gateway.RestartReply
var body string
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
body = string(b)
json.NewEncoder(w).Encode(reply)
}))
defer ts.Close()
reply = gateway.RestartReply{State: "restarting", Version: "v0.1.1", Forced: true}
if got := restartAsk(ts.URL, "v0.1.2", true); !strings.Contains(got, "whatever it has in flight") || !strings.Contains(body, `"force":true`) {
t.Errorf("forced: %q (asked %s)", got, body)
}
reply = gateway.RestartReply{State: "waiting", Version: "v0.1.1", Busy: &gateway.Busy{Requests: 3}, Drain: 600}
got := restartAsk(ts.URL, "v0.1.2", false)
if !strings.Contains(got, "3 request(s)") || !strings.Contains(got, "10 min at the latest") || !strings.Contains(got, "--force") || !strings.Contains(body, `"force":false`) {
t.Errorf("bounded wait: %q (asked %s)", got, body)
}
reply = gateway.RestartReply{State: "waiting", Version: "v0.1.1", Busy: &gateway.Busy{Requests: 3}}
if got := restartAsk(ts.URL, "v0.1.2", true); !strings.Contains(got, "too old to know --force") {
t.Errorf("an old magpie that ignores --force: %q", got)
}
rest, _, _, force, err := updateFlagsForce([]string{"update", "--force"})
if err != nil || !force || len(rest) != 1 {
t.Errorf("--force parsed as %v %v %v", rest, force, err)
}
}