speed up TestHTTPAPI_MethodNotAllowed_OSS from 11s -> 0.5s (#5268)

This commit is contained in:
R.B. Boyer 2019-01-25 10:01:21 -06:00 committed by GitHub
parent 28b94ccd3e
commit 7a8aa2d4a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 20 deletions

View File

@ -62,28 +62,30 @@ func newHttpClient(timeout time.Duration) *http.Client {
} }
func TestHTTPAPI_MethodNotAllowed_OSS(t *testing.T) { func TestHTTPAPI_MethodNotAllowed_OSS(t *testing.T) {
// To avoid actually triggering RPCs that are allowed, lock everything down
a := NewTestAgent(t.Name(), `acl_datacenter = "dc1"`) // with default-deny ACLs. This drops the test runtime from 11s to 0.6s.
a := NewTestAgent(t.Name(), `
primary_datacenter = "dc1"
acl {
enabled = true
default_policy = "deny"
tokens {
master = "sekrit"
agent = "sekrit"
}
}
`)
a.Agent.LogWriter = logger.NewLogWriter(512) a.Agent.LogWriter = logger.NewLogWriter(512)
defer a.Shutdown() defer a.Shutdown()
testrpc.WaitForTestAgent(t, a.RPC, "dc1") // Use the master token here so the wait actually works.
testrpc.WaitForTestAgent(t, a.RPC, "dc1", testrpc.WithToken("sekrit"))
all := []string{"GET", "PUT", "POST", "DELETE", "HEAD", "OPTIONS"} all := []string{"GET", "PUT", "POST", "DELETE", "HEAD", "OPTIONS"}
const testTimeout = 10 * time.Second
fastClient := newHttpClient(15 * time.Second) client := newHttpClient(15 * time.Second)
slowClient := newHttpClient(45 * time.Second)
testMethodNotAllowed := func(method string, path string, allowedMethods []string) { testMethodNotAllowed := func(t *testing.T, method string, path string, allowedMethods []string) {
t.Run(method+" "+path, func(t *testing.T) { t.Run(method+" "+path, func(t *testing.T) {
client := fastClient
switch path {
case "/v1/agent/leave", "/v1/agent/self":
// there are actual sleeps in this code that should take longer
client = slowClient
t.Logf("Using slow http client for leave tests")
}
uri := fmt.Sprintf("http://%s%s", a.HTTPAddr(), path) uri := fmt.Sprintf("http://%s%s", a.HTTPAddr(), path)
req, _ := http.NewRequest(method, uri, nil) req, _ := http.NewRequest(method, uri, nil)
resp, err := client.Do(req) resp, err := client.Do(req)
@ -111,14 +113,14 @@ func TestHTTPAPI_MethodNotAllowed_OSS(t *testing.T) {
for path, methods := range extraTestEndpoints { for path, methods := range extraTestEndpoints {
for _, method := range all { for _, method := range all {
testMethodNotAllowed(method, path, methods) testMethodNotAllowed(t, method, path, methods)
} }
} }
for path, methods := range allowedMethods { for path, methods := range allowedMethods {
if includePathInTest(path) { if includePathInTest(path) {
for _, method := range all { for _, method := range all {
testMethodNotAllowed(method, path, methods) testMethodNotAllowed(t, method, path, methods)
} }
} }
} }

View File

@ -40,13 +40,32 @@ func WaitUntilNoLeader(t *testing.T, rpc rpcFn, dc string) {
}) })
} }
type waitOption struct {
Token string
}
func WithToken(token string) waitOption {
return waitOption{Token: token}
}
// WaitForTestAgent ensures we have a node with serfHealth check registered // WaitForTestAgent ensures we have a node with serfHealth check registered
func WaitForTestAgent(t *testing.T, rpc rpcFn, dc string) { func WaitForTestAgent(t *testing.T, rpc rpcFn, dc string, options ...waitOption) {
var nodes structs.IndexedNodes var nodes structs.IndexedNodes
var checks structs.IndexedHealthChecks var checks structs.IndexedHealthChecks
// first extra arg is an optional acl token
var token string
for _, opt := range options {
if opt.Token != "" {
token = opt.Token
}
}
retry.Run(t, func(r *retry.R) { retry.Run(t, func(r *retry.R) {
dcReq := &structs.DCSpecificRequest{Datacenter: dc} dcReq := &structs.DCSpecificRequest{
Datacenter: dc,
QueryOptions: structs.QueryOptions{Token: token},
}
if err := rpc("Catalog.ListNodes", dcReq, &nodes); err != nil { if err := rpc("Catalog.ListNodes", dcReq, &nodes); err != nil {
r.Fatalf("Catalog.ListNodes failed: %v", err) r.Fatalf("Catalog.ListNodes failed: %v", err)
} }
@ -55,7 +74,11 @@ func WaitForTestAgent(t *testing.T, rpc rpcFn, dc string) {
} }
// This assumes that there is a single agent per dc, typically a TestAgent // This assumes that there is a single agent per dc, typically a TestAgent
nodeReq := &structs.NodeSpecificRequest{Datacenter: dc, Node: nodes.Nodes[0].Node} nodeReq := &structs.NodeSpecificRequest{
Datacenter: dc,
Node: nodes.Nodes[0].Node,
QueryOptions: structs.QueryOptions{Token: token},
}
if err := rpc("Health.NodeChecks", nodeReq, &checks); err != nil { if err := rpc("Health.NodeChecks", nodeReq, &checks); err != nil {
r.Fatalf("Health.NodeChecks failed: %v", err) r.Fatalf("Health.NodeChecks failed: %v", err)
} }