1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/lib/config"
16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 "git.arvados.org/arvados.git/sdk/go/arvadostest"
18 "git.arvados.org/arvados.git/sdk/go/ctxlog"
19 check "gopkg.in/check.v1"
22 var _ = check.Suite(&serverSuite{})
24 type serverSuite struct {
25 cluster *arvados.Cluster
30 func (s *serverSuite) SetUpTest(c *check.C) {
32 s.cluster, err = s.testConfig(c)
33 c.Assert(err, check.IsNil)
34 s.srv = &server{cluster: s.cluster}
37 func (*serverSuite) testConfig(c *check.C) (*arvados.Cluster, error) {
38 ldr := config.NewLoader(nil, ctxlog.TestLogger(c))
39 cfg, err := ldr.Load()
43 cluster, err := cfg.GetCluster("")
47 client := arvados.NewClientFromEnv()
48 cluster.Services.Controller.ExternalURL.Host = client.APIHost
49 cluster.SystemRootToken = client.AuthToken
50 cluster.TLS.Insecure = client.Insecure
51 cluster.PostgreSQL.Connection = testDBConfig()
52 cluster.Services.Websocket.InternalURLs = map[arvados.URL]arvados.ServiceInstance{arvados.URL{Host: ":"}: arvados.ServiceInstance{}}
53 cluster.ManagementToken = arvadostest.ManagementToken
57 // TestBadDB ensures Run() returns an error (instead of panicking or
58 // deadlocking) if it can't connect to the database server at startup.
59 func (s *serverSuite) TestBadDB(c *check.C) {
60 s.cluster.PostgreSQL.Connection["password"] = "1234"
66 c.Check(err, check.NotNil)
75 done := make(chan bool)
82 case <-time.After(10 * time.Second):
87 func (s *serverSuite) TestHealth(c *check.C) {
91 for _, token := range []string{"", "foo", s.cluster.ManagementToken} {
92 req, err := http.NewRequest("GET", "http://"+s.srv.listener.Addr().String()+"/_health/ping", nil)
93 c.Assert(err, check.IsNil)
95 req.Header.Add("Authorization", "Bearer "+token)
97 resp, err := http.DefaultClient.Do(req)
98 c.Check(err, check.IsNil)
99 if token == s.cluster.ManagementToken {
100 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
101 buf, err := ioutil.ReadAll(resp.Body)
102 c.Check(err, check.IsNil)
103 c.Check(string(buf), check.Equals, `{"health":"OK"}`+"\n")
105 c.Check(resp.StatusCode, check.Not(check.Equals), http.StatusOK)
110 func (s *serverSuite) TestStatus(c *check.C) {
114 req, err := http.NewRequest("GET", "http://"+s.srv.listener.Addr().String()+"/status.json", nil)
115 c.Assert(err, check.IsNil)
116 resp, err := http.DefaultClient.Do(req)
117 c.Check(err, check.IsNil)
118 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
119 var status map[string]interface{}
120 err = json.NewDecoder(resp.Body).Decode(&status)
121 c.Check(err, check.IsNil)
122 c.Check(status["Version"], check.Not(check.Equals), "")
125 func (s *serverSuite) TestHealthDisabled(c *check.C) {
126 s.cluster.ManagementToken = ""
132 for _, token := range []string{"", "foo", arvadostest.ManagementToken} {
133 req, err := http.NewRequest("GET", "http://"+s.srv.listener.Addr().String()+"/_health/ping", nil)
134 c.Assert(err, check.IsNil)
135 req.Header.Add("Authorization", "Bearer "+token)
136 resp, err := http.DefaultClient.Do(req)
137 c.Check(err, check.IsNil)
138 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
142 func (s *serverSuite) TestLoadLegacyConfig(c *check.C) {
148 "dbname": "arvados_production"
152 "connect_timeout": "30"
154 "fallback_application_name": "arvados-ws"
162 ManagementToken: qqqqq
164 tmpfile, err := ioutil.TempFile("", "example")
169 defer os.Remove(tmpfile.Name()) // clean up
171 if _, err := tmpfile.Write(content); err != nil {
174 if err := tmpfile.Close(); err != nil {
178 cluster := configure(logger(nil), []string{"arvados-ws", "-config", tmpfile.Name()})
179 c.Check(cluster, check.NotNil)
181 c.Check(cluster.Services.Controller.ExternalURL, check.Equals, arvados.URL{Scheme: "https", Host: "example.com"})
182 c.Check(cluster.SystemRootToken, check.Equals, "abcdefg")
184 c.Check(cluster.PostgreSQL.Connection, check.DeepEquals, arvados.PostgreSQLConnection{
185 "connect_timeout": "30",
186 "dbname": "arvados_production",
187 "fallback_application_name": "arvados-ws",
190 "sslmode": "require",
192 c.Check(cluster.PostgreSQL.ConnectionPool, check.Equals, 63)
193 c.Check(cluster.Services.Websocket.InternalURLs[arvados.URL{Host: ":8765"}], check.NotNil)
194 c.Check(cluster.SystemLogs.LogLevel, check.Equals, "debug")
195 c.Check(cluster.SystemLogs.Format, check.Equals, "text")
196 c.Check(cluster.API.SendTimeout, check.Equals, arvados.Duration(61*time.Second))
197 c.Check(cluster.API.WebsocketClientEventQueue, check.Equals, 62)
198 c.Check(cluster.API.WebsocketServerEventQueue, check.Equals, 5)
199 c.Check(cluster.ManagementToken, check.Equals, "qqqqq")