Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
Signed-off-by: Eunice Kim <kimeuni@amazon.com>
  • Loading branch information
euniceek committed Jul 11, 2024
1 parent 319e80c commit a282057
Showing 1 changed file with 39 additions and 146 deletions.
185 changes: 39 additions & 146 deletions pkg/ruler/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"context"
"encoding/json"
"errors"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

Expand All @@ -23,156 +21,59 @@ import (
)

func TestRuler_rules(t *testing.T) {
rules := map[string]rulespb.RuleGroupList{
"user1": {
&rulespb.RuleGroupDesc{
Name: "group1",
Namespace: "namespace1",
User: "user1",
Rules: []*rulespb.RuleDesc{
{
Record: "UP_RULE",
Expr: "up",
},
{
Alert: "UP_ALERT",
Expr: "up < 1",
},
},
Interval: interval,
},
&rulespb.RuleGroupDesc{
Name: "group2",
Namespace: "namespace2",
User: "user1",
Rules: []*rulespb.RuleDesc{
{
Record: "UP_RULE",
Expr: "up",
},
{
Alert: "UP_ALERT",
Expr: "up < 1",
},
},
Interval: interval,
},
},
}
store := newMockRuleStore(rules, nil)
store := newMockRuleStore(mockRules, nil)
cfg := defaultRulerConfig(t)

r := newTestRuler(t, cfg, store, nil)
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck

a := NewAPI(r, r.store, log.NewNopLogger())

tc := []struct {
name string
input string
output *RuleDiscovery
err string
errorType string
statusCode int
status string
matchFilter []string
}{
{
name: "with no limit",
statusCode: 200,
status: "success",
output: &RuleDiscovery{
RuleGroups: []*RuleGroup{
{
Name: "group1",
File: "namespace1",
Rules: []rule{
&recordingRule{
Name: "UP_RULE",
Query: "up",
Health: "unknown",
Type: "recording",
},
&alertingRule{
Name: "UP_ALERT",
Query: "up < 1",
State: "inactive",
Health: "unknown",
Type: "alerting",
Alerts: []*Alert{},
},
req := requestFor(t, "GET", "https://localhost:8080/api/prom/api/v1/rules", nil, "user1")
w := httptest.NewRecorder()
a.PrometheusRules(w, req)

resp := w.Result()
body, _ := io.ReadAll(resp.Body)

// Check status code and status response
responseJSON := util_api.Response{}
err := json.Unmarshal(body, &responseJSON)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, responseJSON.Status, "success")

// Testing the running rules for user1 in the mock store
expectedResponse, _ := json.Marshal(util_api.Response{
Status: "success",
Data: &RuleDiscovery{
RuleGroups: []*RuleGroup{
{
Name: "group1",
File: "namespace1",
Rules: []rule{
&recordingRule{
Name: "UP_RULE",
Query: "up",
Health: "unknown",
Type: "recording",
},
Interval: 60,
},
{
Name: "group2",
File: "namespace2",
Rules: []rule{
&recordingRule{
Name: "UP_RULE",
Query: "up",
Health: "unknown",
Type: "recording",
},
&alertingRule{
Name: "UP_ALERT",
Query: "up < 1",
State: "inactive",
Health: "unknown",
Type: "alerting",
Alerts: []*Alert{},
},
&alertingRule{
Name: "UP_ALERT",
Query: "up < 1",
State: "inactive",
Health: "unknown",
Type: "alerting",
Alerts: []*Alert{},
},
Interval: 60,
},
Interval: 60,
},
},
err: "",
errorType: "",
},
{
name: "bad matchers",
statusCode: 400,
matchFilter: []string{"bad_matcher~="},
status: "error",
output: nil,
err: "error parsing match params 1:12: parse error: unexpected character: '~'",
errorType: "bad_data",
},
}
})

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
req := requestFor(t, "GET", "https://localhost:8080/api/prom/api/v1/rules", nil, "user1")
urlValues := req.URL.Query()
if tt.matchFilter != nil {
addQueryParams(urlValues, "match[]", tt.matchFilter...)
}
req.URL.RawQuery = urlValues.Encode()
w := httptest.NewRecorder()
a.PrometheusRules(w, req)

resp := w.Result()
body, _ := io.ReadAll(resp.Body)

// Check status code and status response
responseJSON := util_api.Response{}
err := json.Unmarshal(body, &responseJSON)
require.NoError(t, err)
require.Equal(t, tt.statusCode, resp.StatusCode)
require.Equal(t, tt.status, responseJSON.Status)

// Testing the running rules for user1 in the mock store
expectedResponse, _ := json.Marshal(util_api.Response{
Status: tt.status,
Data: tt.output,
ErrorType: v1.ErrorType(tt.errorType),
Error: tt.err,
})

require.Equal(t, string(expectedResponse), string(body))
})
}
require.Equal(t, string(expectedResponse), string(body))
}

func TestRuler_rules_special_characters(t *testing.T) {
Expand Down Expand Up @@ -631,11 +532,3 @@ func requestFor(t *testing.T, method string, url string, body io.Reader, userID

return req.WithContext(ctx)
}

func addQueryParams(urlValues url.Values, paramName string, params ...string) {
for _, paramValue := range params {
if paramValue != "" {
urlValues.Add(paramName, paramValue)
}
}
}

0 comments on commit a282057

Please sign in to comment.