1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
15 "github.com/ghodss/yaml"
16 check "gopkg.in/check.v1"
19 // Gocheck boilerplate
20 func Test(t *testing.T) {
24 var _ = check.Suite(&LoadSuite{})
26 type LoadSuite struct{}
28 func (s *LoadSuite) TestEmpty(c *check.C) {
29 cfg, err := Load(&bytes.Buffer{}, ctxlog.TestLogger(c))
30 c.Check(cfg, check.IsNil)
31 c.Assert(err, check.ErrorMatches, `config does not define any clusters`)
34 func (s *LoadSuite) TestNoConfigs(c *check.C) {
35 cfg, err := Load(bytes.NewBufferString(`Clusters: {"z1111": {}}`), ctxlog.TestLogger(c))
36 c.Assert(err, check.IsNil)
37 c.Assert(cfg.Clusters, check.HasLen, 1)
38 cc, err := cfg.GetCluster("z1111")
39 c.Assert(err, check.IsNil)
40 c.Check(cc.ClusterID, check.Equals, "z1111")
41 c.Check(cc.API.MaxRequestAmplification, check.Equals, 4)
42 c.Check(cc.API.MaxItemsPerResponse, check.Equals, 1000)
45 func (s *LoadSuite) TestMultipleClusters(c *check.C) {
46 cfg, err := Load(bytes.NewBufferString(`{"Clusters":{"z1111":{},"z2222":{}}}`), ctxlog.TestLogger(c))
47 c.Assert(err, check.IsNil)
48 c1, err := cfg.GetCluster("z1111")
49 c.Assert(err, check.IsNil)
50 c.Check(c1.ClusterID, check.Equals, "z1111")
51 c2, err := cfg.GetCluster("z2222")
52 c.Assert(err, check.IsNil)
53 c.Check(c2.ClusterID, check.Equals, "z2222")
56 func (s *LoadSuite) TestPostgreSQLKeyConflict(c *check.C) {
57 _, err := Load(bytes.NewBufferString(`
64 `), ctxlog.TestLogger(c))
65 c.Assert(err, check.ErrorMatches, `Clusters.zzzzz.PostgreSQL.Connection: multiple keys with .*"(dbname|host)".*`)
68 func (s *LoadSuite) TestMovedKeys(c *check.C) {
69 s.checkEquivalent(c, `# config has old keys only
73 MultiClusterRequestConcurrency: 3
74 MaxItemsPerResponse: 999
79 MaxRequestAmplification: 3
80 MaxItemsPerResponse: 999
82 s.checkEquivalent(c, `# config has both old and new keys; old values win
86 MultiClusterRequestConcurrency: 0
87 MaxItemsPerResponse: 555
89 MaxRequestAmplification: 3
90 MaxItemsPerResponse: 999
95 MaxRequestAmplification: 0
96 MaxItemsPerResponse: 555
100 func (s *LoadSuite) checkEquivalent(c *check.C, goty, expectedy string) {
101 got, err := Load(bytes.NewBufferString(goty), ctxlog.TestLogger(c))
102 c.Assert(err, check.IsNil)
103 expected, err := Load(bytes.NewBufferString(expectedy), ctxlog.TestLogger(c))
104 c.Assert(err, check.IsNil)
105 if !c.Check(got, check.DeepEquals, expected) {
106 cmd := exec.Command("diff", "-u", "--label", "expected", "--label", "got", "/dev/fd/3", "/dev/fd/4")
107 for _, obj := range []interface{}{expected, got} {
108 y, _ := yaml.Marshal(obj)
109 pr, pw, err := os.Pipe()
110 c.Assert(err, check.IsNil)
113 io.Copy(pw, bytes.NewBuffer(y))
116 cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
118 diff, err := cmd.CombinedOutput()
120 c.Check(err, check.IsNil)