14812: Fix email defaults
[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         "strings"
13         "testing"
14
15         "git.curoverse.com/arvados.git/sdk/go/arvados"
16         "git.curoverse.com/arvados.git/sdk/go/ctxlog"
17         "github.com/ghodss/yaml"
18         "github.com/sirupsen/logrus"
19         check "gopkg.in/check.v1"
20 )
21
22 // Gocheck boilerplate
23 func Test(t *testing.T) {
24         check.TestingT(t)
25 }
26
27 var _ = check.Suite(&LoadSuite{})
28
29 type LoadSuite struct{}
30
31 func (s *LoadSuite) TestEmpty(c *check.C) {
32         cfg, err := Load(&bytes.Buffer{}, ctxlog.TestLogger(c))
33         c.Check(cfg, check.IsNil)
34         c.Assert(err, check.ErrorMatches, `config does not define any clusters`)
35 }
36
37 func (s *LoadSuite) TestNoConfigs(c *check.C) {
38         cfg, err := Load(bytes.NewBufferString(`Clusters: {"z1111": {}}`), ctxlog.TestLogger(c))
39         c.Assert(err, check.IsNil)
40         c.Assert(cfg.Clusters, check.HasLen, 1)
41         cc, err := cfg.GetCluster("z1111")
42         c.Assert(err, check.IsNil)
43         c.Check(cc.ClusterID, check.Equals, "z1111")
44         c.Check(cc.API.MaxRequestAmplification, check.Equals, 4)
45         c.Check(cc.API.MaxItemsPerResponse, check.Equals, 1000)
46 }
47
48 func (s *LoadSuite) TestSampleKeys(c *check.C) {
49         for _, yaml := range []string{
50                 `{"Clusters":{"z1111":{}}}`,
51                 `{"Clusters":{"z1111":{"InstanceTypes":{"Foo":{"RAM": "12345M"}}}}}`,
52         } {
53                 cfg, err := Load(bytes.NewBufferString(yaml), ctxlog.TestLogger(c))
54                 c.Assert(err, check.IsNil)
55                 cc, err := cfg.GetCluster("z1111")
56                 _, hasSample := cc.InstanceTypes["SAMPLE"]
57                 c.Check(hasSample, check.Equals, false)
58                 if strings.Contains(yaml, "Foo") {
59                         c.Check(cc.InstanceTypes["Foo"].RAM, check.Equals, arvados.ByteSize(12345000000))
60                         c.Check(cc.InstanceTypes["Foo"].Price, check.Equals, 0.0)
61                 }
62         }
63 }
64
65 func (s *LoadSuite) TestMultipleClusters(c *check.C) {
66         cfg, err := Load(bytes.NewBufferString(`{"Clusters":{"z1111":{},"z2222":{}}}`), ctxlog.TestLogger(c))
67         c.Assert(err, check.IsNil)
68         c1, err := cfg.GetCluster("z1111")
69         c.Assert(err, check.IsNil)
70         c.Check(c1.ClusterID, check.Equals, "z1111")
71         c2, err := cfg.GetCluster("z2222")
72         c.Assert(err, check.IsNil)
73         c.Check(c2.ClusterID, check.Equals, "z2222")
74 }
75
76 func (s *LoadSuite) TestDeprecatedOrUnknownWarning(c *check.C) {
77         var logbuf bytes.Buffer
78         logger := logrus.New()
79         logger.Out = &logbuf
80         _, err := Load(bytes.NewBufferString(`
81 Clusters:
82   zzzzz:
83     postgresql: {}
84     BadKey: {}
85     Containers: {}
86     RemoteClusters:
87       z2222:
88         Host: z2222.arvadosapi.com
89         Proxy: true
90         BadKey: badValue
91 `), logger)
92         c.Assert(err, check.IsNil)
93         logs := strings.Split(strings.TrimSuffix(logbuf.String(), "\n"), "\n")
94         for _, log := range logs {
95                 c.Check(log, check.Matches, `.*deprecated or unknown config entry:.*BadKey.*`)
96         }
97         c.Check(logs, check.HasLen, 2)
98 }
99
100 func (s *LoadSuite) TestNoUnrecognizedKeysInDefaultConfig(c *check.C) {
101         var logbuf bytes.Buffer
102         logger := logrus.New()
103         logger.Out = &logbuf
104         var supplied map[string]interface{}
105         yaml.Unmarshal(DefaultYAML, &supplied)
106         cfg, err := Load(bytes.NewBuffer(DefaultYAML), logger)
107         c.Assert(err, check.IsNil)
108         var loaded map[string]interface{}
109         buf, err := yaml.Marshal(cfg)
110         c.Assert(err, check.IsNil)
111         err = yaml.Unmarshal(buf, &loaded)
112         c.Assert(err, check.IsNil)
113
114         logExtraKeys(logger, loaded, supplied, "")
115         c.Check(logbuf.String(), check.Equals, "")
116 }
117
118 func (s *LoadSuite) TestNoWarningsForDumpedConfig(c *check.C) {
119         var logbuf bytes.Buffer
120         logger := logrus.New()
121         logger.Out = &logbuf
122         cfg, err := Load(bytes.NewBufferString(`{"Clusters":{"zzzzz":{}}}`), logger)
123         c.Assert(err, check.IsNil)
124         yaml, err := yaml.Marshal(cfg)
125         c.Assert(err, check.IsNil)
126         cfgDumped, err := Load(bytes.NewBuffer(yaml), logger)
127         c.Assert(err, check.IsNil)
128         c.Check(cfg, check.DeepEquals, cfgDumped)
129         c.Check(logbuf.String(), check.Equals, "")
130 }
131
132 func (s *LoadSuite) TestPostgreSQLKeyConflict(c *check.C) {
133         _, err := Load(bytes.NewBufferString(`
134 Clusters:
135  zzzzz:
136   postgresql:
137    connection:
138      DBName: dbname
139      Host: host
140 `), ctxlog.TestLogger(c))
141         c.Check(err, check.ErrorMatches, `Clusters.zzzzz.PostgreSQL.Connection: multiple entries for "(dbname|host)".*`)
142 }
143
144 func (s *LoadSuite) TestBadType(c *check.C) {
145         for _, data := range []string{`
146 Clusters:
147  zzzzz:
148   PostgreSQL: true
149 `, `
150 Clusters:
151  zzzzz:
152   PostgreSQL:
153    ConnectionPool: true
154 `, `
155 Clusters:
156  zzzzz:
157   PostgreSQL:
158    ConnectionPool: "foo"
159 `, `
160 Clusters:
161  zzzzz:
162   PostgreSQL:
163    ConnectionPool: []
164 `, `
165 Clusters:
166  zzzzz:
167   PostgreSQL:
168    ConnectionPool: [] # {foo: bar} isn't caught here; we rely on config-check
169 `,
170         } {
171                 c.Log(data)
172                 v, err := Load(bytes.NewBufferString(data), ctxlog.TestLogger(c))
173                 if v != nil {
174                         c.Logf("%#v", v.Clusters["zzzzz"].PostgreSQL.ConnectionPool)
175                 }
176                 c.Check(err, check.ErrorMatches, `.*cannot unmarshal .*PostgreSQL.*`)
177         }
178 }
179
180 func (s *LoadSuite) TestMovedKeys(c *check.C) {
181         s.checkEquivalent(c, `# config has old keys only
182 Clusters:
183  zzzzz:
184   RequestLimits:
185    MultiClusterRequestConcurrency: 3
186    MaxItemsPerResponse: 999
187 `, `
188 Clusters:
189  zzzzz:
190   API:
191    MaxRequestAmplification: 3
192    MaxItemsPerResponse: 999
193 `)
194         s.checkEquivalent(c, `# config has both old and new keys; old values win
195 Clusters:
196  zzzzz:
197   RequestLimits:
198    MultiClusterRequestConcurrency: 0
199    MaxItemsPerResponse: 555
200   API:
201    MaxRequestAmplification: 3
202    MaxItemsPerResponse: 999
203 `, `
204 Clusters:
205  zzzzz:
206   API:
207    MaxRequestAmplification: 0
208    MaxItemsPerResponse: 555
209 `)
210 }
211
212 func (s *LoadSuite) checkEquivalent(c *check.C, goty, expectedy string) {
213         got, err := Load(bytes.NewBufferString(goty), ctxlog.TestLogger(c))
214         c.Assert(err, check.IsNil)
215         expected, err := Load(bytes.NewBufferString(expectedy), ctxlog.TestLogger(c))
216         c.Assert(err, check.IsNil)
217         if !c.Check(got, check.DeepEquals, expected) {
218                 cmd := exec.Command("diff", "-u", "--label", "expected", "--label", "got", "/dev/fd/3", "/dev/fd/4")
219                 for _, obj := range []interface{}{expected, got} {
220                         y, _ := yaml.Marshal(obj)
221                         pr, pw, err := os.Pipe()
222                         c.Assert(err, check.IsNil)
223                         defer pr.Close()
224                         go func() {
225                                 io.Copy(pw, bytes.NewBuffer(y))
226                                 pw.Close()
227                         }()
228                         cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
229                 }
230                 diff, err := cmd.CombinedOutput()
231                 c.Log(string(diff))
232                 c.Check(err, check.IsNil)
233         }
234 }