12278: Bump schema-salad and cwltool for SourceLine fix.
[arvados.git] / services / ws / server_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "io/ioutil"
9         "net/http"
10         "sync"
11         "time"
12
13         "git.curoverse.com/arvados.git/sdk/go/arvados"
14         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15         check "gopkg.in/check.v1"
16 )
17
18 var _ = check.Suite(&serverSuite{})
19
20 type serverSuite struct {
21         cfg *wsConfig
22         srv *server
23         wg  sync.WaitGroup
24 }
25
26 func (s *serverSuite) SetUpTest(c *check.C) {
27         s.cfg = s.testConfig()
28         s.srv = &server{wsConfig: s.cfg}
29 }
30
31 func (*serverSuite) testConfig() *wsConfig {
32         cfg := defaultConfig()
33         cfg.Client = *(arvados.NewClientFromEnv())
34         cfg.Postgres = testDBConfig()
35         cfg.Listen = ":"
36         cfg.ManagementToken = arvadostest.ManagementToken
37         return &cfg
38 }
39
40 // TestBadDB ensures Run() returns an error (instead of panicking or
41 // deadlocking) if it can't connect to the database server at startup.
42 func (s *serverSuite) TestBadDB(c *check.C) {
43         s.cfg.Postgres["password"] = "1234"
44
45         var wg sync.WaitGroup
46         wg.Add(1)
47         go func() {
48                 err := s.srv.Run()
49                 c.Check(err, check.NotNil)
50                 wg.Done()
51         }()
52         wg.Add(1)
53         go func() {
54                 s.srv.WaitReady()
55                 wg.Done()
56         }()
57
58         done := make(chan bool)
59         go func() {
60                 wg.Wait()
61                 close(done)
62         }()
63         select {
64         case <-done:
65         case <-time.After(10 * time.Second):
66                 c.Fatal("timeout")
67         }
68 }
69
70 func (s *serverSuite) TestHealth(c *check.C) {
71         go s.srv.Run()
72         defer s.srv.Close()
73         s.srv.WaitReady()
74         for _, token := range []string{"", "foo", s.cfg.ManagementToken} {
75                 req, err := http.NewRequest("GET", "http://"+s.srv.listener.Addr().String()+"/_health/ping", nil)
76                 c.Assert(err, check.IsNil)
77                 if token != "" {
78                         req.Header.Add("Authorization", "Bearer "+token)
79                 }
80                 resp, err := http.DefaultClient.Do(req)
81                 c.Check(err, check.IsNil)
82                 if token == s.cfg.ManagementToken {
83                         c.Check(resp.StatusCode, check.Equals, http.StatusOK)
84                         buf, err := ioutil.ReadAll(resp.Body)
85                         c.Check(err, check.IsNil)
86                         c.Check(string(buf), check.Equals, `{"health":"OK"}`+"\n")
87                 } else {
88                         c.Check(resp.StatusCode, check.Not(check.Equals), http.StatusOK)
89                 }
90         }
91 }
92
93 func (s *serverSuite) TestHealthDisabled(c *check.C) {
94         s.cfg.ManagementToken = ""
95
96         go s.srv.Run()
97         defer s.srv.Close()
98         s.srv.WaitReady()
99
100         for _, token := range []string{"", "foo", arvadostest.ManagementToken} {
101                 req, err := http.NewRequest("GET", "http://"+s.srv.listener.Addr().String()+"/_health/ping", nil)
102                 c.Assert(err, check.IsNil)
103                 req.Header.Add("Authorization", "Bearer "+token)
104                 resp, err := http.DefaultClient.Do(req)
105                 c.Check(err, check.IsNil)
106                 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
107         }
108 }