mirror of
https://github.com/grafana/grafana.git
synced 2025-08-06 05:56:24 +08:00
Alerting: delete orphaned records from kvstore (#40337)
This commit is contained in:

committed by
GitHub

parent
1b7ca0413a
commit
153c356993
@ -165,4 +165,77 @@ func TestKVStore(t *testing.T) {
|
||||
require.False(t, ok, "all keys should be deleted at this point")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("listing existing keys", func(t *testing.T) {
|
||||
kv := createTestableKVStore(t)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
namespace, key := "listtest", "listtest"
|
||||
|
||||
testCases := []*TestCase{
|
||||
{
|
||||
OrgId: 1,
|
||||
Namespace: namespace,
|
||||
Key: key + "_1",
|
||||
},
|
||||
{
|
||||
OrgId: 2,
|
||||
Namespace: namespace,
|
||||
Key: key + "_2",
|
||||
},
|
||||
{
|
||||
OrgId: 3,
|
||||
Namespace: namespace,
|
||||
Key: key + "_3",
|
||||
},
|
||||
{
|
||||
OrgId: 4,
|
||||
Namespace: namespace,
|
||||
Key: key + "_4",
|
||||
},
|
||||
{
|
||||
OrgId: 1,
|
||||
Namespace: namespace,
|
||||
Key: "other_key",
|
||||
},
|
||||
{
|
||||
OrgId: 4,
|
||||
Namespace: namespace,
|
||||
Key: "another_one",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
err := kv.Set(ctx, tc.OrgId, tc.Namespace, tc.Key, tc.Value())
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
keys, err := kv.Keys(ctx, AllOrganizations, namespace, key[0:6])
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Len(t, keys, 4)
|
||||
|
||||
found := 0
|
||||
|
||||
for _, key := range keys {
|
||||
for _, tc := range testCases {
|
||||
if key.Key == tc.Key {
|
||||
found++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
require.Equal(t, 4, found, "querying with the wildcard should return 4 records")
|
||||
|
||||
keys, err = kv.Keys(ctx, 1, namespace, key[0:6])
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Len(t, keys, 1, "querying for a specific org should return 1 record")
|
||||
|
||||
keys, err = kv.Keys(ctx, AllOrganizations, "not_existing_namespace", "not_existing_key")
|
||||
require.NoError(t, err, "querying a not existing namespace and key should not throw an error")
|
||||
require.Len(t, keys, 0, "querying a not existing namespace and key should return an empty slice")
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user