|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/AppsGanin/rospanel/internal/store" |
| 9 | +) |
| 10 | + |
| 11 | +// restartTestManager returns a manager with a node registered, ready to take |
| 12 | +// restart requests. |
| 13 | +func restartTestManager(t *testing.T) (*Manager, int64) { |
| 14 | + t.Helper() |
| 15 | + st, err := store.Open(filepath.Join(t.TempDir(), "restart.db")) |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("open: %v", err) |
| 18 | + } |
| 19 | + t.Cleanup(func() { _ = st.Close() }) |
| 20 | + n, err := st.CreateNode("edge", "203.0.113.9", "") |
| 21 | + if err != nil { |
| 22 | + t.Fatalf("create node: %v", err) |
| 23 | + } |
| 24 | + m := &Manager{ |
| 25 | + store: st, |
| 26 | + nodes: newNodeRegistry(), |
| 27 | + nodeRestart: map[int64]*nodeRestartReq{}, |
| 28 | + } |
| 29 | + return m, n.ID |
| 30 | +} |
| 31 | + |
| 32 | +// The restart button used to report success the moment it was clicked, which is how |
| 33 | +// a node with a dead supervisor could swallow three restarts in a row and still look |
| 34 | +// fine. A request must therefore survive being SENT and only clear when the node |
| 35 | +// reports an Xray that actually came back. |
| 36 | +func TestNodeRestartConfirmsOnlyOnAFreshXray(t *testing.T) { |
| 37 | + m, id := restartTestManager(t) |
| 38 | + |
| 39 | + if got := m.NodeRestartState(id); got != "" { |
| 40 | + t.Fatalf("a node nobody touched reads as %q", got) |
| 41 | + } |
| 42 | + if err := m.RequestNodeXrayRestart(id); err != nil { |
| 43 | + t.Fatalf("request: %v", err) |
| 44 | + } |
| 45 | + if got := m.NodeRestartState(id); got != RestartPending { |
| 46 | + t.Fatalf("right after the operator asked: %q, want %q", got, RestartPending) |
| 47 | + } |
| 48 | + |
| 49 | + // The node's poll returns and carries the command away. Xray was up since 1000. |
| 50 | + if !m.TakeNodeXrayRestart(id, 1000) { |
| 51 | + t.Fatal("the command was not handed to the node") |
| 52 | + } |
| 53 | + // Sending is not doing: the operator is still waiting to hear it happened. |
| 54 | + if got := m.NodeRestartState(id); got != RestartPending { |
| 55 | + t.Errorf("state on delivery = %q — reporting done when it was merely sent is "+ |
| 56 | + "the lie this exists to stop", got) |
| 57 | + } |
| 58 | + // And it is handed over exactly once: re-sending on every poll would restart the |
| 59 | + // node again and again while we wait. |
| 60 | + if m.TakeNodeXrayRestart(id, 1000) { |
| 61 | + t.Error("the command was handed over twice") |
| 62 | + } |
| 63 | + |
| 64 | + // A sync that still shows the SAME Xray proves nothing — it is the report that |
| 65 | + // was already in flight, or a node that never acted. |
| 66 | + m.ConfirmNodeXrayRestart(id, 1000) |
| 67 | + if got := m.NodeRestartState(id); got != RestartPending { |
| 68 | + t.Errorf("state = %q — an unchanged Xray start time was taken as proof", got) |
| 69 | + } |
| 70 | + |
| 71 | + // A different start time is the node's own proof that the process bounced. |
| 72 | + m.ConfirmNodeXrayRestart(id, 1042) |
| 73 | + if got := m.NodeRestartState(id); got != RestartDone { |
| 74 | + t.Errorf("state after the node proved the bounce = %q, want %q", got, RestartDone) |
| 75 | + } |
| 76 | + |
| 77 | + // The answer is held briefly and then stops being news: confirmation lands about |
| 78 | + // a second after the click, so without this window the operator sees nothing |
| 79 | + // change at all and clicks again. |
| 80 | + m.nodeUpdateMu.Lock() |
| 81 | + m.nodeRestart[id].outcomeAt = time.Now().Add(-nodeRestartShow - time.Second) |
| 82 | + m.nodeUpdateMu.Unlock() |
| 83 | + if got := m.NodeRestartState(id); got != "" { |
| 84 | + t.Errorf("state = %q long after it resolved, want it gone", got) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// An agent too old to report its Xray start time can never prove anything, so its |
| 89 | +// request must not hang the button forever — nor be mistaken for a success. |
| 90 | +func TestNodeRestartGivesUpWaiting(t *testing.T) { |
| 91 | + m, id := restartTestManager(t) |
| 92 | + |
| 93 | + if err := m.RequestNodeXrayRestart(id); err != nil { |
| 94 | + t.Fatalf("request: %v", err) |
| 95 | + } |
| 96 | + if !m.TakeNodeXrayRestart(id, 0) { |
| 97 | + t.Fatal("the command was not handed to the node") |
| 98 | + } |
| 99 | + // A node reporting no start time at all (old agent) never confirms. |
| 100 | + m.ConfirmNodeXrayRestart(id, 0) |
| 101 | + if got := m.NodeRestartState(id); got != RestartPending { |
| 102 | + t.Errorf("state = %q — a zero start time was accepted as proof", got) |
| 103 | + } |
| 104 | + |
| 105 | + // Age the request past the wait: the UI must fall back to the server's real |
| 106 | + // status rather than keep claiming a restart is on its way. |
| 107 | + m.nodeUpdateMu.Lock() |
| 108 | + m.nodeRestart[id].at = time.Now().Add(-nodeRestartWait - time.Second) |
| 109 | + m.nodeUpdateMu.Unlock() |
| 110 | + |
| 111 | + if got := m.NodeRestartState(id); got != RestartTimeout { |
| 112 | + t.Errorf("state after the wait = %q, want %q — giving up has to be said out "+ |
| 113 | + "loud, not shown as the badge quietly vanishing", got, RestartTimeout) |
| 114 | + } |
| 115 | + // Expiry also stops the command from being delivered late — a node that was |
| 116 | + // offline must not bounce Xray minutes after the operator gave up. |
| 117 | + if err := m.RequestNodeXrayRestart(id); err != nil { |
| 118 | + t.Fatalf("request: %v", err) |
| 119 | + } |
| 120 | + m.nodeUpdateMu.Lock() |
| 121 | + m.nodeRestart[id].at = time.Now().Add(-nodeRestartWait - time.Second) |
| 122 | + m.nodeUpdateMu.Unlock() |
| 123 | + if m.TakeNodeXrayRestart(id, 500) { |
| 124 | + t.Error("an expired request was still handed to the node") |
| 125 | + } |
| 126 | +} |
0 commit comments