Merge branch '17705-adc-arch-doc'
[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         "fmt"
10         "io"
11         "io/ioutil"
12         "os"
13         "os/exec"
14         "reflect"
15         "strings"
16         "testing"
17
18         "git.arvados.org/arvados.git/sdk/go/arvados"
19         "git.arvados.org/arvados.git/sdk/go/ctxlog"
20         "github.com/ghodss/yaml"
21         "github.com/sirupsen/logrus"
22         check "gopkg.in/check.v1"
23 )
24
25 // Gocheck boilerplate
26 func Test(t *testing.T) {
27         check.TestingT(t)
28 }
29
30 var _ = check.Suite(&LoadSuite{})
31
32 // Return a new Loader that reads cluster config from configdata
33 // (instead of the usual default /etc/arvados/config.yml), and logs to
34 // logdst or (if that's nil) c.Log.
35 func testLoader(c *check.C, configdata string, logdst io.Writer) *Loader {
36         logger := ctxlog.TestLogger(c)
37         if logdst != nil {
38                 lgr := logrus.New()
39                 lgr.Out = logdst
40                 logger = lgr
41         }
42         ldr := NewLoader(bytes.NewBufferString(configdata), logger)
43         ldr.Path = "-"
44         return ldr
45 }
46
47 type LoadSuite struct{}
48
49 func (s *LoadSuite) SetUpSuite(c *check.C) {
50         os.Unsetenv("ARVADOS_API_HOST")
51         os.Unsetenv("ARVADOS_API_HOST_INSECURE")
52         os.Unsetenv("ARVADOS_API_TOKEN")
53 }
54
55 func (s *LoadSuite) TestEmpty(c *check.C) {
56         cfg, err := testLoader(c, "", nil).Load()
57         c.Check(cfg, check.IsNil)
58         c.Assert(err, check.ErrorMatches, `config does not define any clusters`)
59 }
60
61 func (s *LoadSuite) TestNoConfigs(c *check.C) {
62         cfg, err := testLoader(c, `Clusters: {"z1111": {}}`, nil).Load()
63         c.Assert(err, check.IsNil)
64         c.Assert(cfg.Clusters, check.HasLen, 1)
65         cc, err := cfg.GetCluster("z1111")
66         c.Assert(err, check.IsNil)
67         c.Check(cc.ClusterID, check.Equals, "z1111")
68         c.Check(cc.API.MaxRequestAmplification, check.Equals, 4)
69         c.Check(cc.API.MaxItemsPerResponse, check.Equals, 1000)
70 }
71
72 func (s *LoadSuite) TestMungeLegacyConfigArgs(c *check.C) {
73         f, err := ioutil.TempFile("", "")
74         c.Check(err, check.IsNil)
75         defer os.Remove(f.Name())
76         io.WriteString(f, "Debug: true\n")
77         oldfile := f.Name()
78
79         f, err = ioutil.TempFile("", "")
80         c.Check(err, check.IsNil)
81         defer os.Remove(f.Name())
82         io.WriteString(f, "Clusters: {aaaaa: {}}\n")
83         newfile := f.Name()
84
85         for _, trial := range []struct {
86                 argsIn  []string
87                 argsOut []string
88         }{
89                 {
90                         []string{"-config", oldfile},
91                         []string{"-old-config", oldfile},
92                 },
93                 {
94                         []string{"-config=" + oldfile},
95                         []string{"-old-config=" + oldfile},
96                 },
97                 {
98                         []string{"-config", newfile},
99                         []string{"-config", newfile},
100                 },
101                 {
102                         []string{"-config=" + newfile},
103                         []string{"-config=" + newfile},
104                 },
105                 {
106                         []string{"-foo", oldfile},
107                         []string{"-foo", oldfile},
108                 },
109                 {
110                         []string{"-foo=" + oldfile},
111                         []string{"-foo=" + oldfile},
112                 },
113                 {
114                         []string{"-foo", "-config=" + oldfile},
115                         []string{"-foo", "-old-config=" + oldfile},
116                 },
117                 {
118                         []string{"-foo", "bar", "-config=" + oldfile},
119                         []string{"-foo", "bar", "-old-config=" + oldfile},
120                 },
121                 {
122                         []string{"-foo=bar", "baz", "-config=" + oldfile},
123                         []string{"-foo=bar", "baz", "-old-config=" + oldfile},
124                 },
125                 {
126                         []string{"-config=/dev/null"},
127                         []string{"-config=/dev/null"},
128                 },
129                 {
130                         []string{"-config=-"},
131                         []string{"-config=-"},
132                 },
133                 {
134                         []string{"-config="},
135                         []string{"-config="},
136                 },
137                 {
138                         []string{"-foo=bar", "baz", "-config"},
139                         []string{"-foo=bar", "baz", "-config"},
140                 },
141                 {
142                         []string{},
143                         nil,
144                 },
145         } {
146                 var logbuf bytes.Buffer
147                 logger := logrus.New()
148                 logger.Out = &logbuf
149
150                 var ldr Loader
151                 args := ldr.MungeLegacyConfigArgs(logger, trial.argsIn, "-old-config")
152                 c.Check(args, check.DeepEquals, trial.argsOut)
153                 if fmt.Sprintf("%v", trial.argsIn) != fmt.Sprintf("%v", trial.argsOut) {
154                         c.Check(logbuf.String(), check.Matches, `.*`+oldfile+` is not a cluster config file -- interpreting -config as -old-config.*\n`)
155                 }
156         }
157 }
158
159 func (s *LoadSuite) TestSampleKeys(c *check.C) {
160         for _, yaml := range []string{
161                 `{"Clusters":{"z1111":{}}}`,
162                 `{"Clusters":{"z1111":{"InstanceTypes":{"Foo":{"RAM": "12345M"}}}}}`,
163         } {
164                 cfg, err := testLoader(c, yaml, nil).Load()
165                 c.Assert(err, check.IsNil)
166                 cc, err := cfg.GetCluster("z1111")
167                 c.Assert(err, check.IsNil)
168                 _, hasSample := cc.InstanceTypes["SAMPLE"]
169                 c.Check(hasSample, check.Equals, false)
170                 if strings.Contains(yaml, "Foo") {
171                         c.Check(cc.InstanceTypes["Foo"].RAM, check.Equals, arvados.ByteSize(12345000000))
172                         c.Check(cc.InstanceTypes["Foo"].Price, check.Equals, 0.0)
173                 }
174         }
175 }
176
177 func (s *LoadSuite) TestMultipleClusters(c *check.C) {
178         ldr := testLoader(c, `{"Clusters":{"z1111":{},"z2222":{}}}`, nil)
179         ldr.SkipDeprecated = true
180         cfg, err := ldr.Load()
181         c.Assert(err, check.IsNil)
182         c1, err := cfg.GetCluster("z1111")
183         c.Assert(err, check.IsNil)
184         c.Check(c1.ClusterID, check.Equals, "z1111")
185         c2, err := cfg.GetCluster("z2222")
186         c.Assert(err, check.IsNil)
187         c.Check(c2.ClusterID, check.Equals, "z2222")
188 }
189
190 func (s *LoadSuite) TestDeprecatedOrUnknownWarning(c *check.C) {
191         var logbuf bytes.Buffer
192         _, err := testLoader(c, `
193 Clusters:
194   zzzzz:
195     ManagementToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
196     SystemRootToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
197     Collections:
198      BlobSigningKey: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
199     PostgreSQL: {}
200     BadKey1: {}
201     Containers:
202       RunTimeEngine: abc
203     RemoteClusters:
204       z2222:
205         Host: z2222.arvadosapi.com
206         Proxy: true
207         BadKey2: badValue
208     Services:
209       KeepStore:
210         InternalURLs:
211           "http://host.example:12345": {}
212       Keepstore:
213         InternalURLs:
214           "http://host.example:12345":
215             RendezVous: x
216     ServiceS:
217       Keepstore:
218         InternalURLs:
219           "http://host.example:12345": {}
220     Volumes:
221       zzzzz-nyw5e-aaaaaaaaaaaaaaa: {}
222 `, &logbuf).Load()
223         c.Assert(err, check.IsNil)
224         c.Log(logbuf.String())
225         logs := strings.Split(strings.TrimSuffix(logbuf.String(), "\n"), "\n")
226         for _, log := range logs {
227                 c.Check(log, check.Matches, `.*deprecated or unknown config entry:.*(RunTimeEngine.*RuntimeEngine|BadKey1|BadKey2|KeepStore|ServiceS|RendezVous).*`)
228         }
229         c.Check(logs, check.HasLen, 6)
230 }
231
232 func (s *LoadSuite) checkSAMPLEKeys(c *check.C, path string, x interface{}) {
233         v := reflect.Indirect(reflect.ValueOf(x))
234         switch v.Kind() {
235         case reflect.Map:
236                 var stringKeys, sampleKey bool
237                 iter := v.MapRange()
238                 for iter.Next() {
239                         k := iter.Key()
240                         if k.Kind() == reflect.String {
241                                 stringKeys = true
242                                 if k.String() == "SAMPLE" || k.String() == "xxxxx" {
243                                         sampleKey = true
244                                         s.checkSAMPLEKeys(c, path+"."+k.String(), iter.Value().Interface())
245                                 }
246                         }
247                 }
248                 if stringKeys && !sampleKey {
249                         c.Errorf("%s is a map with string keys (type %T) but config.default.yml has no SAMPLE key", path, x)
250                 }
251                 return
252         case reflect.Struct:
253                 for i := 0; i < v.NumField(); i++ {
254                         val := v.Field(i)
255                         if val.CanInterface() {
256                                 s.checkSAMPLEKeys(c, path+"."+v.Type().Field(i).Name, val.Interface())
257                         }
258                 }
259         }
260 }
261
262 func (s *LoadSuite) TestDefaultConfigHasAllSAMPLEKeys(c *check.C) {
263         var logbuf bytes.Buffer
264         loader := testLoader(c, string(DefaultYAML), &logbuf)
265         cfg, err := loader.Load()
266         c.Assert(err, check.IsNil)
267         s.checkSAMPLEKeys(c, "", cfg)
268 }
269
270 func (s *LoadSuite) TestNoUnrecognizedKeysInDefaultConfig(c *check.C) {
271         var logbuf bytes.Buffer
272         var supplied map[string]interface{}
273         yaml.Unmarshal(DefaultYAML, &supplied)
274
275         loader := testLoader(c, string(DefaultYAML), &logbuf)
276         cfg, err := loader.Load()
277         c.Assert(err, check.IsNil)
278         var loaded map[string]interface{}
279         buf, err := yaml.Marshal(cfg)
280         c.Assert(err, check.IsNil)
281         err = yaml.Unmarshal(buf, &loaded)
282         c.Assert(err, check.IsNil)
283
284         c.Check(logbuf.String(), check.Matches, `(?ms).*SystemRootToken: secret token is not set.*`)
285         c.Check(logbuf.String(), check.Matches, `(?ms).*ManagementToken: secret token is not set.*`)
286         c.Check(logbuf.String(), check.Matches, `(?ms).*Collections.BlobSigningKey: secret token is not set.*`)
287         logbuf.Reset()
288         loader.logExtraKeys(loaded, supplied, "")
289         c.Check(logbuf.String(), check.Equals, "")
290 }
291
292 func (s *LoadSuite) TestNoWarningsForDumpedConfig(c *check.C) {
293         var logbuf bytes.Buffer
294         logger := logrus.New()
295         logger.Out = &logbuf
296         cfg, err := testLoader(c, `
297 Clusters:
298  zzzzz:
299   ManagementToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
300   SystemRootToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
301   Collections:
302    BlobSigningKey: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`, &logbuf).Load()
303         c.Assert(err, check.IsNil)
304         yaml, err := yaml.Marshal(cfg)
305         c.Assert(err, check.IsNil)
306         cfgDumped, err := testLoader(c, string(yaml), &logbuf).Load()
307         c.Assert(err, check.IsNil)
308         c.Check(cfg, check.DeepEquals, cfgDumped)
309         c.Check(logbuf.String(), check.Equals, "")
310 }
311
312 func (s *LoadSuite) TestUnacceptableTokens(c *check.C) {
313         for _, trial := range []struct {
314                 short      bool
315                 configPath string
316                 example    string
317         }{
318                 {false, "SystemRootToken", "SystemRootToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_b_c"},
319                 {false, "ManagementToken", "ManagementToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b c"},
320                 {false, "ManagementToken", "ManagementToken: \"$aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc\""},
321                 {false, "Collections.BlobSigningKey", "Collections: {BlobSigningKey: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa⛵\"}"},
322                 {true, "SystemRootToken", "SystemRootToken: a_b_c"},
323                 {true, "ManagementToken", "ManagementToken: a b c"},
324                 {true, "ManagementToken", "ManagementToken: \"$abc\""},
325                 {true, "Collections.BlobSigningKey", "Collections: {BlobSigningKey: \"⛵\"}"},
326         } {
327                 c.Logf("trying bogus config: %s", trial.example)
328                 _, err := testLoader(c, "Clusters:\n zzzzz:\n  "+trial.example, nil).Load()
329                 if trial.short {
330                         c.Check(err, check.ErrorMatches, `Clusters.zzzzz.`+trial.configPath+`: unacceptable characters in token.*`)
331                 } else {
332                         c.Check(err, check.ErrorMatches, `Clusters.zzzzz.`+trial.configPath+`: unacceptable characters in token.*`)
333                 }
334         }
335 }
336
337 func (s *LoadSuite) TestPostgreSQLKeyConflict(c *check.C) {
338         _, err := testLoader(c, `
339 Clusters:
340  zzzzz:
341   PostgreSQL:
342    Connection:
343      DBName: dbname
344      Host: host
345 `, nil).Load()
346         c.Check(err, check.ErrorMatches, `Clusters.zzzzz.PostgreSQL.Connection: multiple entries for "(dbname|host)".*`)
347 }
348
349 func (s *LoadSuite) TestBadClusterIDs(c *check.C) {
350         for _, data := range []string{`
351 Clusters:
352  123456:
353   ManagementToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
354   SystemRootToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
355   Collections:
356    BlobSigningKey: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
357 `, `
358 Clusters:
359  12345:
360   ManagementToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
361   SystemRootToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
362   Collections:
363    BlobSigningKey: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
364   RemoteClusters:
365    Zzzzz:
366     Host: Zzzzz.arvadosapi.com
367     Proxy: true
368 `, `
369 Clusters:
370  abcde:
371   ManagementToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
372   SystemRootToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
373   Collections:
374    BlobSigningKey: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
375   Login:
376    LoginCluster: zz-zz
377 `,
378         } {
379                 c.Log(data)
380                 v, err := testLoader(c, data, nil).Load()
381                 if v != nil {
382                         c.Logf("%#v", v.Clusters)
383                 }
384                 c.Check(err, check.ErrorMatches, `.*cluster ID should be 5 alphanumeric characters.*`)
385         }
386 }
387
388 func (s *LoadSuite) TestBadType(c *check.C) {
389         for _, data := range []string{`
390 Clusters:
391  zzzzz:
392   PostgreSQL: true
393 `, `
394 Clusters:
395  zzzzz:
396   PostgreSQL:
397    ConnectionPool: true
398 `, `
399 Clusters:
400  zzzzz:
401   PostgreSQL:
402    ConnectionPool: "foo"
403 `, `
404 Clusters:
405  zzzzz:
406   PostgreSQL:
407    ConnectionPool: []
408 `, `
409 Clusters:
410  zzzzz:
411   PostgreSQL:
412    ConnectionPool: [] # {foo: bar} isn't caught here; we rely on config-check
413 `,
414         } {
415                 c.Log(data)
416                 v, err := testLoader(c, data, nil).Load()
417                 if v != nil {
418                         c.Logf("%#v", v.Clusters["zzzzz"].PostgreSQL.ConnectionPool)
419                 }
420                 c.Check(err, check.ErrorMatches, `.*cannot unmarshal .*PostgreSQL.*`)
421         }
422 }
423
424 func (s *LoadSuite) TestMovedKeys(c *check.C) {
425         checkEquivalent(c, `# config has old keys only
426 Clusters:
427  zzzzz:
428   RequestLimits:
429    MultiClusterRequestConcurrency: 3
430    MaxItemsPerResponse: 999
431 `, `
432 Clusters:
433  zzzzz:
434   API:
435    MaxRequestAmplification: 3
436    MaxItemsPerResponse: 999
437 `)
438         checkEquivalent(c, `# config has both old and new keys; old values win
439 Clusters:
440  zzzzz:
441   RequestLimits:
442    MultiClusterRequestConcurrency: 0
443    MaxItemsPerResponse: 555
444   API:
445    MaxRequestAmplification: 3
446    MaxItemsPerResponse: 999
447 `, `
448 Clusters:
449  zzzzz:
450   API:
451    MaxRequestAmplification: 0
452    MaxItemsPerResponse: 555
453 `)
454 }
455
456 func checkEquivalent(c *check.C, goty, expectedy string) string {
457         var logbuf bytes.Buffer
458         gotldr := testLoader(c, goty, &logbuf)
459         expectedldr := testLoader(c, expectedy, nil)
460         checkEquivalentLoaders(c, gotldr, expectedldr)
461         return logbuf.String()
462 }
463
464 func checkEqualYAML(c *check.C, got, expected interface{}) {
465         expectedyaml, err := yaml.Marshal(expected)
466         c.Assert(err, check.IsNil)
467         gotyaml, err := yaml.Marshal(got)
468         c.Assert(err, check.IsNil)
469         if !bytes.Equal(gotyaml, expectedyaml) {
470                 cmd := exec.Command("diff", "-u", "--label", "expected", "--label", "got", "/dev/fd/3", "/dev/fd/4")
471                 for _, y := range [][]byte{expectedyaml, gotyaml} {
472                         pr, pw, err := os.Pipe()
473                         c.Assert(err, check.IsNil)
474                         defer pr.Close()
475                         go func(data []byte) {
476                                 pw.Write(data)
477                                 pw.Close()
478                         }(y)
479                         cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
480                 }
481                 diff, err := cmd.CombinedOutput()
482                 // diff should report differences and exit non-zero.
483                 c.Check(err, check.NotNil)
484                 c.Log(string(diff))
485                 c.Error("got != expected; see diff (-expected +got) above")
486         }
487 }
488
489 func checkEquivalentLoaders(c *check.C, gotldr, expectedldr *Loader) {
490         got, err := gotldr.Load()
491         c.Assert(err, check.IsNil)
492         expected, err := expectedldr.Load()
493         c.Assert(err, check.IsNil)
494         checkEqualYAML(c, got, expected)
495 }
496
497 func checkListKeys(path string, x interface{}) (err error) {
498         v := reflect.Indirect(reflect.ValueOf(x))
499         switch v.Kind() {
500         case reflect.Map:
501                 iter := v.MapRange()
502                 for iter.Next() {
503                         k := iter.Key()
504                         if k.Kind() == reflect.String {
505                                 if err = checkListKeys(path+"."+k.String(), iter.Value().Interface()); err != nil {
506                                         return
507                                 }
508                         }
509                 }
510                 return
511
512         case reflect.Struct:
513                 for i := 0; i < v.NumField(); i++ {
514                         val := v.Field(i)
515                         structField := v.Type().Field(i)
516                         fieldname := structField.Name
517                         endsWithList := strings.HasSuffix(fieldname, "List")
518                         isAnArray := structField.Type.Kind() == reflect.Slice
519                         if endsWithList != isAnArray {
520                                 if endsWithList {
521                                         err = fmt.Errorf("%s.%s ends with 'List' but field is not an array (type %v)", path, fieldname, val.Kind())
522                                         return
523                                 }
524                                 if isAnArray && structField.Type.Elem().Kind() != reflect.Uint8 {
525                                         err = fmt.Errorf("%s.%s is an array but field name does not end in 'List' (slice of %v)", path, fieldname, structField.Type.Elem().Kind())
526                                         return
527                                 }
528                         }
529                         if val.CanInterface() {
530                                 checkListKeys(path+"."+fieldname, val.Interface())
531                         }
532                 }
533         }
534         return
535 }
536
537 func (s *LoadSuite) TestListKeys(c *check.C) {
538         v1 := struct {
539                 EndInList []string
540         }{[]string{"a", "b"}}
541         var m1 = make(map[string]interface{})
542         m1["c"] = &v1
543         if err := checkListKeys("", m1); err != nil {
544                 c.Error(err)
545         }
546
547         v2 := struct {
548                 DoesNot []string
549         }{[]string{"a", "b"}}
550         var m2 = make(map[string]interface{})
551         m2["c"] = &v2
552         if err := checkListKeys("", m2); err == nil {
553                 c.Errorf("Should have produced an error")
554         }
555
556         v3 := struct {
557                 EndInList string
558         }{"a"}
559         var m3 = make(map[string]interface{})
560         m3["c"] = &v3
561         if err := checkListKeys("", m3); err == nil {
562                 c.Errorf("Should have produced an error")
563         }
564
565         var logbuf bytes.Buffer
566         loader := testLoader(c, string(DefaultYAML), &logbuf)
567         cfg, err := loader.Load()
568         c.Assert(err, check.IsNil)
569         if err := checkListKeys("", cfg); err != nil {
570                 c.Error(err)
571         }
572 }