13993: Don't export internal types
[arvados.git] / lib / controller / server_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         "net/http"
10         "os"
11         "path/filepath"
12
13         "git.curoverse.com/arvados.git/sdk/go/arvados"
14         "git.curoverse.com/arvados.git/sdk/go/httpserver"
15         "github.com/Sirupsen/logrus"
16         check "gopkg.in/check.v1"
17 )
18
19 // logWriter is an io.Writer that writes by calling a "write log"
20 // function, typically (*check.C)Log().
21 type logWriter struct {
22         logfunc func(...interface{})
23 }
24
25 func (tl *logWriter) Write(buf []byte) (int, error) {
26         tl.logfunc(string(bytes.TrimRight(buf, "\n")))
27         return len(buf), nil
28 }
29
30 func integrationTestCluster() *arvados.Cluster {
31         cfg, err := arvados.GetConfig(filepath.Join(os.Getenv("WORKSPACE"), "tmp", "arvados.yml"))
32         if err != nil {
33                 panic(err)
34         }
35         cc, err := cfg.GetCluster("zzzzz")
36         if err != nil {
37                 panic(err)
38         }
39         return cc
40 }
41
42 // Return a new unstarted controller server, using the Rails API
43 // provided by the integration-testing environment.
44 func newServerFromIntegrationTestEnv(c *check.C) *httpserver.Server {
45         log := logrus.New()
46         log.Formatter = &logrus.JSONFormatter{}
47         log.Out = &logWriter{c.Log}
48
49         nodeProfile := arvados.NodeProfile{
50                 Controller: arvados.SystemServiceInstance{Listen: ":"},
51                 RailsAPI:   arvados.SystemServiceInstance{Listen: os.Getenv("ARVADOS_TEST_API_HOST"), TLS: true, Insecure: true},
52         }
53         handler := &Handler{Cluster: &arvados.Cluster{
54                 ClusterID:  "zzzzz",
55                 PostgreSQL: integrationTestCluster().PostgreSQL,
56                 NodeProfiles: map[string]arvados.NodeProfile{
57                         "*": nodeProfile,
58                 },
59         }, NodeProfile: &nodeProfile}
60
61         srv := &httpserver.Server{
62                 Server: http.Server{
63                         Handler: httpserver.AddRequestIDs(httpserver.LogRequests(log, handler)),
64                 },
65                 Addr: nodeProfile.Controller.Listen,
66         }
67         return srv
68 }