1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
18 "git.curoverse.com/arvados.git/sdk/go/httpserver"
19 "github.com/Sirupsen/logrus"
20 check "gopkg.in/check.v1"
23 // Gocheck boilerplate
24 var _ = check.Suite(&FederationSuite{})
26 type FederationSuite struct {
28 // testServer and testHandler are the controller being tested,
30 testServer *httpserver.Server
32 // remoteServer ("zzzzz") forwards requests to the Rails API
33 // provided by the integration test environment.
34 remoteServer *httpserver.Server
35 // remoteMock ("zmock") appends each incoming request to
36 // remoteMockRequests, and returns an empty 200 response.
37 remoteMock *httpserver.Server
38 remoteMockRequests []http.Request
41 func (s *FederationSuite) SetUpTest(c *check.C) {
43 s.log.Formatter = &logrus.JSONFormatter{}
44 s.log.Out = &logWriter{c.Log}
46 s.remoteServer = newServerFromIntegrationTestEnv(c)
47 c.Assert(s.remoteServer.Start(), check.IsNil)
49 s.remoteMock = newServerFromIntegrationTestEnv(c)
50 s.remoteMock.Server.Handler = http.HandlerFunc(s.remoteMockHandler)
51 c.Assert(s.remoteMock.Start(), check.IsNil)
53 nodeProfile := arvados.NodeProfile{
54 Controller: arvados.SystemServiceInstance{Listen: ":"},
55 RailsAPI: arvados.SystemServiceInstance{Listen: ":1"}, // local reqs will error "connection refused"
57 s.testHandler = &Handler{Cluster: &arvados.Cluster{
59 NodeProfiles: map[string]arvados.NodeProfile{
62 }, NodeProfile: &nodeProfile}
63 s.testServer = newServerFromIntegrationTestEnv(c)
64 s.testServer.Server.Handler = httpserver.AddRequestIDs(httpserver.LogRequests(s.log, s.testHandler))
66 s.testHandler.Cluster.RemoteClusters = map[string]arvados.RemoteCluster{
68 Host: s.remoteServer.Addr,
73 Host: s.remoteMock.Addr,
79 c.Assert(s.testServer.Start(), check.IsNil)
82 func (s *FederationSuite) remoteMockHandler(w http.ResponseWriter, req *http.Request) {
83 s.remoteMockRequests = append(s.remoteMockRequests, *req)
86 func (s *FederationSuite) TearDownTest(c *check.C) {
87 if s.remoteServer != nil {
88 s.remoteServer.Close()
90 if s.testServer != nil {
95 func (s *FederationSuite) testRequest(req *http.Request) *http.Response {
96 resp := httptest.NewRecorder()
97 s.testServer.Server.Handler.ServeHTTP(resp, req)
101 func (s *FederationSuite) TestLocalRequest(c *check.C) {
102 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zhome-", 1), nil)
103 resp := s.testRequest(req)
104 s.checkHandledLocally(c, resp)
107 func (s *FederationSuite) checkHandledLocally(c *check.C, resp *http.Response) {
108 // Our "home" controller can't handle local requests because
109 // it doesn't have its own stub/test Rails API, so we rely on
110 // "connection refused" to indicate the controller tried to
111 // proxy the request to its local Rails API.
112 c.Check(resp.StatusCode, check.Equals, http.StatusInternalServerError)
113 s.checkJSONErrorMatches(c, resp, `.*connection refused`)
116 func (s *FederationSuite) TestNoAuth(c *check.C) {
117 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
118 resp := s.testRequest(req)
119 c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
120 s.checkJSONErrorMatches(c, resp, `Not logged in`)
123 func (s *FederationSuite) TestBadAuth(c *check.C) {
124 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
125 req.Header.Set("Authorization", "Bearer aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
126 resp := s.testRequest(req)
127 c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
128 s.checkJSONErrorMatches(c, resp, `Not logged in`)
131 func (s *FederationSuite) TestNoAccess(c *check.C) {
132 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
133 req.Header.Set("Authorization", "Bearer "+arvadostest.SpectatorToken)
134 resp := s.testRequest(req)
135 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
136 s.checkJSONErrorMatches(c, resp, `.*not found`)
139 func (s *FederationSuite) TestGetUnknownRemote(c *check.C) {
140 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zz404-", 1), nil)
141 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
142 resp := s.testRequest(req)
143 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
144 s.checkJSONErrorMatches(c, resp, `.*no proxy available for cluster zz404`)
147 func (s *FederationSuite) TestRemoteError(c *check.C) {
148 rc := s.testHandler.Cluster.RemoteClusters["zzzzz"]
150 s.testHandler.Cluster.RemoteClusters["zzzzz"] = rc
152 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
153 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
154 resp := s.testRequest(req)
155 c.Check(resp.StatusCode, check.Equals, http.StatusInternalServerError)
156 s.checkJSONErrorMatches(c, resp, `.*HTTP response to HTTPS client`)
159 func (s *FederationSuite) TestGetRemoteWorkflow(c *check.C) {
160 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
161 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
162 resp := s.testRequest(req)
163 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
164 var wf arvados.Workflow
165 c.Check(json.NewDecoder(resp.Body).Decode(&wf), check.IsNil)
166 c.Check(wf.UUID, check.Equals, arvadostest.WorkflowWithDefinitionYAMLUUID)
167 c.Check(wf.OwnerUUID, check.Equals, arvadostest.ActiveUserUUID)
170 func (s *FederationSuite) TestRemoteWithTokenInQuery(c *check.C) {
171 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zmock-", 1)+"?api_token="+arvadostest.ActiveToken, nil)
173 c.Assert(len(s.remoteMockRequests), check.Equals, 1)
174 c.Check(s.remoteMockRequests[0].URL.String(), check.Not(check.Matches), `.*api_token=.*`)
177 func (s *FederationSuite) TestUpdateRemoteWorkflow(c *check.C) {
178 updateDescription := func(descr string) *http.Response {
179 req := httptest.NewRequest("PATCH", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, strings.NewReader(url.Values{
180 "workflow": {`{"description":"` + descr + `"}`},
182 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
183 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
184 resp := s.testRequest(req)
185 s.checkResponseOK(c, resp)
189 // Update description twice so running this test twice in a
190 // row still causes ModifiedAt to change
191 updateDescription("updated once by TestUpdateRemoteWorkflow")
192 resp := updateDescription("updated twice by TestUpdateRemoteWorkflow")
194 var wf arvados.Workflow
195 c.Check(json.NewDecoder(resp.Body).Decode(&wf), check.IsNil)
196 c.Check(wf.UUID, check.Equals, arvadostest.WorkflowWithDefinitionYAMLUUID)
197 c.Assert(wf.ModifiedAt, check.NotNil)
198 c.Logf("%s", *wf.ModifiedAt)
199 c.Check(time.Since(*wf.ModifiedAt) < time.Minute, check.Equals, true)
202 func (s *FederationSuite) checkResponseOK(c *check.C, resp *http.Response) {
203 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
204 if resp.StatusCode != http.StatusOK {
205 body, err := ioutil.ReadAll(resp.Body)
206 c.Logf("... response body = %q, %v\n", body, err)
210 func (s *FederationSuite) checkJSONErrorMatches(c *check.C, resp *http.Response, re string) {
211 var jresp httpserver.ErrorResponse
212 err := json.NewDecoder(resp.Body).Decode(&jresp)
213 c.Check(err, check.IsNil)
214 c.Assert(len(jresp.Errors), check.Equals, 1)
215 c.Check(jresp.Errors[0], check.Matches, re)