From 1b90a2eef0208233b1cb7f5f0866ce0cc2da54cb Mon Sep 17 00:00:00 2001 From: Chelsea Holland Komlo Date: Fri, 6 Oct 2017 20:20:30 -0400 Subject: [PATCH] adding valid case test for http endpoint --- command/agent/alloc_endpoint_test.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/command/agent/alloc_endpoint_test.go b/command/agent/alloc_endpoint_test.go index c8556b1c9..27cd5d2f9 100644 --- a/command/agent/alloc_endpoint_test.go +++ b/command/agent/alloc_endpoint_test.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/nomad/nomad/mock" "github.com/hashicorp/nomad/nomad/structs" "github.com/stretchr/testify/assert" + "golang.org/x/crypto/blake2b" ) func TestHTTP_AllocsList(t *testing.T) { @@ -320,8 +321,6 @@ func TestHTTP_AllocSnapshot_WithMigrateToken(t *testing.T) { t.Parallel() assert := assert.New(t) httpACLTest(t, nil, func(s *TestAgent) { - // TODO add an allocation, assert it is returned - // Request without a token succeeds req, err := http.NewRequest("GET", "/v1/client/allocation/123/snapshot", nil) assert.Nil(err) @@ -331,6 +330,28 @@ func TestHTTP_AllocSnapshot_WithMigrateToken(t *testing.T) { _, err = s.Server.ClientAllocRequest(respW, req) assert.NotNil(err) assert.Contains(err.Error(), "invalid migrate token") + + // Create an allocation + state := s.Agent.server.State() + alloc := mock.Alloc() + state.UpsertJobSummary(998, mock.JobSummary(alloc.JobID)) + + // Set up data to create an authenticated request + h, err := blake2b.New512([]byte(s.Agent.Client().Node().SecretID)) + h.Write([]byte(alloc.ID)) + validMigrateToken, err := string(h.Sum(nil)), nil + assert.Nil(err) + + // Request with a token succeeds + req.Header.Set("X-Nomad-Token", validMigrateToken) + req, err = http.NewRequest("GET", "/v1/client/allocation/123/snapshot", nil) + assert.Nil(err) + + // Make the unauthorized request + respW = httptest.NewRecorder() + _, err = s.Server.ClientAllocRequest(respW, req) + assert.NotNil(err) + assert.Contains(err.Error(), "invalid migrate token") }) }