mirror of https://github.com/hashicorp/consul
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
464 lines
9.8 KiB
464 lines
9.8 KiB
11 years ago
|
package agent
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
11 years ago
|
"reflect"
|
||
11 years ago
|
"testing"
|
||
10 years ago
|
|
||
8 years ago
|
"github.com/hashicorp/consul/agent/consul/structs"
|
||
11 years ago
|
)
|
||
|
|
||
|
func TestKVSEndpoint_PUT_GET_DELETE(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
|
keys := []string{
|
||
|
"baz",
|
||
|
"bar",
|
||
|
"foo/sub1",
|
||
|
"foo/sub2",
|
||
|
"zip",
|
||
|
}
|
||
|
|
||
|
for _, key := range keys {
|
||
|
buf := bytes.NewBuffer([]byte("test"))
|
||
8 years ago
|
req, _ := http.NewRequest("PUT", "/v1/kv/"+key, buf)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if res := obj.(bool); !res {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for _, key := range keys {
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/kv/"+key, nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
11 years ago
|
assertIndex(t, resp)
|
||
11 years ago
|
|
||
|
res, ok := obj.(structs.DirEntries)
|
||
|
if !ok {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
|
|
||
|
if len(res) != 1 {
|
||
|
t.Fatalf("bad: %v", res)
|
||
|
}
|
||
|
|
||
|
if res[0].Key != key {
|
||
|
t.Fatalf("bad: %v", res)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for _, key := range keys {
|
||
8 years ago
|
req, _ := http.NewRequest("DELETE", "/v1/kv/"+key, nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
if _, err := a.srv.KVSEndpoint(resp, req); err != nil {
|
||
11 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestKVSEndpoint_Recurse(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
|
keys := []string{
|
||
|
"bar",
|
||
|
"baz",
|
||
|
"foo/sub1",
|
||
|
"foo/sub2",
|
||
|
"zip",
|
||
|
}
|
||
|
|
||
|
for _, key := range keys {
|
||
|
buf := bytes.NewBuffer([]byte("test"))
|
||
8 years ago
|
req, _ := http.NewRequest("PUT", "/v1/kv/"+key, buf)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if res := obj.(bool); !res {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
{
|
||
|
// Get all the keys
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/kv/?recurse", nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
11 years ago
|
assertIndex(t, resp)
|
||
11 years ago
|
|
||
|
res, ok := obj.(structs.DirEntries)
|
||
|
if !ok {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
|
|
||
|
if len(res) != len(keys) {
|
||
|
t.Fatalf("bad: %v", res)
|
||
|
}
|
||
|
|
||
|
for idx, key := range keys {
|
||
|
if res[idx].Key != key {
|
||
|
t.Fatalf("bad: %v %v", res[idx].Key, key)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
{
|
||
8 years ago
|
req, _ := http.NewRequest("DELETE", "/v1/kv/?recurse", nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
if _, err := a.srv.KVSEndpoint(resp, req); err != nil {
|
||
11 years ago
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
{
|
||
|
// Get all the keys
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/kv/?recurse", nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if obj != nil {
|
||
|
t.Fatalf("bad: %v", obj)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
func TestKVSEndpoint_DELETE_CAS(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
10 years ago
|
|
||
|
{
|
||
|
buf := bytes.NewBuffer([]byte("test"))
|
||
8 years ago
|
req, _ := http.NewRequest("PUT", "/v1/kv/test", buf)
|
||
10 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
10 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if res := obj.(bool); !res {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/kv/test", nil)
|
||
10 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
10 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
d := obj.(structs.DirEntries)[0]
|
||
|
|
||
|
// Create a CAS request, bad index
|
||
|
{
|
||
|
buf := bytes.NewBuffer([]byte("zip"))
|
||
8 years ago
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/v1/kv/test?cas=%d", d.ModifyIndex-1), buf)
|
||
10 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
10 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if res := obj.(bool); res {
|
||
|
t.Fatalf("should NOT work")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Create a CAS request, good index
|
||
|
{
|
||
|
buf := bytes.NewBuffer([]byte("zip"))
|
||
8 years ago
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/v1/kv/test?cas=%d", d.ModifyIndex), buf)
|
||
10 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
10 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if res := obj.(bool); !res {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Verify the delete
|
||
|
req, _ = http.NewRequest("GET", "/v1/kv/test", nil)
|
||
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, _ = a.srv.KVSEndpoint(resp, req)
|
||
10 years ago
|
if obj != nil {
|
||
|
t.Fatalf("should be destroyed")
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
func TestKVSEndpoint_CAS(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
|
{
|
||
|
buf := bytes.NewBuffer([]byte("test"))
|
||
8 years ago
|
req, _ := http.NewRequest("PUT", "/v1/kv/test?flags=50", buf)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if res := obj.(bool); !res {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/kv/test", nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
d := obj.(structs.DirEntries)[0]
|
||
|
|
||
|
// Check the flags
|
||
|
if d.Flags != 50 {
|
||
|
t.Fatalf("bad: %v", d)
|
||
|
}
|
||
|
|
||
|
// Create a CAS request, bad index
|
||
|
{
|
||
|
buf := bytes.NewBuffer([]byte("zip"))
|
||
8 years ago
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/kv/test?flags=42&cas=%d", d.ModifyIndex-1), buf)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if res := obj.(bool); res {
|
||
|
t.Fatalf("should NOT work")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Create a CAS request, good index
|
||
|
{
|
||
|
buf := bytes.NewBuffer([]byte("zip"))
|
||
8 years ago
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/kv/test?flags=42&cas=%d", d.ModifyIndex), buf)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if res := obj.(bool); !res {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Verify the update
|
||
|
req, _ = http.NewRequest("GET", "/v1/kv/test", nil)
|
||
|
resp = httptest.NewRecorder()
|
||
8 years ago
|
obj, _ = a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
d = obj.(structs.DirEntries)[0]
|
||
|
|
||
|
if d.Flags != 42 {
|
||
|
t.Fatalf("bad: %v", d)
|
||
|
}
|
||
|
if string(d.Value) != "zip" {
|
||
|
t.Fatalf("bad: %v", d)
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
|
func TestKVSEndpoint_ListKeys(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
|
keys := []string{
|
||
|
"bar",
|
||
|
"baz",
|
||
|
"foo/sub1",
|
||
|
"foo/sub2",
|
||
|
"zip",
|
||
|
}
|
||
|
|
||
|
for _, key := range keys {
|
||
|
buf := bytes.NewBuffer([]byte("test"))
|
||
8 years ago
|
req, _ := http.NewRequest("PUT", "/v1/kv/"+key, buf)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if res := obj.(bool); !res {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
{
|
||
|
// Get all the keys
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/kv/?keys&seperator=/", nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
assertIndex(t, resp)
|
||
|
|
||
|
res, ok := obj.([]string)
|
||
|
if !ok {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
|
|
||
|
expect := []string{"bar", "baz", "foo/", "zip"}
|
||
|
if !reflect.DeepEqual(res, expect) {
|
||
|
t.Fatalf("bad: %v", res)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
|
func TestKVSEndpoint_AcquireRelease(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
8 years ago
|
// Acquire the lock
|
||
|
id := makeTestSession(t, a.srv)
|
||
|
req, _ := http.NewRequest("PUT", "/v1/kv/test?acquire="+id, bytes.NewReader(nil))
|
||
|
resp := httptest.NewRecorder()
|
||
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
if res := obj.(bool); !res {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// Verify we have the lock
|
||
|
req, _ = http.NewRequest("GET", "/v1/kv/test", nil)
|
||
|
resp = httptest.NewRecorder()
|
||
|
obj, err = a.srv.KVSEndpoint(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
d := obj.(structs.DirEntries)[0]
|
||
11 years ago
|
|
||
8 years ago
|
// Check the flags
|
||
|
if d.Session != id {
|
||
|
t.Fatalf("bad: %v", d)
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// Release the lock
|
||
|
req, _ = http.NewRequest("PUT", "/v1/kv/test?release="+id, bytes.NewReader(nil))
|
||
|
resp = httptest.NewRecorder()
|
||
|
obj, err = a.srv.KVSEndpoint(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
if res := obj.(bool); !res {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// Verify we do not have the lock
|
||
|
req, _ = http.NewRequest("GET", "/v1/kv/test", nil)
|
||
|
resp = httptest.NewRecorder()
|
||
|
obj, err = a.srv.KVSEndpoint(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
d = obj.(structs.DirEntries)[0]
|
||
|
|
||
|
// Check the flags
|
||
|
if d.Session != "" {
|
||
|
t.Fatalf("bad: %v", d)
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
func TestKVSEndpoint_GET_Raw(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
8 years ago
|
buf := bytes.NewBuffer([]byte("test"))
|
||
|
req, _ := http.NewRequest("PUT", "/v1/kv/test", buf)
|
||
|
resp := httptest.NewRecorder()
|
||
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
if res := obj.(bool); !res {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
req, _ = http.NewRequest("GET", "/v1/kv/test?raw", nil)
|
||
|
resp = httptest.NewRecorder()
|
||
|
obj, err = a.srv.KVSEndpoint(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
assertIndex(t, resp)
|
||
|
|
||
|
// Check the body
|
||
|
if !bytes.Equal(resp.Body.Bytes(), []byte("test")) {
|
||
|
t.Fatalf("bad: %s", resp.Body.Bytes())
|
||
|
}
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
func TestKVSEndpoint_PUT_ConflictingFlags(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
10 years ago
|
|
||
8 years ago
|
req, _ := http.NewRequest("PUT", "/v1/kv/test?cas=0&acquire=xxx", nil)
|
||
|
resp := httptest.NewRecorder()
|
||
|
if _, err := a.srv.KVSEndpoint(resp, req); err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if resp.Code != 400 {
|
||
|
t.Fatalf("expected 400, got %d", resp.Code)
|
||
|
}
|
||
|
if !bytes.Contains(resp.Body.Bytes(), []byte("Conflicting")) {
|
||
|
t.Fatalf("expected conflicting args error")
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
func TestKVSEndpoint_DELETE_ConflictingFlags(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
10 years ago
|
|
||
8 years ago
|
req, _ := http.NewRequest("DELETE", "/v1/kv/test?recurse&cas=0", nil)
|
||
|
resp := httptest.NewRecorder()
|
||
|
if _, err := a.srv.KVSEndpoint(resp, req); err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
|
||
|
if resp.Code != 400 {
|
||
|
t.Fatalf("expected 400, got %d", resp.Code)
|
||
|
}
|
||
|
if !bytes.Contains(resp.Body.Bytes(), []byte("Conflicting")) {
|
||
|
t.Fatalf("expected conflicting args error")
|
||
|
}
|
||
10 years ago
|
}
|