Commit ae9081c4 authored by pfandzelter's avatar pfandzelter
Browse files

Merge branch 'tp/improve-badgerdb-tests' into 'main'

improve badgerdb tests

See merge request !152
parents 336cab5b fe2e7f4e
Pipeline #33918 passed with stages
in 17 minutes and 59 seconds
......@@ -21,6 +21,12 @@ var db *Storage
func TestMain(m *testing.M) {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Logger = log.Output(
zerolog.ConsoleWriter{
Out: os.Stderr,
NoColor: false,
},
)
fInfo, err := os.Stat(badgerDBPath)
......@@ -59,25 +65,19 @@ func TestKeygroups(t *testing.T) {
kg := "test-kg"
err := db.CreateKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
}
assert.NoError(t, err)
exists := db.ExistsKeygroup(kg)
if !exists {
t.Fatal("Keygroup does not exist after creation")
}
assert.True(t, exists, "Keygroup does not exist after creation")
err = db.DeleteKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
}
assert.NoError(t, err)
exists = db.ExistsKeygroup(kg)
if exists {
t.Fatal("Keygroup does still exist after deletion")
}
assert.False(t, exists, "Keygroup still exists after deletion")
}
......@@ -89,10 +89,7 @@ func TestReadSome(t *testing.T) {
err := db.CreateKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
// 2. put in a bunch of items
ids := make([]string, updates)
......@@ -104,19 +101,13 @@ func TestReadSome(t *testing.T) {
err = db.Update(kg, ids[i], vals[i], false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
}
res, err := db.ReadSome(kg, "id"+strconv.Itoa(scanStart), uint64(scanRange))
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
assert.Len(t, res, scanRange)
......@@ -130,68 +121,41 @@ func TestReadAll(t *testing.T) {
kg := "test-read-all"
err := db.CreateKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg, "id-1", "data-1", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg, "id-2", "data-2", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg, "id-3", "data-3", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
kg2 := "test-read-all-2"
err = db.CreateKeygroup(kg2)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg2, "id-1", "data-1", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg2, "id-2", "data-2", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg2, "id-3", "data-3", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
res, err := db.ReadAll(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
assert.Equal(t, "data-1", res["id-1"])
assert.Equal(t, "data-2", res["id-2"])
......@@ -203,71 +167,46 @@ func TestIDs(t *testing.T) {
kg := "test-ids"
err := db.CreateKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg, "id-1", "data-1", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg, "id-2", "data-2", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg, "id-3", "data-3", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
kg2 := "test-read-all-2"
err = db.CreateKeygroup(kg2)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg2, "id-1", "data-1", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg2, "id-2", "data-2", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg2, "id-3", "data-3", false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
res, err := db.IDs(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.Equal(t, []string{"id-1", "id-2", "id-3"}, res)
assert.NoError(t, err)
assert.Len(t, res, 3)
assert.Contains(t, res, "id-1")
assert.Contains(t, res, "id-2")
assert.Contains(t, res, "id-3")
}
func TestItemExists(t *testing.T) {
......@@ -278,27 +217,17 @@ func TestItemExists(t *testing.T) {
err := db.CreateKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg, id, value, false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
ex := db.Exists(kg, id)
if !ex {
t.Error("exists says existing item doesn't exist")
}
assert.True(t, ex, "exists says existing item doesn't exist")
ex = db.Exists(kg, id2)
if ex {
t.Error("exists says non-existent item exists")
}
assert.False(t, ex, "exists says non-existent item exists")
}
......@@ -309,25 +238,17 @@ func TestItemGet(t *testing.T) {
err := db.CreateKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg, id, value, false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
retr, err := db.Read(kg, id)
if err != nil {
t.Error(err)
}
if retr != value {
t.Errorf("Expected to get %s but got %s", value, retr)
}
assert.NoError(t, err)
assert.Equal(t, value, retr)
}
func TestItemDelete(t *testing.T) {
......@@ -338,43 +259,27 @@ func TestItemDelete(t *testing.T) {
err := db.CreateKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg, id, value, false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
retr, err := db.Read(kg, id)
if err != nil {
t.Error(err)
}
if retr != value {
t.Errorf("Expected to get %s but got %s", value, retr)
}
assert.NoError(t, err)
assert.Equal(t, value, retr)
err = db.Delete(kg, id)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
retr, err = db.Read(kg, id)
if err == nil {
t.Errorf("read a deleted item: %s", retr)
}
_, err = db.Read(kg, id)
assert.Error(t, err, "read a deleted item")
err = db.Delete(kg, id2)
if err != nil {
t.Error(err, "deleting non-existent keys should be allowed")
}
assert.NoError(t, err, "deleting non-existent keys should be allowed")
}
......@@ -385,29 +290,19 @@ func TestItemAfterDeleteKeygroup(t *testing.T) {
err := db.CreateKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg, id, value, false, 0)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.DeleteKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
retr, err := db.Read(kg, id)
if err == nil {
t.Errorf("Expected an error, but got %s", retr)
}
_, err = db.Read(kg, id)
assert.Error(t, err)
}
func TestExpiry(t *testing.T) {
......@@ -417,32 +312,23 @@ func TestExpiry(t *testing.T) {
err := db.CreateKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
err = db.Update(kg, id, value, false, 10)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
retr, err := db.Read(kg, id)
if err != nil {
t.Error(err)
}
if retr != value {
t.Errorf("Expected to get %s but got %s", value, retr)
}
assert.NoError(t, err)
assert.Equal(t, value, retr)
time.Sleep(10 * time.Second)
_, err = db.Read(kg, id)
if err == nil {
t.Error(err)
}
assert.Error(t, err)
}
func TestAppend(t *testing.T) {
......@@ -452,40 +338,27 @@ func TestAppend(t *testing.T) {
err := db.CreateKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
key1, err := db.Append(kg, v1, 0)
if err != nil {
t.Error(err)
}
if key1 != "0" {
t.Errorf("Expected to get %s but got %s", "0", key1)
}
assert.NoError(t, err)
assert.Equal(t, "0", key1)
key2, err := db.Append(kg, v2, 0)
if err != nil {
t.Error(err)
}
if key2 != "1" {
t.Errorf("Expected to get %s but got %s", "0", key2)
}
assert.NoError(t, err)
assert.Equal(t, "1", key2)
for i := 2; i < 100; i++ {
v := "value-" + strconv.Itoa(i)
key, err := db.Append(kg, v, 0)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
if key != strconv.Itoa(i) {
t.Errorf("Expected to get %s but got %s", strconv.Itoa(i), key)
}
assert.Equal(t, strconv.Itoa(i), key)
}
}
......@@ -496,10 +369,7 @@ func TestConcurrentAppend(t *testing.T) {
err := db.CreateKeygroup(kg)
if err != nil {
log.Err(err).Msg(err.(*errors.Error).ErrorStack())
t.Error(err)
}
assert.NoError(t, err)
keys := make([]map[string]struct{}, concurrent)
done := make(chan struct{})
......@@ -511,9 +381,7 @@ func TestConcurrentAppend(t *testing.T) {
v := fmt.Sprintf("value-%d-%d", id, j)
key, err := db.Append(kg, v, 0)
if err != nil {
t.Error(err.(*errors.Error).ErrorStack())
}
assert.NoError(t, err)
(*keys)[key] = struct{}{}
}
......@@ -543,9 +411,7 @@ func TestTriggerNodes(t *testing.T) {
err := db.CreateKeygroup(kg)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
t1 := "t1"
t1host := "1.1.1.1:3000"
......@@ -558,95 +424,55 @@ func TestTriggerNodes(t *testing.T) {
err = db.AddKeygroupTrigger(kg, t1, t1host)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
err = db.AddKeygroupTrigger(kg, t1, t1host)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
err = db.AddKeygroupTrigger(kg, t2, t2host)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
tList, err := db.GetKeygroupTrigger(kg)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
log.Debug().Msgf("List of keygroup triggers: %#v", tList)
if len(tList) != 2 {
t.Error("not the right number of triggers for this keygroup")
}
if _, ok := tList[t1]; !ok {
t.Error("t1 not in list of triggers for this keygroup")
}
if _, ok := tList[t2]; !ok {
t.Error("t2 not in list of triggers for this keygroup")
}
if host := tList[t1]; host != t1host {
t.Error("t1host not correct")
}
if host := tList[t2]; host != t2host {
t.Error("t1host not correct")
}
assert.Len(t, tList, 2)
assert.Contains(t, tList, t1)
assert.Equal(t, tList[t1], t1host, "t1host not correct")
assert.Contains(t, tList, t2)
assert.Equal(t, tList[t2], t2host, "t2host not correct")
err = db.DeleteKeygroupTrigger(kg, t1)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
tList, err = db.GetKeygroupTrigger(kg)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
log.Debug().Msgf("List of keygroup triggers: %#v", tList)
if len(tList) != 1 {
t.Error("not the right number of triggers for this keygroup")
}
if _, ok := tList[t2]; !ok {
t.Error("t2 not in list of triggers for this keygroup")
}
if host := tList[t2]; host != t2host {
t.Error("t1host not correct")
}
assert.Len(t, tList, 1)
assert.Contains(t, tList, t2)
assert.Equal(t, tList[t2], t2host, "t2host not correct")
err = db.AddKeygroupTrigger(kg, t3, t3host)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
err = db.DeleteKeygroup(kg)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)