15954: Use user tokens for integration test.
[arvados.git] / lib / controller / integration_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package controller
6
7 import (
8         "bytes"
9         "context"
10         "net"
11         "net/url"
12         "os"
13         "path/filepath"
14
15         "git.arvados.org/arvados.git/lib/boot"
16         "git.arvados.org/arvados.git/lib/config"
17         "git.arvados.org/arvados.git/lib/controller/rpc"
18         "git.arvados.org/arvados.git/lib/service"
19         "git.arvados.org/arvados.git/sdk/go/arvados"
20         "git.arvados.org/arvados.git/sdk/go/arvadosclient"
21         "git.arvados.org/arvados.git/sdk/go/auth"
22         "git.arvados.org/arvados.git/sdk/go/ctxlog"
23         "git.arvados.org/arvados.git/sdk/go/keepclient"
24         check "gopkg.in/check.v1"
25 )
26
27 var _ = check.Suite(&IntegrationSuite{})
28
29 type testCluster struct {
30         booter        boot.Booter
31         config        arvados.Config
32         controllerURL *url.URL
33 }
34
35 type IntegrationSuite struct {
36         testClusters map[string]*testCluster
37 }
38
39 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
40         if forceLegacyAPI14 {
41                 c.Skip("heavy integration tests don't run with forceLegacyAPI14")
42                 return
43         }
44
45         cwd, _ := os.Getwd()
46         s.testClusters = map[string]*testCluster{
47                 "z1111": nil,
48                 "z2222": nil,
49                 "z3333": nil,
50         }
51         hostport := map[string]string{}
52         for id := range s.testClusters {
53                 hostport[id] = func() string {
54                         // TODO: Instead of expecting random ports on
55                         // 127.0.0.11, 22, 33 to be race-safe, try
56                         // different 127.x.y.z until finding one that
57                         // isn't in use.
58                         ln, err := net.Listen("tcp", ":0")
59                         c.Assert(err, check.IsNil)
60                         ln.Close()
61                         _, port, err := net.SplitHostPort(ln.Addr().String())
62                         c.Assert(err, check.IsNil)
63                         return "127.0.0." + id[3:] + ":" + port
64                 }()
65         }
66         for id := range s.testClusters {
67                 yaml := `Clusters:
68   ` + id + `:
69     Services:
70       Controller:
71         ExternalURL: https://` + hostport[id] + `
72     TLS:
73       Insecure: true
74     Login:
75       # LoginCluster: z1111
76     SystemLogs:
77       Format: text
78     RemoteClusters:
79       z1111:
80         Host: ` + hostport["z1111"] + `
81         Scheme: https
82         Insecure: true
83         Proxy: true
84       z2222:
85         Host: ` + hostport["z2222"] + `
86         Scheme: https
87         Insecure: true
88         Proxy: true
89       z3333:
90         Host: ` + hostport["z3333"] + `
91         Scheme: https
92         Insecure: true
93         Proxy: true
94 `
95                 loader := config.NewLoader(bytes.NewBufferString(yaml), ctxlog.TestLogger(c))
96                 loader.Path = "-"
97                 loader.SkipLegacy = true
98                 loader.SkipAPICalls = true
99                 cfg, err := loader.Load()
100                 c.Assert(err, check.IsNil)
101                 s.testClusters[id] = &testCluster{
102                         booter: boot.Booter{
103                                 SourcePath:           filepath.Join(cwd, "..", ".."),
104                                 LibPath:              filepath.Join(cwd, "..", "..", "tmp"),
105                                 ClusterType:          "test",
106                                 ListenHost:           "127.0.0." + id[3:],
107                                 ControllerAddr:       ":0",
108                                 OwnTemporaryDatabase: true,
109                                 Stderr:               &service.LogPrefixer{Writer: ctxlog.LogWriter(c.Log), Prefix: []byte("[" + id + "] ")},
110                         },
111                         config: *cfg,
112                 }
113                 s.testClusters[id].booter.Start(context.Background(), &s.testClusters[id].config)
114         }
115         for _, tc := range s.testClusters {
116                 au, ok := tc.booter.WaitReady()
117                 c.Assert(ok, check.Equals, true)
118                 u := url.URL(*au)
119                 tc.controllerURL = &u
120         }
121 }
122
123 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
124         for _, c := range s.testClusters {
125                 c.booter.Stop()
126         }
127 }
128
129 func (s *IntegrationSuite) conn(clusterID string) *rpc.Conn {
130         return rpc.NewConn(clusterID, s.testClusters[clusterID].controllerURL, true, rpc.PassthroughTokenProvider)
131 }
132
133 func (s *IntegrationSuite) clientsWithToken(clusterID string, token string) (context.Context, *arvados.Client, *keepclient.KeepClient) {
134         cl := s.testClusters[clusterID].config.Clusters[clusterID]
135         rootctx := auth.NewContext(context.Background(), auth.NewCredentials(token))
136         ac, err := arvados.NewClientFromConfig(&cl)
137         if err != nil {
138                 panic(err)
139         }
140         ac.AuthToken = token
141         arv, err := arvadosclient.New(ac)
142         if err != nil {
143                 panic(err)
144         }
145         kc := keepclient.New(arv)
146         return rootctx, ac, kc
147 }
148
149 func (s *IntegrationSuite) userClients(c *check.C, conn *rpc.Conn, rootctx context.Context, clusterID string, activate bool) (context.Context, *arvados.Client, *keepclient.KeepClient) {
150         login, err := conn.UserSessionCreate(rootctx, rpc.UserSessionCreateOptions{
151                 ReturnTo: ",https://example.com",
152                 AuthInfo: rpc.UserSessionAuthInfo{
153                         Email:     "user@example.com",
154                         FirstName: "Example",
155                         LastName:  "User",
156                         Username:  "example",
157                 },
158         })
159         c.Assert(err, check.IsNil)
160         redirURL, err := url.Parse(login.RedirectLocation)
161         c.Assert(err, check.IsNil)
162         userToken := redirURL.Query().Get("api_token")
163         ctx, ac, kc := s.clientsWithToken(clusterID, userToken)
164         user, err := conn.UserGetCurrent(ctx, arvados.GetOptions{})
165         if err != nil {
166                 panic(err)
167         }
168         _, err = conn.UserSetup(rootctx, arvados.UserSetupOptions{UUID: user.UUID})
169         if err != nil {
170                 panic(err)
171         }
172         user, err = conn.UserGetCurrent(ctx, arvados.GetOptions{})
173         if err != nil {
174                 panic(err)
175         }
176         return ctx, ac, kc
177 }
178
179 func (s *IntegrationSuite) rootClients(clusterID string) (context.Context, *arvados.Client, *keepclient.KeepClient) {
180         return s.clientsWithToken(clusterID, s.testClusters[clusterID].config.Clusters[clusterID].SystemRootToken)
181 }
182
183 func (s *IntegrationSuite) TestLoopDetection(c *check.C) {
184         conn1 := s.conn("z1111")
185         rootctx1, _, _ := s.rootClients("z1111")
186         conn3 := s.conn("z3333")
187         // rootctx3, _, _ := s.rootClients("z3333")
188
189         userctx1, ac1, kc1 := s.userClients(c, conn1, rootctx1, "z1111", true)
190         _, err := conn1.CollectionGet(rootctx1, arvados.GetOptions{UUID: "1f4b0bc7583c2a7f9102c395f4ffc5e3+45"})
191         c.Check(err, check.ErrorMatches, `.*404 Not Found.*`)
192
193         var coll1 arvados.Collection
194         fs1, err := coll1.FileSystem(ac1, kc1)
195         if err != nil {
196                 c.Error(err)
197         }
198         f, err := fs1.OpenFile("foo", os.O_CREATE|os.O_RDWR, 0777)
199         f.Write([]byte("foo"))
200         f.Close()
201         mtxt, err := fs1.MarshalManifest(".")
202         coll1, err = conn1.CollectionCreate(userctx1, arvados.CreateOptions{Attrs: map[string]interface{}{
203                 "manifest_text": mtxt,
204         }})
205         coll, err := conn3.CollectionGet(userctx1, arvados.GetOptions{UUID: "1f4b0bc7583c2a7f9102c395f4ffc5e3+45"})
206         c.Check(err, check.IsNil)
207         c.Check(coll.PortableDataHash, check.Equals, "1f4b0bc7583c2a7f9102c395f4ffc5e3+45")
208 }