1package modes
2
3import (
4 "errors"
5 "strings"
6 "testing"
7
8 "github.com/patriceckhart/zot/packages/agent/extensions"
9)
10
11func TestFormatReloadStatusIncludesErrorDetails(t *testing.T) {
12 msg, failed := formatReloadStatus(extensions.ReloadStats{
13 Stopped: 2,
14 Loaded: 1,
15 Ready: 1,
16 Errors: []error{
17 errors.New("broken manifest"),
18 errors.New("spawn worker: executable not found"),
19 },
20 })
21 if !failed {
22 t.Fatal("reload with errors was not marked failed")
23 }
24 for _, want := range []string{
25 "2 stopped, 1 loaded (1 ready)",
26 "2 error(s)",
27 "broken manifest",
28 "spawn worker: executable not found",
29 } {
30 if !strings.Contains(msg, want) {
31 t.Fatalf("reload status missing %q: %s", want, msg)
32 }
33 }
34}
35
36func TestFormatReloadStatusSuccess(t *testing.T) {
37 msg, failed := formatReloadStatus(extensions.ReloadStats{Stopped: 2, Loaded: 3, Ready: 3})
38 if failed {
39 t.Fatal("successful reload was marked failed")
40 }
41 if want := "reloaded: 2 stopped, 3 loaded (3 ready)"; msg != want {
42 t.Fatalf("reload status = %q, want %q", msg, want)
43 }
44}
45
46func TestSetReloadStatusUsesErrorChannelForFailures(t *testing.T) {
47 i := &Interactive{}
48 i.setReloadStatus("broken extension", true)
49 if i.statusErr != "broken extension" || i.statusOK != "" {
50 t.Fatalf("failed reload status used wrong channel: err=%q ok=%q", i.statusErr, i.statusOK)
51 }
52 i.setReloadStatus("reload complete", false)
53 if i.statusOK != "reload complete" || i.statusErr != "" {
54 t.Fatalf("successful reload status used wrong channel: err=%q ok=%q", i.statusErr, i.statusOK)
55 }
56}
57
58func TestClearReloadStatusDoesNotClearNewerStatus(t *testing.T) {
59 i := &Interactive{statusErr: "newer error", statusOK: "newer success"}
60 if i.clearReloadStatus(0, "old error", true) {
61 t.Fatal("stale error dismissal reported a clear")
62 }
63 if i.clearReloadStatus(0, "old success", false) {
64 t.Fatal("stale success dismissal reported a clear")
65 }
66 if i.statusErr != "newer error" || i.statusOK != "newer success" {
67 t.Fatalf("stale dismissal changed status: err=%q ok=%q", i.statusErr, i.statusOK)
68 }
69
70 if !i.clearReloadStatus(0, "newer error", true) || i.statusErr != "" {
71 t.Fatalf("matching error status was not cleared: %q", i.statusErr)
72 }
73 if !i.clearReloadStatus(0, "newer success", false) || i.statusOK != "" {
74 t.Fatalf("matching success status was not cleared: %q", i.statusOK)
75 }
76
77 first := i.setReloadStatus("same result", false)
78 second := i.setReloadStatus("same result", false)
79 if first == second {
80 t.Fatal("reload status generation did not advance")
81 }
82 if i.clearReloadStatus(first, "same result", false) {
83 t.Fatal("older identical reload status cleared the newer result")
84 }
85}