1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "git.arvados.org/arvados.git/lib/boot"
13 "git.arvados.org/arvados.git/lib/config"
14 "git.arvados.org/arvados.git/lib/controller/rpc"
15 "git.arvados.org/arvados.git/lib/service"
16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
18 "git.arvados.org/arvados.git/sdk/go/auth"
19 "git.arvados.org/arvados.git/sdk/go/ctxlog"
20 "git.arvados.org/arvados.git/sdk/go/keepclient"
24 // TestCluster stores a working test cluster data
25 type TestCluster struct {
28 ControllerURL *url.URL
33 loggerfunc func(...interface{})
36 func (l logger) Log(args ...interface{}) {
40 // NewTestCluster loads the provided configuration, and sets up a test cluster
41 // ready for being started.
42 func NewTestCluster(srcPath string, clusterID, yamlConf, listenHost string, logWriter func(...interface{})) (*TestCluster, error) {
43 loader := config.NewLoader(bytes.NewBufferString(yamlConf), ctxlog.TestLogger(logger{logWriter}))
45 loader.SkipLegacy = true
46 loader.SkipAPICalls = true
47 cfg, err := loader.Load()
52 Super: boot.Supervisor{
55 ListenHost: listenHost,
57 OwnTemporaryDatabase: true,
58 Stderr: &service.LogPrefixer{
59 Writer: ctxlog.LogWriter(logWriter),
60 Prefix: []byte("[" + clusterID + "] ")},
67 // Start the test cluster.
68 func (tc *TestCluster) Start() {
69 tc.Super.Start(context.Background(), &tc.Config, "-")
72 // WaitReady waits for all components to report healthy, and finishes setting
73 // up the TestCluster struct.
74 func (tc *TestCluster) WaitReady() bool {
75 au, ok := tc.Super.WaitReady()
84 // ClientsWithToken returns Context, Arvados.Client and keepclient structs
85 // initialized to connect to the cluster with the supplied Arvados token.
86 func (tc *TestCluster) ClientsWithToken(token string) (context.Context, *arvados.Client, *keepclient.KeepClient) {
87 cl := tc.Config.Clusters[tc.ClusterID]
88 ctx := auth.NewContext(context.Background(), auth.NewCredentials(token))
89 ac, err := arvados.NewClientFromConfig(&cl)
94 arv, err := arvadosclient.New(ac)
98 kc := keepclient.New(arv)
102 // UserClients logs in as a user called "example", get the user's API token,
103 // initialize clients with the API token, set up the user and
104 // optionally activate the user. Return client structs for
105 // communicating with the cluster on behalf of the 'example' user.
106 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) {
107 login, err := conn.UserSessionCreate(rootctx, rpc.UserSessionCreateOptions{
108 ReturnTo: ",https://example.com",
109 AuthInfo: rpc.UserSessionAuthInfo{
111 FirstName: "Example",
116 c.Assert(err, check.IsNil)
117 redirURL, err := url.Parse(login.RedirectLocation)
118 c.Assert(err, check.IsNil)
119 userToken := redirURL.Query().Get("api_token")
120 c.Logf("user token: %q", userToken)
121 ctx, ac, kc := tc.ClientsWithToken(userToken)
122 user, err := conn.UserGetCurrent(ctx, arvados.GetOptions{})
123 c.Assert(err, check.IsNil)
124 _, err = conn.UserSetup(rootctx, arvados.UserSetupOptions{UUID: user.UUID})
125 c.Assert(err, check.IsNil)
127 _, err = conn.UserActivate(rootctx, arvados.UserActivateOptions{UUID: user.UUID})
128 c.Assert(err, check.IsNil)
129 user, err = conn.UserGetCurrent(ctx, arvados.GetOptions{})
130 c.Assert(err, check.IsNil)
131 c.Logf("user UUID: %q", user.UUID)
133 c.Fatalf("failed to activate user -- %#v", user)
136 return ctx, ac, kc, user
139 // RootClients returns Context, arvados.Client and keepclient structs initialized
140 // to communicate with the cluster as the system root user.
141 func (tc *TestCluster) RootClients() (context.Context, *arvados.Client, *keepclient.KeepClient) {
142 return tc.ClientsWithToken(tc.Config.Clusters[tc.ClusterID].SystemRootToken)
145 // AnonymousClients returns Context, arvados.Client and keepclient structs initialized
146 // to communicate with the cluster as the anonymous user.
147 func (tc *TestCluster) AnonymousClients() (context.Context, *arvados.Client, *keepclient.KeepClient) {
148 return tc.ClientsWithToken(tc.Config.Clusters[tc.ClusterID].Users.AnonymousUserToken)
151 // Conn gets rpc connection struct initialized to communicate with the
152 // specified cluster.
153 func (tc *TestCluster) Conn() *rpc.Conn {
154 return rpc.NewConn(tc.ClusterID, tc.ControllerURL, true, rpc.PassthroughTokenProvider)