-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test/e2e: Refactor framework setup & wait for query logic
- Instead of counting the returned time series, inspect the first and only returned value from a query. This enables checking specific timeseries values. The previous behavior can still be achieved by using the `count` PromQL function. - Introduce `waitForQueryReturn{GreaterEqualOne,One}` helper functions for easier access. - Intialize Prometheus client at test framework creation time, as more function will need to use it in the long run. - Wait for Prometheus k8s before running any tests as multiple of them depend on its existence.
- Loading branch information
Showing
5 changed files
with
268 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2019 The Cluster Monitoring Operator Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package framework | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetFirstValueFromPromQuery(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
F func(t *testing.T) | ||
}{ | ||
{ | ||
Name: "should fail on multiple timeseries", | ||
F: func(t *testing.T) { | ||
body := ` | ||
{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"ALERTS","alertname":"TargetDown","alertstate":"firing","job":"metrics","severity":"warning"},"value":[1551102571.196,"1"]},{"metric":{"__name__":"ALERTS","alertname":"Watchdog","alertstate":"firing","severity":"none"},"value":[1551102571.196,"1"]}]}} | ||
` | ||
|
||
_, err := GetFirstValueFromPromQuery([]byte(body)) | ||
if err == nil || err.Error() != "expected body to contain single timeseries but got 2" { | ||
t.Fatalf("expected GetFirstValueFromPromQuery to fail on multiple timeseries but got err %q instead", err) | ||
} | ||
}, | ||
}, | ||
{ | ||
Name: "should return first value", | ||
F: func(t *testing.T) { | ||
body := ` | ||
{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"ALERTS","alertname":"Watchdog","alertstate":"firing","severity":"none"},"value":[1551102571.196,"1"]}]}} | ||
` | ||
|
||
v, err := GetFirstValueFromPromQuery([]byte(body)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if v != 1 { | ||
t.Fatalf("expected query to return %v but got %v", 1, v) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Name, test.F) | ||
} | ||
} |
Oops, something went wrong.