1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package kvstore
4
5import (
6 "context"
7
8 kv "bnema.dev/ermite-kv"
9
10 "bnema.dev/ermite/internal/domain"
11)
12
13// SetAfterSettingsSnapshot installs the hook that runs between the snapshot of
14// DeleteEnvironment and its batch, and returns the function that removes it.
15func SetAfterSettingsSnapshot(f func()) (restore func()) {
16 afterSettingsSnapshot = f
17 return func() { afterSettingsSnapshot = nil }
18}
19
20// DecodeConcurrencyRecord, EncodeConcurrencyRecord and EncodeConcurrencyMark
21// expose the stored layout of a concurrency group to the tests.
22var (
23 DecodeConcurrencyRecord = decodeConcurrencyRecord
24 EncodeConcurrencyRecord = encodeConcurrencyRecord
25 EncodeConcurrencyMark = encodeConcurrencyMark
26)
27
28// RawVersionForTest is the kv version of a runner record: it moves on every
29// write.
30func (c *CI) RawVersionForTest(ctx context.Context, id string) (uint64, bool) {
31 item, ok := c.db.Get(ctx, runnerKey(id))
32 return item.Version, ok
33}
34
35// CountKeysForTest counts the keys of the database under prefix.
36func (s *Store) CountKeysForTest(ctx context.Context, prefix string) int {
37 n := 0
38 for range s.db.Snapshot(ctx).Prefix(ctx, []byte(prefix)) {
39 n++
40 }
41 return n
42}
43
44// DropIssueRecordForTest deletes the issue record and leaves its open index
45// entry: the state of an index that is stale.
46func (i *Issues) DropIssueRecordForTest(ctx context.Context, repo domain.RepoName, n int64) error {
47 var b kv.Batch
48 b.Delete(issueKey(repo, n))
49 _, err := i.db.Apply(ctx, &b)
50 return err
51}
52
53// DropMRRecordForTest is DropIssueRecordForTest for a merge request.
54func (m *MergeRequests) DropMRRecordForTest(ctx context.Context, repo domain.RepoName, n int64) error {
55 var b kv.Batch
56 b.Delete(mrKey(repo, n))
57 _, err := m.db.Apply(ctx, &b)
58 return err
59}
60
61// PutRawIssueForTest stores value as the issue record, bypassing the codec.
62func (i *Issues) PutRawIssueForTest(ctx context.Context, repo domain.RepoName, n int64, value []byte) error {
63 var b kv.Batch
64 b.Put(issueKey(repo, n), value)
65 _, err := i.db.Apply(ctx, &b)
66 return err
67}
68
69// RawMirrorKey is the stored protobuf of the mirror key, for encryption-at-rest tests.
70func (s *Store) RawMirrorKey(ctx context.Context) ([]byte, bool) {
71 item, ok := s.db.Get(ctx, []byte(domain.MirrorKeyPath))
72 return item.Value, ok
73}