Skip to content

Commit

Permalink
Fixes agent endpoint verbs and removes obsolete tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
slackpad committed Sep 26, 2017
1 parent e00c68d commit f095d49
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 83 deletions.
115 changes: 36 additions & 79 deletions agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func TestAgent_Join(t *testing.T) {
defer a2.Shutdown()

addr := fmt.Sprintf("127.0.0.1:%d", a2.Config.SerfPortLAN)
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s", addr), nil)
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/agent/join/%s", addr), nil)
obj, err := a1.srv.AgentJoin(nil, req)
if err != nil {
t.Fatalf("Err: %v", err)
Expand Down Expand Up @@ -449,7 +449,7 @@ func TestAgent_Join_WAN(t *testing.T) {
defer a2.Shutdown()

addr := fmt.Sprintf("127.0.0.1:%d", a2.Config.SerfPortWAN)
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s?wan=true", addr), nil)
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/agent/join/%s?wan=true", addr), nil)
obj, err := a1.srv.AgentJoin(nil, req)
if err != nil {
t.Fatalf("Err: %v", err)
Expand Down Expand Up @@ -479,14 +479,14 @@ func TestAgent_Join_ACLDeny(t *testing.T) {
addr := fmt.Sprintf("127.0.0.1:%d", a2.Config.SerfPortLAN)

t.Run("no token", func(t *testing.T) {
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s", addr), nil)
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/agent/join/%s", addr), nil)
if _, err := a1.srv.AgentJoin(nil, req); !acl.IsErrPermissionDenied(err) {
t.Fatalf("err: %v", err)
}
})

t.Run("agent master token", func(t *testing.T) {
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s?token=towel", addr), nil)
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/agent/join/%s?token=towel", addr), nil)
_, err := a1.srv.AgentJoin(nil, req)
if err != nil {
t.Fatalf("err: %v", err)
Expand All @@ -495,7 +495,7 @@ func TestAgent_Join_ACLDeny(t *testing.T) {

t.Run("read-only token", func(t *testing.T) {
ro := makeReadOnlyAgentACL(t, a1.srv)
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s?token=%s", addr, ro), nil)
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/agent/join/%s?token=%s", addr, ro), nil)
if _, err := a1.srv.AgentJoin(nil, req); !acl.IsErrPermissionDenied(err) {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -616,7 +616,7 @@ func TestAgent_ForceLeave(t *testing.T) {
a2.Shutdown()

// Force leave now
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/force-leave/%s", a2.Config.NodeName), nil)
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/agent/force-leave/%s", a2.Config.NodeName), nil)
obj, err := a1.srv.AgentForceLeave(nil, req)
if err != nil {
t.Fatalf("Err: %v", err)
Expand All @@ -639,22 +639,22 @@ func TestAgent_ForceLeave_ACLDeny(t *testing.T) {
defer a.Shutdown()

t.Run("no token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/force-leave/nope", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/force-leave/nope", nil)
if _, err := a.srv.AgentForceLeave(nil, req); !acl.IsErrPermissionDenied(err) {
t.Fatalf("err: %v", err)
}
})

t.Run("agent master token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/force-leave/nope?token=towel", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/force-leave/nope?token=towel", nil)
if _, err := a.srv.AgentForceLeave(nil, req); err != nil {
t.Fatalf("err: %v", err)
}
})

t.Run("read-only token", func(t *testing.T) {
ro := makeReadOnlyAgentACL(t, a.srv)
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/agent/force-leave/nope?token=%s", ro), nil)
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/agent/force-leave/nope?token=%s", ro), nil)
if _, err := a.srv.AgentForceLeave(nil, req); !acl.IsErrPermissionDenied(err) {
t.Fatalf("err: %v", err)
}
Expand All @@ -671,7 +671,7 @@ func TestAgent_RegisterCheck(t *testing.T) {
Name: "test",
TTL: 15 * time.Second,
}
req, _ := http.NewRequest("GET", "/v1/agent/check/register?token=abc123", jsonReader(args))
req, _ := http.NewRequest("PUT", "/v1/agent/check/register?token=abc123", jsonReader(args))
obj, err := a.srv.AgentRegisterCheck(nil, req)
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -713,7 +713,7 @@ func TestAgent_RegisterCheck_Passing(t *testing.T) {
TTL: 15 * time.Second,
Status: api.HealthPassing,
}
req, _ := http.NewRequest("GET", "/v1/agent/check/register", jsonReader(args))
req, _ := http.NewRequest("PUT", "/v1/agent/check/register", jsonReader(args))
obj, err := a.srv.AgentRegisterCheck(nil, req)
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -749,7 +749,7 @@ func TestAgent_RegisterCheck_BadStatus(t *testing.T) {
TTL: 15 * time.Second,
Status: "fluffy",
}
req, _ := http.NewRequest("GET", "/v1/agent/check/register", jsonReader(args))
req, _ := http.NewRequest("PUT", "/v1/agent/check/register", jsonReader(args))
resp := httptest.NewRecorder()
if _, err := a.srv.AgentRegisterCheck(resp, req); err != nil {
t.Fatalf("err: %v", err)
Expand All @@ -770,14 +770,14 @@ func TestAgent_RegisterCheck_ACLDeny(t *testing.T) {
}

t.Run("no token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/check/register", jsonReader(args))
req, _ := http.NewRequest("PUT", "/v1/agent/check/register", jsonReader(args))
if _, err := a.srv.AgentRegisterCheck(nil, req); !acl.IsErrPermissionDenied(err) {
t.Fatalf("err: %v", err)
}
})

t.Run("root token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/check/register?token=root", jsonReader(args))
req, _ := http.NewRequest("PUT", "/v1/agent/check/register?token=root", jsonReader(args))
if _, err := a.srv.AgentRegisterCheck(nil, req); err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -795,7 +795,7 @@ func TestAgent_DeregisterCheck(t *testing.T) {
}

// Register node
req, _ := http.NewRequest("GET", "/v1/agent/check/deregister/test", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/test", nil)
obj, err := a.srv.AgentDeregisterCheck(nil, req)
if err != nil {
t.Fatalf("err: %v", err)
Expand All @@ -821,14 +821,14 @@ func TestAgent_DeregisterCheckACLDeny(t *testing.T) {
}

t.Run("no token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/check/deregister/test", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/test", nil)
if _, err := a.srv.AgentDeregisterCheck(nil, req); !acl.IsErrPermissionDenied(err) {
t.Fatalf("err: %v", err)
}
})

t.Run("root token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/check/deregister/test?token=root", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/test?token=root", nil)
if _, err := a.srv.AgentDeregisterCheck(nil, req); err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -846,7 +846,7 @@ func TestAgent_PassCheck(t *testing.T) {
t.Fatalf("err: %v", err)
}

req, _ := http.NewRequest("GET", "/v1/agent/check/pass/test", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/pass/test", nil)
obj, err := a.srv.AgentCheckPass(nil, req)
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -874,14 +874,14 @@ func TestAgent_PassCheck_ACLDeny(t *testing.T) {
}

t.Run("no token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/check/pass/test", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/pass/test", nil)
if _, err := a.srv.AgentCheckPass(nil, req); !acl.IsErrPermissionDenied(err) {
t.Fatalf("err: %v", err)
}
})

t.Run("root token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/check/pass/test?token=root", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/pass/test?token=root", nil)
if _, err := a.srv.AgentCheckPass(nil, req); err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -899,7 +899,7 @@ func TestAgent_WarnCheck(t *testing.T) {
t.Fatalf("err: %v", err)
}

req, _ := http.NewRequest("GET", "/v1/agent/check/warn/test", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/warn/test", nil)
obj, err := a.srv.AgentCheckWarn(nil, req)
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -927,14 +927,14 @@ func TestAgent_WarnCheck_ACLDeny(t *testing.T) {
}

t.Run("no token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/check/warn/test", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/warn/test", nil)
if _, err := a.srv.AgentCheckWarn(nil, req); !acl.IsErrPermissionDenied(err) {
t.Fatalf("err: %v", err)
}
})

t.Run("root token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/check/warn/test?token=root", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/warn/test?token=root", nil)
if _, err := a.srv.AgentCheckWarn(nil, req); err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -952,7 +952,7 @@ func TestAgent_FailCheck(t *testing.T) {
t.Fatalf("err: %v", err)
}

req, _ := http.NewRequest("GET", "/v1/agent/check/fail/test", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/fail/test", nil)
obj, err := a.srv.AgentCheckFail(nil, req)
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -980,14 +980,14 @@ func TestAgent_FailCheck_ACLDeny(t *testing.T) {
}

t.Run("no token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/check/fail/test", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/fail/test", nil)
if _, err := a.srv.AgentCheckFail(nil, req); !acl.IsErrPermissionDenied(err) {
t.Fatalf("err: %v", err)
}
})

t.Run("root token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/check/fail/test?token=root", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/check/fail/test?token=root", nil)
if _, err := a.srv.AgentCheckFail(nil, req); err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -1075,22 +1075,6 @@ func TestAgent_UpdateCheck(t *testing.T) {
t.Fatalf("expected 400, got %d", resp.Code)
}
})

t.Run("bogus verb", func(t *testing.T) {
args := checkUpdate{Status: api.HealthPassing}
req, _ := http.NewRequest("POST", "/v1/agent/check/update/test", jsonReader(args))
resp := httptest.NewRecorder()
obj, err := a.srv.AgentCheckUpdate(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
if obj != nil {
t.Fatalf("bad: %v", obj)
}
if resp.Code != 405 {
t.Fatalf("expected 405, got %d", resp.Code)
}
})
}

func TestAgent_UpdateCheck_ACLDeny(t *testing.T) {
Expand Down Expand Up @@ -1142,7 +1126,7 @@ func TestAgent_RegisterService(t *testing.T) {
},
},
}
req, _ := http.NewRequest("GET", "/v1/agent/service/register?token=abc123", jsonReader(args))
req, _ := http.NewRequest("PUT", "/v1/agent/service/register?token=abc123", jsonReader(args))

obj, err := a.srv.AgentRegisterService(nil, req)
if err != nil {
Expand Down Expand Up @@ -1196,14 +1180,14 @@ func TestAgent_RegisterService_ACLDeny(t *testing.T) {
}

t.Run("no token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/service/register", jsonReader(args))
req, _ := http.NewRequest("PUT", "/v1/agent/service/register", jsonReader(args))
if _, err := a.srv.AgentRegisterService(nil, req); !acl.IsErrPermissionDenied(err) {
t.Fatalf("err: %v", err)
}
})

t.Run("root token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/service/register?token=root", jsonReader(args))
req, _ := http.NewRequest("PUT", "/v1/agent/service/register?token=root", jsonReader(args))
if _, err := a.srv.AgentRegisterService(nil, req); err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -1251,7 +1235,7 @@ func TestAgent_DeregisterService(t *testing.T) {
t.Fatalf("err: %v", err)
}

req, _ := http.NewRequest("GET", "/v1/agent/service/deregister/test", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/service/deregister/test", nil)
obj, err := a.srv.AgentDeregisterService(nil, req)
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -1284,14 +1268,14 @@ func TestAgent_DeregisterService_ACLDeny(t *testing.T) {
}

t.Run("no token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/service/deregister/test", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/service/deregister/test", nil)
if _, err := a.srv.AgentDeregisterService(nil, req); !acl.IsErrPermissionDenied(err) {
t.Fatalf("err: %v", err)
}
})

t.Run("root token", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/service/deregister/test?token=root", nil)
req, _ := http.NewRequest("PUT", "/v1/agent/service/deregister/test?token=root", nil)
if _, err := a.srv.AgentDeregisterService(nil, req); err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -1303,17 +1287,6 @@ func TestAgent_ServiceMaintenance_BadRequest(t *testing.T) {
a := NewTestAgent(t.Name(), "")
defer a.Shutdown()

t.Run("not PUT", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/agent/service/maintenance/test?enable=true", nil)
resp := httptest.NewRecorder()
if _, err := a.srv.AgentServiceMaintenance(resp, req); err != nil {
t.Fatalf("err: %s", err)
}
if resp.Code != 405 {
t.Fatalf("expected 405, got %d", resp.Code)
}
})

t.Run("not enabled", func(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/agent/service/maintenance/test", nil)
resp := httptest.NewRecorder()
Expand Down Expand Up @@ -1460,19 +1433,9 @@ func TestAgent_NodeMaintenance_BadRequest(t *testing.T) {
a := NewTestAgent(t.Name(), "")
defer a.Shutdown()

// Fails on non-PUT
req, _ := http.NewRequest("GET", "/v1/agent/self/maintenance?enable=true", nil)
resp := httptest.NewRecorder()
if _, err := a.srv.AgentNodeMaintenance(resp, req); err != nil {
t.Fatalf("err: %s", err)
}
if resp.Code != 405 {
t.Fatalf("expected 405, got %d", resp.Code)
}

// Fails when no enable flag provided
req, _ = http.NewRequest("PUT", "/v1/agent/self/maintenance", nil)
resp = httptest.NewRecorder()
req, _ := http.NewRequest("PUT", "/v1/agent/self/maintenance", nil)
resp := httptest.NewRecorder()
if _, err := a.srv.AgentNodeMaintenance(resp, req); err != nil {
t.Fatalf("err: %s", err)
}
Expand Down Expand Up @@ -1571,7 +1534,7 @@ func TestAgent_RegisterCheck_Service(t *testing.T) {
}

// First register the service
req, _ := http.NewRequest("GET", "/v1/agent/service/register", jsonReader(args))
req, _ := http.NewRequest("PUT", "/v1/agent/service/register", jsonReader(args))
if _, err := a.srv.AgentRegisterService(nil, req); err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -1582,7 +1545,7 @@ func TestAgent_RegisterCheck_Service(t *testing.T) {
ServiceID: "memcache",
TTL: 15 * time.Second,
}
req, _ = http.NewRequest("GET", "/v1/agent/check/register", jsonReader(checkArgs))
req, _ = http.NewRequest("PUT", "/v1/agent/check/register", jsonReader(checkArgs))
if _, err := a.srv.AgentRegisterCheck(nil, req); err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -1721,12 +1684,6 @@ func TestAgent_Token(t *testing.T) {
code int
got, want tokens
}{
{
name: "bad method",
method: "GET",
url: "acl_token",
code: http.StatusMethodNotAllowed,
},
{
name: "bad token name",
method: "PUT",
Expand Down
Loading

0 comments on commit f095d49

Please sign in to comment.