MCPcopy Index your code
hub / github.com/cortexproject/cortex / TestSyncRuleGroups

Function TestSyncRuleGroups

pkg/ruler/manager_test.go:24–114  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

22)
23
24func TestSyncRuleGroups(t *testing.T) {
25 dir := t.TempDir()
26
27 waitDurations := []time.Duration{
28 1 * time.Millisecond,
29 1 * time.Millisecond,
30 }
31
32 ruleManagerFactory := RuleManagerFactory(nil, waitDurations)
33 limits := &ruleLimits{externalLabels: labels.FromStrings("from", "cortex")}
34
35 m, err := NewDefaultMultiTenantManager(Config{RulePath: dir}, limits, ruleManagerFactory, nil, nil, log.NewNopLogger())
36 require.NoError(t, err)
37
38 const user = "testUser"
39
40 userRules := map[string]rulespb.RuleGroupList{
41 user: {
42 &rulespb.RuleGroupDesc{
43 Name: "group1",
44 Namespace: "ns",
45 Interval: 1 * time.Minute,
46 User: user,
47 },
48 },
49 }
50 m.SyncRuleGroups(context.Background(), userRules)
51
52 mgr := getManager(m, user)
53 require.NotNil(t, mgr)
54
55 test.Poll(t, 1*time.Second, true, func() any {
56 return mgr.(*mockRulesManager).running.Load()
57 })
58
59 // Verify that user rule groups are now cached locally and notifiers are created.
60 {
61 users, err := m.mapper.users()
62 _, ok := m.notifiers[user]
63 require.NoError(t, err)
64 require.Equal(t, []string{user}, users)
65 require.True(t, ok)
66 lset, ok := m.userExternalLabels.get(user)
67 require.True(t, ok)
68 require.Equal(t, limits.RulerExternalLabels(user), lset)
69 }
70
71 // Passing empty map / nil stops all managers.
72 m.SyncRuleGroups(context.Background(), nil)
73 require.Nil(t, getManager(m, user))
74
75 // Make sure old manager was stopped.
76 test.Poll(t, 1*time.Second, false, func() any {
77 return mgr.(*mockRulesManager).running.Load()
78 })
79
80 // Verify that local rule groups were removed.
81 {

Callers

nothing calls this directly

Calls 11

SyncRuleGroupsMethod · 0.95
RulerExternalLabelsMethod · 0.95
StopMethod · 0.95
PollFunction · 0.92
RuleManagerFactoryFunction · 0.85
getManagerFunction · 0.85
LoadMethod · 0.80
usersMethod · 0.80
EqualMethod · 0.65
getMethod · 0.45

Tested by

no test coverage detected