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