fix(apply,shaterd): the offline stub alarmed about an apply nobody attempted
`config_applied: false` means "/etc/config/shater was read and REFUSED — what is running is the PREVIOUS configuration, your edit is not in effect", and the panel draws a critical band saying exactly that. The field was a plain bool, so that alarm was the ZERO VALUE OF THE TYPE — and `shaterd status`'s offline stub, built by a process that never applied anything, over a data plane that may have been installed and enforcing for weeks, published it by simply never mentioning the field. It is the config_readable defect returning in a new field, with the one difference that decides the fix: config_readable can be MEASURED by the stub and now is, while this one cannot be measured at all without a daemon. So the field says nothing when nobody measured it. ConfigApplied becomes a *bool with omitempty; the live Applier.Status() assigns a verdict on BOTH arms, so an absent key can only come from something that is not a live status. That is the same closed-set-plus-unknown shape `plane`, `traffic` and `daemon_answered` already have, and the one panel/src/appliedConfig.ts already implements (=== true / === false / else unknown). The Go doc claiming absence should read as false is gone: it contradicted the only consumer, and the consumer was right. The four fields around it (apply_error, apply_error_stage, apply_attempts, apply_failed_since_unix) stay plain: they are qualified by config_applied the way enabled/kill_switch/panel_port are qualified by config_readable, and their zero values point at "nothing was refused" — the quiet side, not the alarm. Also: the stub shipped `warnings: null` on its happy path while apply.Status documents Warnings as always non-nil so a consumer can map over it unconditionally. Three states, distinguishable ON THE WIRE through one `shaterd status`, with the control that would catch the opposite break (a build that omitted the key for a real refusal, deleting the alarm from the product). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BHw89tdWddzhjUc4bAH4tS
This commit is contained in:
1 parent
9d7f0dc92f
commit
1708159ecf
5 files changed
+401
-22
No files matched your search
+63
-14
@@ -2332,20 +2332,37 @@ type Status struct {
|
||||
EngineRunning bool `json:"engine_running"`
|
||||
|
||||
// ConfigApplied is whether the configuration ON DISK is the one that is
|
||||
// RUNNING. False means /etc/config/shater was read, refused, and what the engine
|
||||
// carries is something else — see ApplyError/ApplyErrorStage for why, and
|
||||
// Applier.rejected for the measurement that made this field necessary.
|
||||
// RUNNING, and it has THREE states, because "nobody tried" is a different
|
||||
// answer from "it was tried and refused":
|
||||
//
|
||||
// It is a different question from every neighbouring field, and that is the
|
||||
// point: `running`/`engine_running` say a box is up, `hash` identifies the config
|
||||
// that box is running, `plane` says the kernel side is installed, and all four
|
||||
// can be at their healthiest value while the file the operator just edited has
|
||||
// not been applied at all.
|
||||
// true — what the engine carries IS what /etc/config/shater says.
|
||||
// false — the file was read, REFUSED, and what the engine carries is
|
||||
// something else. See ApplyError/ApplyErrorStage for why, and
|
||||
// Applier.rejected for the measurement that made this necessary.
|
||||
// ABSENT — nobody measured it. A live Applier.Status() ALWAYS publishes one
|
||||
// of the two verdicts (see Status below), so a missing key can only
|
||||
// come from an object that is not a live status: the offline stub
|
||||
// `shaterd status` prints when no daemon answers
|
||||
// (cmd/shaterd/main.go offlineStatusStub), or a daemon predating
|
||||
// this field.
|
||||
//
|
||||
// Positive phrasing, for the same reason as ConfigReadable: a consumer that has
|
||||
// not been taught this field reads the absent value as false and lands on the
|
||||
// side that makes it look, not on the side that reassures.
|
||||
ConfigApplied bool `json:"config_applied"`
|
||||
// WHY IT IS A POINTER. `false` here publishes a CRITICAL finding that opens
|
||||
// "THE CONFIGURATION ON DISK HAS NOT BEEN APPLIED … your edit is not in
|
||||
// effect". A plain bool has no third value, so that alarm was the ZERO VALUE OF
|
||||
// THE TYPE — and the offline stub, built by a process that never applied
|
||||
// anything, over a data plane that may have been installed and working for
|
||||
// weeks, published it by simply not mentioning the field. That is the
|
||||
// ConfigReadable defect again in a new field, with one difference that decides
|
||||
// the fix: ConfigReadable could be MEASURED by the stub and now is, while this
|
||||
// one cannot be measured at all without a daemon. What cannot be measured must
|
||||
// be left unsaid, and nil is the only value of a bool that says nothing.
|
||||
//
|
||||
// Consumers read it as a CLOSED SET OF TWO and treat anything else as unknown —
|
||||
// the same shape `daemon_answered` already has in luci-app-shater's
|
||||
// dashboard.js and `config_applied` already has in panel/src/appliedConfig.ts
|
||||
// (appliedState: === true, === false, else 'unknown'). Go consumers should use
|
||||
// Status.Applied() rather than dereferencing.
|
||||
ConfigApplied *bool `json:"config_applied,omitempty"`
|
||||
|
||||
// ApplyError is why the configuration on disk was refused, verbatim, and
|
||||
// ApplyErrorStage is which step refused it ("the schema gate", "building the
|
||||
@@ -2358,6 +2375,15 @@ type Status struct {
|
||||
// standing condition, and the retry is on a widening interval (retryBackoff) —
|
||||
// so a consumer that only saw "failed" would not be able to tell an operator
|
||||
// whether anything is still trying.
|
||||
//
|
||||
// ALL FOUR ARE QUALIFIED BY ConfigApplied, exactly as Enabled/KillSwitch/
|
||||
// PanelPort are qualified by ConfigReadable: they describe a refusal, so they
|
||||
// are only readable once ConfigApplied has said there IS one. With the verdict
|
||||
// absent they are zero values and mean nothing — and note that their zero
|
||||
// values ("", 0) are the QUIET side, so a consumer that reads them without the
|
||||
// verdict is misled towards "nothing was refused", never towards a refusal
|
||||
// that did not happen. That is why the pointer above is on the one field whose
|
||||
// zero value shouts.
|
||||
ApplyError string `json:"apply_error"`
|
||||
ApplyErrorStage string `json:"apply_error_stage"`
|
||||
ApplyAttempts int `json:"apply_attempts"`
|
||||
@@ -2486,14 +2512,22 @@ func (a *Applier) Status() Status {
|
||||
// Is what is running what is on disk? Published as its own fact rather than left
|
||||
// to be inferred from the warning list, so a machine consumer (the LuCI
|
||||
// dashboard, the cron watchdog) can act on it without matching prose.
|
||||
//
|
||||
// A VERDICT IS ALWAYS PUBLISHED HERE, both ways, and that is what makes the
|
||||
// absent third state mean anything: this is a live Applier, so "refused" and
|
||||
// "applied" are both measurements, and a status object with no config_applied
|
||||
// key therefore cannot have come from one. Assign the pointer outside the
|
||||
// branches so a future edit to either arm cannot leave it nil and turn a real
|
||||
// refusal into "nobody knows".
|
||||
applied := true
|
||||
if rej := a.rejectedSnapshot(); rej != nil {
|
||||
applied = false
|
||||
s.ApplyError = rej.err.Error()
|
||||
s.ApplyErrorStage = rej.stage
|
||||
s.ApplyAttempts = rej.attempts
|
||||
s.ApplyFailedSinceUnix = rej.firstAt.Unix()
|
||||
} else {
|
||||
s.ConfigApplied = true
|
||||
}
|
||||
s.ConfigApplied = &applied
|
||||
switch {
|
||||
case !tableLoaded:
|
||||
s.Plane = "none"
|
||||
@@ -2582,6 +2616,21 @@ func (a *Applier) Status() Status {
|
||||
return s
|
||||
}
|
||||
|
||||
// Applied is ConfigApplied read as the three states it actually has: known=false
|
||||
// means NOBODY MEASURED IT (the offline stub, or a daemon predating the field),
|
||||
// and applied carries nothing in that case.
|
||||
//
|
||||
// It exists so no Go consumer has to remember the nil case. `if s.ConfigApplied`
|
||||
// does not compile against a pointer — which is the point of the type — and the
|
||||
// obvious repair, `if *s.ConfigApplied`, panics on exactly the object this whole
|
||||
// distinction is about. One accessor, one place to be right.
|
||||
func (s Status) Applied() (applied, known bool) {
|
||||
if s.ConfigApplied == nil {
|
||||
return false, false
|
||||
}
|
||||
return *s.ConfigApplied, true
|
||||
}
|
||||
|
||||
// effectivePanelPort maps the configured globals.panel_port to the port reported
|
||||
// in status: the configured value, or the built-in default (8088) when unset.
|
||||
func effectivePanelPort(configured int) int {
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
package apply
|
||||
|
||||
// CONFIG_APPLIED HAS THREE STATES ON THE WIRE, AND THE THIRD ONE IS LOAD-BEARING.
|
||||
//
|
||||
// THE DEFECT (2026-07-27). `false` on this field publishes a critical finding
|
||||
// that opens "THE CONFIGURATION ON DISK HAS NOT BEEN APPLIED … what is running is
|
||||
// the PREVIOUS CONFIGURATION … your edit is not in effect", and the panel draws a
|
||||
// band across the page to that effect. The field was a plain `bool`, so that
|
||||
// alarm was the ZERO VALUE OF THE TYPE — and `shaterd status`'s offline stub
|
||||
// (cmd/shaterd/main.go offlineStatusStub), built by a process that never applied
|
||||
// anything, over a data plane that may have been installed and working for weeks,
|
||||
// published it by simply never mentioning the field.
|
||||
//
|
||||
// The fix is a *bool with omitempty: nil is omitted, so "nobody measured this" is
|
||||
// sayable. That only helps if the LIVE path never leaves it nil — otherwise a
|
||||
// real refusal would become "nobody knows" and the alarm would be lost in the one
|
||||
// state it exists for. THAT is what this file pins, in both directions, on the
|
||||
// wire:
|
||||
//
|
||||
// live + refused -> "config_applied": false (present, and alarming)
|
||||
// live + applied -> "config_applied": true (present, and calm)
|
||||
// not a live status -> the key is ABSENT (says nothing at all)
|
||||
//
|
||||
// THE CONTROL is that all three are DISTINGUISHABLE. A build that omitted the key
|
||||
// whenever the answer was `false` would satisfy "the stub says nothing" perfectly
|
||||
// and would be the worse defect pointed the other way.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/shater/engine"
|
||||
"github.com/sagernet/sing-box/shater/model"
|
||||
)
|
||||
|
||||
// appliedKeyOnTheWire marshals s and reports what `config_applied` looks like to
|
||||
// a consumer: present, and if so with which value. Every consumer reads JSON, not
|
||||
// the struct, so the struct alone would prove nothing about the state that is
|
||||
// expressed by a key NOT BEING THERE.
|
||||
func appliedKeyOnTheWire(t *testing.T, s Status) (raw any, present bool) {
|
||||
t.Helper()
|
||||
b, err := s.JSON()
|
||||
if err != nil {
|
||||
t.Fatalf("Status.JSON: %v", err)
|
||||
}
|
||||
var obj map[string]any
|
||||
if err := json.Unmarshal(b, &obj); err != nil {
|
||||
t.Fatalf("status is not a JSON object (%v):\n%s", err, b)
|
||||
}
|
||||
raw, present = obj["config_applied"]
|
||||
return raw, present
|
||||
}
|
||||
|
||||
// TestLiveStatusAlwaysShipsAnAppliedVerdict: both live answers are published, so
|
||||
// an absent key can only have come from something that is not a live Applier.
|
||||
// That is the entire justification for reading absence as "unknown", and if it
|
||||
// stopped holding, every consumer's third state would start swallowing real
|
||||
// refusals.
|
||||
func TestLiveStatusAlwaysShipsAnAppliedVerdict(t *testing.T) {
|
||||
applier := New(engine.New(), nil)
|
||||
|
||||
// Applied — nothing refused.
|
||||
raw, present := appliedKeyOnTheWire(t, applier.Status())
|
||||
if !present {
|
||||
t.Error("a live status with nothing refused omits config_applied: absence is reserved " +
|
||||
"for objects that did not measure it, and a consumer reading it as 'unknown' would " +
|
||||
"never be told the configuration IS running")
|
||||
} else if raw != true {
|
||||
t.Errorf(`config_applied = %v with nothing refused, want true`, raw)
|
||||
}
|
||||
|
||||
// Refused — the state the alarm exists for.
|
||||
applier.noteRejected(&model.Model{}, stageEngine, errors.New("start: address already in use"), time.Now())
|
||||
raw, present = appliedKeyOnTheWire(t, applier.Status())
|
||||
if !present {
|
||||
t.Fatal("A STANDING REFUSAL WAS PUBLISHED WITH NO config_applied KEY. Every consumer " +
|
||||
"reads an absent key as 'nothing is known' (panel appliedState, dashboard.js), so the " +
|
||||
"refusal — the one thing this field was added for — would be silently dropped")
|
||||
}
|
||||
if raw != false {
|
||||
t.Errorf("config_applied = %v over a refused configuration, want false", raw)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAStatusNobodyMeasuredShipsNoAppliedVerdict is the other end: a Status that
|
||||
// was assembled by something other than a live Applier — which is exactly what
|
||||
// cmd/shaterd's offline stub is — must carry no verdict at all.
|
||||
//
|
||||
// It is asserted here, on the struct's own marshalling, because that is where the
|
||||
// property lives: it is the zero value of *bool plus omitempty, not anything the
|
||||
// stub does. A future producer of a Status gets it for free, which is the point.
|
||||
func TestAStatusNobodyMeasuredShipsNoAppliedVerdict(t *testing.T) {
|
||||
if raw, present := appliedKeyOnTheWire(t, Status{}); present {
|
||||
t.Errorf("a Status nobody measured published config_applied = %v. `false` here is read as "+
|
||||
"'the file was refused, the PREVIOUS configuration is running, your edit is not in "+
|
||||
"effect' — over a box where nothing ever tried to apply anything", raw)
|
||||
}
|
||||
if applied, known := (Status{}).Applied(); known || applied {
|
||||
t.Errorf("Status{}.Applied() = (%v, %v), want (false, false): unknown is a state, not a verdict",
|
||||
applied, known)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTheThreeAppliedStatesAreDistinguishable IS THE CONTROL for both tests
|
||||
// above. Each of them, alone, is satisfied by a constant: "always absent" passes
|
||||
// the second, "always present" passes the first. Three states have to be three.
|
||||
func TestTheThreeAppliedStatesAreDistinguishable(t *testing.T) {
|
||||
applier := New(engine.New(), nil)
|
||||
okRaw, okPresent := appliedKeyOnTheWire(t, applier.Status())
|
||||
|
||||
applier.noteRejected(&model.Model{}, stageGenerate, errors.New("boom"), time.Now())
|
||||
badRaw, badPresent := appliedKeyOnTheWire(t, applier.Status())
|
||||
|
||||
unknownRaw, unknownPresent := appliedKeyOnTheWire(t, Status{})
|
||||
|
||||
if !okPresent || !badPresent {
|
||||
t.Fatalf("a live status must always publish a verdict (applied present=%v, refused present=%v)",
|
||||
okPresent, badPresent)
|
||||
}
|
||||
if okRaw == badRaw {
|
||||
t.Errorf("applied and refused are indistinguishable on the wire: both %v", okRaw)
|
||||
}
|
||||
if unknownPresent {
|
||||
t.Errorf("unmeasured is indistinguishable from a verdict: it published %v", unknownRaw)
|
||||
}
|
||||
// And the pairing that the offline stub turns on: "nobody tried" must not look
|
||||
// like "it was tried and refused".
|
||||
if unknownPresent == badPresent {
|
||||
t.Error("an unmeasured status and a REFUSED one look the same to a consumer — " +
|
||||
"which is a router with no daemon being told its edit is not in effect")
|
||||
}
|
||||
}
|
||||
@@ -95,7 +95,10 @@ func TestRejectedConfigIsVisibleInStatus(t *testing.T) {
|
||||
}
|
||||
|
||||
s := a.Status()
|
||||
if s.ConfigApplied {
|
||||
if applied, known := s.Applied(); !known {
|
||||
t.Error("config_applied is ABSENT from a LIVE status — absence is reserved for the offline " +
|
||||
"stub, which cannot measure this; a live daemon that says nothing here loses the refusal")
|
||||
} else if applied {
|
||||
t.Error("config_applied = true over a configuration that was refused")
|
||||
}
|
||||
if !strings.Contains(s.ApplyError, "invalid domain") {
|
||||
@@ -143,7 +146,9 @@ func TestHealthyBoxRaisesNoRejectedState(t *testing.T) {
|
||||
}
|
||||
|
||||
s := a.Status()
|
||||
if !s.ConfigApplied {
|
||||
if applied, known := s.Applied(); !known {
|
||||
t.Error("config_applied is ABSENT from a LIVE status after a successful apply")
|
||||
} else if !applied {
|
||||
t.Error("config_applied = false after a successful apply: what is running IS what is on disk")
|
||||
}
|
||||
if s.ApplyError != "" || s.ApplyErrorStage != "" || s.ApplyAttempts != 0 || s.ApplyFailedSinceUnix != 0 {
|
||||
@@ -179,8 +184,9 @@ func TestASuccessfulApplyRetiresTheRejectedState(t *testing.T) {
|
||||
if err := applyOnce(t, a, m); err == nil {
|
||||
t.Fatal("fixture: expected a failure")
|
||||
}
|
||||
if a.Status().ConfigApplied {
|
||||
t.Fatal("fixture: the refusal was not recorded, so this test would prove nothing")
|
||||
if applied, known := a.Status().Applied(); !known || applied {
|
||||
t.Fatalf("fixture: the refusal was not recorded (applied=%v known=%v), so this test "+
|
||||
"would prove nothing", applied, known)
|
||||
}
|
||||
|
||||
fail = false
|
||||
@@ -188,8 +194,8 @@ func TestASuccessfulApplyRetiresTheRejectedState(t *testing.T) {
|
||||
t.Fatalf("the second apply must succeed: %v", err)
|
||||
}
|
||||
s := a.Status()
|
||||
if !s.ConfigApplied || s.ApplyError != "" {
|
||||
t.Errorf("the refusal outlived its cause: applied=%v err=%q", s.ConfigApplied, s.ApplyError)
|
||||
if applied, known := s.Applied(); !known || !applied || s.ApplyError != "" {
|
||||
t.Errorf("the refusal outlived its cause: applied=%v known=%v err=%q", applied, known, s.ApplyError)
|
||||
}
|
||||
if w, ok := rejectedFinding(s.Warnings); ok {
|
||||
t.Errorf("the finding outlived its cause:\n%s", w.Message)
|
||||
@@ -267,8 +273,10 @@ func TestRejectedConfigIsRecordedForEveryStageThatCanRefuse(t *testing.T) {
|
||||
if err := applyOnce(t, a, holdModel("closed")); err == nil {
|
||||
t.Fatal("fixture: the schema gate was supposed to refuse")
|
||||
}
|
||||
if s := a.Status(); s.ConfigApplied || s.ApplyErrorStage != stageSchemaGate {
|
||||
t.Errorf("schema gate: applied=%v stage=%q, want false/%q", s.ConfigApplied, s.ApplyErrorStage, stageSchemaGate)
|
||||
s := a.Status()
|
||||
if applied, known := s.Applied(); !known || applied || s.ApplyErrorStage != stageSchemaGate {
|
||||
t.Errorf("schema gate: applied=%v known=%v stage=%q, want false/true/%q",
|
||||
applied, known, s.ApplyErrorStage, stageSchemaGate)
|
||||
}
|
||||
|
||||
// Stage: the engine. (generate's own refusal is exercised through the same
|
||||
|
||||
@@ -1195,6 +1195,29 @@ func cmdStatus() int {
|
||||
// luci-app-shater currently keys "daemon: down" off, and dropping it would break
|
||||
// that page silently. It is a COMPATIBILITY carry-over, not the contract.
|
||||
//
|
||||
// "The zero value plus daemon_answered=false" only holds while every zero value
|
||||
// is a QUIET one. Two fields have needed work for exactly that reason, and the
|
||||
// rule they share is the one to apply to the next field a daemon grows:
|
||||
// A ZERO VALUE THAT SHOUTS IS NOT AN ABSENCE OF MEASUREMENT, IT IS A FALSE ONE.
|
||||
//
|
||||
// - config_applied is left ABSENT (apply.Status.ConfigApplied is a *bool, nil
|
||||
// here). `false` on that field means "the file was read and REFUSED; what is
|
||||
// running is the PREVIOUS configuration; your edit is not in effect", and the
|
||||
// panel draws a critical band saying so — over a router where the daemon is
|
||||
// simply not running and nobody has tried to apply anything. Nothing here can
|
||||
// measure it: applying is what the daemon does. So this says nothing, which is
|
||||
// a state the field can express and a bool could not.
|
||||
// - warnings is an empty slice, never nil, because the live path guarantees
|
||||
// non-nil (Applier.warningsWith returns []Warning{}) and a consumer that maps
|
||||
// over it unconditionally — which is what that guarantee is FOR — would throw
|
||||
// on the stub's `null`.
|
||||
//
|
||||
// apply_error / apply_error_stage / apply_attempts / apply_failed_since_unix stay
|
||||
// at their zero values and are NOT a problem of the same kind: they describe a
|
||||
// refusal, they are only readable once config_applied has said there is one, and
|
||||
// their zero values ("", 0) point at "nothing was refused" rather than at a
|
||||
// refusal that never happened.
|
||||
//
|
||||
// # The two defects this function exists to close
|
||||
//
|
||||
// The stub used to publish `config_readable: false` after a SUCCESSFUL read. So
|
||||
@@ -1221,6 +1244,10 @@ func offlineStatusStub(read func() (*model.Model, error), active, table bool) ap
|
||||
Running: false,
|
||||
Active: active,
|
||||
Table: table,
|
||||
// Non-nil for the same reason the live path guarantees it: the panel maps
|
||||
// over this list without checking, and `null` on the wire is a crash where
|
||||
// an empty list is a page.
|
||||
Warnings: []apply.Warning{},
|
||||
// A compile-time constant, so the stub knows it as well as the daemon does.
|
||||
// It is the half that makes schema_version legible: on a DOWNGRADED router
|
||||
// the disk says v3 and looks perfectly normal until it is read next to the
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -177,6 +178,166 @@ func TestOfflineStubTellsSwitchedOffFromCannotTell(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// --- config_applied: the field the stub must NOT answer ----------------------
|
||||
//
|
||||
// THE DEFECT (2026-07-27), and it is ConfigReadable's twin with one difference
|
||||
// that decides the fix. `config_applied: false` means "/etc/config/shater was
|
||||
// read and REFUSED — what is running is the PREVIOUS configuration, your edit is
|
||||
// not in effect", and panel/src/appliedConfig.ts draws a critical band saying
|
||||
// exactly that. The field was a plain bool, so the stub published that alarm as
|
||||
// the zero value of a type, on a router where the daemon is simply not running
|
||||
// and NOTHING EVER TRIED TO APPLY ANYTHING — quite possibly over a data plane
|
||||
// that has been installed and working for weeks.
|
||||
//
|
||||
// ConfigReadable was fixed by MEASURING it here, because the stub can read the
|
||||
// file. This one cannot be measured here at all: applying is what the daemon
|
||||
// does. So the honest form is silence, and silence is a state a *bool can express
|
||||
// (apply.Status.ConfigApplied, nil + omitempty) where a bool could not.
|
||||
|
||||
// appliedKey reports how `config_applied` appears in a printed status object.
|
||||
func appliedKey(t *testing.T, out string) (raw any, present bool) {
|
||||
t.Helper()
|
||||
var obj map[string]any
|
||||
if err := json.Unmarshal([]byte(out), &obj); err != nil {
|
||||
t.Fatalf("status is not a JSON object (%v):\n%s", err, out)
|
||||
}
|
||||
raw, present = obj["config_applied"]
|
||||
return raw, present
|
||||
}
|
||||
|
||||
// TestOfflineStubAnswersNothingAboutTheAppliedConfig — the struct, both read
|
||||
// branches. A readable configuration and an unreadable one are the two ways into
|
||||
// this function, and neither one learns anything about whether what is on disk is
|
||||
// running.
|
||||
func TestOfflineStubAnswersNothingAboutTheAppliedConfig(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
read func() (*model.Model, error)
|
||||
}{
|
||||
{"a readable configuration", readOK(&model.Model{Globals: model.Globals{Enabled: true}})},
|
||||
{"an unreadable configuration", readFail(errors.New("no space left on device"))},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := offlineStatusStub(tc.read, true, true)
|
||||
if applied, known := s.Applied(); known {
|
||||
t.Errorf("the stub claims a verdict (applied=%v) it cannot have measured: with no "+
|
||||
"daemon nothing has tried to apply anything, and `false` here tells the owner "+
|
||||
"their edit is not in effect and an older configuration is carrying their traffic",
|
||||
applied)
|
||||
}
|
||||
// The four fields that describe a refusal must stay quiet with no refusal
|
||||
// to describe. Their zero values point AWAY from an alarm, which is why
|
||||
// they are not pointers — but a stub that filled them in would be
|
||||
// inventing a refusal outright.
|
||||
if s.ApplyError != "" || s.ApplyErrorStage != "" || s.ApplyAttempts != 0 || s.ApplyFailedSinceUnix != 0 {
|
||||
t.Errorf("the stub described a refusal that never happened: err=%q stage=%q attempts=%d since=%d",
|
||||
s.ApplyError, s.ApplyErrorStage, s.ApplyAttempts, s.ApplyFailedSinceUnix)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestStatusTellsRefusedFromNobodyTried IS THE CONTROL, and it is the whole
|
||||
// point of the change: three states, through the ONE command an operator and the
|
||||
// rpcd plugin actually run.
|
||||
//
|
||||
// Without the live half, "the stub omits the key" is satisfied by a build that
|
||||
// omits it always — which would delete the refusal alarm from the product in the
|
||||
// one state it exists for. So the same `shaterd status` is driven against a
|
||||
// daemon that reports a REFUSAL, a daemon that reports a healthy apply, and no
|
||||
// daemon at all, and the three outputs must differ from each other.
|
||||
func TestStatusTellsRefusedFromNobodyTried(t *testing.T) {
|
||||
// A daemon's own answer, relayed verbatim by cmdStatus.
|
||||
rejectedReply := `{
|
||||
"running": true,
|
||||
"plane": "full",
|
||||
"config_applied": false,
|
||||
"apply_error": "start: address already in use",
|
||||
"apply_error_stage": "starting the engine"
|
||||
}`
|
||||
appliedReply := `{
|
||||
"running": true,
|
||||
"plane": "full",
|
||||
"config_applied": true
|
||||
}`
|
||||
|
||||
var refused, healthy, nobody any
|
||||
var refusedPresent, healthyPresent, nobodyPresent bool
|
||||
|
||||
t.Run("a live daemon reporting a REFUSAL", func(t *testing.T) {
|
||||
liveDaemon(t, rejectedReply)
|
||||
out, code := captureStdout(t, cmdStatus)
|
||||
if code != 0 {
|
||||
t.Fatalf("`shaterd status` exited %d while a daemon answered:\n%s", code, out)
|
||||
}
|
||||
refused, refusedPresent = appliedKey(t, out)
|
||||
if !refusedPresent || refused != false {
|
||||
t.Fatalf("the CLI lost the daemon's refusal: config_applied = %v (present=%v)\n%s",
|
||||
refused, refusedPresent, out)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("a live daemon reporting a healthy apply", func(t *testing.T) {
|
||||
liveDaemon(t, appliedReply)
|
||||
out, code := captureStdout(t, cmdStatus)
|
||||
if code != 0 {
|
||||
t.Fatalf("`shaterd status` exited %d while a daemon answered:\n%s", code, out)
|
||||
}
|
||||
healthy, healthyPresent = appliedKey(t, out)
|
||||
if !healthyPresent || healthy != true {
|
||||
t.Fatalf("config_applied = %v (present=%v), want true\n%s", healthy, healthyPresent, out)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no daemon at all", func(t *testing.T) {
|
||||
noDaemon(t)
|
||||
out, _ := captureStdout(t, cmdStatus)
|
||||
nobody, nobodyPresent = appliedKey(t, out)
|
||||
if nobodyPresent {
|
||||
t.Fatalf("THE OFFLINE STUB PUBLISHED A VERDICT (%v). A router whose daemon is not "+
|
||||
"running gets 'the configuration on disk has not been applied, the PREVIOUS "+
|
||||
"configuration is what is running, your edit is not in effect' — about an apply "+
|
||||
"nobody attempted:\n%s", nobody, out)
|
||||
}
|
||||
// The stub is still a stub in every other respect, so this is not being
|
||||
// bought by turning it into something else.
|
||||
if !strings.Contains(out, `"`+statusDaemonAnsweredKey+`": false`) {
|
||||
t.Errorf("the stub lost its %q marker:\n%s", statusDaemonAnsweredKey, out)
|
||||
}
|
||||
})
|
||||
|
||||
if !refusedPresent || !healthyPresent {
|
||||
t.Fatal("a live daemon's verdict did not reach stdout; the comparison below would prove nothing")
|
||||
}
|
||||
if refused == healthy {
|
||||
t.Errorf("applied and refused are indistinguishable on the wire: both %v", refused)
|
||||
}
|
||||
if nobodyPresent == refusedPresent {
|
||||
t.Error("A DEAD DAEMON AND A REFUSED CONFIGURATION LOOK THE SAME to a consumer — " +
|
||||
"which is precisely the report this change exists to stop")
|
||||
}
|
||||
}
|
||||
|
||||
// TestOfflineStubShipsAWarningListThatCanBeMappedOver. apply.Status documents
|
||||
// Warnings as "always non-nil so the panel can map over it unconditionally", and
|
||||
// the live path keeps that promise (Applier.warningsWith returns []Warning{}).
|
||||
// The stub used to return nil on its happy path, which marshals to `null` — so
|
||||
// the one object shipped to a consumer that is ALREADY in trouble was the one
|
||||
// object that could break an unconditional map.
|
||||
func TestOfflineStubShipsAWarningListThatCanBeMappedOver(t *testing.T) {
|
||||
s := offlineStatusStub(readOK(&model.Model{Globals: model.Globals{Enabled: true}}), false, false)
|
||||
if s.Warnings == nil {
|
||||
t.Error("the stub's warning list is nil where the live path guarantees non-nil")
|
||||
}
|
||||
b, err := s.JSON()
|
||||
if err != nil {
|
||||
t.Fatalf("JSON: %v", err)
|
||||
}
|
||||
if strings.Contains(string(b), `"warnings": null`) {
|
||||
t.Errorf("the stub shipped `warnings: null`:\n%s", b)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOfflineStubShipsConfigReadableOnTheWire: the fix has to survive
|
||||
// marshalling, because every consumer reads JSON, not the struct.
|
||||
func TestOfflineStubShipsConfigReadableOnTheWire(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user