1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.arvados.org/arvados.git/lib/boot"
17 "git.arvados.org/arvados.git/lib/config"
18 "git.arvados.org/arvados.git/lib/controller/rpc"
19 "git.arvados.org/arvados.git/lib/service"
20 "git.arvados.org/arvados.git/sdk/go/arvados"
21 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
22 "git.arvados.org/arvados.git/sdk/go/auth"
23 "git.arvados.org/arvados.git/sdk/go/ctxlog"
24 "git.arvados.org/arvados.git/sdk/go/keepclient"
25 check "gopkg.in/check.v1"
28 var _ = check.Suite(&IntegrationSuite{})
30 type testCluster struct {
33 controllerURL *url.URL
36 type IntegrationSuite struct {
37 testClusters map[string]*testCluster
40 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
42 c.Skip("heavy integration tests don't run with forceLegacyAPI14")
47 s.testClusters = map[string]*testCluster{
52 hostport := map[string]string{}
53 for id := range s.testClusters {
54 hostport[id] = func() string {
55 // TODO: Instead of expecting random ports on
56 // 127.0.0.11, 22, 33 to be race-safe, try
57 // different 127.x.y.z until finding one that
59 ln, err := net.Listen("tcp", ":0")
60 c.Assert(err, check.IsNil)
62 _, port, err := net.SplitHostPort(ln.Addr().String())
63 c.Assert(err, check.IsNil)
64 return "127.0.0." + id[3:] + ":" + port
67 for id := range s.testClusters {
72 ExternalURL: https://` + hostport[id] + `
81 Host: ` + hostport["z1111"] + `
87 Host: ` + hostport["z2222"] + `
93 Host: ` + hostport["z3333"] + `
99 loader := config.NewLoader(bytes.NewBufferString(yaml), ctxlog.TestLogger(c))
101 loader.SkipLegacy = true
102 loader.SkipAPICalls = true
103 cfg, err := loader.Load()
104 c.Assert(err, check.IsNil)
105 s.testClusters[id] = &testCluster{
106 super: boot.Supervisor{
107 SourcePath: filepath.Join(cwd, "..", ".."),
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 + "] ")},
116 s.testClusters[id].super.Start(context.Background(), &s.testClusters[id].config)
118 for _, tc := range s.testClusters {
119 au, ok := tc.super.WaitReady()
120 c.Assert(ok, check.Equals, true)
122 tc.controllerURL = &u
126 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
127 for _, c := range s.testClusters {
132 func (s *IntegrationSuite) conn(clusterID string) *rpc.Conn {
133 return rpc.NewConn(clusterID, s.testClusters[clusterID].controllerURL, true, rpc.PassthroughTokenProvider)
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)
144 arv, err := arvadosclient.New(ac)
148 kc := keepclient.New(arv)
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",
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("user token: %q", userToken)
167 ctx, ac, kc := s.clientsWithToken(clusterID, userToken)
168 user, err := conn.UserGetCurrent(ctx, arvados.GetOptions{})
169 c.Assert(err, check.IsNil)
170 _, err = conn.UserSetup(rootctx, arvados.UserSetupOptions{UUID: user.UUID})
171 c.Assert(err, check.IsNil)
173 _, err = conn.UserActivate(rootctx, arvados.UserActivateOptions{UUID: user.UUID})
174 c.Assert(err, check.IsNil)
175 user, err = conn.UserGetCurrent(ctx, arvados.GetOptions{})
176 c.Assert(err, check.IsNil)
177 c.Logf("user UUID: %q", user.UUID)
179 c.Fatalf("failed to activate user -- %#v", user)
185 func (s *IntegrationSuite) rootClients(clusterID string) (context.Context, *arvados.Client, *keepclient.KeepClient) {
186 return s.clientsWithToken(clusterID, s.testClusters[clusterID].config.Clusters[clusterID].SystemRootToken)
189 func (s *IntegrationSuite) TestGetCollectionByPDH(c *check.C) {
190 conn1 := s.conn("z1111")
191 rootctx1, _, _ := s.rootClients("z1111")
192 conn3 := s.conn("z3333")
193 userctx1, ac1, kc1 := s.userClients(c, conn1, rootctx1, "z1111", true)
195 // Create the collection to find its PDH (but don't save it
197 var coll1 arvados.Collection
198 fs1, err := coll1.FileSystem(ac1, kc1)
199 c.Assert(err, check.IsNil)
200 f, err := fs1.OpenFile("test.txt", os.O_CREATE|os.O_RDWR, 0777)
201 c.Assert(err, check.IsNil)
202 _, err = io.WriteString(f, "IntegrationSuite.TestGetCollectionByPDH")
203 c.Assert(err, check.IsNil)
205 c.Assert(err, check.IsNil)
206 mtxt, err := fs1.MarshalManifest(".")
207 c.Assert(err, check.IsNil)
208 pdh := arvados.PortableDataHash(mtxt)
210 // Looking up the PDH before saving returns 404 if cycle
211 // detection is working.
212 _, err = conn1.CollectionGet(userctx1, arvados.GetOptions{UUID: pdh})
213 c.Assert(err, check.ErrorMatches, `.*404 Not Found.*`)
215 // Save the collection on cluster z1111.
216 coll1, err = conn1.CollectionCreate(userctx1, arvados.CreateOptions{Attrs: map[string]interface{}{
217 "manifest_text": mtxt,
219 c.Assert(err, check.IsNil)
221 // Retrieve the collection from cluster z3333.
222 coll, err := conn3.CollectionGet(userctx1, arvados.GetOptions{UUID: pdh})
223 c.Check(err, check.IsNil)
224 c.Check(coll.PortableDataHash, check.Equals, pdh)