@@ -18,6 +18,8 @@ import (
18
18
19
19
"github.com/go-kit/kit/log"
20
20
21
+ "github.com/cortexproject/cortex/pkg/alertmanager/alertspb"
22
+ "github.com/cortexproject/cortex/pkg/alertmanager/alertstore"
21
23
"github.com/cortexproject/cortex/pkg/util/services"
22
24
)
23
25
@@ -76,6 +78,25 @@ func (f *fakeReplicator) ReadFullStateForUser(ctx context.Context, userID string
76
78
return f .read .res , f .read .err
77
79
}
78
80
81
+ type fakeAlertStore struct {
82
+ alertstore.AlertStore
83
+
84
+ states map [string ]alertspb.FullStateDesc
85
+ }
86
+
87
+ func newFakeAlertStore () * fakeAlertStore {
88
+ return & fakeAlertStore {
89
+ states : make (map [string ]alertspb.FullStateDesc ),
90
+ }
91
+ }
92
+
93
+ func (f * fakeAlertStore ) GetFullState (ctx context.Context , user string ) (alertspb.FullStateDesc , error ) {
94
+ if result , ok := f .states [user ]; ok {
95
+ return result , nil
96
+ }
97
+ return alertspb.FullStateDesc {}, alertspb .ErrNotFound
98
+ }
99
+
79
100
func TestStateReplication (t * testing.T ) {
80
101
tc := []struct {
81
102
name string
@@ -102,7 +123,8 @@ func TestStateReplication(t *testing.T) {
102
123
reg := prometheus .NewPedanticRegistry ()
103
124
replicator := newFakeReplicator ()
104
125
replicator .read = readStateResult {res : nil , err : nil }
105
- s := newReplicatedStates ("user-1" , tt .replicationFactor , replicator , log .NewNopLogger (), reg )
126
+ store := newFakeAlertStore ()
127
+ s := newReplicatedStates ("user-1" , tt .replicationFactor , replicator , store , log .NewNopLogger (), reg )
106
128
107
129
require .False (t , s .Ready ())
108
130
{
@@ -163,6 +185,7 @@ func TestStateReplication_Settle(t *testing.T) {
163
185
name string
164
186
replicationFactor int
165
187
read readStateResult
188
+ storeStates map [string ]alertspb.FullStateDesc
166
189
results map [string ][][]byte
167
190
}{
168
191
{
@@ -228,9 +251,26 @@ func TestStateReplication_Settle(t *testing.T) {
228
251
},
229
252
},
230
253
{
231
- name : "when reading the full state fails, still become ready." ,
254
+ name : "when reading from replicas fails, state is read from storage." ,
255
+ replicationFactor : 3 ,
256
+ read : readStateResult {err : errors .New ("Read Error 1" )},
257
+ storeStates : map [string ]alertspb.FullStateDesc {
258
+ "user-1" : {
259
+ State : & clusterpb.FullState {
260
+ Parts : []clusterpb.Part {{Key : "key1" , Data : []byte ("Datum1" )}},
261
+ },
262
+ },
263
+ },
264
+ results : map [string ][][]byte {
265
+ "key1" : {[]byte ("Datum1" )},
266
+ "key2" : nil ,
267
+ },
268
+ },
269
+ {
270
+ name : "when reading from replicas and from storage fails, still become ready." ,
232
271
replicationFactor : 3 ,
233
272
read : readStateResult {err : errors .New ("Read Error 1" )},
273
+ storeStates : map [string ]alertspb.FullStateDesc {},
234
274
results : map [string ][][]byte {
235
275
"key1" : nil ,
236
276
"key2" : nil ,
@@ -253,7 +293,9 @@ func TestStateReplication_Settle(t *testing.T) {
253
293
254
294
replicator := newFakeReplicator ()
255
295
replicator .read = tt .read
256
- s := newReplicatedStates ("user-1" , tt .replicationFactor , replicator , log .NewNopLogger (), reg )
296
+ store := newFakeAlertStore ()
297
+ store .states = tt .storeStates
298
+ s := newReplicatedStates ("user-1" , tt .replicationFactor , replicator , store , log .NewNopLogger (), reg )
257
299
258
300
key1State := & fakeState {}
259
301
key2State := & fakeState {}
@@ -322,7 +364,7 @@ func TestStateReplication_GetFullState(t *testing.T) {
322
364
for _ , tt := range tc {
323
365
t .Run (tt .name , func (t * testing.T ) {
324
366
reg := prometheus .NewPedanticRegistry ()
325
- s := newReplicatedStates ("user-1" , 1 , nil , log .NewNopLogger (), reg )
367
+ s := newReplicatedStates ("user-1" , 1 , nil , nil , log .NewNopLogger (), reg )
326
368
327
369
for key , datum := range tt .data {
328
370
state := & fakeState {binary : datum }
0 commit comments