Skip to content

Commit f19c401

Browse files
Minor refactoring and update documentation (#5437)
Signed-off-by: Anand Rajagopal <[email protected]>
1 parent c83a26d commit f19c401

File tree

3 files changed

+12
-50
lines changed

3 files changed

+12
-50
lines changed

Diff for: docs/contributing/how-integration-tests-work.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ This will locally build the `quay.io/cortexproject/cortex:latest` image used by
2020
Once the Docker image is built, you can run integration tests:
2121

2222
```
23-
go test -v -tags=requires_docker ./integration/...
23+
go test -v -tags=integration,requires_docker,integration_alertmanager,integration_memberlist,integration_querier,integration_ruler,integration_query_fuzz ./integration/...
2424
```
2525

26-
If you want to run a single test you can use a filter. For example, to only run `TestChunksStorageAllIndexBackends`:
26+
If you want to run a single test you can use a filter. For example, to only run `TestRulerAPISharding`:
2727

2828
```
29-
go test -v -tags=requires_docker ./integration -run "^TestChunksStorageAllIndexBackends$"
29+
go test -v -tags=integration,integration_ruler -timeout 2400s -v -count=1 ./integration/... -run "^TestRulerAPISharding$"
3030
```
3131

3232
### Supported environment variables

Diff for: integration/e2ecortex/client.go

+7-45
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131

3232
var ErrNotFound = errors.New("not found")
3333

34+
var DefaultFilter = RuleFilter{}
35+
3436
// Client is a client used to interact with Cortex in integration tests
3537
type Client struct {
3638
alertmanagerClient promapi.Client
@@ -370,12 +372,14 @@ type RuleFilter struct {
370372

371373
func addQueryParams(urlValues url.Values, paramName string, params ...string) {
372374
for _, paramValue := range params {
373-
urlValues.Add(paramName, paramValue)
375+
if paramValue != "" {
376+
urlValues.Add(paramName, paramValue)
377+
}
374378
}
375379
}
376380

377-
// GetPrometheusRulesWithFilter fetches the rules from the Prometheus endpoint /api/v1/rules.
378-
func (c *Client) GetPrometheusRulesWithFilter(filter RuleFilter) ([]*ruler.RuleGroup, error) {
381+
// GetPrometheusRules fetches the rules from the Prometheus endpoint /api/v1/rules.
382+
func (c *Client) GetPrometheusRules(filter RuleFilter) ([]*ruler.RuleGroup, error) {
379383
// Create HTTP request
380384

381385
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/api/prom/api/v1/rules", c.rulerAddress), nil)
@@ -424,48 +428,6 @@ func (c *Client) GetPrometheusRulesWithFilter(filter RuleFilter) ([]*ruler.RuleG
424428
return decoded.Data.RuleGroups, nil
425429
}
426430

427-
// GetPrometheusRules fetches the rules from the Prometheus endpoint /api/v1/rules.
428-
func (c *Client) GetPrometheusRules() ([]*ruler.RuleGroup, error) {
429-
// Create HTTP request
430-
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/api/prom/api/v1/rules", c.rulerAddress), nil)
431-
if err != nil {
432-
return nil, err
433-
}
434-
req.Header.Set("X-Scope-OrgID", c.orgID)
435-
436-
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
437-
defer cancel()
438-
439-
// Execute HTTP request
440-
res, err := c.httpClient.Do(req.WithContext(ctx))
441-
if err != nil {
442-
return nil, err
443-
}
444-
defer res.Body.Close()
445-
446-
body, err := io.ReadAll(res.Body)
447-
if err != nil {
448-
return nil, err
449-
}
450-
451-
// Decode the response.
452-
type response struct {
453-
Status string `json:"status"`
454-
Data ruler.RuleDiscovery `json:"data"`
455-
}
456-
457-
decoded := &response{}
458-
if err := json.Unmarshal(body, decoded); err != nil {
459-
return nil, err
460-
}
461-
462-
if decoded.Status != "success" {
463-
return nil, fmt.Errorf("unexpected response status '%s'", decoded.Status)
464-
}
465-
466-
return decoded.Data.RuleGroups, nil
467-
}
468-
469431
// GetRuleGroups gets the configured rule groups from the ruler.
470432
func (c *Client) GetRuleGroups() (map[string][]rulefmt.RuleGroup, error) {
471433
// Create HTTP request

Diff for: integration/ruler_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func TestRulerSharding(t *testing.T) {
382382
require.NoError(t, ruler2.WaitSumMetrics(e2e.Less(numRulesGroups), "cortex_prometheus_rule_group_rules"))
383383

384384
// Fetch the rules and ensure they match the configured ones.
385-
actualGroups, err := c.GetPrometheusRules()
385+
actualGroups, err := c.GetPrometheusRules(e2ecortex.DefaultFilter)
386386
require.NoError(t, err)
387387

388388
var actualNames []string
@@ -542,7 +542,7 @@ func TestRulerAPISharding(t *testing.T) {
542542
// For each test case, fetch the rules with configured filters, and ensure the results match.
543543
for name, tc := range testCases {
544544
t.Run(name, func(t *testing.T) {
545-
actualGroups, err := c.GetPrometheusRulesWithFilter(tc.filter)
545+
actualGroups, err := c.GetPrometheusRules(tc.filter)
546546
require.NoError(t, err)
547547
tc.resultCheckFn(t, actualGroups)
548548
})

0 commit comments

Comments
 (0)