@@ -4,11 +4,13 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"errors"
7
+ "fmt"
7
8
"io"
8
9
"net/http"
9
10
"net/http/httptest"
10
11
"strings"
11
12
"testing"
13
+ "time"
12
14
13
15
"github.com/go-kit/log"
14
16
"github.com/gorilla/mux"
@@ -20,6 +22,90 @@ import (
20
22
"github.com/cortexproject/cortex/pkg/util/services"
21
23
)
22
24
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
+
23
109
func TestRuler_rules (t * testing.T ) {
24
110
store := newMockRuleStore (mockRules , nil )
25
111
cfg := defaultRulerConfig (t )
0 commit comments