1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 check "gopkg.in/check.v1"
17 var _ = check.Suite(&mainSuite{})
19 type mainSuite struct{}
21 func (s *mainSuite) TestVersionFlag(c *check.C) {
22 var stdout, stderr bytes.Buffer
23 runCommand("keep-balance", []string{"-version"}, nil, &stdout, &stderr)
24 c.Check(stderr.String(), check.Equals, "")
25 c.Log(stdout.String())
28 func (s *mainSuite) TestHTTPServer(c *check.C) {
29 ln, err := net.Listen("tcp", ":0")
33 _, p, err := net.SplitHostPort(ln.Addr().String())
34 c.Check(err, check.IsNil)
36 config := "Clusters:\n zzzzz:\n ManagementToken: abcdefg\n Services: {Keepbalance: {InternalURLs: {'http://localhost:" + p + "/': {}}}}\n"
38 var stdout bytes.Buffer
39 go runCommand("keep-balance", []string{"-config", "-"}, bytes.NewBufferString(config), &stdout, &stdout)
40 done := make(chan struct{})
44 time.Sleep(time.Second / 10)
45 req, err := http.NewRequest(http.MethodGet, "http://:"+p+"/metrics", nil)
50 req.Header.Set("Authorization", "Bearer abcdefg")
51 resp, err := http.DefaultClient.Do(req)
53 c.Logf("error %s", err)
56 defer resp.Body.Close()
57 if resp.StatusCode != http.StatusOK {
58 c.Logf("http status %d", resp.StatusCode)
61 buf, err := ioutil.ReadAll(resp.Body)
63 c.Logf("read body: %s", err)
66 c.Check(string(buf), check.Matches, `(?ms).*arvados_keepbalance_sweep_seconds_sum.*`)
72 case <-time.After(time.Second):
73 c.Log(stdout.String())
77 // Check non-metrics URL that gets passed through to us from
79 req, err := http.NewRequest(http.MethodGet, "http://:"+p+"/not-metrics", nil)
80 c.Assert(err, check.IsNil)
81 resp, err := http.DefaultClient.Do(req)
82 c.Check(err, check.IsNil)
83 defer resp.Body.Close()
84 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)