Skip to content

Commit 7d593ea

Browse files
authored
Use correct version of yaml (cortexproject#2908)
* Use correct version of yaml Signed-off-by: Joe Elliott <number101010@gmail.com> * Fixed integration test Signed-off-by: Joe Elliott <number101010@gmail.com> * Added basic get/post test Signed-off-by: Joe Elliott <number101010@gmail.com> * Made the mockRuleStore thread safe(r) Signed-off-by: Joe Elliott <number101010@gmail.com>
1 parent f4ef7f0 commit 7d593ea

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

integration/e2ecortex/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/prometheus/common/model"
2020
"github.com/prometheus/prometheus/pkg/rulefmt"
2121
"github.com/prometheus/prometheus/prompb"
22-
yaml "gopkg.in/yaml.v2"
22+
yaml "gopkg.in/yaml.v3"
2323
)
2424

2525
var (

pkg/ruler/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/prometheus/prometheus/pkg/labels"
1818
"github.com/prometheus/prometheus/pkg/rulefmt"
1919
"github.com/weaveworks/common/user"
20-
"gopkg.in/yaml.v2"
20+
"gopkg.in/yaml.v3"
2121

2222
"github.com/cortexproject/cortex/pkg/ingester/client"
2323
"github.com/cortexproject/cortex/pkg/ruler/rules"

pkg/ruler/api_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import (
66
"io/ioutil"
77
"net/http"
88
"net/http/httptest"
9+
"strings"
910
"testing"
1011

12+
"github.com/gorilla/mux"
1113
"github.com/stretchr/testify/require"
1214
"github.com/weaveworks/common/user"
1315

16+
"github.com/cortexproject/cortex/pkg/ruler/rules"
1417
"github.com/cortexproject/cortex/pkg/util/services"
1518
)
1619

@@ -160,3 +163,49 @@ func TestRuler_alerts(t *testing.T) {
160163

161164
require.Equal(t, string(expectedResponse), string(body))
162165
}
166+
167+
func TestRuler_Create(t *testing.T) {
168+
cfg, cleanup := defaultRulerConfig(newMockRuleStore(make(map[string]rules.RuleGroupList)))
169+
defer cleanup()
170+
171+
r, rcleanup := newTestRuler(t, cfg)
172+
defer rcleanup()
173+
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck
174+
175+
rules := `
176+
name: test
177+
interval: 15s
178+
rules:
179+
- record: up_rule
180+
expr: up{}
181+
- alert: up_alert
182+
expr: sum(up{}) > 1
183+
for: 30s
184+
annotations:
185+
test: test
186+
labels:
187+
test: test
188+
`
189+
190+
router := mux.NewRouter()
191+
router.Path("/api/v1/rules/{namespace}").Methods("POST").HandlerFunc(r.CreateRuleGroup)
192+
router.Path("/api/v1/rules/{namespace}/{groupName}").Methods("GET").HandlerFunc(r.GetRuleGroup)
193+
194+
// POST
195+
req := httptest.NewRequest("POST", "https://localhost:8080/api/v1/rules/namespace", strings.NewReader(rules))
196+
req.Header.Add(user.OrgIDHeaderName, "user1")
197+
ctx := user.InjectOrgID(req.Context(), "user1")
198+
w := httptest.NewRecorder()
199+
200+
router.ServeHTTP(w, req.WithContext(ctx))
201+
require.Equal(t, 202, w.Code)
202+
203+
// GET
204+
req = httptest.NewRequest("GET", "https://localhost:8080/api/v1/rules/namespace/test", nil)
205+
req.Header.Add(user.OrgIDHeaderName, "user1")
206+
ctx = user.InjectOrgID(req.Context(), "user1")
207+
w = httptest.NewRecorder()
208+
209+
router.ServeHTTP(w, req.WithContext(ctx))
210+
require.Equal(t, 200, w.Code)
211+
}

pkg/ruler/store_mock_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package ruler
22

33
import (
44
"context"
5+
"sync"
56
"time"
67

78
"github.com/cortexproject/cortex/pkg/ruler/rules"
89
)
910

1011
type mockRuleStore struct {
1112
rules map[string]rules.RuleGroupList
13+
mtx sync.Mutex
1214
}
1315

1416
var (
@@ -77,10 +79,21 @@ func newMockRuleStore(rules map[string]rules.RuleGroupList) *mockRuleStore {
7779
}
7880

7981
func (m *mockRuleStore) ListAllRuleGroups(ctx context.Context) (map[string]rules.RuleGroupList, error) {
80-
return m.rules, nil
82+
m.mtx.Lock()
83+
defer m.mtx.Unlock()
84+
85+
copy := make(map[string]rules.RuleGroupList)
86+
for k, v := range m.rules {
87+
copy[k] = v
88+
}
89+
90+
return copy, nil
8191
}
8292

8393
func (m *mockRuleStore) ListRuleGroups(ctx context.Context, userID, namespace string) (rules.RuleGroupList, error) {
94+
m.mtx.Lock()
95+
defer m.mtx.Unlock()
96+
8497
userRules, exists := m.rules[userID]
8598
if !exists {
8699
return nil, rules.ErrUserNotFound
@@ -106,6 +119,9 @@ func (m *mockRuleStore) ListRuleGroups(ctx context.Context, userID, namespace st
106119
}
107120

108121
func (m *mockRuleStore) GetRuleGroup(ctx context.Context, userID string, namespace string, group string) (*rules.RuleGroupDesc, error) {
122+
m.mtx.Lock()
123+
defer m.mtx.Unlock()
124+
109125
userRules, exists := m.rules[userID]
110126
if !exists {
111127
return nil, rules.ErrUserNotFound
@@ -125,6 +141,9 @@ func (m *mockRuleStore) GetRuleGroup(ctx context.Context, userID string, namespa
125141
}
126142

127143
func (m *mockRuleStore) SetRuleGroup(ctx context.Context, userID string, namespace string, group *rules.RuleGroupDesc) error {
144+
m.mtx.Lock()
145+
defer m.mtx.Unlock()
146+
128147
userRules, exists := m.rules[userID]
129148
if !exists {
130149
userRules = rules.RuleGroupList{}
@@ -147,6 +166,9 @@ func (m *mockRuleStore) SetRuleGroup(ctx context.Context, userID string, namespa
147166
}
148167

149168
func (m *mockRuleStore) DeleteRuleGroup(ctx context.Context, userID string, namespace string, group string) error {
169+
m.mtx.Lock()
170+
defer m.mtx.Unlock()
171+
150172
userRules, exists := m.rules[userID]
151173
if !exists {
152174
userRules = rules.RuleGroupList{}

0 commit comments

Comments
 (0)