This repository has been archived by the owner on May 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kdb_test.go
142 lines (100 loc) · 3.5 KB
/
kdb_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package kdb_test
import (
"errors"
"testing"
elektra "go.libelektra.org/kdb"
. "go.libelektra.org/test"
)
func TestOpen(t *testing.T) {
kdb := elektra.New()
err := kdb.Open()
defer kdb.Close()
Checkf(t, err, "kdb.Open() failed: %v", err)
}
func TestSet(t *testing.T) {
kdb := elektra.New()
err := kdb.Open()
defer kdb.Close()
Checkf(t, err, "kdb.Open() failed: %v", err)
ks := elektra.NewKeySet()
parentKey, _ := elektra.NewKey("user:/tests/go/elektra/set")
_, _ = kdb.Get(ks, parentKey)
key, _ := elektra.NewKey("user:/tests/go/elektra/set/key")
ks.AppendKey(key)
_, err = kdb.Set(ks, parentKey)
Checkf(t, err, "kdb Set failed %v", err)
}
func TestRemoveKey(t *testing.T) {
kdb := elektra.New()
namespace := "user:/tests/go/elektra/removekey"
parentKey, err := elektra.NewKey(namespace)
Check(t, err, "could not create parent Key")
err = kdb.Open()
Check(t, err, "could not open KDB")
defer kdb.Close()
k, err := elektra.NewKey(namespace+"/helloworld", "Hello World")
Check(t, err, "could not create Key")
k2, err := elektra.NewKey(namespace+"/helloworld2", "Hello World 2")
Check(t, err, "could not create Key")
ks := elektra.NewKeySet()
Check(t, err, "could not create KeySet")
changed, err := kdb.Get(ks, parentKey)
Assert(t, changed, "kdb.Get() has not retrieved any keys")
Check(t, err, "could not Get KeySet")
ks.AppendKey(k)
Check(t, err, "could not append Key to KeySet")
ks.AppendKey(k2)
Check(t, err, "could not append Key to KeySet")
changed, err = kdb.Set(ks, parentKey)
Assert(t, changed, "kdb.Set() has not updated any keys")
Check(t, err, "could not Set KeySet")
_, err = kdb.Get(ks, parentKey)
Check(t, err, "could not Get KeySet")
foundKey := ks.LookupByName("/tests/go/elektra/removekey/helloworld")
Assertf(t, foundKey != nil, "KeySet does not contain key %s", k.Name())
foundKey = ks.Lookup(k2)
Assertf(t, foundKey != nil, "KeySet does not contain key %s", k2.Name())
removed := ks.Remove(k2)
Assert(t, removed != nil, "could not delete Key")
changed, err = kdb.Set(ks, parentKey)
Assert(t, changed, "kdb.Set() has not updated any keys")
Check(t, err, "could not set KeySet")
_, err = kdb.Get(ks, parentKey)
Check(t, err, "could not Get KeySet")
foundKey = ks.Lookup(k)
Assertf(t, foundKey != nil, "KeySet does not contain key %s", k.Name())
foundKey = ks.Lookup(k2)
Assertf(t, foundKey == nil, "KeySet contains key %s", k2.Name())
}
func TestConflict(t *testing.T) {
kdb1 := elektra.New()
kdb2 := elektra.New()
ks1 := elektra.NewKeySet()
ks2 := elektra.NewKeySet()
rootKey1, _ := elektra.NewKey("user:/tests/go/elektra/conflict")
rootKey2, _ := elektra.NewKey("user:/tests/go/elektra/conflict")
firstKey, _ := elektra.NewKey("user:/tests/go/elektra/conflict/first")
secondKey, _ := elektra.NewKey("user:/tests/go/elektra/conflict/second")
conflictKey, _ := elektra.NewKey("user:/tests/go/elektra/conflict/second")
_ = kdb1.Open()
defer kdb1.Close()
_, _ = kdb1.Get(ks1, rootKey1)
ks1.AppendKey(firstKey)
_, _ = kdb1.Set(ks1, rootKey1)
_ = kdb2.Open()
defer kdb2.Close()
_, _ = kdb2.Get(ks2, rootKey2)
ks1.AppendKey(secondKey)
_, _ = kdb1.Set(ks1, rootKey1)
ks2.AppendKey(conflictKey)
_, err := kdb2.Set(ks2, rootKey2)
Assertf(t, errors.Is(err, elektra.ErrConflictingState), "expected conflict err: %v", err)
}
func TestVersion(t *testing.T) {
kdb := elektra.New()
err := kdb.Open()
defer kdb.Close()
version, err := kdb.Version()
Checkf(t, err, "kdb.Version() failed: %v", err)
Assert(t, version != "", "kdb.Version() is empty")
}