forked from prymitive/karma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete_test.go
117 lines (107 loc) · 2.63 KB
/
autocomplete_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"encoding/json"
"fmt"
"net/http/httptest"
"testing"
"github.com/prymitive/karma/internal/mock"
)
type requestTest struct {
PathSuffix string
StatusCode int
Results []string
}
type autocompleteTest struct {
PathPrefix string
Tests []requestTest
}
var autocompleteTests = []autocompleteTest{
{
PathPrefix: "/labelNames.json",
Tests: []requestTest{
{
PathSuffix: "",
StatusCode: 200,
Results: []string{"alertname", "cluster", "instance", "job"},
},
{
PathSuffix: "?term=",
StatusCode: 200,
Results: []string{"alertname", "cluster", "instance", "job"},
},
{
PathSuffix: "?term=a",
StatusCode: 200,
Results: []string{"alertname", "instance"},
},
{
PathSuffix: "?term=alertname",
StatusCode: 200,
Results: []string{"alertname"},
},
{
PathSuffix: "?term=1234567890",
StatusCode: 200,
Results: []string{},
},
},
},
{
PathPrefix: "/labelValues.json",
Tests: []requestTest{
{
PathSuffix: "?name=",
StatusCode: 400,
Results: []string{},
},
{
PathSuffix: "?name=foobar",
StatusCode: 200,
Results: []string{},
},
{
PathSuffix: "?name=alertname",
StatusCode: 200,
Results: []string{"Free_Disk_Space_Too_Low", "HTTP_Probe_Failed", "Host_Down", "Memory_Usage_Too_High"},
},
{
PathSuffix: "?name=cluster",
StatusCode: 200,
Results: []string{"dev", "prod", "staging"},
},
},
},
}
func TestLabelAutocomplete(t *testing.T) {
mockConfig()
for _, version := range mock.ListAllMocks() {
t.Logf("Testing labels autocomplete using mock files from Alertmanager %s", version)
mockAlerts(version)
r := ginTestEngine()
for _, testVariant := range autocompleteTests {
for _, testCase := range testVariant.Tests {
// repeat each test a few times to test cached responses
for i := 1; i <= 3; i++ {
url := fmt.Sprintf("%s%s", testVariant.PathPrefix, testCase.PathSuffix)
req := httptest.NewRequest("GET", url, nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != testCase.StatusCode {
t.Errorf("GET %s returned status %d, expected %d", url, resp.Code, testCase.StatusCode)
}
if resp.Code < 300 {
ur := []string{}
err := json.Unmarshal(resp.Body.Bytes(), &ur)
if err != nil {
t.Errorf("Failed to unmarshal response: %s", err)
}
if len(ur) != len(testCase.Results) {
t.Errorf("Invalid number of responses for %s, got %d, expected %d", url, len(ur), len(testCase.Results))
t.Errorf("Results: %s", ur)
}
}
}
}
}
}
}