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