From e9a2eab3162d7c89278ad9db01c6036bc8e551ae Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" Date: Fri, 25 Jan 2019 10:01:21 -0600 Subject: [PATCH] speed up TestHTTPAPI_MethodNotAllowed_OSS from 11s -> 0.5s (#5268) --- agent/http_oss_test.go | 36 +++++++++++++++++++----------------- testrpc/wait.go | 29 ++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/agent/http_oss_test.go b/agent/http_oss_test.go index 8810f06fc7..93cb464b4d 100644 --- a/agent/http_oss_test.go +++ b/agent/http_oss_test.go @@ -62,28 +62,30 @@ func newHttpClient(timeout time.Duration) *http.Client { } func TestHTTPAPI_MethodNotAllowed_OSS(t *testing.T) { - - a := NewTestAgent(t.Name(), `acl_datacenter = "dc1"`) + // To avoid actually triggering RPCs that are allowed, lock everything down + // 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) 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"} - const testTimeout = 10 * time.Second - fastClient := newHttpClient(15 * time.Second) - slowClient := newHttpClient(45 * time.Second) + client := newHttpClient(15 * 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) { - 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) req, _ := http.NewRequest(method, uri, nil) resp, err := client.Do(req) @@ -111,14 +113,14 @@ func TestHTTPAPI_MethodNotAllowed_OSS(t *testing.T) { for path, methods := range extraTestEndpoints { for _, method := range all { - testMethodNotAllowed(method, path, methods) + testMethodNotAllowed(t, method, path, methods) } } for path, methods := range allowedMethods { if includePathInTest(path) { for _, method := range all { - testMethodNotAllowed(method, path, methods) + testMethodNotAllowed(t, method, path, methods) } } } diff --git a/testrpc/wait.go b/testrpc/wait.go index 3019eba789..3efb03b9aa 100644 --- a/testrpc/wait.go +++ b/testrpc/wait.go @@ -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 -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 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) { - dcReq := &structs.DCSpecificRequest{Datacenter: dc} + dcReq := &structs.DCSpecificRequest{ + Datacenter: dc, + QueryOptions: structs.QueryOptions{Token: token}, + } if err := rpc("Catalog.ListNodes", dcReq, &nodes); err != nil { 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 - 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 { r.Fatalf("Health.NodeChecks failed: %v", err) }