15954: Logging and env var fixes.
[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         port := map[string]string{}
52         for id := range s.testClusters {
53                 port[id] = func() string {
54                         ln, err := net.Listen("tcp", "localhost:0")
55                         c.Assert(err, check.IsNil)
56                         ln.Close()
57                         _, port, err := net.SplitHostPort(ln.Addr().String())
58                         c.Assert(err, check.IsNil)
59                         return port
60                 }()
61         }
62         for id := range s.testClusters {
63                 yaml := `Clusters:
64   ` + id + `:
65     Services:
66       Controller:
67         ExternalURL: https://localhost:` + port[id] + `
68     TLS:
69       Insecure: true
70     Login:
71       LoginCluster: z1111
72     SystemLogs:
73       Format: text
74     RemoteClusters:
75       z1111:
76         Host: localhost:` + port["z1111"] + `
77         Scheme: https
78         Insecure: true
79       z2222:
80         Host: localhost:` + port["z2222"] + `
81         Scheme: https
82         Insecure: true
83       z3333:
84         Host: localhost:` + port["z3333"] + `
85         Scheme: https
86         Insecure: true
87 `
88                 loader := config.NewLoader(bytes.NewBufferString(yaml), ctxlog.TestLogger(c))
89                 loader.Path = "-"
90                 loader.SkipLegacy = true
91                 loader.SkipAPICalls = true
92                 cfg, err := loader.Load()
93                 c.Assert(err, check.IsNil)
94                 s.testClusters[id] = &testCluster{
95                         booter: boot.Booter{
96                                 SourcePath:           filepath.Join(cwd, "..", ".."),
97                                 LibPath:              filepath.Join(cwd, "..", "..", "tmp"),
98                                 ClusterType:          "test",
99                                 ListenHost:           "localhost",
100                                 ControllerAddr:       ":0",
101                                 OwnTemporaryDatabase: true,
102                                 Stderr:               &service.LogPrefixer{Writer: ctxlog.LogWriter(c.Log), Prefix: []byte("[" + id + "] ")},
103                         },
104                         config: *cfg,
105                 }
106                 s.testClusters[id].booter.Start(context.Background(), &s.testClusters[id].config)
107         }
108         for _, tc := range s.testClusters {
109                 au, ok := tc.booter.WaitReady()
110                 c.Assert(ok, check.Equals, true)
111                 u := url.URL(*au)
112                 tc.controllerURL = &u
113         }
114 }
115
116 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
117         for _, c := range s.testClusters {
118                 c.booter.Stop()
119         }
120 }
121
122 func (s *IntegrationSuite) conn(clusterID string) (*rpc.Conn, context.Context, *arvados.Client, *keepclient.KeepClient) {
123         cl := s.testClusters[clusterID].config.Clusters[clusterID]
124         conn := rpc.NewConn(clusterID, s.testClusters[clusterID].controllerURL, true, rpc.PassthroughTokenProvider)
125         rootctx := auth.NewContext(context.Background(), auth.NewCredentials(cl.SystemRootToken))
126         ac, err := arvados.NewClientFromConfig(&cl)
127         if err != nil {
128                 panic(err)
129         }
130         ac.AuthToken = cl.SystemRootToken
131         arv, err := arvadosclient.New(ac)
132         if err != nil {
133                 panic(err)
134         }
135         kc := keepclient.New(arv)
136         return conn, rootctx, ac, kc
137 }
138
139 func (s *IntegrationSuite) TestLoopDetection(c *check.C) {
140         conn1, rootctx1, _, _ := s.conn("z1111")
141         conn3, rootctx3, ac3, kc3 := s.conn("z3333")
142
143         _, err := conn1.CollectionGet(rootctx1, arvados.GetOptions{UUID: "1f4b0bc7583c2a7f9102c395f4ffc5e3+45"})
144         c.Check(err, check.ErrorMatches, `.*404 Not Found.*`)
145
146         var coll3 arvados.Collection
147         fs3, err := coll3.FileSystem(ac3, kc3)
148         if err != nil {
149                 c.Error(err)
150         }
151         f, err := fs3.OpenFile("foo", os.O_CREATE|os.O_RDWR, 0777)
152         f.Write([]byte("foo"))
153         f.Close()
154         mtxt, err := fs3.MarshalManifest(".")
155         coll3, err = conn3.CollectionCreate(rootctx3, arvados.CreateOptions{Attrs: map[string]interface{}{
156                 "manifest_text": mtxt,
157         }})
158         coll, err := conn1.CollectionGet(rootctx1, arvados.GetOptions{UUID: "1f4b0bc7583c2a7f9102c395f4ffc5e3+45"})
159         c.Check(err, check.IsNil)
160         c.Check(coll.PortableDataHash, check.Equals, "1f4b0bc7583c2a7f9102c395f4ffc5e3+45")
161 }