1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.arvados.org/arvados.git/lib/config"
20 "git.arvados.org/arvados.git/sdk/go/arvadostest"
21 "git.arvados.org/arvados.git/sdk/go/ctxlog"
22 check "gopkg.in/check.v1"
25 var _ = check.Suite(&railsRestartSuite{})
27 type railsRestartSuite struct{}
29 // This tests RailsAPI, not controller -- but tests RailsAPI's
30 // integration with passenger, so it needs to run against the
31 // run-tests.sh environment where RailsAPI runs under passenger, not
32 // in the Rails test environment.
33 func (s *railsRestartSuite) TestConfigReload(c *check.C) {
34 hc := http.Client{Transport: &http.Transport{
35 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
38 confdata, err := os.ReadFile(os.Getenv("ARVADOS_CONFIG"))
39 c.Assert(err, check.IsNil)
40 oldhash := fmt.Sprintf("%x", sha256.Sum256(confdata))
41 c.Logf("oldhash %s", oldhash)
43 ldr := config.NewLoader(&bytes.Buffer{}, ctxlog.TestLogger(c))
44 cfg, err := ldr.Load()
45 c.Assert(err, check.IsNil)
46 cc, err := cfg.GetCluster("")
47 c.Assert(err, check.IsNil)
49 for u := range cc.Services.RailsAPI.InternalURLs {
51 mu, err := u.Parse("/metrics")
52 c.Assert(err, check.IsNil)
53 metricsURL = mu.String()
56 req, err := http.NewRequest(http.MethodGet, metricsURL, nil)
57 c.Assert(err, check.IsNil)
58 req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
60 resp, err := hc.Do(req)
61 c.Assert(err, check.IsNil)
62 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
63 body, err := ioutil.ReadAll(resp.Body)
64 c.Assert(err, check.IsNil)
65 c.Check(string(body), check.Matches, `(?ms).*`+oldhash+`.*`)
67 f, err := os.OpenFile(os.Getenv("ARVADOS_CONFIG"), os.O_WRONLY|os.O_APPEND, 0)
68 c.Assert(err, check.IsNil)
69 _, err = f.Write([]byte{'\n'})
70 c.Assert(err, check.IsNil)
72 c.Assert(err, check.IsNil)
73 newhash := fmt.Sprintf("%x", sha256.Sum256(append(confdata, '\n')))
74 c.Logf("newhash %s", newhash)
76 // Wait for RailsAPI's 1 Hz reload_config thread to poll and
78 pollstart := time.Now()
79 for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(time.Second) {
80 resp, err = hc.Do(req)
81 c.Assert(err, check.IsNil)
82 defer resp.Body.Close()
83 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
84 body, err = ioutil.ReadAll(resp.Body)
85 c.Assert(err, check.IsNil)
87 if strings.Contains(string(body), newhash) {
91 c.Logf("waited %s for rails to restart", time.Now().Sub(pollstart))
92 c.Check(string(body), check.Matches, `(?ms).*`+newhash+`.*`)