Merge branch '15003-preprocess-config'
[arvados.git] / lib / config / load_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package config
6
7 import (
8         "bytes"
9         "io"
10         "os"
11         "os/exec"
12         "testing"
13
14         "git.curoverse.com/arvados.git/sdk/go/ctxlog"
15         "github.com/ghodss/yaml"
16         check "gopkg.in/check.v1"
17 )
18
19 // Gocheck boilerplate
20 func Test(t *testing.T) {
21         check.TestingT(t)
22 }
23
24 var _ = check.Suite(&LoadSuite{})
25
26 type LoadSuite struct{}
27
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`)
32 }
33
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)
43 }
44
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")
54 }
55
56 func (s *LoadSuite) TestPostgreSQLKeyConflict(c *check.C) {
57         _, err := Load(bytes.NewBufferString(`
58 Clusters:
59  zzzzz:
60   postgresql:
61    connection:
62      dbname: dbname
63      host: host
64 `), ctxlog.TestLogger(c))
65         c.Check(err, check.ErrorMatches, `Clusters.zzzzz.PostgreSQL.Connection: multiple entries for "(dbname|host)".*`)
66 }
67
68 func (s *LoadSuite) TestBadType(c *check.C) {
69         for _, data := range []string{`
70 Clusters:
71  zzzzz:
72   PostgreSQL: true
73 `, `
74 Clusters:
75  zzzzz:
76   PostgreSQL:
77    ConnectionPool: true
78 `, `
79 Clusters:
80  zzzzz:
81   PostgreSQL:
82    ConnectionPool: "foo"
83 `, `
84 Clusters:
85  zzzzz:
86   PostgreSQL:
87    ConnectionPool: []
88 `, `
89 Clusters:
90  zzzzz:
91   PostgreSQL:
92    ConnectionPool: [] # {foo: bar} isn't caught here; we rely on config-check
93 `,
94         } {
95                 c.Log(data)
96                 v, err := Load(bytes.NewBufferString(data), ctxlog.TestLogger(c))
97                 if v != nil {
98                         c.Logf("%#v", v.Clusters["zzzzz"].PostgreSQL.ConnectionPool)
99                 }
100                 c.Check(err, check.ErrorMatches, `.*cannot unmarshal .*PostgreSQL.*`)
101         }
102 }
103
104 func (s *LoadSuite) TestMovedKeys(c *check.C) {
105         s.checkEquivalent(c, `# config has old keys only
106 Clusters:
107  zzzzz:
108   RequestLimits:
109    MultiClusterRequestConcurrency: 3
110    MaxItemsPerResponse: 999
111 `, `
112 Clusters:
113  zzzzz:
114   API:
115    MaxRequestAmplification: 3
116    MaxItemsPerResponse: 999
117 `)
118         s.checkEquivalent(c, `# config has both old and new keys; old values win
119 Clusters:
120  zzzzz:
121   RequestLimits:
122    MultiClusterRequestConcurrency: 0
123    MaxItemsPerResponse: 555
124   API:
125    MaxRequestAmplification: 3
126    MaxItemsPerResponse: 999
127 `, `
128 Clusters:
129  zzzzz:
130   API:
131    MaxRequestAmplification: 0
132    MaxItemsPerResponse: 555
133 `)
134 }
135
136 func (s *LoadSuite) checkEquivalent(c *check.C, goty, expectedy string) {
137         got, err := Load(bytes.NewBufferString(goty), ctxlog.TestLogger(c))
138         c.Assert(err, check.IsNil)
139         expected, err := Load(bytes.NewBufferString(expectedy), ctxlog.TestLogger(c))
140         c.Assert(err, check.IsNil)
141         if !c.Check(got, check.DeepEquals, expected) {
142                 cmd := exec.Command("diff", "-u", "--label", "expected", "--label", "got", "/dev/fd/3", "/dev/fd/4")
143                 for _, obj := range []interface{}{expected, got} {
144                         y, _ := yaml.Marshal(obj)
145                         pr, pw, err := os.Pipe()
146                         c.Assert(err, check.IsNil)
147                         defer pr.Close()
148                         go func() {
149                                 io.Copy(pw, bytes.NewBuffer(y))
150                                 pw.Close()
151                         }()
152                         cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
153                 }
154                 diff, err := cmd.CombinedOutput()
155                 c.Log(string(diff))
156                 c.Check(err, check.IsNil)
157         }
158 }