Skip to content

Commit 9e1975f

Browse files
committed
Fix bug determining final state in query subcommand
1 parent 251be8a commit 9e1975f

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

cmd/query.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Note: Time range values e.G. 'go_memstats_alloc_bytes_total[0s]' only the latest
133133
okCounter++
134134
}
135135

136-
vStates = append(states, rc)
136+
vStates = append(vStates, rc)
137137
// Format the metric and RC output for console output
138138
metricOutput.WriteString(generateMetricOutput(rc, sample.Metric.String(), sample.Value.String()))
139139

@@ -167,7 +167,7 @@ Note: Time range values e.G. 'go_memstats_alloc_bytes_total[0s]' only the latest
167167
okCounter++
168168
}
169169

170-
mStates = append(states, rc)
170+
mStates = append(mStates, rc)
171171
// Format the metric and RC output for console output
172172
metricOutput.WriteString(generateMetricOutput(rc, samplepair.String(), samplepair.Value.String()))
173173

cmd/query_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,62 @@ func TestQueryCmd(t *testing.T) {
146146
})
147147
}
148148
}
149+
150+
func TestExtendedQueryCmd(t *testing.T) {
151+
tests := []QueryTest{
152+
{
153+
name: "vector-multiple-ok",
154+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
155+
w.WriteHeader(http.StatusOK)
156+
w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","instance":"localhost:9100","job":"node"},"value":[1696589905.608,"1"]},{"metric":{"__name__":"up","instance":"localhost:9104","job":"mysqld"},"value":[1696589905.608,"99"]},{"metric":{"__name__":"up","instance":"localhost:9117","job":"apache"},"value":[1696589905.608,"1"]}]}}`))
157+
})),
158+
args: []string{"run", "../main.go", "query", "--query", "up", "-w", "100", "-c", "200"},
159+
expected: "[OK] - 3 Metrics OK |",
160+
},
161+
{
162+
name: "vector-multiple-critical",
163+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
164+
w.WriteHeader(http.StatusOK)
165+
w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","instance":"localhost:9100","job":"node"},"value":[1696589905.608,"1"]},{"metric":{"__name__":"up","instance":"localhost:9104","job":"mysqld"},"value":[1696589905.608,"11"]},{"metric":{"__name__":"up","instance":"localhost:9117","job":"apache"},"value":[1696589905.608,"6"]}]}}`))
166+
})),
167+
args: []string{"run", "../main.go", "query", "--query", "up", "-w", "5", "-c", "10"},
168+
expected: "[CRITICAL] - 3 Metrics: 1 Critical - 1 Warning - 1 Ok",
169+
},
170+
{
171+
name: "matrix-multiple-critical",
172+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
173+
w.WriteHeader(http.StatusOK)
174+
w.Write([]byte(`{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"up","instance":"localhost:9100","job":"node"},"values":[[1696589212.987,"1"],[1696589272.987,"1"],[1696589332.987,"1"],[1696589392.987,"1"],[1696589452.987,"1"]]},{"metric":{"__name__":"up","instance":"localhost:9104","job":"mysqld"},"values":[[1696589209.089,"25"],[1696589269.089,"25"],[1696589329.089,"25"],[1696589389.089,"25"],[1696589449.089,"25"]]},{"metric":{"__name__":"up","instance":"localhost:9117","job":"apache"},"values":[[1696589209.369,"1"],[1696589269.369,"1"],[1696589329.369,"1"],[1696589389.369,"1"],[1696589449.369,"1"]]}]}}`))
175+
})),
176+
args: []string{"run", "../main.go", "query", "--query", "up", "-w", "10", "-c", "20"},
177+
expected: "[CRITICAL] - 3 Metrics: 1 Critical - 0 Warning - 2 Ok",
178+
},
179+
{
180+
name: "matrix-multiple-warning",
181+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
182+
w.WriteHeader(http.StatusOK)
183+
w.Write([]byte(`{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"up","instance":"localhost:9100","job":"node"},"values":[[1696589212.987,"1"],[1696589272.987,"1"],[1696589332.987,"1"],[1696589392.987,"1"],[1696589452.987,"1"]]},{"metric":{"__name__":"up","instance":"localhost:9104","job":"mysqld"},"values":[[1696589209.089,"15"],[1696589269.089,"15"],[1696589329.089,"15"],[1696589389.089,"15"],[1696589449.089,"15"]]},{"metric":{"__name__":"up","instance":"localhost:9117","job":"apache"},"values":[[1696589209.369,"1"],[1696589269.369,"1"],[1696589329.369,"1"],[1696589389.369,"1"],[1696589449.369,"1"]]}]}}`))
184+
})),
185+
args: []string{"run", "../main.go", "query", "--query", "up", "-w", "10", "-c", "20"},
186+
expected: "[WARNING] - 3 Metrics: 0 Critical - 1 Warning - 2 Ok",
187+
},
188+
}
189+
190+
for _, test := range tests {
191+
t.Run(test.name, func(t *testing.T) {
192+
defer test.server.Close()
193+
194+
// We need the random Port extracted
195+
u, _ := url.Parse(test.server.URL)
196+
cmd := exec.Command("go", append(test.args, "--port", u.Port())...)
197+
out, _ := cmd.CombinedOutput()
198+
199+
actual := string(out)
200+
201+
if !strings.Contains(actual, test.expected) {
202+
t.Error("\nActual: ", actual, "\nExpected: ", test.expected)
203+
}
204+
205+
})
206+
}
207+
}

0 commit comments

Comments
 (0)