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 pr := s.remoteMockRequests[0]
175 c.Check(pr.URL.String(), check.Not(check.Matches), `.*api_token=.*`)
176 c.Check(pr.Header.Get("Authorization"), check.Equals, "Bearer "+arvadostest.ActiveToken)
179 func (s *FederationSuite) TestWorkflowCRUD(c *check.C) {
180 wf := arvados.Workflow{
181 Description: "TestCRUD",
184 body := &strings.Builder{}
185 json.NewEncoder(body).Encode(&wf)
186 req := httptest.NewRequest("POST", "/arvados/v1/workflows", strings.NewReader(url.Values{
187 "workflow": {body.String()},
189 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
190 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
191 rec := httptest.NewRecorder()
192 s.remoteServer.Server.Handler.ServeHTTP(rec, req) // direct to remote -- can't proxy a create req because no uuid
194 s.checkResponseOK(c, resp)
195 json.NewDecoder(resp.Body).Decode(&wf)
198 req := httptest.NewRequest("DELETE", "/arvados/v1/workflows/"+wf.UUID, nil)
199 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
200 s.remoteServer.Server.Handler.ServeHTTP(httptest.NewRecorder(), req)
202 c.Check(wf.UUID, check.Not(check.Equals), "")
204 c.Assert(wf.ModifiedAt, check.NotNil)
205 c.Logf("wf.ModifiedAt: %v", wf.ModifiedAt)
206 c.Check(time.Since(*wf.ModifiedAt) < time.Minute, check.Equals, true)
208 for _, method := range []string{"PATCH", "PUT", "POST"} {
210 "workflow": {`{"description": "Updated with ` + method + `"}`},
212 if method == "POST" {
213 form["_method"] = []string{"PATCH"}
215 req := httptest.NewRequest(method, "/arvados/v1/workflows/"+wf.UUID, strings.NewReader(form.Encode()))
216 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
217 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
218 resp := s.testRequest(req)
219 s.checkResponseOK(c, resp)
220 err := json.NewDecoder(resp.Body).Decode(&wf)
221 c.Check(err, check.IsNil)
223 c.Check(wf.Description, check.Equals, "Updated with "+method)
226 req := httptest.NewRequest("DELETE", "/arvados/v1/workflows/"+wf.UUID, nil)
227 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
228 resp := s.testRequest(req)
229 s.checkResponseOK(c, resp)
230 err := json.NewDecoder(resp.Body).Decode(&wf)
231 c.Check(err, check.IsNil)
234 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+wf.UUID, nil)
235 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
236 resp := s.testRequest(req)
237 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
241 func (s *FederationSuite) checkResponseOK(c *check.C, resp *http.Response) {
242 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
243 if resp.StatusCode != http.StatusOK {
244 body, err := ioutil.ReadAll(resp.Body)
245 c.Logf("... response body = %q, %v\n", body, err)
249 func (s *FederationSuite) checkJSONErrorMatches(c *check.C, resp *http.Response, re string) {
250 var jresp httpserver.ErrorResponse
251 err := json.NewDecoder(resp.Body).Decode(&jresp)
252 c.Check(err, check.IsNil)
253 c.Assert(len(jresp.Errors), check.Equals, 1)
254 c.Check(jresp.Errors[0], check.Matches, re)