mirror of https://github.com/status-im/consul.git
testutil: support retrieving kv pairs
This commit is contained in:
parent
63f05b1c56
commit
ed32eb3917
|
@ -13,6 +13,7 @@ package testutil
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -96,6 +97,11 @@ type TestCheck struct {
|
|||
TTL string `json:",omitempty"`
|
||||
}
|
||||
|
||||
// TestKVResponse is what we use to decode KV data.
|
||||
type TestKVResponse struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
// TestServer is the main server wrapper struct.
|
||||
type TestServer struct {
|
||||
PID int
|
||||
|
@ -289,6 +295,23 @@ func (s *TestServer) SetKV(key string, val []byte) {
|
|||
s.put("/v1/kv/"+key, bytes.NewBuffer(val))
|
||||
}
|
||||
|
||||
// GetKV retrieves a single key and returns its value
|
||||
func (s *TestServer) GetKV(key string) []byte {
|
||||
data := s.get("/v1/kv/" + key)
|
||||
|
||||
var result []*TestKVResponse
|
||||
if err := json.Unmarshal(data, &result); err != nil {
|
||||
s.t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
v, err := base64.StdEncoding.DecodeString(result[0].Value)
|
||||
if err != nil {
|
||||
s.t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
return []byte(v)
|
||||
}
|
||||
|
||||
// PopulateKV fills the Consul KV with data from a generic map.
|
||||
func (s *TestServer) PopulateKV(data map[string][]byte) {
|
||||
for k, v := range data {
|
||||
|
|
Loading…
Reference in New Issue