1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
11 "git.arvados.org/arvados.git/lib/boot"
12 "git.arvados.org/arvados.git/lib/controller/rpc"
13 "git.arvados.org/arvados.git/lib/service"
14 "git.arvados.org/arvados.git/sdk/go/arvados"
15 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
16 "git.arvados.org/arvados.git/sdk/go/auth"
17 "git.arvados.org/arvados.git/sdk/go/ctxlog"
18 "git.arvados.org/arvados.git/sdk/go/keepclient"
22 // TestCluster stores a working test cluster data
23 type TestCluster struct {
26 ControllerURL *url.URL
31 loggerfunc func(...interface{})
34 func (l logger) Log(args ...interface{}) {
38 // NewTestCluster loads the provided configuration, and sets up a test cluster
39 // ready for being started.
40 func NewTestCluster(srcPath, clusterID string, cfg *arvados.Config, listenHost string, logWriter func(...interface{})) *TestCluster {
42 Super: boot.Supervisor{
45 ListenHost: listenHost,
47 OwnTemporaryDatabase: true,
48 Stderr: &service.LogPrefixer{
49 Writer: ctxlog.LogWriter(logWriter),
50 Prefix: []byte("[" + clusterID + "] ")},
57 // Start the test cluster.
58 func (tc *TestCluster) Start() {
59 tc.Super.Start(context.Background(), &tc.Config, "-")
62 // WaitReady waits for all components to report healthy, and finishes setting
63 // up the TestCluster struct.
64 func (tc *TestCluster) WaitReady() bool {
65 au, ok := tc.Super.WaitReady()
74 // ClientsWithToken returns Context, Arvados.Client and keepclient structs
75 // initialized to connect to the cluster with the supplied Arvados token.
76 func (tc *TestCluster) ClientsWithToken(token string) (context.Context, *arvados.Client, *keepclient.KeepClient) {
77 cl := tc.Config.Clusters[tc.ClusterID]
78 ctx := auth.NewContext(context.Background(), auth.NewCredentials(token))
79 ac, err := arvados.NewClientFromConfig(&cl)
84 arv, err := arvadosclient.New(ac)
88 kc := keepclient.New(arv)
92 // UserClients logs in as a user called "example", get the user's API token,
93 // initialize clients with the API token, set up the user and
94 // optionally activate the user. Return client structs for
95 // communicating with the cluster on behalf of the 'example' user.
96 func (tc *TestCluster) UserClients(rootctx context.Context, c *check.C, conn *rpc.Conn, authEmail string, activate bool) (context.Context, *arvados.Client, *keepclient.KeepClient, arvados.User) {
97 login, err := conn.UserSessionCreate(rootctx, rpc.UserSessionCreateOptions{
98 ReturnTo: ",https://example.com",
99 AuthInfo: rpc.UserSessionAuthInfo{
101 FirstName: "Example",
106 c.Assert(err, check.IsNil)
107 redirURL, err := url.Parse(login.RedirectLocation)
108 c.Assert(err, check.IsNil)
109 userToken := redirURL.Query().Get("api_token")
110 c.Logf("user token: %q", userToken)
111 ctx, ac, kc := tc.ClientsWithToken(userToken)
112 user, err := conn.UserGetCurrent(ctx, arvados.GetOptions{})
113 c.Assert(err, check.IsNil)
114 _, err = conn.UserSetup(rootctx, arvados.UserSetupOptions{UUID: user.UUID})
115 c.Assert(err, check.IsNil)
117 _, err = conn.UserActivate(rootctx, arvados.UserActivateOptions{UUID: user.UUID})
118 c.Assert(err, check.IsNil)
119 user, err = conn.UserGetCurrent(ctx, arvados.GetOptions{})
120 c.Assert(err, check.IsNil)
121 c.Logf("user UUID: %q", user.UUID)
123 c.Fatalf("failed to activate user -- %#v", user)
126 return ctx, ac, kc, user
129 // RootClients returns Context, arvados.Client and keepclient structs initialized
130 // to communicate with the cluster as the system root user.
131 func (tc *TestCluster) RootClients() (context.Context, *arvados.Client, *keepclient.KeepClient) {
132 return tc.ClientsWithToken(tc.Config.Clusters[tc.ClusterID].SystemRootToken)
135 // AnonymousClients returns Context, arvados.Client and keepclient structs initialized
136 // to communicate with the cluster as the anonymous user.
137 func (tc *TestCluster) AnonymousClients() (context.Context, *arvados.Client, *keepclient.KeepClient) {
138 return tc.ClientsWithToken(tc.Config.Clusters[tc.ClusterID].Users.AnonymousUserToken)
141 // Conn gets rpc connection struct initialized to communicate with the
142 // specified cluster.
143 func (tc *TestCluster) Conn() *rpc.Conn {
144 return rpc.NewConn(tc.ClusterID, tc.ControllerURL, true, rpc.PassthroughTokenProvider)