Skip to content

Commit

Permalink
fixup: Alloc endpoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
endocrimes committed Apr 11, 2019
1 parent 18a2715 commit 90d6f1a
Showing 1 changed file with 66 additions and 2 deletions.
68 changes: 66 additions & 2 deletions command/agent/alloc_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,11 @@ func TestHTTP_AllocQuery_Payload(t *testing.T) {
})
}

// TODO(dani): Add ACL Test

func TestHTTP_AllocRestart(t *testing.T) {
t.Parallel()
require := require.New(t)

// Validates that all methods of forwarding the request are processed correctly
httpTest(t, nil, func(s *TestAgent) {
// Local node, local resp
{
Expand Down Expand Up @@ -330,6 +329,71 @@ func TestHTTP_AllocRestart(t *testing.T) {
})
}

func TestHTTP_AllocRestart_ACL(t *testing.T) {
t.Parallel()
require := require.New(t)

httpACLTest(t, nil, func(s *TestAgent) {
state := s.Agent.server.State()

// If there's no token, we expect the request to fail.
{
buf := encodeReq(map[string]string{})
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/restart", uuid.Generate()), buf)
require.NoError(err)

respW := httptest.NewRecorder()
_, err = s.Server.ClientAllocRequest(respW, req)
require.NotNil(err)
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
}

// Try request with an invalid token and expect it to fail
{
buf := encodeReq(map[string]string{})
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/restart", uuid.Generate()), buf)
require.NoError(err)

respW := httptest.NewRecorder()
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.NodePolicy(acl.PolicyWrite))
setToken(req, token)
_, err = s.Server.ClientAllocRequest(respW, req)
require.NotNil(err)
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
}

// Try request with a valid token
// Still returns an error because the alloc does not exist
{
buf := encodeReq(map[string]string{})
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/restart", uuid.Generate()), buf)
require.NoError(err)

respW := httptest.NewRecorder()
policy := mock.NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityAllocLifecycle})
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", policy)
setToken(req, token)
_, err = s.Server.ClientAllocRequest(respW, req)
require.NotNil(err)
require.True(structs.IsErrUnknownAllocation(err))
}

// Try request with a management token
// Still returns an error because the alloc does not exist
{
buf := encodeReq(map[string]string{})
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/client/allocation/%s/restart", uuid.Generate()), buf)
require.NoError(err)

respW := httptest.NewRecorder()
setToken(req, s.RootToken)
_, err = s.Server.ClientAllocRequest(respW, req)
require.NotNil(err)
require.True(structs.IsErrUnknownAllocation(err))
}
})
}

func TestHTTP_AllocStats(t *testing.T) {
t.Parallel()
require := require.New(t)
Expand Down

0 comments on commit 90d6f1a

Please sign in to comment.