@@ -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 ("\n Actual: " , actual , "\n Expected: " , test .expected )
203+ }
204+
205+ })
206+ }
207+ }
0 commit comments