carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package console
2
3import (
4 "net/url"
5 "testing"
6)
7
8func rowsFor(hosts ...string) []InstanceRow {
9 out := make([]InstanceRow, len(hosts))
10 for i, h := range hosts {
11 out[i] = InstanceRow{Host: h, HealthWord: "Running"}
12 }
13 return out
14}
15
16func TestHealthSlugRoundTrips(t *testing.T) {
17 for _, w := range healthWords {
18 if got := healthFromSlug(healthSlug(w)); got != w {
19 t.Fatalf("%q -> %q -> %q", w, healthSlug(w), got)
20 }
21 }
22 if healthFromSlug("nonsense") != "" {
23 t.Fatal("unknown slug must mean no filter, not an error")
24 }
25}
26
27func TestHealthToneUsesExistingChipStyles(t *testing.T) {
28 tests := map[string]string{
29 "Running": "ok", "Serving": "ok",
30 "Failed": "attention", "Not responding": "attention",
31 "Asleep": "notdeployed", "Setting up": "notdeployed",
32 "No readings": "unknown",
33 }
34 for word, want := range tests {
35 if got := healthTone(word); got != want {
36 t.Errorf("healthTone(%q) = %q, want %q", word, got, want)
37 }
38 }
39}
40
41func TestPageFiltersAndCounts(t *testing.T) {
42 rows := rowsFor("a.x", "ab.x", "b.x")
43 rows[2].HealthWord = "Asleep"
44 p := pageInstanceRows(rows, instanceQuery{Q: "a"}, 50)
45 if p.Total != 2 || len(p.Rows) != 2 || p.NextCursor != "" {
46 t.Fatalf("%+v", p)
47 }
48 if p.Counts["Running"] != 2 || p.Counts["Asleep"] != 1 {
49 t.Fatalf("counts are over ALL rows, filtered or not: %+v", p.Counts)
50 }
51 p = pageInstanceRows(rows, instanceQuery{Health: "Asleep"}, 50)
52 if p.Total != 1 || p.Rows[0].Host != "b.x" {
53 t.Fatalf("%+v", p)
54 }
55}
56
57func TestPageCursors(t *testing.T) {
58 rows := rowsFor("a.x", "b.x", "c.x", "d.x", "e.x")
59 p1 := pageInstanceRows(rows, instanceQuery{}, 2)
60 if len(p1.Rows) != 2 || p1.NextCursor == "" || p1.Rows[1].Host != "b.x" {
61 t.Fatalf("page1: %+v", p1)
62 }
63 p2 := pageInstanceRows(rows, instanceQuery{Cursor: p1.NextCursor}, 2)
64 if len(p2.Rows) != 2 || p2.Rows[0].Host != "c.x" {
65 t.Fatalf("page2: %+v", p2)
66 }
67 p3 := pageInstanceRows(rows, instanceQuery{Cursor: p2.NextCursor}, 2)
68 if len(p3.Rows) != 1 || p3.Rows[0].Host != "e.x" || p3.NextCursor != "" {
69 t.Fatalf("page3: %+v", p3)
70 }
71}
72
73func TestPageGarbageCursorStartsOver(t *testing.T) {
74 rows := rowsFor("a.x", "b.x")
75 p := pageInstanceRows(rows, instanceQuery{Cursor: "!!not-base64!!"}, 50)
76 if len(p.Rows) != 2 {
77 t.Fatalf("bad cursor must degrade to page one: %+v", p)
78 }
79}
80
81func TestPageAcceptsLegacyCursorAndPastLastIsEmpty(t *testing.T) {
82 rows := rowsFor("a.x", "b.x")
83 legacy := encodeInstanceCursor("a.x")
84 p := pageInstanceRows(rows, instanceQuery{Cursor: legacy}, 50)
85 if len(p.Rows) != 1 || p.Rows[0].Host != "b.x" {
86 t.Fatalf("legacy cursor: %+v", p)
87 }
88 p = pageInstanceRows(rows, instanceQuery{Cursor: encodeInstanceCursor("z.x")}, 50)
89 if len(p.Rows) != 0 || p.NextCursor != "" {
90 t.Fatalf("past-last cursor: %+v", p)
91 }
92}
93
94func TestParseInstanceQuery(t *testing.T) {
95 q := parseInstanceQuery(url.Values{"q": {" api "}, "health": {"not-responding"}, "cursor": {"abc"}})
96 if q.Q != "api" || q.Health != "Not responding" || q.Cursor != "abc" {
97 t.Fatalf("%+v", q)
98 }
99}
100