2014-04-01 03:45:57 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2014-04-28 23:55:11 +00:00
|
|
|
"reflect"
|
2014-04-01 03:45:57 +00:00
|
|
|
"testing"
|
2015-01-09 01:31:32 +00:00
|
|
|
|
2018-08-14 16:08:33 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2014-04-01 03:45:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestKVSEndpoint_PUT_GET_DELETE(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2014-04-01 03:45:57 +00:00
|
|
|
|
|
|
|
keys := []string{
|
|
|
|
"baz",
|
|
|
|
"bar",
|
|
|
|
"foo/sub1",
|
|
|
|
"foo/sub2",
|
|
|
|
"zip",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
buf := bytes.NewBuffer([]byte("test"))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/kv/"+key, buf)
|
2014-04-01 03:45:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2014-04-01 03:45:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := obj.(bool); !res {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/kv/"+key, nil)
|
2014-04-01 03:45:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2014-04-01 03:45:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-21 20:11:05 +00:00
|
|
|
assertIndex(t, resp)
|
2014-04-01 03:45:57 +00:00
|
|
|
|
|
|
|
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 {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("DELETE", "/v1/kv/"+key, nil)
|
2014-04-01 03:45:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.KVSEndpoint(resp, req); err != nil {
|
2014-04-01 03:45:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKVSEndpoint_Recurse(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2014-04-01 03:45:57 +00:00
|
|
|
|
|
|
|
keys := []string{
|
|
|
|
"bar",
|
|
|
|
"baz",
|
|
|
|
"foo/sub1",
|
|
|
|
"foo/sub2",
|
|
|
|
"zip",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
buf := bytes.NewBuffer([]byte("test"))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/kv/"+key, buf)
|
2014-04-01 03:45:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2014-04-01 03:45:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := obj.(bool); !res {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Get all the keys
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/kv/?recurse", nil)
|
2014-04-01 03:45:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2014-04-01 03:45:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-21 20:11:05 +00:00
|
|
|
assertIndex(t, resp)
|
2014-04-01 03:45:57 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("DELETE", "/v1/kv/?recurse", nil)
|
2014-04-01 03:45:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a.srv.KVSEndpoint(resp, req); err != nil {
|
2014-04-01 03:45:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Get all the keys
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/kv/?recurse", nil)
|
2014-04-01 03:45:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2014-04-01 03:45:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 01:31:32 +00:00
|
|
|
func TestKVSEndpoint_DELETE_CAS(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2015-01-09 01:31:32 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
buf := bytes.NewBuffer([]byte("test"))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/kv/test", buf)
|
2015-01-09 01:31:32 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2015-01-09 01:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := obj.(bool); !res {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/kv/test", nil)
|
2015-01-09 01:31:32 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2015-01-09 01:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
d := obj.(structs.DirEntries)[0]
|
|
|
|
|
|
|
|
// Create a CAS request, bad index
|
|
|
|
{
|
|
|
|
buf := bytes.NewBuffer([]byte("zip"))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/v1/kv/test?cas=%d", d.ModifyIndex-1), buf)
|
2015-01-09 01:31:32 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2015-01-09 01:31:32 +00:00
|
|
|
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"))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/v1/kv/test?cas=%d", d.ModifyIndex), buf)
|
2015-01-09 01:31:32 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2015-01-09 01:31:32 +00:00
|
|
|
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()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, _ = a.srv.KVSEndpoint(resp, req)
|
2015-01-09 01:31:32 +00:00
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("should be destroyed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 03:45:57 +00:00
|
|
|
func TestKVSEndpoint_CAS(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2014-04-01 03:45:57 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
buf := bytes.NewBuffer([]byte("test"))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/kv/test?flags=50", buf)
|
2014-04-01 03:45:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2014-04-01 03:45:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := obj.(bool); !res {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/kv/test", nil)
|
2014-04-01 03:45:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2014-04-01 03:45:57 +00:00
|
|
|
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"))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/kv/test?flags=42&cas=%d", d.ModifyIndex-1), buf)
|
2014-04-01 03:45:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2014-04-01 03:45:57 +00:00
|
|
|
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"))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/kv/test?flags=42&cas=%d", d.ModifyIndex), buf)
|
2014-04-01 03:45:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2014-04-01 03:45:57 +00:00
|
|
|
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()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, _ = a.srv.KVSEndpoint(resp, req)
|
2014-04-01 03:45:57 +00:00
|
|
|
d = obj.(structs.DirEntries)[0]
|
|
|
|
|
|
|
|
if d.Flags != 42 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if string(d.Value) != "zip" {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
}
|
2014-04-28 23:55:11 +00:00
|
|
|
|
|
|
|
func TestKVSEndpoint_ListKeys(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2014-04-28 23:55:11 +00:00
|
|
|
|
|
|
|
keys := []string{
|
|
|
|
"bar",
|
|
|
|
"baz",
|
|
|
|
"foo/sub1",
|
|
|
|
"foo/sub2",
|
|
|
|
"zip",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
buf := bytes.NewBuffer([]byte("test"))
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/kv/"+key, buf)
|
2014-04-28 23:55:11 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2014-04-28 23:55:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := obj.(bool); !res {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Get all the keys
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/kv/?keys&seperator=/", nil)
|
2014-04-28 23:55:11 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
2014-04-28 23:55:11 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-19 23:14:03 +00:00
|
|
|
|
|
|
|
func TestKVSEndpoint_AcquireRelease(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2014-05-19 23:14:03 +00:00
|
|
|
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// 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")
|
|
|
|
}
|
2014-05-19 23:14:03 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// 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]
|
2014-05-19 23:14:03 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// Check the flags
|
|
|
|
if d.Session != id {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
2014-05-19 23:14:03 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// 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")
|
|
|
|
}
|
2014-05-19 23:14:03 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2014-05-19 23:14:03 +00:00
|
|
|
}
|
2014-05-20 23:53:43 +00:00
|
|
|
|
|
|
|
func TestKVSEndpoint_GET_Raw(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2014-05-20 23:53:43 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
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")
|
|
|
|
}
|
2014-05-20 23:53:43 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/kv/test?raw", nil)
|
|
|
|
resp = httptest.NewRecorder()
|
2020-05-19 22:44:06 +00:00
|
|
|
_, err = a.srv.KVSEndpoint(resp, req)
|
2017-05-21 18:31:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
2021-04-14 22:49:14 +00:00
|
|
|
// Check the headers
|
|
|
|
contentTypeHdr := resp.Header().Values("Content-Type")
|
|
|
|
if len(contentTypeHdr) != 1 {
|
|
|
|
t.Fatalf("expected 1 value for Content-Type header, got %d: %+v", len(contentTypeHdr), contentTypeHdr)
|
|
|
|
}
|
|
|
|
if contentTypeHdr[0] != "text/plain" {
|
|
|
|
t.Fatalf("expected Content-Type header to be \"text/plain\", got %q", contentTypeHdr[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
optionsHdr := resp.Header().Values("X-Content-Type-Options")
|
|
|
|
if len(optionsHdr) != 1 {
|
|
|
|
t.Fatalf("expected 1 value for X-Content-Type-Options header, got %d: %+v", len(optionsHdr), optionsHdr)
|
|
|
|
}
|
|
|
|
if optionsHdr[0] != "nosniff" {
|
|
|
|
t.Fatalf("expected X-Content-Type-Options header to be \"nosniff\", got %q", optionsHdr[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
cspHeader := resp.Header().Values("Content-Security-Policy")
|
|
|
|
if len(cspHeader) != 1 {
|
|
|
|
t.Fatalf("expected 1 value for Content-Security-Policy header, got %d: %+v", len(optionsHdr), optionsHdr)
|
|
|
|
}
|
|
|
|
if cspHeader[0] != "sandbox" {
|
|
|
|
t.Fatalf("expected X-Content-Type-Options header to be \"sandbox\", got %q", optionsHdr[0])
|
|
|
|
}
|
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// Check the body
|
|
|
|
if !bytes.Equal(resp.Body.Bytes(), []byte("test")) {
|
|
|
|
t.Fatalf("bad: %s", resp.Body.Bytes())
|
|
|
|
}
|
2014-05-20 23:53:43 +00:00
|
|
|
}
|
2015-01-23 20:48:39 +00:00
|
|
|
|
|
|
|
func TestKVSEndpoint_PUT_ConflictingFlags(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2015-01-23 20:48:39 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
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")
|
|
|
|
}
|
2015-01-23 20:48:39 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 22:49:14 +00:00
|
|
|
func TestKVSEndpoint_GET(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
a := NewTestAgent(t, "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
req, _ = http.NewRequest("GET", "/v1/kv/test", nil)
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
_, err = a.srv.KVSEndpoint(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
// The following headers are only included when returning a raw KV response
|
|
|
|
|
|
|
|
contentTypeHdr := resp.Header().Values("Content-Type")
|
|
|
|
if len(contentTypeHdr) != 0 {
|
|
|
|
t.Fatalf("expected no Content-Type header, got %d: %+v", len(contentTypeHdr), contentTypeHdr)
|
|
|
|
}
|
|
|
|
|
|
|
|
optionsHdr := resp.Header().Values("X-Content-Type-Options")
|
|
|
|
if len(optionsHdr) != 0 {
|
|
|
|
t.Fatalf("expected no X-Content-Type-Options header, got %d: %+v", len(optionsHdr), optionsHdr)
|
|
|
|
}
|
|
|
|
|
|
|
|
cspHeader := resp.Header().Values("Content-Security-Policy")
|
|
|
|
if len(cspHeader) != 0 {
|
|
|
|
t.Fatalf("expected no Content-Security-Policy header, got %d: %+v", len(optionsHdr), optionsHdr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 20:48:39 +00:00
|
|
|
func TestKVSEndpoint_DELETE_ConflictingFlags(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2015-01-23 20:48:39 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
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")
|
|
|
|
}
|
2015-01-23 20:48:39 +00:00
|
|
|
}
|