13647: Return recognizable error when no clusters are defined.
[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 := NewLoader(&bytes.Buffer{}, ctxlog.TestLogger(c)).Load()
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 := NewLoader(bytes.NewBufferString(`Clusters: {"z1111": {}}`), ctxlog.TestLogger(c)).Load()
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 := NewLoader(bytes.NewBufferString(yaml), ctxlog.TestLogger(c)).Load()
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 := NewLoader(bytes.NewBufferString(`{"Clusters":{"z1111":{},"z2222":{}}}`), ctxlog.TestLogger(c)).Load()
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 := NewLoader(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).Load()
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
107         loader := NewLoader(bytes.NewBuffer(DefaultYAML), logger)
108         cfg, err := loader.Load()
109         c.Assert(err, check.IsNil)
110         var loaded map[string]interface{}
111         buf, err := yaml.Marshal(cfg)
112         c.Assert(err, check.IsNil)
113         err = yaml.Unmarshal(buf, &loaded)
114         c.Assert(err, check.IsNil)
115
116         loader.logExtraKeys(loaded, supplied, "")
117         c.Check(logbuf.String(), check.Equals, "")
118 }
119
120 func (s *LoadSuite) TestNoWarningsForDumpedConfig(c *check.C) {
121         var logbuf bytes.Buffer
122         logger := logrus.New()
123         logger.Out = &logbuf
124         cfg, err := NewLoader(bytes.NewBufferString(`{"Clusters":{"zzzzz":{}}}`), logger).Load()
125         c.Assert(err, check.IsNil)
126         yaml, err := yaml.Marshal(cfg)
127         c.Assert(err, check.IsNil)
128         cfgDumped, err := NewLoader(bytes.NewBuffer(yaml), logger).Load()
129         c.Assert(err, check.IsNil)
130         c.Check(cfg, check.DeepEquals, cfgDumped)
131         c.Check(logbuf.String(), check.Equals, "")
132 }
133
134 func (s *LoadSuite) TestPostgreSQLKeyConflict(c *check.C) {
135         _, err := NewLoader(bytes.NewBufferString(`
136 Clusters:
137  zzzzz:
138   postgresql:
139    connection:
140      DBName: dbname
141      Host: host
142 `), ctxlog.TestLogger(c)).Load()
143         c.Check(err, check.ErrorMatches, `Clusters.zzzzz.PostgreSQL.Connection: multiple entries for "(dbname|host)".*`)
144 }
145
146 func (s *LoadSuite) TestBadType(c *check.C) {
147         for _, data := range []string{`
148 Clusters:
149  zzzzz:
150   PostgreSQL: true
151 `, `
152 Clusters:
153  zzzzz:
154   PostgreSQL:
155    ConnectionPool: true
156 `, `
157 Clusters:
158  zzzzz:
159   PostgreSQL:
160    ConnectionPool: "foo"
161 `, `
162 Clusters:
163  zzzzz:
164   PostgreSQL:
165    ConnectionPool: []
166 `, `
167 Clusters:
168  zzzzz:
169   PostgreSQL:
170    ConnectionPool: [] # {foo: bar} isn't caught here; we rely on config-check
171 `,
172         } {
173                 c.Log(data)
174                 v, err := NewLoader(bytes.NewBufferString(data), ctxlog.TestLogger(c)).Load()
175                 if v != nil {
176                         c.Logf("%#v", v.Clusters["zzzzz"].PostgreSQL.ConnectionPool)
177                 }
178                 c.Check(err, check.ErrorMatches, `.*cannot unmarshal .*PostgreSQL.*`)
179         }
180 }
181
182 func (s *LoadSuite) TestMovedKeys(c *check.C) {
183         s.checkEquivalent(c, `# config has old keys only
184 Clusters:
185  zzzzz:
186   RequestLimits:
187    MultiClusterRequestConcurrency: 3
188    MaxItemsPerResponse: 999
189 `, `
190 Clusters:
191  zzzzz:
192   API:
193    MaxRequestAmplification: 3
194    MaxItemsPerResponse: 999
195 `)
196         s.checkEquivalent(c, `# config has both old and new keys; old values win
197 Clusters:
198  zzzzz:
199   RequestLimits:
200    MultiClusterRequestConcurrency: 0
201    MaxItemsPerResponse: 555
202   API:
203    MaxRequestAmplification: 3
204    MaxItemsPerResponse: 999
205 `, `
206 Clusters:
207  zzzzz:
208   API:
209    MaxRequestAmplification: 0
210    MaxItemsPerResponse: 555
211 `)
212 }
213
214 func (s *LoadSuite) checkEquivalent(c *check.C, goty, expectedy string) {
215         got, err := NewLoader(bytes.NewBufferString(goty), ctxlog.TestLogger(c)).Load()
216         c.Assert(err, check.IsNil)
217         expected, err := NewLoader(bytes.NewBufferString(expectedy), ctxlog.TestLogger(c)).Load()
218         c.Assert(err, check.IsNil)
219         if !c.Check(got, check.DeepEquals, expected) {
220                 cmd := exec.Command("diff", "-u", "--label", "expected", "--label", "got", "/dev/fd/3", "/dev/fd/4")
221                 for _, obj := range []interface{}{expected, got} {
222                         y, _ := yaml.Marshal(obj)
223                         pr, pw, err := os.Pipe()
224                         c.Assert(err, check.IsNil)
225                         defer pr.Close()
226                         go func() {
227                                 io.Copy(pw, bytes.NewBuffer(y))
228                                 pw.Close()
229                         }()
230                         cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
231                 }
232                 diff, err := cmd.CombinedOutput()
233                 c.Log(string(diff))
234                 c.Check(err, check.IsNil)
235         }
236 }