21700: Install Bundler system-wide in Rails postinst
[arvados.git] / lib / controller / federation / federation_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package federation
6
7 import (
8         "context"
9         "net/url"
10         "os"
11         "testing"
12
13         "git.arvados.org/arvados.git/lib/config"
14         "git.arvados.org/arvados.git/lib/controller/router"
15         "git.arvados.org/arvados.git/lib/controller/rpc"
16         "git.arvados.org/arvados.git/lib/ctrlctx"
17         "git.arvados.org/arvados.git/sdk/go/arvados"
18         "git.arvados.org/arvados.git/sdk/go/arvadostest"
19         "git.arvados.org/arvados.git/sdk/go/auth"
20         "git.arvados.org/arvados.git/sdk/go/ctxlog"
21         "git.arvados.org/arvados.git/sdk/go/httpserver"
22         "github.com/jmoiron/sqlx"
23         check "gopkg.in/check.v1"
24 )
25
26 // Gocheck boilerplate
27 func Test(t *testing.T) {
28         check.TestingT(t)
29 }
30
31 // FederationSuite does some generic setup/teardown. Don't add Test*
32 // methods to FederationSuite itself.
33 type FederationSuite struct {
34         integrationTestCluster *arvados.Cluster
35         cluster                *arvados.Cluster
36         ctx                    context.Context
37         tx                     *sqlx.Tx
38         fed                    *Conn
39 }
40
41 func (s *FederationSuite) SetUpSuite(c *check.C) {
42         cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
43         c.Assert(err, check.IsNil)
44         s.integrationTestCluster, err = cfg.GetCluster("")
45         c.Assert(err, check.IsNil)
46 }
47
48 func (s *FederationSuite) SetUpTest(c *check.C) {
49         s.cluster = &arvados.Cluster{
50                 ClusterID:       "aaaaa",
51                 SystemRootToken: arvadostest.SystemRootToken,
52                 RemoteClusters: map[string]arvados.RemoteCluster{
53                         "aaaaa": {
54                                 Host: os.Getenv("ARVADOS_API_HOST"),
55                         },
56                 },
57                 PostgreSQL: s.integrationTestCluster.PostgreSQL,
58         }
59         arvadostest.SetServiceURL(&s.cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
60         s.cluster.TLS.Insecure = true
61         s.cluster.API.MaxItemsPerResponse = 3
62
63         tx, err := arvadostest.DB(c, s.cluster).Beginx()
64         c.Assert(err, check.IsNil)
65         s.tx = tx
66
67         ctx := context.Background()
68         ctx = ctxlog.Context(ctx, ctxlog.TestLogger(c))
69         ctx = auth.NewContext(ctx, &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
70         ctx = ctrlctx.NewWithTransaction(ctx, s.tx)
71         s.ctx = ctx
72
73         s.fed = New(ctx, s.cluster, nil, (&ctrlctx.DBConnector{PostgreSQL: s.cluster.PostgreSQL}).GetDB)
74 }
75
76 func (s *FederationSuite) TearDownTest(c *check.C) {
77         s.tx.Rollback()
78 }
79
80 func (s *FederationSuite) addDirectRemote(c *check.C, id string, backend backend) {
81         s.cluster.RemoteClusters[id] = arvados.RemoteCluster{
82                 Host: "in-process.local",
83         }
84         s.fed.remotes[id] = backend
85 }
86
87 func (s *FederationSuite) addHTTPRemote(c *check.C, id string, backend backend) {
88         srv := httpserver.Server{Addr: ":"}
89         srv.Handler = router.New(backend, router.Config{})
90         c.Check(srv.Start(), check.IsNil)
91         s.cluster.RemoteClusters[id] = arvados.RemoteCluster{
92                 Scheme: "http",
93                 Host:   srv.Addr,
94                 Proxy:  true,
95         }
96         s.fed.remotes[id] = rpc.NewConn(id, &url.URL{Scheme: "http", Host: srv.Addr}, true, saltedTokenProvider(s.cluster, s.fed.local, id))
97 }