Skip to content

Commit 841c296

Browse files
Bug fix on JSON Tag (#6339)
Signed-off-by: Anand Rajagopal <[email protected]>
1 parent b8156a0 commit 841c296

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

Diff for: pkg/ruler/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type Alert struct {
5151
// RuleDiscovery has info for all rules
5252
type RuleDiscovery struct {
5353
RuleGroups []*RuleGroup `json:"groups"`
54-
GroupNextToken string `json:"groupNextToken:omitempty"`
54+
GroupNextToken string `json:"groupNextToken,omitempty"`
5555
}
5656

5757
// RuleGroup has info for rules which are part of a group

Diff for: pkg/ruler/api_test.go

+86
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"io"
89
"net/http"
910
"net/http/httptest"
1011
"strings"
1112
"testing"
13+
"time"
1214

1315
"github.com/go-kit/log"
1416
"github.com/gorilla/mux"
@@ -20,6 +22,90 @@ import (
2022
"github.com/cortexproject/cortex/pkg/util/services"
2123
)
2224

25+
func TestAPIResponseSerialization(t *testing.T) {
26+
lastEvalTime := time.Now()
27+
responseTime := lastEvalTime.Format(time.RFC3339Nano)
28+
testCases := map[string]struct {
29+
rules RuleDiscovery
30+
expectedJSON string
31+
}{
32+
"No rules": {
33+
rules: RuleDiscovery{
34+
RuleGroups: make([]*RuleGroup, 0),
35+
},
36+
expectedJSON: `{
37+
"groups":[]
38+
}`,
39+
},
40+
"Rules with no next token": {
41+
rules: RuleDiscovery{
42+
RuleGroups: []*RuleGroup{
43+
{
44+
Name: "Test",
45+
File: "/rules/Test",
46+
Rules: make([]rule, 0),
47+
Interval: 60,
48+
LastEvaluation: lastEvalTime,
49+
EvaluationTime: 10,
50+
Limit: 0,
51+
},
52+
},
53+
},
54+
expectedJSON: fmt.Sprintf(`{
55+
"groups": [
56+
{
57+
"evaluationTime": 10,
58+
"limit": 0,
59+
"name": "Test",
60+
"file": "/rules/Test",
61+
"interval": 60,
62+
"rules": [],
63+
"lastEvaluation": "%s"
64+
}
65+
]
66+
}`, responseTime),
67+
},
68+
"Rules with next token": {
69+
rules: RuleDiscovery{
70+
RuleGroups: []*RuleGroup{
71+
{
72+
Name: "Test",
73+
File: "/rules/Test",
74+
Rules: make([]rule, 0),
75+
Interval: 60,
76+
LastEvaluation: lastEvalTime,
77+
EvaluationTime: 10,
78+
Limit: 0,
79+
},
80+
},
81+
GroupNextToken: "abcdef",
82+
},
83+
expectedJSON: fmt.Sprintf(`{
84+
"groups": [
85+
{
86+
"evaluationTime": 10,
87+
"limit": 0,
88+
"name": "Test",
89+
"file": "/rules/Test",
90+
"interval": 60,
91+
"rules": [],
92+
"lastEvaluation": "%s"
93+
}
94+
],
95+
"groupNextToken": "abcdef"
96+
}`, responseTime),
97+
},
98+
}
99+
100+
for name, tc := range testCases {
101+
t.Run(name, func(t *testing.T) {
102+
data, err := json.Marshal(&tc.rules)
103+
require.NoError(t, err)
104+
require.JSONEq(t, tc.expectedJSON, string(data))
105+
})
106+
}
107+
}
108+
23109
func TestRuler_rules(t *testing.T) {
24110
store := newMockRuleStore(mockRules, nil)
25111
cfg := defaultRulerConfig(t)

0 commit comments

Comments
 (0)