From 4956c96c8bf86d6512231b8ea4118dee9e918779 Mon Sep 17 00:00:00 2001 From: Peter Amstutz Date: Mon, 22 Oct 2018 12:13:38 -0400 Subject: [PATCH] 14262: Tests for setting and checking container tokens. Arvados-DCO-1.1-Signed-off-by: Peter Amstutz --- lib/controller/federation.go | 4 +- lib/controller/federation_test.go | 67 +++++++++++++++++++++++++++++++ sdk/go/arvados/container.go | 1 + 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/lib/controller/federation.go b/lib/controller/federation.go index 18f3e4479e..dc0aa908c5 100644 --- a/lib/controller/federation.go +++ b/lib/controller/federation.go @@ -185,9 +185,9 @@ func (h *Handler) createAPItoken(req *http.Request, userUUID string, scopes []st (uuid, api_token, expires_at, scopes, user_id, api_client_id, created_at, updated_at) -VALUES ($1, $2, now() + INTERVAL '2 weeks', $3, +VALUES ($1, $2, CURRENT_TIMESTAMP + INTERVAL '2 weeks', $3, (SELECT id FROM users WHERE users.uuid=$4 LIMIT 1), -0, now(), now())`, +0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`, uuid, token, string(scopesjson), userUUID) if err != nil { diff --git a/lib/controller/federation_test.go b/lib/controller/federation_test.go index 23d5d7ca76..7842ad05d7 100644 --- a/lib/controller/federation_test.go +++ b/lib/controller/federation_test.go @@ -5,8 +5,10 @@ package controller import ( + "bytes" "encoding/json" "fmt" + "io" "io/ioutil" "net/http" "net/http/httptest" @@ -90,6 +92,10 @@ func (s *FederationSuite) SetUpTest(c *check.C) { } func (s *FederationSuite) remoteMockHandler(w http.ResponseWriter, req *http.Request) { + b := &bytes.Buffer{} + io.Copy(b, req.Body) + req.Body = ioutil.NopCloser(b) + req.Body.Close() s.remoteMockRequests = append(s.remoteMockRequests, *req) } @@ -567,6 +573,67 @@ func (s *FederationSuite) TestCreateRemoteContainerRequest(c *check.C) { c.Check(strings.HasPrefix(cr.UUID, "zzzzz-"), check.Equals, true) } +func (s *FederationSuite) TestCreateRemoteContainerRequestCheckRuntimeToken(c *check.C) { + // Send request to zmock and check that outgoing request has + // runtime_token sent (because runtime_token isn't returned in + // the response). + + defer s.localServiceReturns404(c).Close() + // pass cluster_id via query parameter, this allows arvados-controller + // to avoid parsing the body + req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zmock", + strings.NewReader(`{ + "container_request": { + "name": "hello world", + "state": "Uncommitted", + "output_path": "/", + "container_image": "123", + "command": ["abc"] + } +} +`)) + req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken) + req.Header.Set("Content-type", "application/json") + resp := s.testRequest(req) + c.Check(resp.StatusCode, check.Equals, http.StatusOK) + var cr struct { + arvados.ContainerRequest `json:"container_request"` + } + c.Check(json.NewDecoder(s.remoteMockRequests[0].Body).Decode(&cr), check.IsNil) + c.Check(strings.HasPrefix(cr.ContainerRequest.RuntimeToken, "v2/"), check.Equals, true) +} + +func (s *FederationSuite) TestCreateRemoteContainerRequestCheckSetRuntimeToken(c *check.C) { + // Send request to zmock and check that outgoing request has + // runtime_token sent (because runtime_token isn't returned in + // the response). + + defer s.localServiceReturns404(c).Close() + // pass cluster_id via query parameter, this allows arvados-controller + // to avoid parsing the body + req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zmock", + strings.NewReader(`{ + "container_request": { + "name": "hello world", + "state": "Uncommitted", + "output_path": "/", + "container_image": "123", + "command": ["abc"], + "runtime_token": "xyz" + } +} +`)) + req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken) + req.Header.Set("Content-type", "application/json") + resp := s.testRequest(req) + c.Check(resp.StatusCode, check.Equals, http.StatusOK) + var cr struct { + arvados.ContainerRequest `json:"container_request"` + } + c.Check(json.NewDecoder(s.remoteMockRequests[0].Body).Decode(&cr), check.IsNil) + c.Check(cr.ContainerRequest.RuntimeToken, check.Equals, "xyz") +} + func (s *FederationSuite) TestCreateRemoteContainerRequestError(c *check.C) { defer s.localServiceReturns404(c).Close() // pass cluster_id via query parameter, this allows arvados-controller diff --git a/sdk/go/arvados/container.go b/sdk/go/arvados/container.go index 2622c13703..b70b4ac917 100644 --- a/sdk/go/arvados/container.go +++ b/sdk/go/arvados/container.go @@ -56,6 +56,7 @@ type ContainerRequest struct { UseExisting bool `json:"use_existing"` LogUUID string `json:"log_uuid"` OutputUUID string `json:"output_uuid"` + RuntimeToken string `json:"runtime_token"` } // Mount is special behavior to attach to a filesystem path or device. -- 2.30.2