test: refactor httpTest with TestAgent

This commit is contained in:
Frank Schroeder 2017-05-21 20:31:20 +02:00
parent 308f9929b3
commit 0be63d7060
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
10 changed files with 1664 additions and 1544 deletions

View File

@ -32,8 +32,10 @@ func makeTestACL(t *testing.T, srv *HTTPServer) string {
func TestACLUpdate(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
id := makeTestACL(t, srv)
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
id := makeTestACL(t, a.srv)
body := bytes.NewBuffer(nil)
enc := json.NewEncoder(body)
@ -47,7 +49,7 @@ func TestACLUpdate(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/acl/update?token=root", body)
resp := httptest.NewRecorder()
obj, err := srv.ACLUpdate(resp, req)
obj, err := a.srv.ACLUpdate(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -55,12 +57,13 @@ func TestACLUpdate(t *testing.T) {
if aclResp.ID != id {
t.Fatalf("bad: %v", aclResp)
}
})
}
func TestACLUpdate_Upsert(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
enc := json.NewEncoder(body)
raw := map[string]interface{}{
@ -73,7 +76,7 @@ func TestACLUpdate_Upsert(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/acl/update?token=root", body)
resp := httptest.NewRecorder()
obj, err := srv.ACLUpdate(resp, req)
obj, err := a.srv.ACLUpdate(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -81,16 +84,17 @@ func TestACLUpdate_Upsert(t *testing.T) {
if aclResp.ID != "my-old-id" {
t.Fatalf("bad: %v", aclResp)
}
})
}
func TestACLDestroy(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
id := makeTestACL(t, srv)
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
id := makeTestACL(t, a.srv)
req, _ := http.NewRequest("PUT", "/v1/acl/destroy/"+id+"?token=root", nil)
resp := httptest.NewRecorder()
obj, err := srv.ACLDestroy(resp, req)
obj, err := a.srv.ACLDestroy(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -100,7 +104,7 @@ func TestACLDestroy(t *testing.T) {
req, _ = http.NewRequest("GET", "/v1/acl/info/"+id, nil)
resp = httptest.NewRecorder()
obj, err = srv.ACLGet(resp, req)
obj, err = a.srv.ACLGet(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -111,17 +115,18 @@ func TestACLDestroy(t *testing.T) {
if len(respObj) != 0 {
t.Fatalf("bad: %v", respObj)
}
})
}
func TestACLClone(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
id := makeTestACL(t, srv)
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
id := makeTestACL(t, a.srv)
req, _ := http.NewRequest("PUT", "/v1/acl/clone/"+id+"?token=root", nil)
resp := httptest.NewRecorder()
obj, err := srv.ACLClone(resp, req)
obj, err := a.srv.ACLClone(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -135,7 +140,7 @@ func TestACLClone(t *testing.T) {
req, _ = http.NewRequest("GET", "/v1/acl/info/"+aclResp.ID, nil)
resp = httptest.NewRecorder()
obj, err = srv.ACLGet(resp, req)
obj, err = a.srv.ACLGet(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -146,15 +151,17 @@ func TestACLClone(t *testing.T) {
if len(respObj) != 1 {
t.Fatalf("bad: %v", respObj)
}
})
}
func TestACLGet(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
req, _ := http.NewRequest("GET", "/v1/acl/info/nope", nil)
resp := httptest.NewRecorder()
obj, err := srv.ACLGet(resp, req)
obj, err := a.srv.ACLGet(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -167,12 +174,15 @@ func TestACLGet(t *testing.T) {
}
})
httpTest(t, func(srv *HTTPServer) {
id := makeTestACL(t, srv)
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
id := makeTestACL(t, a.srv)
req, _ := http.NewRequest("GET", "/v1/acl/info/"+id, nil)
resp := httptest.NewRecorder()
obj, err := srv.ACLGet(resp, req)
obj, err := a.srv.ACLGet(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -188,15 +198,17 @@ func TestACLGet(t *testing.T) {
func TestACLList(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
var ids []string
for i := 0; i < 10; i++ {
ids = append(ids, makeTestACL(t, srv))
ids = append(ids, makeTestACL(t, a.srv))
}
req, _ := http.NewRequest("GET", "/v1/acl/list?token=root", nil)
resp := httptest.NewRecorder()
obj, err := srv.ACLList(resp, req)
obj, err := a.srv.ACLList(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -209,15 +221,16 @@ func TestACLList(t *testing.T) {
if len(respObj) != 12 {
t.Fatalf("bad: %v", respObj)
}
})
}
func TestACLReplicationStatus(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
req, _ := http.NewRequest("GET", "/v1/acl/replication", nil)
resp := httptest.NewRecorder()
obj, err := srv.ACLReplicationStatus(resp, req)
obj, err := a.srv.ACLReplicationStatus(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -225,5 +238,4 @@ func TestACLReplicationStatus(t *testing.T) {
if !ok {
t.Fatalf("should work")
}
})
}

View File

@ -15,12 +15,14 @@ import (
func TestEventFire(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer([]byte("test"))
url := "/v1/event/fire/test?node=Node&service=foo&tag=bar"
req, _ := http.NewRequest("PUT", url, body)
resp := httptest.NewRecorder()
obj, err := srv.EventFire(resp, req)
obj, err := a.srv.EventFire(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -48,12 +50,15 @@ func TestEventFire(t *testing.T) {
if event.TagFilter != "bar" {
t.Fatalf("bad: %#v", event)
}
})
}
func TestEventFire_token(t *testing.T) {
t.Parallel()
httpTestWithConfig(t, func(srv *HTTPServer) {
conf := TestConfig()
conf.ACLDefaultPolicy = "deny"
a := NewTestAgent(t.Name(), conf)
defer a.Shutdown()
// Create an ACL token
args := structs.ACLRequest{
Datacenter: "dc1",
@ -66,7 +71,7 @@ func TestEventFire_token(t *testing.T) {
WriteRequest: structs.WriteRequest{Token: "root"},
}
var token string
if err := srv.agent.RPC("ACL.Apply", &args, &token); err != nil {
if err := a.RPC("ACL.Apply", &args, &token); err != nil {
t.Fatalf("err: %v", err)
}
@ -84,7 +89,7 @@ func TestEventFire_token(t *testing.T) {
url := fmt.Sprintf("/v1/event/fire/%s?token=%s", c.event, token)
req, _ := http.NewRequest("PUT", url, nil)
resp := httptest.NewRecorder()
if _, err := srv.EventFire(resp, req); err != nil {
if _, err := a.srv.EventFire(resp, req); err != nil {
t.Fatalf("err: %s", err)
}
@ -106,23 +111,22 @@ func TestEventFire_token(t *testing.T) {
}
}
}
}, func(c *Config) {
c.ACLDefaultPolicy = "deny"
})
}
func TestEventList(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
p := &UserEvent{Name: "test"}
if err := srv.agent.UserEvent("dc1", "root", p); err != nil {
if err := a.UserEvent("dc1", "root", p); err != nil {
t.Fatalf("err: %v", err)
}
retry.Run(t, func(r *retry.R) {
req, _ := http.NewRequest("GET", "/v1/event/list", nil)
resp := httptest.NewRecorder()
obj, err := srv.EventList(resp, req)
obj, err := a.srv.EventList(resp, req)
if err != nil {
r.Fatal(err)
}
@ -139,26 +143,27 @@ func TestEventList(t *testing.T) {
r.Fatalf("bad: %#v", header)
}
})
})
}
func TestEventList_Filter(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
p := &UserEvent{Name: "test"}
if err := srv.agent.UserEvent("dc1", "root", p); err != nil {
if err := a.UserEvent("dc1", "root", p); err != nil {
t.Fatalf("err: %v", err)
}
p = &UserEvent{Name: "foo"}
if err := srv.agent.UserEvent("dc1", "root", p); err != nil {
if err := a.UserEvent("dc1", "root", p); err != nil {
t.Fatalf("err: %v", err)
}
retry.Run(t, func(r *retry.R) {
req, _ := http.NewRequest("GET", "/v1/event/list?name=foo", nil)
resp := httptest.NewRecorder()
obj, err := srv.EventList(resp, req)
obj, err := a.srv.EventList(resp, req)
if err != nil {
r.Fatal(err)
}
@ -175,7 +180,6 @@ func TestEventList_Filter(t *testing.T) {
r.Fatalf("bad: %#v", header)
}
})
})
}
func TestEventList_ACLFilter(t *testing.T) {
@ -230,9 +234,11 @@ func TestEventList_ACLFilter(t *testing.T) {
func TestEventList_Blocking(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
p := &UserEvent{Name: "test"}
if err := srv.agent.UserEvent("dc1", "root", p); err != nil {
if err := a.UserEvent("dc1", "root", p); err != nil {
t.Fatalf("err: %v", err)
}
@ -240,7 +246,7 @@ func TestEventList_Blocking(t *testing.T) {
retry.Run(t, func(r *retry.R) {
req, _ := http.NewRequest("GET", "/v1/event/list", nil)
resp := httptest.NewRecorder()
if _, err := srv.EventList(resp, req); err != nil {
if _, err := a.srv.EventList(resp, req); err != nil {
r.Fatal(err)
}
header := resp.Header().Get("X-Consul-Index")
@ -253,7 +259,7 @@ func TestEventList_Blocking(t *testing.T) {
go func() {
time.Sleep(50 * time.Millisecond)
p := &UserEvent{Name: "second"}
if err := srv.agent.UserEvent("dc1", "root", p); err != nil {
if err := a.UserEvent("dc1", "root", p); err != nil {
t.Fatalf("err: %v", err)
}
}()
@ -262,7 +268,7 @@ func TestEventList_Blocking(t *testing.T) {
url := "/v1/event/list?index=" + index
req, _ := http.NewRequest("GET", url, nil)
resp := httptest.NewRecorder()
obj, err := srv.EventList(resp, req)
obj, err := a.srv.EventList(resp, req)
if err != nil {
r.Fatal(err)
}
@ -275,12 +281,13 @@ func TestEventList_Blocking(t *testing.T) {
r.Fatalf("bad: %#v", list)
}
})
})
}
func TestEventList_EventBufOrder(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
// Fire some events in a non-sequential order
expected := &UserEvent{Name: "foo"}
@ -291,7 +298,7 @@ func TestEventList_EventBufOrder(t *testing.T) {
expected,
&UserEvent{Name: "bar"},
} {
if err := srv.agent.UserEvent("dc1", "root", e); err != nil {
if err := a.UserEvent("dc1", "root", e); err != nil {
t.Fatalf("err: %v", err)
}
}
@ -301,7 +308,7 @@ func TestEventList_EventBufOrder(t *testing.T) {
url := "/v1/event/list?name=foo"
req, _ := http.NewRequest("GET", url, nil)
resp := httptest.NewRecorder()
obj, err := srv.EventList(resp, req)
obj, err := a.srv.EventList(resp, req)
if err != nil {
r.Fatal(err)
}
@ -313,7 +320,6 @@ func TestEventList_EventBufOrder(t *testing.T) {
r.Fatalf("bad: %#v", list)
}
})
})
}
func TestUUIDToUint64(t *testing.T) {

View File

@ -15,11 +15,14 @@ import (
func TestHealthChecksInState(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
req, _ := http.NewRequest("GET", "/v1/health/state/warning?dc=dc1", nil)
retry.Run(t, func(r *retry.R) {
resp := httptest.NewRecorder()
obj, err := srv.HealthChecksInState(resp, req)
obj, err := a.srv.HealthChecksInState(resp, req)
if err != nil {
r.Fatal(err)
}
@ -35,11 +38,14 @@ func TestHealthChecksInState(t *testing.T) {
})
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
req, _ := http.NewRequest("GET", "/v1/health/state/passing?dc=dc1", nil)
retry.Run(t, func(r *retry.R) {
resp := httptest.NewRecorder()
obj, err := srv.HealthChecksInState(resp, req)
obj, err := a.srv.HealthChecksInState(resp, req)
if err != nil {
r.Fatal(err)
}
@ -58,7 +64,9 @@ func TestHealthChecksInState(t *testing.T) {
func TestHealthChecksInState_NodeMetaFilter(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "bar",
@ -71,14 +79,14 @@ func TestHealthChecksInState_NodeMetaFilter(t *testing.T) {
},
}
var out struct{}
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
}
req, _ := http.NewRequest("GET", "/v1/health/state/critical?node-meta=somekey:somevalue", nil)
retry.Run(t, func(r *retry.R) {
resp := httptest.NewRecorder()
obj, err := srv.HealthChecksInState(resp, req)
obj, err := a.srv.HealthChecksInState(resp, req)
if err != nil {
r.Fatal(err)
}
@ -92,7 +100,6 @@ func TestHealthChecksInState_NodeMetaFilter(t *testing.T) {
r.Fatalf("bad: %v", obj)
}
})
})
}
func TestHealthChecksInState_DistanceSort(t *testing.T) {

View File

@ -481,53 +481,55 @@ func TestACLResolution(t *testing.T) {
reqBothTokens, _ := http.NewRequest("GET", "/v1/catalog/nodes?token=baz", nil)
reqBothTokens.Header.Add("X-Consul-Token", "zap")
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
// Check when no token is set
srv.agent.config.ACLToken = ""
srv.parseToken(req, &token)
a.config.ACLToken = ""
a.srv.parseToken(req, &token)
if token != "" {
t.Fatalf("bad: %s", token)
}
// Check when ACLToken set
srv.agent.config.ACLToken = "agent"
srv.parseToken(req, &token)
a.config.ACLToken = "agent"
a.srv.parseToken(req, &token)
if token != "agent" {
t.Fatalf("bad: %s", token)
}
// Explicit token has highest precedence
srv.parseToken(reqToken, &token)
a.srv.parseToken(reqToken, &token)
if token != "foo" {
t.Fatalf("bad: %s", token)
}
// Header token has precedence over agent token
srv.parseToken(reqHeaderToken, &token)
a.srv.parseToken(reqHeaderToken, &token)
if token != "bar" {
t.Fatalf("bad: %s", token)
}
// Querystring token has precedence over header and agent tokens
srv.parseToken(reqBothTokens, &token)
a.srv.parseToken(reqBothTokens, &token)
if token != "baz" {
t.Fatalf("bad: %s", token)
}
})
}
func TestEnableWebUI(t *testing.T) {
t.Parallel()
httpTestWithConfig(t, func(s *HTTPServer) {
conf := TestConfig()
conf.EnableUI = true
a := NewTestAgent(t.Name(), conf)
defer a.Shutdown()
req, _ := http.NewRequest("GET", "/ui/", nil)
resp := httptest.NewRecorder()
s.Handler.ServeHTTP(resp, req)
a.srv.Handler.ServeHTTP(resp, req)
if resp.Code != 200 {
t.Fatalf("should handle ui")
}
}, func(c *Config) {
c.EnableUI = true
})
}
// assertIndex tests that X-Consul-Index is set and non-zero
@ -560,20 +562,6 @@ func getIndex(t *testing.T, resp *httptest.ResponseRecorder) uint64 {
return uint64(val)
}
func httpTest(t *testing.T, f func(srv *HTTPServer)) {
httpTestWithConfig(t, f, nil)
}
func httpTestWithConfig(t *testing.T, f func(srv *HTTPServer), cb func(c *Config)) {
c := TestConfig()
if cb != nil {
cb(c)
}
a := NewTestAgent(t.Name(), c)
defer a.Shutdown()
f(a.srv)
}
func isPermissionDenied(err error) bool {
return err != nil && strings.Contains(err.Error(), errPermissionDenied.Error())
}

View File

@ -339,12 +339,14 @@ func TestKVSEndpoint_ListKeys(t *testing.T) {
func TestKVSEndpoint_AcquireRelease(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
// Acquire the lock
id := makeTestSession(t, srv)
id := makeTestSession(t, a.srv)
req, _ := http.NewRequest("PUT", "/v1/kv/test?acquire="+id, bytes.NewReader(nil))
resp := httptest.NewRecorder()
obj, err := srv.KVSEndpoint(resp, req)
obj, err := a.srv.KVSEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -355,7 +357,7 @@ func TestKVSEndpoint_AcquireRelease(t *testing.T) {
// Verify we have the lock
req, _ = http.NewRequest("GET", "/v1/kv/test", nil)
resp = httptest.NewRecorder()
obj, err = srv.KVSEndpoint(resp, req)
obj, err = a.srv.KVSEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -369,7 +371,7 @@ func TestKVSEndpoint_AcquireRelease(t *testing.T) {
// Release the lock
req, _ = http.NewRequest("PUT", "/v1/kv/test?release="+id, bytes.NewReader(nil))
resp = httptest.NewRecorder()
obj, err = srv.KVSEndpoint(resp, req)
obj, err = a.srv.KVSEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -380,7 +382,7 @@ func TestKVSEndpoint_AcquireRelease(t *testing.T) {
// Verify we do not have the lock
req, _ = http.NewRequest("GET", "/v1/kv/test", nil)
resp = httptest.NewRecorder()
obj, err = srv.KVSEndpoint(resp, req)
obj, err = a.srv.KVSEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -390,16 +392,17 @@ func TestKVSEndpoint_AcquireRelease(t *testing.T) {
if d.Session != "" {
t.Fatalf("bad: %v", d)
}
})
}
func TestKVSEndpoint_GET_Raw(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
buf := bytes.NewBuffer([]byte("test"))
req, _ := http.NewRequest("PUT", "/v1/kv/test", buf)
resp := httptest.NewRecorder()
obj, err := srv.KVSEndpoint(resp, req)
obj, err := a.srv.KVSEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -409,7 +412,7 @@ func TestKVSEndpoint_GET_Raw(t *testing.T) {
req, _ = http.NewRequest("GET", "/v1/kv/test?raw", nil)
resp = httptest.NewRecorder()
obj, err = srv.KVSEndpoint(resp, req)
obj, err = a.srv.KVSEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -419,15 +422,16 @@ func TestKVSEndpoint_GET_Raw(t *testing.T) {
if !bytes.Equal(resp.Body.Bytes(), []byte("test")) {
t.Fatalf("bad: %s", resp.Body.Bytes())
}
})
}
func TestKVSEndpoint_PUT_ConflictingFlags(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
req, _ := http.NewRequest("PUT", "/v1/kv/test?cas=0&acquire=xxx", nil)
resp := httptest.NewRecorder()
if _, err := srv.KVSEndpoint(resp, req); err != nil {
if _, err := a.srv.KVSEndpoint(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
@ -437,15 +441,16 @@ func TestKVSEndpoint_PUT_ConflictingFlags(t *testing.T) {
if !bytes.Contains(resp.Body.Bytes(), []byte("Conflicting")) {
t.Fatalf("expected conflicting args error")
}
})
}
func TestKVSEndpoint_DELETE_ConflictingFlags(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
req, _ := http.NewRequest("DELETE", "/v1/kv/test?recurse&cas=0", nil)
resp := httptest.NewRecorder()
if _, err := srv.KVSEndpoint(resp, req); err != nil {
if _, err := a.srv.KVSEndpoint(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
@ -455,5 +460,4 @@ func TestKVSEndpoint_DELETE_ConflictingFlags(t *testing.T) {
if !bytes.Contains(resp.Body.Bytes(), []byte("Conflicting")) {
t.Fatalf("expected conflicting args error")
}
})
}

View File

@ -16,11 +16,13 @@ import (
func TestOperator_RaftConfiguration(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/operator/raft/configuration", body)
resp := httptest.NewRecorder()
obj, err := srv.OperatorRaftConfiguration(resp, req)
obj, err := a.srv.OperatorRaftConfiguration(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -36,31 +38,36 @@ func TestOperator_RaftConfiguration(t *testing.T) {
!out.Servers[0].Voter {
t.Fatalf("bad: %v", out)
}
})
}
func TestOperator_RaftPeer(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("DELETE", "/v1/operator/raft/peer?address=nope", body)
// If we get this error, it proves we sent the address all the
// way through.
resp := httptest.NewRecorder()
_, err := srv.OperatorRaftPeer(resp, req)
_, err := a.srv.OperatorRaftPeer(resp, req)
if err == nil || !strings.Contains(err.Error(),
"address \"nope\" was not found in the Raft configuration") {
t.Fatalf("err: %v", err)
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("DELETE", "/v1/operator/raft/peer?id=nope", body)
// If we get this error, it proves we sent the ID all the
// way through.
resp := httptest.NewRecorder()
_, err := srv.OperatorRaftPeer(resp, req)
_, err := a.srv.OperatorRaftPeer(resp, req)
if err == nil || !strings.Contains(err.Error(),
"id \"nope\" was not found in the Raft configuration") {
t.Fatalf("err: %v", err)
@ -72,19 +79,20 @@ func TestOperator_KeyringInstall(t *testing.T) {
t.Parallel()
oldKey := "H3/9gBxcKKRf45CaI2DlRg=="
newKey := "z90lFx3sZZLtTOkutXcwYg=="
configFunc := func(c *Config) {
c.EncryptKey = oldKey
}
httpTestWithConfig(t, func(srv *HTTPServer) {
config := TestConfig()
config.EncryptKey = oldKey
a := NewTestAgent(t.Name(), config)
defer a.Shutdown()
body := bytes.NewBufferString(fmt.Sprintf("{\"Key\":\"%s\"}", newKey))
req, _ := http.NewRequest("POST", "/v1/operator/keyring", body)
resp := httptest.NewRecorder()
_, err := srv.OperatorKeyringEndpoint(resp, req)
_, err := a.srv.OperatorKeyringEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %s", err)
}
listResponse, err := srv.agent.ListKeys("", 0)
listResponse, err := a.ListKeys("", 0)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -101,19 +109,19 @@ func TestOperator_KeyringInstall(t *testing.T) {
t.Fatalf("bad: %d, %d", count, response.NumNodes)
}
}
}, configFunc)
}
func TestOperator_KeyringList(t *testing.T) {
t.Parallel()
key := "H3/9gBxcKKRf45CaI2DlRg=="
configFunc := func(c *Config) {
c.EncryptKey = key
}
httpTestWithConfig(t, func(srv *HTTPServer) {
config := TestConfig()
config.EncryptKey = key
a := NewTestAgent(t.Name(), config)
defer a.Shutdown()
req, _ := http.NewRequest("GET", "/v1/operator/keyring", nil)
resp := httptest.NewRecorder()
r, err := srv.OperatorKeyringEndpoint(resp, req)
r, err := a.srv.OperatorKeyringEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -149,24 +157,24 @@ func TestOperator_KeyringList(t *testing.T) {
if _, ok := responses[1].Keys[key]; !ok {
t.Fatalf("bad: %v", ok)
}
}, configFunc)
}
func TestOperator_KeyringRemove(t *testing.T) {
t.Parallel()
key := "H3/9gBxcKKRf45CaI2DlRg=="
tempKey := "z90lFx3sZZLtTOkutXcwYg=="
configFunc := func(c *Config) {
c.EncryptKey = key
}
httpTestWithConfig(t, func(srv *HTTPServer) {
_, err := srv.agent.InstallKey(tempKey, "", 0)
config := TestConfig()
config.EncryptKey = key
a := NewTestAgent(t.Name(), config)
defer a.Shutdown()
_, err := a.InstallKey(tempKey, "", 0)
if err != nil {
t.Fatalf("err: %v", err)
}
// Make sure the temp key is installed
list, err := srv.agent.ListKeys("", 0)
list, err := a.ListKeys("", 0)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -186,12 +194,12 @@ func TestOperator_KeyringRemove(t *testing.T) {
body := bytes.NewBufferString(fmt.Sprintf("{\"Key\":\"%s\"}", tempKey))
req, _ := http.NewRequest("DELETE", "/v1/operator/keyring", body)
resp := httptest.NewRecorder()
if _, err := srv.OperatorKeyringEndpoint(resp, req); err != nil {
if _, err := a.srv.OperatorKeyringEndpoint(resp, req); err != nil {
t.Fatalf("err: %s", err)
}
// Make sure the temp key has been removed
list, err = srv.agent.ListKeys("", 0)
list, err = a.ListKeys("", 0)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -207,35 +215,35 @@ func TestOperator_KeyringRemove(t *testing.T) {
t.Fatalf("bad: %v", ok)
}
}
}, configFunc)
}
func TestOperator_KeyringUse(t *testing.T) {
t.Parallel()
oldKey := "H3/9gBxcKKRf45CaI2DlRg=="
newKey := "z90lFx3sZZLtTOkutXcwYg=="
configFunc := func(c *Config) {
c.EncryptKey = oldKey
}
httpTestWithConfig(t, func(srv *HTTPServer) {
if _, err := srv.agent.InstallKey(newKey, "", 0); err != nil {
config := TestConfig()
config.EncryptKey = oldKey
a := NewTestAgent(t.Name(), config)
defer a.Shutdown()
if _, err := a.InstallKey(newKey, "", 0); err != nil {
t.Fatalf("err: %v", err)
}
body := bytes.NewBufferString(fmt.Sprintf("{\"Key\":\"%s\"}", newKey))
req, _ := http.NewRequest("PUT", "/v1/operator/keyring", body)
resp := httptest.NewRecorder()
_, err := srv.OperatorKeyringEndpoint(resp, req)
_, err := a.srv.OperatorKeyringEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %s", err)
}
if _, err := srv.agent.RemoveKey(oldKey, "", 0); err != nil {
if _, err := a.RemoveKey(oldKey, "", 0); err != nil {
t.Fatalf("err: %v", err)
}
// Make sure only the new key remains
list, err := srv.agent.ListKeys("", 0)
list, err := a.ListKeys("", 0)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -251,16 +259,16 @@ func TestOperator_KeyringUse(t *testing.T) {
t.Fatalf("bad: %v", ok)
}
}
}, configFunc)
}
func TestOperator_Keyring_InvalidRelayFactor(t *testing.T) {
t.Parallel()
key := "H3/9gBxcKKRf45CaI2DlRg=="
configFunc := func(c *Config) {
c.EncryptKey = key
}
httpTestWithConfig(t, func(srv *HTTPServer) {
config := TestConfig()
config.EncryptKey = key
a := NewTestAgent(t.Name(), config)
defer a.Shutdown()
cases := map[string]string{
"999": "Relay factor must be in range",
"asdf": "Error parsing relay factor",
@ -268,7 +276,7 @@ func TestOperator_Keyring_InvalidRelayFactor(t *testing.T) {
for relayFactor, errString := range cases {
req, _ := http.NewRequest("GET", "/v1/operator/keyring?relay-factor="+relayFactor, nil)
resp := httptest.NewRecorder()
_, err := srv.OperatorKeyringEndpoint(resp, req)
_, err := a.srv.OperatorKeyringEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -277,16 +285,17 @@ func TestOperator_Keyring_InvalidRelayFactor(t *testing.T) {
t.Fatalf("bad: %v", body)
}
}
}, configFunc)
}
func TestOperator_AutopilotGetConfiguration(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/operator/autopilot/configuration", body)
resp := httptest.NewRecorder()
obj, err := srv.OperatorAutopilotConfiguration(resp, req)
obj, err := a.srv.OperatorAutopilotConfiguration(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -300,16 +309,17 @@ func TestOperator_AutopilotGetConfiguration(t *testing.T) {
if !out.CleanupDeadServers {
t.Fatalf("bad: %#v", out)
}
})
}
func TestOperator_AutopilotSetConfiguration(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer([]byte(`{"CleanupDeadServers": false}`))
req, _ := http.NewRequest("PUT", "/v1/operator/autopilot/configuration", body)
resp := httptest.NewRecorder()
if _, err := srv.OperatorAutopilotConfiguration(resp, req); err != nil {
if _, err := a.srv.OperatorAutopilotConfiguration(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 200 {
@ -321,22 +331,23 @@ func TestOperator_AutopilotSetConfiguration(t *testing.T) {
}
var reply structs.AutopilotConfig
if err := srv.agent.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
if err := a.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
t.Fatalf("err: %v", err)
}
if reply.CleanupDeadServers {
t.Fatalf("bad: %#v", reply)
}
})
}
func TestOperator_AutopilotCASConfiguration(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer([]byte(`{"CleanupDeadServers": false}`))
req, _ := http.NewRequest("PUT", "/v1/operator/autopilot/configuration", body)
resp := httptest.NewRecorder()
if _, err := srv.OperatorAutopilotConfiguration(resp, req); err != nil {
if _, err := a.srv.OperatorAutopilotConfiguration(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 200 {
@ -348,7 +359,7 @@ func TestOperator_AutopilotCASConfiguration(t *testing.T) {
}
var reply structs.AutopilotConfig
if err := srv.agent.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
if err := a.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
t.Fatalf("err: %v", err)
}
@ -361,7 +372,7 @@ func TestOperator_AutopilotCASConfiguration(t *testing.T) {
buf := bytes.NewBuffer([]byte(`{"CleanupDeadServers": true}`))
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/operator/autopilot/configuration?cas=%d", reply.ModifyIndex-1), buf)
resp := httptest.NewRecorder()
obj, err := srv.OperatorAutopilotConfiguration(resp, req)
obj, err := a.srv.OperatorAutopilotConfiguration(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -376,7 +387,7 @@ func TestOperator_AutopilotCASConfiguration(t *testing.T) {
buf := bytes.NewBuffer([]byte(`{"CleanupDeadServers": true}`))
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/operator/autopilot/configuration?cas=%d", reply.ModifyIndex), buf)
resp := httptest.NewRecorder()
obj, err := srv.OperatorAutopilotConfiguration(resp, req)
obj, err := a.srv.OperatorAutopilotConfiguration(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -387,26 +398,26 @@ func TestOperator_AutopilotCASConfiguration(t *testing.T) {
}
// Verify the update
if err := srv.agent.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
if err := a.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
t.Fatalf("err: %v", err)
}
if !reply.CleanupDeadServers {
t.Fatalf("bad: %#v", reply)
}
})
}
func TestOperator_ServerHealth(t *testing.T) {
t.Parallel()
cb := func(c *Config) {
c.RaftProtocol = 3
}
httpTestWithConfig(t, func(srv *HTTPServer) {
config := TestConfig()
config.RaftProtocol = 3
a := NewTestAgent(t.Name(), config)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/operator/autopilot/health", body)
retry.Run(t, func(r *retry.R) {
resp := httptest.NewRecorder()
obj, err := srv.OperatorServerHealth(resp, req)
obj, err := a.srv.OperatorServerHealth(resp, req)
if err != nil {
r.Fatalf("err: %v", err)
}
@ -419,29 +430,28 @@ func TestOperator_ServerHealth(t *testing.T) {
}
if len(out.Servers) != 1 ||
!out.Servers[0].Healthy ||
out.Servers[0].Name != srv.agent.config.NodeName ||
out.Servers[0].Name != a.config.NodeName ||
out.Servers[0].SerfStatus != "alive" ||
out.FailureTolerance != 0 {
r.Fatalf("bad: %v", out)
}
})
}, cb)
}
func TestOperator_ServerHealth_Unhealthy(t *testing.T) {
t.Parallel()
cb := func(c *Config) {
c.RaftProtocol = 3
config := TestConfig()
config.RaftProtocol = 3
threshold := time.Duration(-1)
c.Autopilot.LastContactThreshold = &threshold
}
httpTestWithConfig(t, func(srv *HTTPServer) {
config.Autopilot.LastContactThreshold = &threshold
a := NewTestAgent(t.Name(), config)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/operator/autopilot/health", body)
retry.Run(t, func(r *retry.R) {
resp := httptest.NewRecorder()
obj, err := srv.OperatorServerHealth(resp, req)
obj, err := a.srv.OperatorServerHealth(resp, req)
if err != nil {
r.Fatalf("err: %v", err)
}
@ -454,9 +464,8 @@ func TestOperator_ServerHealth_Unhealthy(t *testing.T) {
}
if len(out.Servers) != 1 ||
out.Healthy ||
out.Servers[0].Name != srv.agent.config.NodeName {
out.Servers[0].Name != a.config.NodeName {
r.Fatalf("bad: %#v", out.Servers)
}
})
}, cb)
}

View File

@ -70,9 +70,11 @@ func (m *MockPreparedQuery) Explain(args *structs.PreparedQueryExecuteRequest,
func TestPreparedQuery_Create(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -134,7 +136,7 @@ func TestPreparedQuery_Create(t *testing.T) {
req, _ := http.NewRequest("POST", "/v1/query?token=my-token", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQueryGeneral(resp, req)
obj, err := a.srv.PreparedQueryGeneral(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -148,14 +150,16 @@ func TestPreparedQuery_Create(t *testing.T) {
if r.ID != "my-id" {
t.Fatalf("bad ID: %s", r.ID)
}
})
}
func TestPreparedQuery_List(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -167,7 +171,7 @@ func TestPreparedQuery_List(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQueryGeneral(resp, req)
obj, err := a.srv.PreparedQueryGeneral(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -183,9 +187,12 @@ func TestPreparedQuery_List(t *testing.T) {
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -211,7 +218,7 @@ func TestPreparedQuery_List(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query?token=my-token&consistent=true", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQueryGeneral(resp, req)
obj, err := a.srv.PreparedQueryGeneral(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -230,9 +237,12 @@ func TestPreparedQuery_List(t *testing.T) {
func TestPreparedQuery_Execute(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -244,7 +254,7 @@ func TestPreparedQuery_Execute(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query/my-id/execute", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQuerySpecific(resp, req)
obj, err := a.srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -260,9 +270,12 @@ func TestPreparedQuery_Execute(t *testing.T) {
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -276,8 +289,8 @@ func TestPreparedQuery_Execute(t *testing.T) {
Node: "my-node",
},
Agent: structs.QuerySource{
Datacenter: srv.agent.config.Datacenter,
Node: srv.agent.config.NodeName,
Datacenter: a.config.Datacenter,
Node: a.config.NodeName,
},
QueryOptions: structs.QueryOptions{
Token: "my-token",
@ -296,7 +309,7 @@ func TestPreparedQuery_Execute(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query/my-id/execute?token=my-token&consistent=true&near=my-node&limit=5", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQuerySpecific(resp, req)
obj, err := a.srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -313,9 +326,12 @@ func TestPreparedQuery_Execute(t *testing.T) {
})
// Ensure the proper params are set when no special args are passed
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -324,8 +340,8 @@ func TestPreparedQuery_Execute(t *testing.T) {
t.Fatalf("expect node to be empty, got %q", args.Source.Node)
}
expect := structs.QuerySource{
Datacenter: srv.agent.config.Datacenter,
Node: srv.agent.config.NodeName,
Datacenter: a.config.Datacenter,
Node: a.config.NodeName,
}
if !reflect.DeepEqual(args.Agent, expect) {
t.Fatalf("expect: %#v\nactual: %#v", expect, args.Agent)
@ -335,15 +351,21 @@ func TestPreparedQuery_Execute(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/query/my-id/execute", nil)
resp := httptest.NewRecorder()
if _, err := srv.PreparedQuerySpecific(resp, req); err != nil {
if _, err := a.srv.PreparedQuerySpecific(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
})
// Ensure WAN translation occurs for a response outside of the local DC.
httpTestWithConfig(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
config := TestConfig()
config.Datacenter = "dc1"
config.TranslateWanAddrs = true
a := NewTestAgent(t.Name(), config)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -363,7 +385,7 @@ func TestPreparedQuery_Execute(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query/my-id/execute?dc=dc2", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQuerySpecific(resp, req)
obj, err := a.srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -382,15 +404,18 @@ func TestPreparedQuery_Execute(t *testing.T) {
if node.Node.Address != "127.0.0.2" {
t.Fatalf("bad: %v", node.Node)
}
}, func(c *Config) {
c.Datacenter = "dc1"
c.TranslateWanAddrs = true
})
// Ensure WAN translation doesn't occur for the local DC.
httpTestWithConfig(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
config := TestConfig()
config.Datacenter = "dc1"
config.TranslateWanAddrs = true
a := NewTestAgent(t.Name(), config)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -410,7 +435,7 @@ func TestPreparedQuery_Execute(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query/my-id/execute?dc=dc2", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQuerySpecific(resp, req)
obj, err := a.srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -429,16 +454,16 @@ func TestPreparedQuery_Execute(t *testing.T) {
if node.Node.Address != "127.0.0.1" {
t.Fatalf("bad: %v", node.Node)
}
}, func(c *Config) {
c.Datacenter = "dc1"
c.TranslateWanAddrs = true
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query/not-there/execute", body)
resp := httptest.NewRecorder()
if _, err := srv.PreparedQuerySpecific(resp, req); err != nil {
if _, err := a.srv.PreparedQuerySpecific(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 404 {
@ -449,9 +474,12 @@ func TestPreparedQuery_Execute(t *testing.T) {
func TestPreparedQuery_Explain(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -465,8 +493,8 @@ func TestPreparedQuery_Explain(t *testing.T) {
Node: "my-node",
},
Agent: structs.QuerySource{
Datacenter: srv.agent.config.Datacenter,
Node: srv.agent.config.NodeName,
Datacenter: a.config.Datacenter,
Node: a.config.NodeName,
},
QueryOptions: structs.QueryOptions{
Token: "my-token",
@ -485,7 +513,7 @@ func TestPreparedQuery_Explain(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query/my-id/explain?token=my-token&consistent=true&near=my-node&limit=5", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQuerySpecific(resp, req)
obj, err := a.srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -501,11 +529,14 @@ func TestPreparedQuery_Explain(t *testing.T) {
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query/not-there/explain", body)
resp := httptest.NewRecorder()
if _, err := srv.PreparedQuerySpecific(resp, req); err != nil {
if _, err := a.srv.PreparedQuerySpecific(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 404 {
@ -516,9 +547,12 @@ func TestPreparedQuery_Explain(t *testing.T) {
func TestPreparedQuery_Get(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -545,7 +579,7 @@ func TestPreparedQuery_Get(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query/my-id?token=my-token&consistent=true", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQuerySpecific(resp, req)
obj, err := a.srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -561,11 +595,14 @@ func TestPreparedQuery_Get(t *testing.T) {
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query/f004177f-2c28-83b7-4229-eacc25fe55d1", body)
resp := httptest.NewRecorder()
if _, err := srv.PreparedQuerySpecific(resp, req); err != nil {
if _, err := a.srv.PreparedQuerySpecific(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 404 {
@ -576,9 +613,11 @@ func TestPreparedQuery_Get(t *testing.T) {
func TestPreparedQuery_Update(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -642,20 +681,21 @@ func TestPreparedQuery_Update(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/query/my-id?token=my-token", body)
resp := httptest.NewRecorder()
if _, err := srv.PreparedQuerySpecific(resp, req); err != nil {
if _, err := a.srv.PreparedQuerySpecific(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 200 {
t.Fatalf("bad code: %d", resp.Code)
}
})
}
func TestPreparedQuery_Delete(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
if err := a.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}
@ -689,22 +729,24 @@ func TestPreparedQuery_Delete(t *testing.T) {
req, _ := http.NewRequest("DELETE", "/v1/query/my-id?token=my-token", body)
resp := httptest.NewRecorder()
if _, err := srv.PreparedQuerySpecific(resp, req); err != nil {
if _, err := a.srv.PreparedQuerySpecific(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 200 {
t.Fatalf("bad code: %d", resp.Code)
}
})
}
func TestPreparedQuery_BadMethods(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("DELETE", "/v1/query", body)
resp := httptest.NewRecorder()
if _, err := srv.PreparedQueryGeneral(resp, req); err != nil {
if _, err := a.srv.PreparedQueryGeneral(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 405 {
@ -712,11 +754,14 @@ func TestPreparedQuery_BadMethods(t *testing.T) {
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("POST", "/v1/query/my-id", body)
resp := httptest.NewRecorder()
if _, err := srv.PreparedQuerySpecific(resp, req); err != nil {
if _, err := a.srv.PreparedQuerySpecific(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 405 {
@ -756,19 +801,21 @@ func TestPreparedQuery_parseLimit(t *testing.T) {
// correctly when calling through to the real endpoints.
func TestPreparedQuery_Integration(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
// Register a node and a service.
{
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: srv.agent.config.NodeName,
Node: a.config.NodeName,
Address: "127.0.0.1",
Service: &structs.NodeService{
Service: "my-service",
},
}
var out struct{}
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
}
}
@ -790,7 +837,7 @@ func TestPreparedQuery_Integration(t *testing.T) {
req, _ := http.NewRequest("POST", "/v1/query", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQueryGeneral(resp, req)
obj, err := a.srv.PreparedQueryGeneral(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -809,7 +856,7 @@ func TestPreparedQuery_Integration(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query?token=root", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQueryGeneral(resp, req)
obj, err := a.srv.PreparedQueryGeneral(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -830,7 +877,7 @@ func TestPreparedQuery_Integration(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query/"+id+"/execute", body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQuerySpecific(resp, req)
obj, err := a.srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -851,7 +898,7 @@ func TestPreparedQuery_Integration(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/query/"+id, body)
resp := httptest.NewRecorder()
obj, err := srv.PreparedQuerySpecific(resp, req)
obj, err := a.srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -884,7 +931,7 @@ func TestPreparedQuery_Integration(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/query/"+id, body)
resp := httptest.NewRecorder()
if _, err := srv.PreparedQuerySpecific(resp, req); err != nil {
if _, err := a.srv.PreparedQuerySpecific(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 200 {
@ -897,12 +944,11 @@ func TestPreparedQuery_Integration(t *testing.T) {
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("DELETE", "/v1/query/"+id, body)
resp := httptest.NewRecorder()
if _, err := srv.PreparedQuerySpecific(resp, req); err != nil {
if _, err := a.srv.PreparedQuerySpecific(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 200 {
t.Fatalf("bad code: %d", resp.Code)
}
}
})
}

View File

@ -16,22 +16,24 @@ import (
func TestSessionCreate(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
// Create a health check
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: srv.agent.config.NodeName,
Node: a.config.NodeName,
Address: "127.0.0.1",
Check: &structs.HealthCheck{
CheckID: "consul",
Node: srv.agent.config.NodeName,
Node: a.config.NodeName,
Name: "consul",
ServiceID: "consul",
Status: api.HealthPassing,
},
}
var out struct{}
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
}
@ -40,7 +42,7 @@ func TestSessionCreate(t *testing.T) {
enc := json.NewEncoder(body)
raw := map[string]interface{}{
"Name": "my-cool-session",
"Node": srv.agent.config.NodeName,
"Node": a.config.NodeName,
"Checks": []types.CheckID{consul.SerfCheckID, "consul"},
"LockDelay": "20s",
}
@ -48,7 +50,7 @@ func TestSessionCreate(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
resp := httptest.NewRecorder()
obj, err := srv.SessionCreate(resp, req)
obj, err := a.srv.SessionCreate(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -56,27 +58,28 @@ func TestSessionCreate(t *testing.T) {
if _, ok := obj.(sessionCreateResponse); !ok {
t.Fatalf("should work")
}
})
}
func TestSessionCreateDelete(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
// Create a health check
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: srv.agent.config.NodeName,
Node: a.config.NodeName,
Address: "127.0.0.1",
Check: &structs.HealthCheck{
CheckID: "consul",
Node: srv.agent.config.NodeName,
Node: a.config.NodeName,
Name: "consul",
ServiceID: "consul",
Status: api.HealthPassing,
},
}
var out struct{}
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
}
@ -85,7 +88,7 @@ func TestSessionCreateDelete(t *testing.T) {
enc := json.NewEncoder(body)
raw := map[string]interface{}{
"Name": "my-cool-session",
"Node": srv.agent.config.NodeName,
"Node": a.config.NodeName,
"Checks": []types.CheckID{consul.SerfCheckID, "consul"},
"LockDelay": "20s",
"Behavior": structs.SessionKeysDelete,
@ -94,7 +97,7 @@ func TestSessionCreateDelete(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
resp := httptest.NewRecorder()
obj, err := srv.SessionCreate(resp, req)
obj, err := a.srv.SessionCreate(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -102,7 +105,6 @@ func TestSessionCreateDelete(t *testing.T) {
if _, ok := obj.(sessionCreateResponse); !ok {
t.Fatalf("should work")
}
})
}
func TestFixupLockDelay(t *testing.T) {
@ -189,43 +191,36 @@ func makeTestSessionTTL(t *testing.T, srv *HTTPServer, ttl string) string {
func TestSessionDestroy(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
id := makeTestSession(t, srv)
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
id := makeTestSession(t, a.srv)
req, _ := http.NewRequest("PUT", "/v1/session/destroy/"+id, nil)
resp := httptest.NewRecorder()
obj, err := srv.SessionDestroy(resp, req)
obj, err := a.srv.SessionDestroy(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
if resp := obj.(bool); !resp {
t.Fatalf("should work")
}
})
}
func TestSessionCustomTTL(t *testing.T) {
t.Parallel()
ttl := 250 * time.Millisecond
testSessionTTL(t, ttl, customTTL(ttl))
}
config := TestConfig()
config.SessionTTLMin = ttl
config.SessionTTLMinRaw = ttl.String()
a := NewTestAgent(t.Name(), config)
defer a.Shutdown()
func customTTL(d time.Duration) func(c *Config) {
return func(c *Config) {
c.SessionTTLMinRaw = d.String()
c.SessionTTLMin = d
}
}
func testSessionTTL(t *testing.T, ttl time.Duration, cb func(c *Config)) {
httpTestWithConfig(t, func(srv *HTTPServer) {
TTL := ttl.String()
id := makeTestSessionTTL(t, srv, TTL)
id := makeTestSessionTTL(t, a.srv, ttl.String())
req, _ := http.NewRequest("GET", "/v1/session/info/"+id, nil)
resp := httptest.NewRecorder()
obj, err := srv.SessionGet(resp, req)
obj, err := a.srv.SessionGet(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -236,7 +231,7 @@ func testSessionTTL(t *testing.T, ttl time.Duration, cb func(c *Config)) {
if len(respObj) != 1 {
t.Fatalf("bad: %v", respObj)
}
if respObj[0].TTL != TTL {
if respObj[0].TTL != ttl.String() {
t.Fatalf("Incorrect TTL: %s", respObj[0].TTL)
}
@ -244,7 +239,7 @@ func testSessionTTL(t *testing.T, ttl time.Duration, cb func(c *Config)) {
req, _ = http.NewRequest("GET", "/v1/session/info/"+id, nil)
resp = httptest.NewRecorder()
obj, err = srv.SessionGet(resp, req)
obj, err = a.srv.SessionGet(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -252,19 +247,22 @@ func testSessionTTL(t *testing.T, ttl time.Duration, cb func(c *Config)) {
if len(respObj) != 0 {
t.Fatalf("session '%s' should have been destroyed", id)
}
}, cb)
}
func TestSessionTTLRenew(t *testing.T) {
t.Parallel()
ttl := 250 * time.Millisecond
TTL := ttl.String()
httpTestWithConfig(t, func(srv *HTTPServer) {
id := makeTestSessionTTL(t, srv, TTL)
config := TestConfig()
config.SessionTTLMin = ttl
config.SessionTTLMinRaw = ttl.String()
a := NewTestAgent(t.Name(), config)
defer a.Shutdown()
id := makeTestSessionTTL(t, a.srv, ttl.String())
req, _ := http.NewRequest("GET", "/v1/session/info/"+id, nil)
resp := httptest.NewRecorder()
obj, err := srv.SessionGet(resp, req)
obj, err := a.srv.SessionGet(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -275,7 +273,7 @@ func TestSessionTTLRenew(t *testing.T) {
if len(respObj) != 1 {
t.Fatalf("bad: %v", respObj)
}
if respObj[0].TTL != TTL {
if respObj[0].TTL != ttl.String() {
t.Fatalf("Incorrect TTL: %s", respObj[0].TTL)
}
@ -284,7 +282,7 @@ func TestSessionTTLRenew(t *testing.T) {
req, _ = http.NewRequest("PUT", "/v1/session/renew/"+id, nil)
resp = httptest.NewRecorder()
obj, err = srv.SessionRenew(resp, req)
obj, err = a.srv.SessionRenew(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -301,7 +299,7 @@ func TestSessionTTLRenew(t *testing.T) {
req, _ = http.NewRequest("GET", "/v1/session/info/"+id, nil)
resp = httptest.NewRecorder()
obj, err = srv.SessionGet(resp, req)
obj, err = a.srv.SessionGet(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -318,7 +316,7 @@ func TestSessionTTLRenew(t *testing.T) {
req, _ = http.NewRequest("GET", "/v1/session/info/"+id, nil)
resp = httptest.NewRecorder()
obj, err = srv.SessionGet(resp, req)
obj, err = a.srv.SessionGet(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -329,15 +327,17 @@ func TestSessionTTLRenew(t *testing.T) {
if len(respObj) != 0 {
t.Fatalf("session '%s' should have destroyed", id)
}
}, customTTL(ttl))
}
func TestSessionGet(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
req, _ := http.NewRequest("GET", "/v1/session/info/adf4238a-882b-9ddc-4a9d-5b6758e4159e", nil)
resp := httptest.NewRecorder()
obj, err := srv.SessionGet(resp, req)
obj, err := a.srv.SessionGet(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -350,12 +350,15 @@ func TestSessionGet(t *testing.T) {
}
})
httpTest(t, func(srv *HTTPServer) {
id := makeTestSession(t, srv)
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
id := makeTestSession(t, a.srv)
req, _ := http.NewRequest("GET", "/v1/session/info/"+id, nil)
resp := httptest.NewRecorder()
obj, err := srv.SessionGet(resp, req)
obj, err := a.srv.SessionGet(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -371,10 +374,13 @@ func TestSessionGet(t *testing.T) {
func TestSessionList(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
req, _ := http.NewRequest("GET", "/v1/session/list", nil)
resp := httptest.NewRecorder()
obj, err := srv.SessionList(resp, req)
obj, err := a.srv.SessionList(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -387,15 +393,18 @@ func TestSessionList(t *testing.T) {
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
var ids []string
for i := 0; i < 10; i++ {
ids = append(ids, makeTestSession(t, srv))
ids = append(ids, makeTestSession(t, a.srv))
}
req, _ := http.NewRequest("GET", "/v1/session/list", nil)
resp := httptest.NewRecorder()
obj, err := srv.SessionList(resp, req)
obj, err := a.srv.SessionList(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -411,10 +420,13 @@ func TestSessionList(t *testing.T) {
func TestSessionsForNode(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
req, _ := http.NewRequest("GET", "/v1/session/node/"+srv.agent.config.NodeName, nil)
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
req, _ := http.NewRequest("GET", "/v1/session/node/"+a.Config.NodeName, nil)
resp := httptest.NewRecorder()
obj, err := srv.SessionsForNode(resp, req)
obj, err := a.srv.SessionsForNode(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -427,15 +439,18 @@ func TestSessionsForNode(t *testing.T) {
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
var ids []string
for i := 0; i < 10; i++ {
ids = append(ids, makeTestSession(t, srv))
ids = append(ids, makeTestSession(t, a.srv))
}
req, _ := http.NewRequest("GET", "/v1/session/node/"+srv.agent.config.NodeName, nil)
req, _ := http.NewRequest("GET", "/v1/session/node/"+a.Config.NodeName, nil)
resp := httptest.NewRecorder()
obj, err := srv.SessionsForNode(resp, req)
obj, err := a.srv.SessionsForNode(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -451,14 +466,16 @@ func TestSessionsForNode(t *testing.T) {
func TestSessionDeleteDestroy(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
id := makeTestSessionDelete(t, srv)
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
id := makeTestSessionDelete(t, a.srv)
// now create a new key for the session and acquire it
buf := bytes.NewBuffer([]byte("test"))
req, _ := http.NewRequest("PUT", "/v1/kv/ephemeral?acquire="+id, buf)
resp := httptest.NewRecorder()
obj, err := srv.KVSEndpoint(resp, req)
obj, err := a.srv.KVSEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -470,7 +487,7 @@ func TestSessionDeleteDestroy(t *testing.T) {
// now destroy the session, this should delete the key created above
req, _ = http.NewRequest("PUT", "/v1/session/destroy/"+id, nil)
resp = httptest.NewRecorder()
obj, err = srv.SessionDestroy(resp, req)
obj, err = a.srv.SessionDestroy(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -481,10 +498,9 @@ func TestSessionDeleteDestroy(t *testing.T) {
// Verify that the key is gone
req, _ = http.NewRequest("GET", "/v1/kv/ephemeral", nil)
resp = httptest.NewRecorder()
obj, _ = srv.KVSEndpoint(resp, req)
obj, _ = a.srv.KVSEndpoint(resp, req)
res, found := obj.(structs.DirEntries)
if found || len(res) != 0 {
t.Fatalf("bad: %v found, should be nothing", res)
}
})
}

View File

@ -12,11 +12,14 @@ import (
func TestSnapshot(t *testing.T) {
t.Parallel()
var snap io.Reader
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("GET", "/v1/snapshot?token=root", body)
resp := httptest.NewRecorder()
if _, err := srv.Snapshot(resp, req); err != nil {
if _, err := a.srv.Snapshot(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
snap = resp.Body
@ -35,10 +38,13 @@ func TestSnapshot(t *testing.T) {
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
req, _ := http.NewRequest("PUT", "/v1/snapshot?token=root", snap)
resp := httptest.NewRecorder()
if _, err := srv.Snapshot(resp, req); err != nil {
if _, err := a.srv.Snapshot(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
})
@ -47,31 +53,40 @@ func TestSnapshot(t *testing.T) {
func TestSnapshot_Options(t *testing.T) {
t.Parallel()
for _, method := range []string{"GET", "PUT"} {
httpTest(t, func(srv *HTTPServer) {
t.Run(method, func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest(method, "/v1/snapshot?token=anonymous", body)
resp := httptest.NewRecorder()
_, err := srv.Snapshot(resp, req)
_, err := a.srv.Snapshot(resp, req)
if err == nil || !strings.Contains(err.Error(), "Permission denied") {
t.Fatalf("err: %v", err)
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run(method, func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest(method, "/v1/snapshot?dc=nope", body)
resp := httptest.NewRecorder()
_, err := srv.Snapshot(resp, req)
_, err := a.srv.Snapshot(resp, req)
if err == nil || !strings.Contains(err.Error(), "No path to datacenter") {
t.Fatalf("err: %v", err)
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run(method, func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest(method, "/v1/snapshot?token=root&stale", body)
resp := httptest.NewRecorder()
_, err := srv.Snapshot(resp, req)
_, err := a.srv.Snapshot(resp, req)
if method == "GET" {
if err != nil {
t.Fatalf("err: %v", err)
@ -87,11 +102,14 @@ func TestSnapshot_Options(t *testing.T) {
func TestSnapshot_BadMethods(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("POST", "/v1/snapshot", body)
resp := httptest.NewRecorder()
_, err := srv.Snapshot(resp, req)
_, err := a.srv.Snapshot(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -100,11 +118,14 @@ func TestSnapshot_BadMethods(t *testing.T) {
}
})
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
body := bytes.NewBuffer(nil)
req, _ := http.NewRequest("DELETE", "/v1/snapshot", body)
resp := httptest.NewRecorder()
_, err := srv.Snapshot(resp, req)
_, err := a.srv.Snapshot(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}

View File

@ -14,11 +14,13 @@ import (
func TestTxnEndpoint_Bad_JSON(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
buf := bytes.NewBuffer([]byte("{"))
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
resp := httptest.NewRecorder()
if _, err := srv.Txn(resp, req); err != nil {
if _, err := a.srv.Txn(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 400 {
@ -27,27 +29,29 @@ func TestTxnEndpoint_Bad_JSON(t *testing.T) {
if !bytes.Contains(resp.Body.Bytes(), []byte("Failed to parse")) {
t.Fatalf("expected conflicting args error")
}
})
}
func TestTxnEndpoint_Bad_Method(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
buf := bytes.NewBuffer([]byte("{}"))
req, _ := http.NewRequest("GET", "/v1/txn", buf)
resp := httptest.NewRecorder()
if _, err := srv.Txn(resp, req); err != nil {
if _, err := a.srv.Txn(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 405 {
t.Fatalf("expected 405, got %d", resp.Code)
}
})
}
func TestTxnEndpoint_Bad_Size_Item(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
[
{
@ -61,18 +65,19 @@ func TestTxnEndpoint_Bad_Size_Item(t *testing.T) {
`, strings.Repeat("bad", 2*maxKVSize))))
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
resp := httptest.NewRecorder()
if _, err := srv.Txn(resp, req); err != nil {
if _, err := a.srv.Txn(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 413 {
t.Fatalf("expected 413, got %d", resp.Code)
}
})
}
func TestTxnEndpoint_Bad_Size_Net(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
value := strings.Repeat("X", maxKVSize/2)
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
[
@ -101,18 +106,19 @@ func TestTxnEndpoint_Bad_Size_Net(t *testing.T) {
`, value, value, value)))
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
resp := httptest.NewRecorder()
if _, err := srv.Txn(resp, req); err != nil {
if _, err := a.srv.Txn(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 413 {
t.Fatalf("expected 413, got %d", resp.Code)
}
})
}
func TestTxnEndpoint_Bad_Size_Ops(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
[
%s
@ -127,22 +133,24 @@ func TestTxnEndpoint_Bad_Size_Ops(t *testing.T) {
`, strings.Repeat(`{ "KV": { "Verb": "get", "Key": "key" } },`, 2*maxTxnOps))))
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
resp := httptest.NewRecorder()
if _, err := srv.Txn(resp, req); err != nil {
if _, err := a.srv.Txn(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 413 {
t.Fatalf("expected 413, got %d", resp.Code)
}
})
}
func TestTxnEndpoint_KV_Actions(t *testing.T) {
t.Parallel()
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
// Make sure all incoming fields get converted properly to the internal
// RPC format.
var index uint64
id := makeTestSession(t, srv)
id := makeTestSession(t, a.srv)
{
buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
[
@ -165,7 +173,7 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
`, id)))
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
resp := httptest.NewRecorder()
obj, err := srv.Txn(resp, req)
obj, err := a.srv.Txn(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -237,7 +245,7 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
`))
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
resp := httptest.NewRecorder()
obj, err := srv.Txn(resp, req)
obj, err := a.srv.Txn(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -321,7 +329,7 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
`, index)))
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
resp := httptest.NewRecorder()
obj, err := srv.Txn(resp, req)
obj, err := a.srv.Txn(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
@ -370,7 +378,10 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
})
// Verify an error inside a transaction.
httpTest(t, func(srv *HTTPServer) {
t.Run("", func(t *testing.T) {
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
buf := bytes.NewBuffer([]byte(`
[
{
@ -391,7 +402,7 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
`))
req, _ := http.NewRequest("PUT", "/v1/txn", buf)
resp := httptest.NewRecorder()
if _, err := srv.Txn(resp, req); err != nil {
if _, err := a.srv.Txn(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 409 {