17170: Don't reuse transport that might have http2 enabled.
[arvados.git] / lib / controller / rpc / conn_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package rpc
6
7 import (
8         "context"
9         "net/url"
10         "os"
11         "testing"
12
13         "git.arvados.org/arvados.git/sdk/go/arvados"
14         "git.arvados.org/arvados.git/sdk/go/arvadostest"
15         "git.arvados.org/arvados.git/sdk/go/ctxlog"
16         "github.com/sirupsen/logrus"
17         check "gopkg.in/check.v1"
18 )
19
20 // Gocheck boilerplate
21 func Test(t *testing.T) {
22         check.TestingT(t)
23 }
24
25 var _ = check.Suite(&RPCSuite{})
26
27 type key int
28
29 const (
30         contextKeyTestTokens key = iota
31 )
32
33 type RPCSuite struct {
34         log  logrus.FieldLogger
35         ctx  context.Context
36         conn *Conn
37 }
38
39 func (s *RPCSuite) SetUpTest(c *check.C) {
40         ctx := ctxlog.Context(context.Background(), ctxlog.TestLogger(c))
41         s.ctx = context.WithValue(ctx, contextKeyTestTokens, []string{arvadostest.ActiveToken})
42         s.conn = NewConn("zzzzz", &url.URL{Scheme: "https", Host: os.Getenv("ARVADOS_TEST_API_HOST")}, true, func(ctx context.Context) ([]string, error) {
43                 tokens, _ := ctx.Value(contextKeyTestTokens).([]string)
44                 return tokens, nil
45         })
46 }
47
48 func (s *RPCSuite) TestLogin(c *check.C) {
49         s.ctx = context.Background()
50         opts := arvados.LoginOptions{
51                 ReturnTo: "https://foo.example.com/bar",
52         }
53         resp, err := s.conn.Login(s.ctx, opts)
54         c.Check(err, check.IsNil)
55         c.Check(resp.RedirectLocation, check.Equals, "/auth/joshid?return_to="+url.QueryEscape(","+opts.ReturnTo))
56 }
57
58 func (s *RPCSuite) TestLogout(c *check.C) {
59         s.ctx = context.Background()
60         opts := arvados.LogoutOptions{
61                 ReturnTo: "https://foo.example.com/bar",
62         }
63         resp, err := s.conn.Logout(s.ctx, opts)
64         c.Check(err, check.IsNil)
65         c.Check(resp.RedirectLocation, check.Equals, "http://localhost:3002/users/sign_out?redirect_uri="+url.QueryEscape(opts.ReturnTo))
66 }
67
68 func (s *RPCSuite) TestCollectionCreate(c *check.C) {
69         coll, err := s.conn.CollectionCreate(s.ctx, arvados.CreateOptions{Attrs: map[string]interface{}{
70                 "owner_uuid":         arvadostest.ActiveUserUUID,
71                 "portable_data_hash": "d41d8cd98f00b204e9800998ecf8427e+0",
72         }})
73         c.Check(err, check.IsNil)
74         c.Check(coll.UUID, check.HasLen, 27)
75 }
76
77 func (s *RPCSuite) TestSpecimenCRUD(c *check.C) {
78         sp, err := s.conn.SpecimenCreate(s.ctx, arvados.CreateOptions{Attrs: map[string]interface{}{
79                 "owner_uuid": arvadostest.ActiveUserUUID,
80                 "properties": map[string]string{"foo": "bar"},
81         }})
82         c.Check(err, check.IsNil)
83         c.Check(sp.UUID, check.HasLen, 27)
84         c.Check(sp.Properties, check.HasLen, 1)
85         c.Check(sp.Properties["foo"], check.Equals, "bar")
86
87         spGet, err := s.conn.SpecimenGet(s.ctx, arvados.GetOptions{UUID: sp.UUID})
88         c.Check(err, check.IsNil)
89         c.Check(spGet.UUID, check.Equals, sp.UUID)
90         c.Check(spGet.Properties["foo"], check.Equals, "bar")
91
92         spList, err := s.conn.SpecimenList(s.ctx, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", sp.UUID}}})
93         c.Check(err, check.IsNil)
94         c.Check(spList.ItemsAvailable, check.Equals, 1)
95         c.Assert(spList.Items, check.HasLen, 1)
96         c.Check(spList.Items[0].UUID, check.Equals, sp.UUID)
97         c.Check(spList.Items[0].Properties["foo"], check.Equals, "bar")
98
99         anonCtx := context.WithValue(context.Background(), contextKeyTestTokens, []string{arvadostest.AnonymousToken})
100         spList, err = s.conn.SpecimenList(anonCtx, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", sp.UUID}}})
101         c.Check(err, check.IsNil)
102         c.Check(spList.ItemsAvailable, check.Equals, 0)
103         c.Check(spList.Items, check.HasLen, 0)
104
105         spDel, err := s.conn.SpecimenDelete(s.ctx, arvados.DeleteOptions{UUID: sp.UUID})
106         c.Check(err, check.IsNil)
107         c.Check(spDel.UUID, check.Equals, sp.UUID)
108 }