test: inline request body encoding

This commit is contained in:
Frank Schroeder 2017-05-09 18:58:12 +02:00 committed by Frank Schröder
parent 82cb9c50ad
commit d711b9bba0
4 changed files with 49 additions and 88 deletions

View File

@ -2,7 +2,6 @@ package agent
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -25,16 +24,12 @@ import (
) )
func makeReadOnlyAgentACL(t *testing.T, srv *HTTPServer) string { func makeReadOnlyAgentACL(t *testing.T, srv *HTTPServer) string {
body := bytes.NewBuffer(nil) args := map[string]interface{}{
enc := json.NewEncoder(body)
raw := map[string]interface{}{
"Name": "User Token", "Name": "User Token",
"Type": "client", "Type": "client",
"Rules": fmt.Sprintf(`agent "" { policy = "read" }`), "Rules": `agent "" { policy = "read" }`,
} }
enc.Encode(raw) req, _ := http.NewRequest("PUT", "/v1/acl/create?token=root", jsonReader(args))
req, _ := http.NewRequest("PUT", "/v1/acl/create?token=root", body)
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
obj, err := srv.ACLCreate(resp, req) obj, err := srv.ACLCreate(resp, req)
if err != nil { if err != nil {
@ -678,15 +673,13 @@ func TestAgent_RegisterCheck(t *testing.T) {
defer srv.agent.Shutdown() defer srv.agent.Shutdown()
// Register node // Register node
req, _ := http.NewRequest("GET", "/v1/agent/check/register?token=abc123", nil)
args := &CheckDefinition{ args := &CheckDefinition{
Name: "test", Name: "test",
CheckType: CheckType{ CheckType: CheckType{
TTL: 15 * time.Second, TTL: 15 * time.Second,
}, },
} }
req.Body = encodeReq(args) req, _ := http.NewRequest("GET", "/v1/agent/check/register?token=abc123", jsonReader(args))
obj, err := srv.AgentRegisterCheck(nil, req) obj, err := srv.AgentRegisterCheck(nil, req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -724,7 +717,6 @@ func TestAgent_RegisterCheck_Passing(t *testing.T) {
defer srv.agent.Shutdown() defer srv.agent.Shutdown()
// Register node // Register node
req, _ := http.NewRequest("GET", "/v1/agent/check/register", nil)
args := &CheckDefinition{ args := &CheckDefinition{
Name: "test", Name: "test",
CheckType: CheckType{ CheckType: CheckType{
@ -732,8 +724,7 @@ func TestAgent_RegisterCheck_Passing(t *testing.T) {
}, },
Status: api.HealthPassing, Status: api.HealthPassing,
} }
req.Body = encodeReq(args) req, _ := http.NewRequest("GET", "/v1/agent/check/register", jsonReader(args))
obj, err := srv.AgentRegisterCheck(nil, req) obj, err := srv.AgentRegisterCheck(nil, req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -765,7 +756,6 @@ func TestAgent_RegisterCheck_BadStatus(t *testing.T) {
defer srv.agent.Shutdown() defer srv.agent.Shutdown()
// Register node // Register node
req, _ := http.NewRequest("GET", "/v1/agent/check/register", nil)
args := &CheckDefinition{ args := &CheckDefinition{
Name: "test", Name: "test",
CheckType: CheckType{ CheckType: CheckType{
@ -773,8 +763,7 @@ func TestAgent_RegisterCheck_BadStatus(t *testing.T) {
}, },
Status: "fluffy", Status: "fluffy",
} }
req.Body = encodeReq(args) req, _ := http.NewRequest("GET", "/v1/agent/check/register", jsonReader(args))
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
if _, err := srv.AgentRegisterCheck(resp, req); err != nil { if _, err := srv.AgentRegisterCheck(resp, req); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -790,22 +779,21 @@ func TestAgent_RegisterCheck_ACLDeny(t *testing.T) {
defer srv.Shutdown() defer srv.Shutdown()
defer srv.agent.Shutdown() defer srv.agent.Shutdown()
// Try with no token.
req, _ := http.NewRequest("GET", "/v1/agent/check/register", nil)
args := &CheckDefinition{ args := &CheckDefinition{
Name: "test", Name: "test",
CheckType: CheckType{ CheckType: CheckType{
TTL: 15 * time.Second, TTL: 15 * time.Second,
}, },
} }
req.Body = encodeReq(args)
// Try with no token.
req, _ := http.NewRequest("GET", "/v1/agent/check/register", jsonReader(args))
if _, err := srv.AgentRegisterCheck(nil, req); !isPermissionDenied(err) { if _, err := srv.AgentRegisterCheck(nil, req); !isPermissionDenied(err) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Try the root token. // Try the root token.
req, _ = http.NewRequest("GET", "/v1/agent/check/register?token=root", nil) req, _ = http.NewRequest("GET", "/v1/agent/check/register?token=root", jsonReader(args))
req.Body = encodeReq(args)
if _, err := srv.AgentRegisterCheck(nil, req); err != nil { if _, err := srv.AgentRegisterCheck(nil, req); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1040,9 +1028,7 @@ func TestAgent_UpdateCheck(t *testing.T) {
} }
for _, c := range cases { for _, c := range cases {
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", nil) req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", jsonReader(c))
req.Body = encodeReq(c)
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
obj, err := srv.AgentCheckUpdate(resp, req) obj, err := srv.AgentCheckUpdate(resp, req)
if err != nil { if err != nil {
@ -1063,13 +1049,11 @@ func TestAgent_UpdateCheck(t *testing.T) {
// Make sure abusive levels of output are capped. // Make sure abusive levels of output are capped.
{ {
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", nil) args := checkUpdate{
update := checkUpdate{
Status: api.HealthPassing, Status: api.HealthPassing,
Output: strings.Repeat("-= bad -=", 5*CheckBufSize), Output: strings.Repeat("-= bad -=", 5*CheckBufSize),
} }
req.Body = encodeReq(update) req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", jsonReader(args))
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
obj, err := srv.AgentCheckUpdate(resp, req) obj, err := srv.AgentCheckUpdate(resp, req)
if err != nil { if err != nil {
@ -1093,12 +1077,8 @@ func TestAgent_UpdateCheck(t *testing.T) {
// Check a bogus status. // Check a bogus status.
{ {
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", nil) args := checkUpdate{Status: "itscomplicated"}
update := checkUpdate{ req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", jsonReader(args))
Status: "itscomplicated",
}
req.Body = encodeReq(update)
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
obj, err := srv.AgentCheckUpdate(resp, req) obj, err := srv.AgentCheckUpdate(resp, req)
if err != nil { if err != nil {
@ -1114,12 +1094,8 @@ func TestAgent_UpdateCheck(t *testing.T) {
// Check a bogus verb. // Check a bogus verb.
{ {
req, _ := http.NewRequest("POST", "/v1/agent/check/update/test", nil) args := checkUpdate{Status: api.HealthPassing}
update := checkUpdate{ req, _ := http.NewRequest("POST", "/v1/agent/check/update/test", jsonReader(args))
Status: api.HealthPassing,
}
req.Body = encodeReq(update)
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
obj, err := srv.AgentCheckUpdate(resp, req) obj, err := srv.AgentCheckUpdate(resp, req)
if err != nil { if err != nil {
@ -1147,15 +1123,15 @@ func TestAgent_UpdateCheck_ACLDeny(t *testing.T) {
} }
// Try with no token. // Try with no token.
req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", nil) args := checkUpdate{api.HealthPassing, "hello-passing"}
req.Body = encodeReq(checkUpdate{api.HealthPassing, "hello-passing"}) req, _ := http.NewRequest("PUT", "/v1/agent/check/update/test", jsonReader(args))
if _, err := srv.AgentCheckUpdate(nil, req); !isPermissionDenied(err) { if _, err := srv.AgentCheckUpdate(nil, req); !isPermissionDenied(err) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Try with the root token. // Try with the root token.
req, _ = http.NewRequest("PUT", "/v1/agent/check/update/test?token=root", nil) args = checkUpdate{api.HealthPassing, "hello-passing"}
req.Body = encodeReq(checkUpdate{api.HealthPassing, "hello-passing"}) req, _ = http.NewRequest("PUT", "/v1/agent/check/update/test?token=root", jsonReader(args))
if _, err := srv.AgentCheckUpdate(nil, req); err != nil { if _, err := srv.AgentCheckUpdate(nil, req); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1167,7 +1143,6 @@ func TestAgent_RegisterService(t *testing.T) {
defer srv.Shutdown() defer srv.Shutdown()
defer srv.agent.Shutdown() defer srv.agent.Shutdown()
req, _ := http.NewRequest("GET", "/v1/agent/service/register?token=abc123", nil)
args := &ServiceDefinition{ args := &ServiceDefinition{
Name: "test", Name: "test",
Tags: []string{"master"}, Tags: []string{"master"},
@ -1184,7 +1159,7 @@ func TestAgent_RegisterService(t *testing.T) {
}, },
}, },
} }
req.Body = encodeReq(args) req, _ := http.NewRequest("GET", "/v1/agent/service/register?token=abc123", jsonReader(args))
obj, err := srv.AgentRegisterService(nil, req) obj, err := srv.AgentRegisterService(nil, req)
if err != nil { if err != nil {
@ -1239,15 +1214,13 @@ func TestAgent_RegisterService_ACLDeny(t *testing.T) {
} }
// Try with no token. // Try with no token.
req, _ := http.NewRequest("GET", "/v1/agent/service/register", nil) req, _ := http.NewRequest("GET", "/v1/agent/service/register", jsonReader(args))
req.Body = encodeReq(args)
if _, err := srv.AgentRegisterService(nil, req); !isPermissionDenied(err) { if _, err := srv.AgentRegisterService(nil, req); !isPermissionDenied(err) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Try with the root token. // Try with the root token.
req, _ = http.NewRequest("GET", "/v1/agent/service/register?token=root", nil) req, _ = http.NewRequest("GET", "/v1/agent/service/register?token=root", jsonReader(args))
req.Body = encodeReq(args)
if _, err := srv.AgentRegisterService(nil, req); err != nil { if _, err := srv.AgentRegisterService(nil, req); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1261,19 +1234,17 @@ func TestAgent_RegisterService_InvalidAddress(t *testing.T) {
for _, addr := range []string{"0.0.0.0", "::", "[::]"} { for _, addr := range []string{"0.0.0.0", "::", "[::]"} {
t.Run("addr "+addr, func(t *testing.T) { t.Run("addr "+addr, func(t *testing.T) {
req, err := http.NewRequest("GET", "/v1/agent/service/register?token=abc123", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
args := &ServiceDefinition{ args := &ServiceDefinition{
Name: "test", Name: "test",
Address: addr, Address: addr,
Port: 8000, Port: 8000,
} }
req.Body = encodeReq(args) req, _ := http.NewRequest("GET", "/v1/agent/service/register?token=abc123", jsonReader(args))
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
_, err = srv.AgentRegisterService(resp, req) _, err := srv.AgentRegisterService(resp, req)
if err != nil {
t.Fatalf("got error %v want nil", err)
}
if got, want := resp.Code, 400; got != want { if got, want := resp.Code, 400; got != want {
t.Fatalf("got code %d want %d", got, want) t.Fatalf("got code %d want %d", got, want)
} }
@ -1609,8 +1580,6 @@ func TestAgent_RegisterCheck_Service(t *testing.T) {
defer srv.Shutdown() defer srv.Shutdown()
defer srv.agent.Shutdown() defer srv.agent.Shutdown()
// First register the service
req, _ := http.NewRequest("GET", "/v1/agent/service/register", nil)
args := &ServiceDefinition{ args := &ServiceDefinition{
Name: "memcache", Name: "memcache",
Port: 8000, Port: 8000,
@ -1618,14 +1587,14 @@ func TestAgent_RegisterCheck_Service(t *testing.T) {
TTL: 15 * time.Second, TTL: 15 * time.Second,
}, },
} }
req.Body = encodeReq(args)
// First register the service
req, _ := http.NewRequest("GET", "/v1/agent/service/register", jsonReader(args))
if _, err := srv.AgentRegisterService(nil, req); err != nil { if _, err := srv.AgentRegisterService(nil, req); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Now register an additional check // Now register an additional check
req, _ = http.NewRequest("GET", "/v1/agent/check/register", nil)
checkArgs := &CheckDefinition{ checkArgs := &CheckDefinition{
Name: "memcache_check2", Name: "memcache_check2",
ServiceID: "memcache", ServiceID: "memcache",
@ -1633,8 +1602,7 @@ func TestAgent_RegisterCheck_Service(t *testing.T) {
TTL: 15 * time.Second, TTL: 15 * time.Second,
}, },
} }
req.Body = encodeReq(checkArgs) req, _ = http.NewRequest("GET", "/v1/agent/check/register", jsonReader(checkArgs))
if _, err := srv.AgentRegisterCheck(nil, req); err != nil { if _, err := srv.AgentRegisterCheck(nil, req); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }

View File

@ -23,13 +23,11 @@ func TestCatalogRegister(t *testing.T) {
testrpc.WaitForLeader(t, srv.agent.RPC, "dc1") testrpc.WaitForLeader(t, srv.agent.RPC, "dc1")
// Register node // Register node
req, _ := http.NewRequest("GET", "/v1/catalog/register", nil)
args := &structs.RegisterRequest{ args := &structs.RegisterRequest{
Node: "foo", Node: "foo",
Address: "127.0.0.1", Address: "127.0.0.1",
} }
req.Body = encodeReq(args) req, _ := http.NewRequest("GET", "/v1/catalog/register", jsonReader(args))
obj, err := srv.CatalogRegister(nil, req) obj, err := srv.CatalogRegister(nil, req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -62,10 +60,6 @@ func TestCatalogRegister_Service_InvalidAddress(t *testing.T) {
for _, addr := range []string{"0.0.0.0", "::", "[::]"} { for _, addr := range []string{"0.0.0.0", "::", "[::]"} {
t.Run("addr "+addr, func(t *testing.T) { t.Run("addr "+addr, func(t *testing.T) {
req, err := http.NewRequest("GET", "/v1/catalog/register", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
args := &structs.RegisterRequest{ args := &structs.RegisterRequest{
Node: "foo", Node: "foo",
Address: "127.0.0.1", Address: "127.0.0.1",
@ -75,9 +69,8 @@ func TestCatalogRegister_Service_InvalidAddress(t *testing.T) {
Port: 8080, Port: 8080,
}, },
} }
req.Body = encodeReq(args) req, _ := http.NewRequest("GET", "/v1/catalog/register", jsonReader(args))
_, err := srv.CatalogRegister(nil, req)
_, err = srv.CatalogRegister(nil, req)
if err == nil || err.Error() != "Invalid service address" { if err == nil || err.Error() != "Invalid service address" {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -94,12 +87,8 @@ func TestCatalogDeregister(t *testing.T) {
testrpc.WaitForLeader(t, srv.agent.RPC, "dc1") testrpc.WaitForLeader(t, srv.agent.RPC, "dc1")
// Register node // Register node
req, _ := http.NewRequest("GET", "/v1/catalog/deregister", nil) args := &structs.DeregisterRequest{Node: "foo"}
args := &structs.DeregisterRequest{ req, _ := http.NewRequest("GET", "/v1/catalog/deregister", jsonReader(args))
Node: "foo",
}
req.Body = encodeReq(args)
obj, err := srv.CatalogDeregister(nil, req) obj, err := srv.CatalogDeregister(nil, req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)

View File

@ -207,7 +207,7 @@ func TestEventList_ACLFilter(t *testing.T) {
}) })
} }
// Try the root token. // Try root token.
{ {
retry.Run(t, func(r *retry.R) { retry.Run(t, func(r *retry.R) {
req, _ := http.NewRequest("GET", "/v1/event/list?token=root", nil) req, _ := http.NewRequest("GET", "/v1/event/list?token=root", nil)

View File

@ -72,13 +72,6 @@ RECONF:
return dir, servers[0] return dir, servers[0]
} }
func encodeReq(obj interface{}) io.ReadCloser {
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
enc.Encode(obj)
return ioutil.NopCloser(buf)
}
func TestHTTPServer_UnixSocket(t *testing.T) { func TestHTTPServer_UnixSocket(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.SkipNow() t.SkipNow()
@ -701,3 +694,14 @@ func httpTestWithConfig(t *testing.T, f func(srv *HTTPServer), cb func(c *Config
func isPermissionDenied(err error) bool { func isPermissionDenied(err error) bool {
return err != nil && strings.Contains(err.Error(), errPermissionDenied.Error()) return err != nil && strings.Contains(err.Error(), errPermissionDenied.Error())
} }
func jsonReader(v interface{}) io.Reader {
if v == nil {
return nil
}
b := new(bytes.Buffer)
if err := json.NewEncoder(b).Encode(v); err != nil {
panic(err)
}
return b
}