e679106dce5d8f9d41f84b21f74aefaff4cf292e
[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         ActivateUsers: true
85       z2222:
86         Host: ` + hostport["z2222"] + `
87         Scheme: https
88         Insecure: true
89         Proxy: true
90         ActivateUsers: true
91       z3333:
92         Host: ` + hostport["z3333"] + `
93         Scheme: https
94         Insecure: true
95         Proxy: true
96         ActivateUsers: true
97 `
98                 loader := config.NewLoader(bytes.NewBufferString(yaml), ctxlog.TestLogger(c))
99                 loader.Path = "-"
100                 loader.SkipLegacy = true
101                 loader.SkipAPICalls = true
102                 cfg, err := loader.Load()
103                 c.Assert(err, check.IsNil)
104                 s.testClusters[id] = &testCluster{
105                         booter: boot.Booter{
106                                 SourcePath:           filepath.Join(cwd, "..", ".."),
107                                 LibPath:              filepath.Join(cwd, "..", "..", "tmp"),
108                                 ClusterType:          "test",
109                                 ListenHost:           "127.0.0." + id[3:],
110                                 ControllerAddr:       ":0",
111                                 OwnTemporaryDatabase: true,
112                                 Stderr:               &service.LogPrefixer{Writer: ctxlog.LogWriter(c.Log), Prefix: []byte("[" + id + "] ")},
113                         },
114                         config: *cfg,
115                 }
116                 s.testClusters[id].booter.Start(context.Background(), &s.testClusters[id].config)
117         }
118         for _, tc := range s.testClusters {
119                 au, ok := tc.booter.WaitReady()
120                 c.Assert(ok, check.Equals, true)
121                 u := url.URL(*au)
122                 tc.controllerURL = &u
123         }
124 }
125
126 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
127         for _, c := range s.testClusters {
128                 c.booter.Stop()
129         }
130 }
131
132 func (s *IntegrationSuite) conn(clusterID string) *rpc.Conn {
133         return rpc.NewConn(clusterID, s.testClusters[clusterID].controllerURL, true, rpc.PassthroughTokenProvider)
134 }
135
136 func (s *IntegrationSuite) clientsWithToken(clusterID string, token string) (context.Context, *arvados.Client, *keepclient.KeepClient) {
137         cl := s.testClusters[clusterID].config.Clusters[clusterID]
138         ctx := auth.NewContext(context.Background(), auth.NewCredentials(token))
139         ac, err := arvados.NewClientFromConfig(&cl)
140         if err != nil {
141                 panic(err)
142         }
143         ac.AuthToken = token
144         arv, err := arvadosclient.New(ac)
145         if err != nil {
146                 panic(err)
147         }
148         kc := keepclient.New(arv)
149         return ctx, ac, kc
150 }
151
152 func (s *IntegrationSuite) userClients(c *check.C, conn *rpc.Conn, rootctx context.Context, clusterID string, activate bool) (context.Context, *arvados.Client, *keepclient.KeepClient) {
153         login, err := conn.UserSessionCreate(rootctx, rpc.UserSessionCreateOptions{
154                 ReturnTo: ",https://example.com",
155                 AuthInfo: rpc.UserSessionAuthInfo{
156                         Email:     "user@example.com",
157                         FirstName: "Example",
158                         LastName:  "User",
159                         Username:  "example",
160                 },
161         })
162         c.Assert(err, check.IsNil)
163         redirURL, err := url.Parse(login.RedirectLocation)
164         c.Assert(err, check.IsNil)
165         userToken := redirURL.Query().Get("api_token")
166         c.Logf("userToken: %q", userToken)
167         ctx, ac, kc := s.clientsWithToken(clusterID, userToken)
168         user, err := conn.UserGetCurrent(ctx, arvados.GetOptions{})
169         if err != nil {
170                 panic(err)
171         }
172         _, err = conn.UserSetup(rootctx, arvados.UserSetupOptions{UUID: user.UUID})
173         if err != nil {
174                 panic(err)
175         }
176         _, err = conn.UserActivate(rootctx, arvados.UserActivateOptions{UUID: user.UUID})
177         if err != nil {
178                 panic(err)
179         }
180         user, err = conn.UserGetCurrent(ctx, arvados.GetOptions{})
181         if err != nil {
182                 panic(err)
183         }
184         c.Logf("user: %#v", user)
185         if !user.IsActive {
186                 c.Fatal("failed to activate user")
187         }
188         return ctx, ac, kc
189 }
190
191 func (s *IntegrationSuite) rootClients(clusterID string) (context.Context, *arvados.Client, *keepclient.KeepClient) {
192         return s.clientsWithToken(clusterID, s.testClusters[clusterID].config.Clusters[clusterID].SystemRootToken)
193 }
194
195 func (s *IntegrationSuite) TestLoopDetection(c *check.C) {
196         conn1 := s.conn("z1111")
197         rootctx1, _, _ := s.rootClients("z1111")
198         conn3 := s.conn("z3333")
199         // rootctx3, _, _ := s.rootClients("z3333")
200
201         userctx1, ac1, kc1 := s.userClients(c, conn1, rootctx1, "z1111", true)
202         _, err := conn1.CollectionGet(userctx1, arvados.GetOptions{UUID: "1f4b0bc7583c2a7f9102c395f4ffc5e3+45"})
203         c.Assert(err, check.ErrorMatches, `.*404 Not Found.*`)
204
205         var coll1 arvados.Collection
206         fs1, err := coll1.FileSystem(ac1, kc1)
207         if err != nil {
208                 c.Error(err)
209         }
210         f, err := fs1.OpenFile("foo", os.O_CREATE|os.O_RDWR, 0777)
211         f.Write([]byte("foo"))
212         f.Close()
213         mtxt, err := fs1.MarshalManifest(".")
214         coll1, err = conn1.CollectionCreate(userctx1, arvados.CreateOptions{Attrs: map[string]interface{}{
215                 "manifest_text": mtxt,
216         }})
217         c.Assert(err, check.IsNil)
218         coll, err := conn3.CollectionGet(userctx1, arvados.GetOptions{UUID: "1f4b0bc7583c2a7f9102c395f4ffc5e3+45"})
219         c.Check(err, check.IsNil)
220         c.Check(coll.PortableDataHash, check.Equals, "1f4b0bc7583c2a7f9102c395f4ffc5e3+45")
221 }