func deleteTree(kv kvdb.Kvdb, t *testing.T) { fmt.Println("deleteTree") prefix := "tree" keys := map[string]string{ prefix + "/1cbc9a98-072a-4793-8608-01ab43db96c8": "bar", prefix + "/foo": "baz", } for key, val := range keys { _, err := kv.Put(key, []byte(val), 0) assert.NoError(t, err, "Unexpected error on Put") } for key := range keys { _, err := kv.Get(key) assert.NoError(t, err, "Unexpected error on Get") } err := kv.DeleteTree(prefix) assert.NoError(t, err, "Unexpected error on DeleteTree") for key := range keys { _, err := kv.Get(key) assert.Error(t, err, "Get should fail on all keys after DeleteTree") } }
func cas(kv kvdb.Kvdb, t *testing.T) { fmt.Println("\ncas") key := "foo/docker" val := "great" defer func() { kv.Delete(key) }() kvPair, err := kv.Put(key, []byte(val), 0) assert.NoError(t, err, "Unxpected error in Put") kvPair, err = kv.Get(key) assert.NoError(t, err, "Failed in Get") _, err = kv.CompareAndSet(kvPair, kvdb.KVFlags(0), []byte("badval")) assert.Error(t, err, "CompareAndSet should fail on an incorrect previous value") //assert.EqualError(t, err, kvdb.ErrValueMismatch.Error(), "CompareAndSet should return value mismatch error") copyKVPair := *kvPair copyKVPair.ModifiedIndex++ _, err = kv.CompareAndSet(©KVPair, kvdb.KVModifiedIndex, nil) assert.Error(t, err, "CompareAndSet should fail on an incorrect modified index") //kvPair.ModifiedIndex-- copyKVPair.ModifiedIndex-- kvPair, err = kv.CompareAndSet(©KVPair, kvdb.KVModifiedIndex, nil) assert.NoError(t, err, "CompareAndSet should succeed on an correct modified index") kvPairNew, err := kv.CompareAndSet(kvPair, kvdb.KVFlags(0), []byte(val)) if err != nil { // consul does not handle this kind of compare and set assert.EqualError(t, err, kvdb.ErrNotSupported.Error(), "Invalid error returned : %v", err) } else { assert.NoError(t, err, "CompareAndSet should succeed on an correct value") } if kvPairNew != nil { kvPair = kvPairNew } kvPair, err = kv.CompareAndSet(kvPair, kvdb.KVModifiedIndex, []byte(val)) assert.NoError(t, err, "CompareAndSet should succeed on an correct value and modified index") }
func deleteKey(kv kvdb.Kvdb, t *testing.T) { fmt.Println("deleteKey") key := "delete_key" _, err := kv.Delete(key) _, err = kv.Put(key, []byte("delete_me"), 0) assert.NoError(t, err, "Unexpected error on Put") _, err = kv.Get(key) assert.NoError(t, err, "Unexpected error on Get") _, err = kv.Delete(key) assert.NoError(t, err, "Unexpected error on Delete") _, err = kv.Get(key) assert.Error(t, err, "Get should fail on deleted key") _, err = kv.Delete(key) assert.Error(t, err, "Delete should fail on non existent key") }
func get(kv kvdb.Kvdb, t *testing.T) { fmt.Println("get") kvPair, err := kv.Get("DEADCAFE") assert.Error(t, err, "Expecting error value for non-existent value") key := "foo/docker" val := "great" defer func() { kv.Delete(key) }() kvPair, err = kv.Put(key, []byte(val), 0) assert.NoError(t, err, "Unexpected error in Put") kvPair, err = kv.Get(key) assert.NoError(t, err, "Failed in Get") assert.Equal(t, key, kvPair.Key, "Key mismatch in Get") assert.Equal(t, string(kvPair.Value), val, "value mismatch in Get") }
func createWithTTL(kv kvdb.Kvdb, t *testing.T) { fmt.Println("create with ttl") key := "create/foo" kv.Delete(key) assert.NotNil(t, kv, "Default KVDB is not set") _, err := kv.Create(key, []byte("bar"), 6) if err != nil { // Consul does not support ttl less than 10 assert.EqualError(t, err, kvdb.ErrTTLNotSupported.Error(), "ttl not supported") _, err := kv.Create(key, []byte("bar"), 20) assert.NoError(t, err, "Error on create") // Consul doubles the ttl value time.Sleep(time.Second * 20) _, err = kv.Get(key) assert.Error(t, err, "Expecting error value for expired value") } else { assert.NoError(t, err, "Error on create") time.Sleep(time.Second * 7) _, err = kv.Get(key) assert.Error(t, err, "Expecting error value for expired value") } }