|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + tea "charm.land/bubbletea/v2" |
| 9 | + |
| 10 | + "github.com/open-source-cloud/devstack/internal/docker" |
| 11 | + "github.com/open-source-cloud/devstack/internal/generate" |
| 12 | +) |
| 13 | + |
| 14 | +// statsMockClient seeds a shared + a project container plus their canned stats |
| 15 | +// so the collector's stats fetch is exercised without a daemon. |
| 16 | +func statsMockClient() *docker.MockClient { |
| 17 | + return &docker.MockClient{ |
| 18 | + Containers: []docker.Container{ |
| 19 | + {ID: "pg1", Name: "devstack-shared-postgres-1", State: "running", Labels: map[string]string{ |
| 20 | + generate.LabelManaged: "true", generate.LabelShared: "postgres", |
| 21 | + }}, |
| 22 | + {ID: "web1", Name: "devstack-shop-web-1", State: "running", Labels: map[string]string{ |
| 23 | + generate.LabelManaged: "true", generate.LabelProject: "shop", generate.LabelService: "web", |
| 24 | + }}, |
| 25 | + }, |
| 26 | + Stats: map[string]docker.Stats{ |
| 27 | + "pg1": {CPUPercent: 3.1, MemUsage: 412 * 1024 * 1024, MemLimit: 2 * 1024 * 1024 * 1024, MemPercent: 20}, |
| 28 | + "web1": {CPUPercent: 12.0, MemUsage: 190 * 1024 * 1024, MemLimit: 0}, |
| 29 | + }, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// TestCollectDashboardStats verifies the collector keys each container's sample |
| 34 | +// by the dashboard row's display name (shared alias / "<project>/<service>"). |
| 35 | +func TestCollectDashboardStats(t *testing.T) { |
| 36 | + m := statsMockClient() |
| 37 | + got := collectDashboardStats(context.Background(), m, true) |
| 38 | + if len(got) != 2 { |
| 39 | + t.Fatalf("stats map = %d entries, want 2: %+v", len(got), got) |
| 40 | + } |
| 41 | + if st, ok := got["shared-postgres"]; !ok || st.CPUPercent != 3.1 { |
| 42 | + t.Errorf("shared-postgres stats = %+v (ok=%v)", st, ok) |
| 43 | + } |
| 44 | + if st, ok := got["shop/web"]; !ok || st.MemUsage != 190*1024*1024 { |
| 45 | + t.Errorf("shop/web stats = %+v (ok=%v)", st, ok) |
| 46 | + } |
| 47 | + if m.StatsCalls != 2 { |
| 48 | + t.Fatalf("StatsCalls = %d, want 2", m.StatsCalls) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// TestCollectDashboardStatsDisabled asserts --no-stats (enabled=false) issues no |
| 53 | +// ContainerStats calls at all. |
| 54 | +func TestCollectDashboardStatsDisabled(t *testing.T) { |
| 55 | + m := statsMockClient() |
| 56 | + if got := collectDashboardStats(context.Background(), m, false); got != nil { |
| 57 | + t.Fatalf("disabled stats should be nil, got %+v", got) |
| 58 | + } |
| 59 | + if m.StatsCalls != 0 { |
| 60 | + t.Fatalf("StatsCalls = %d, want 0 (fetch must be skipped)", m.StatsCalls) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// TestAttachDashboardStats folds seeded stats into the matching rows and leaves |
| 65 | +// unmatched rows without stats. |
| 66 | +func TestAttachDashboardStats(t *testing.T) { |
| 67 | + m := statsMockClient() |
| 68 | + rows := []dashRow{ |
| 69 | + {Name: "shared-postgres", Kind: "shared"}, |
| 70 | + {Name: "shop/web", Kind: "project"}, |
| 71 | + {Name: "shop/api", Kind: "project"}, // no container → stays statless |
| 72 | + } |
| 73 | + attachDashboardStats(context.Background(), m, rows, true) |
| 74 | + |
| 75 | + if !rows[0].HasStats || rows[0].CPUPercent != 3.1 { |
| 76 | + t.Errorf("shared-postgres row = %+v", rows[0]) |
| 77 | + } |
| 78 | + if !rows[1].HasStats || rows[1].MemUsage != 190*1024*1024 { |
| 79 | + t.Errorf("shop/web row = %+v", rows[1]) |
| 80 | + } |
| 81 | + if rows[2].HasStats { |
| 82 | + t.Errorf("shop/api should have no stats: %+v", rows[2]) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// TestAttachDashboardStatsDisabled asserts disabled stats leave every row untouched. |
| 87 | +func TestAttachDashboardStats_Disabled(t *testing.T) { |
| 88 | + m := statsMockClient() |
| 89 | + rows := []dashRow{{Name: "shared-postgres", Kind: "shared"}} |
| 90 | + attachDashboardStats(context.Background(), m, rows, false) |
| 91 | + if rows[0].HasStats { |
| 92 | + t.Fatalf("row should have no stats when disabled: %+v", rows[0]) |
| 93 | + } |
| 94 | + if m.StatsCalls != 0 { |
| 95 | + t.Fatalf("StatsCalls = %d, want 0", m.StatsCalls) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// TestDashboardStatsInModel drives the Bubble Tea Update/View with stats-bearing |
| 100 | +// rows and asserts the CPU%/MEM columns render in the table and detail pane. |
| 101 | +func TestDashboardStatsInModel(t *testing.T) { |
| 102 | + data := dashboardData{Rows: []dashRow{ |
| 103 | + {Name: "shared-postgres", Kind: "shared", State: "running", Refs: 2, |
| 104 | + HasStats: true, CPUPercent: 3.1, MemUsage: 412 * 1024 * 1024, MemLimit: 2 * 1024 * 1024 * 1024}, |
| 105 | + }} |
| 106 | + m := newDashboardModel(context.Background(), func(context.Context) dashboardData { return data }, 0) |
| 107 | + updated, _ := m.Update(dashDataMsg(data)) |
| 108 | + dm := updated.(dashboardModel) |
| 109 | + |
| 110 | + tr := dm.table.Rows() |
| 111 | + if len(tr) != 1 { |
| 112 | + t.Fatalf("table rows = %d, want 1", len(tr)) |
| 113 | + } |
| 114 | + // Columns: SERVICE, STATE, HEALTH, CPU%, MEM, REFS. |
| 115 | + if got := tr[0][3]; got != "3.1%" { |
| 116 | + t.Errorf("CPU%% cell = %q, want 3.1%%", got) |
| 117 | + } |
| 118 | + if got := tr[0][4]; !strings.Contains(got, "412.0MB") { |
| 119 | + t.Errorf("MEM cell = %q, want it to contain 412.0MB", got) |
| 120 | + } |
| 121 | + |
| 122 | + detail := dm.selectedDetail() |
| 123 | + if !strings.Contains(detail, "cpu: 3.1%") || !strings.Contains(detail, "mem:") { |
| 124 | + t.Errorf("detail missing stats: %q", detail) |
| 125 | + } |
| 126 | + |
| 127 | + dm2, _ := dm.Update(tea.WindowSizeMsg{Width: 120, Height: 30}) |
| 128 | + view := dm2.(dashboardModel).View() |
| 129 | + if !strings.Contains(view.Content, "CPU% (engine)") { |
| 130 | + t.Errorf("view missing CPU%% column header") |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// TestDashboardNoStatsRendersDash verifies a statless row shows the "—" placeholder. |
| 135 | +func TestDashboardNoStatsRendersDash(t *testing.T) { |
| 136 | + r := dashRow{Name: "shop/api", Kind: "project", State: "running", HasStats: false} |
| 137 | + if got := dashCPU(r); got != "—" { |
| 138 | + t.Errorf("dashCPU = %q, want —", got) |
| 139 | + } |
| 140 | + if got := dashMem(r); got != "—" { |
| 141 | + t.Errorf("dashMem = %q, want —", got) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// TestFormatBytes covers the byte humanizer used by the MEM column. |
| 146 | +func TestFormatBytes(t *testing.T) { |
| 147 | + tests := []struct { |
| 148 | + in uint64 |
| 149 | + want string |
| 150 | + }{ |
| 151 | + {512, "512B"}, |
| 152 | + {1024, "1.0KB"}, |
| 153 | + {412 * 1024 * 1024, "412.0MB"}, |
| 154 | + {2 * 1024 * 1024 * 1024, "2.0GB"}, |
| 155 | + } |
| 156 | + for _, tt := range tests { |
| 157 | + if got := formatBytes(tt.in); got != tt.want { |
| 158 | + t.Errorf("formatBytes(%d) = %q, want %q", tt.in, got, tt.want) |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments