testutil: support retrieving kv pairs

This commit is contained in:
Ryan Uber 2015-03-11 16:10:07 -07:00
parent 63f05b1c56
commit ed32eb3917
1 changed files with 23 additions and 0 deletions

View File

@ -13,6 +13,7 @@ package testutil
import ( import (
"bytes" "bytes"
"encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -96,6 +97,11 @@ type TestCheck struct {
TTL string `json:",omitempty"` 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. // TestServer is the main server wrapper struct.
type TestServer struct { type TestServer struct {
PID int PID int
@ -289,6 +295,23 @@ func (s *TestServer) SetKV(key string, val []byte) {
s.put("/v1/kv/"+key, bytes.NewBuffer(val)) 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. // PopulateKV fills the Consul KV with data from a generic map.
func (s *TestServer) PopulateKV(data map[string][]byte) { func (s *TestServer) PopulateKV(data map[string][]byte) {
for k, v := range data { for k, v := range data {