1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
18 "git.curoverse.com/arvados.git/sdk/go/arvados"
19 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
20 "github.com/ghodss/yaml"
21 "github.com/sirupsen/logrus"
22 check "gopkg.in/check.v1"
25 // Gocheck boilerplate
26 func Test(t *testing.T) {
30 var _ = check.Suite(&LoadSuite{})
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)
42 ldr := NewLoader(bytes.NewBufferString(configdata), logger)
47 type LoadSuite struct{}
49 func (s *LoadSuite) TestEmpty(c *check.C) {
50 cfg, err := testLoader(c, "", nil).Load()
51 c.Check(cfg, check.IsNil)
52 c.Assert(err, check.ErrorMatches, `config does not define any clusters`)
55 func (s *LoadSuite) TestNoConfigs(c *check.C) {
56 cfg, err := testLoader(c, `Clusters: {"z1111": {}}`, nil).Load()
57 c.Assert(err, check.IsNil)
58 c.Assert(cfg.Clusters, check.HasLen, 1)
59 cc, err := cfg.GetCluster("z1111")
60 c.Assert(err, check.IsNil)
61 c.Check(cc.ClusterID, check.Equals, "z1111")
62 c.Check(cc.API.MaxRequestAmplification, check.Equals, 4)
63 c.Check(cc.API.MaxItemsPerResponse, check.Equals, 1000)
66 func (s *LoadSuite) TestMungeLegacyConfigArgs(c *check.C) {
67 f, err := ioutil.TempFile("", "")
68 c.Check(err, check.IsNil)
69 defer os.Remove(f.Name())
70 io.WriteString(f, "Debug: true\n")
73 f, err = ioutil.TempFile("", "")
74 c.Check(err, check.IsNil)
75 defer os.Remove(f.Name())
76 io.WriteString(f, "Clusters: {aaaaa: {}}\n")
79 for _, trial := range []struct {
84 []string{"-config", oldfile},
85 []string{"-old-config", oldfile},
88 []string{"-config=" + oldfile},
89 []string{"-old-config=" + oldfile},
92 []string{"-config", newfile},
93 []string{"-config", newfile},
96 []string{"-config=" + newfile},
97 []string{"-config=" + newfile},
100 []string{"-foo", oldfile},
101 []string{"-foo", oldfile},
104 []string{"-foo=" + oldfile},
105 []string{"-foo=" + oldfile},
108 []string{"-foo", "-config=" + oldfile},
109 []string{"-foo", "-old-config=" + oldfile},
112 []string{"-foo", "bar", "-config=" + oldfile},
113 []string{"-foo", "bar", "-old-config=" + oldfile},
116 []string{"-foo=bar", "baz", "-config=" + oldfile},
117 []string{"-foo=bar", "baz", "-old-config=" + oldfile},
120 []string{"-config=/dev/null"},
121 []string{"-config=/dev/null"},
124 []string{"-config=-"},
125 []string{"-config=-"},
128 []string{"-config="},
129 []string{"-config="},
132 []string{"-foo=bar", "baz", "-config"},
133 []string{"-foo=bar", "baz", "-config"},
140 var logbuf bytes.Buffer
141 logger := logrus.New()
145 args := ldr.MungeLegacyConfigArgs(logger, trial.argsIn, "-old-config")
146 c.Check(args, check.DeepEquals, trial.argsOut)
147 if fmt.Sprintf("%v", trial.argsIn) != fmt.Sprintf("%v", trial.argsOut) {
148 c.Check(logbuf.String(), check.Matches, `.*`+oldfile+` is not a cluster config file -- interpreting -config as -old-config.*\n`)
153 func (s *LoadSuite) TestSampleKeys(c *check.C) {
154 for _, yaml := range []string{
155 `{"Clusters":{"z1111":{}}}`,
156 `{"Clusters":{"z1111":{"InstanceTypes":{"Foo":{"RAM": "12345M"}}}}}`,
158 cfg, err := testLoader(c, yaml, nil).Load()
159 c.Assert(err, check.IsNil)
160 cc, err := cfg.GetCluster("z1111")
161 _, hasSample := cc.InstanceTypes["SAMPLE"]
162 c.Check(hasSample, check.Equals, false)
163 if strings.Contains(yaml, "Foo") {
164 c.Check(cc.InstanceTypes["Foo"].RAM, check.Equals, arvados.ByteSize(12345000000))
165 c.Check(cc.InstanceTypes["Foo"].Price, check.Equals, 0.0)
170 func (s *LoadSuite) TestMultipleClusters(c *check.C) {
171 ldr := testLoader(c, `{"Clusters":{"z1111":{},"z2222":{}}}`, nil)
172 ldr.SkipDeprecated = true
173 cfg, err := ldr.Load()
174 c.Assert(err, check.IsNil)
175 c1, err := cfg.GetCluster("z1111")
176 c.Assert(err, check.IsNil)
177 c.Check(c1.ClusterID, check.Equals, "z1111")
178 c2, err := cfg.GetCluster("z2222")
179 c.Assert(err, check.IsNil)
180 c.Check(c2.ClusterID, check.Equals, "z2222")
183 func (s *LoadSuite) TestDeprecatedOrUnknownWarning(c *check.C) {
184 var logbuf bytes.Buffer
185 _, err := testLoader(c, `
193 Host: z2222.arvadosapi.com
197 c.Assert(err, check.IsNil)
198 logs := strings.Split(strings.TrimSuffix(logbuf.String(), "\n"), "\n")
199 for _, log := range logs {
200 c.Check(log, check.Matches, `.*deprecated or unknown config entry:.*BadKey.*`)
202 c.Check(logs, check.HasLen, 2)
205 func (s *LoadSuite) checkSAMPLEKeys(c *check.C, path string, x interface{}) {
206 v := reflect.Indirect(reflect.ValueOf(x))
209 var stringKeys, sampleKey bool
213 if k.Kind() == reflect.String {
215 if k.String() == "SAMPLE" || k.String() == "xxxxx" {
217 s.checkSAMPLEKeys(c, path+"."+k.String(), iter.Value().Interface())
221 if stringKeys && !sampleKey {
222 c.Errorf("%s is a map with string keys (type %T) but config.default.yml has no SAMPLE key", path, x)
226 for i := 0; i < v.NumField(); i++ {
228 if val.CanInterface() {
229 s.checkSAMPLEKeys(c, path+"."+v.Type().Field(i).Name, val.Interface())
235 func (s *LoadSuite) TestDefaultConfigHasAllSAMPLEKeys(c *check.C) {
236 var logbuf bytes.Buffer
237 loader := testLoader(c, string(DefaultYAML), &logbuf)
238 cfg, err := loader.Load()
239 c.Assert(err, check.IsNil)
240 s.checkSAMPLEKeys(c, "", cfg)
243 func (s *LoadSuite) TestNoUnrecognizedKeysInDefaultConfig(c *check.C) {
244 var logbuf bytes.Buffer
245 var supplied map[string]interface{}
246 yaml.Unmarshal(DefaultYAML, &supplied)
248 loader := testLoader(c, string(DefaultYAML), &logbuf)
249 cfg, err := loader.Load()
250 c.Assert(err, check.IsNil)
251 var loaded map[string]interface{}
252 buf, err := yaml.Marshal(cfg)
253 c.Assert(err, check.IsNil)
254 err = yaml.Unmarshal(buf, &loaded)
255 c.Assert(err, check.IsNil)
257 loader.logExtraKeys(loaded, supplied, "")
258 c.Check(logbuf.String(), check.Equals, "")
261 func (s *LoadSuite) TestNoWarningsForDumpedConfig(c *check.C) {
262 var logbuf bytes.Buffer
263 logger := logrus.New()
265 cfg, err := testLoader(c, `{"Clusters":{"zzzzz":{}}}`, &logbuf).Load()
266 c.Assert(err, check.IsNil)
267 yaml, err := yaml.Marshal(cfg)
268 c.Assert(err, check.IsNil)
269 cfgDumped, err := testLoader(c, string(yaml), &logbuf).Load()
270 c.Assert(err, check.IsNil)
271 c.Check(cfg, check.DeepEquals, cfgDumped)
272 c.Check(logbuf.String(), check.Equals, "")
275 func (s *LoadSuite) TestPostgreSQLKeyConflict(c *check.C) {
276 _, err := testLoader(c, `
284 c.Check(err, check.ErrorMatches, `Clusters.zzzzz.PostgreSQL.Connection: multiple entries for "(dbname|host)".*`)
287 func (s *LoadSuite) TestBadType(c *check.C) {
288 for _, data := range []string{`
301 ConnectionPool: "foo"
311 ConnectionPool: [] # {foo: bar} isn't caught here; we rely on config-check
315 v, err := testLoader(c, data, nil).Load()
317 c.Logf("%#v", v.Clusters["zzzzz"].PostgreSQL.ConnectionPool)
319 c.Check(err, check.ErrorMatches, `.*cannot unmarshal .*PostgreSQL.*`)
323 func (s *LoadSuite) TestMovedKeys(c *check.C) {
324 s.checkEquivalent(c, `# config has old keys only
328 MultiClusterRequestConcurrency: 3
329 MaxItemsPerResponse: 999
334 MaxRequestAmplification: 3
335 MaxItemsPerResponse: 999
337 s.checkEquivalent(c, `# config has both old and new keys; old values win
341 MultiClusterRequestConcurrency: 0
342 MaxItemsPerResponse: 555
344 MaxRequestAmplification: 3
345 MaxItemsPerResponse: 999
350 MaxRequestAmplification: 0
351 MaxItemsPerResponse: 555
355 func (s *LoadSuite) checkEquivalent(c *check.C, goty, expectedy string) {
356 got, err := testLoader(c, goty, nil).Load()
357 c.Assert(err, check.IsNil)
358 expected, err := testLoader(c, expectedy, nil).Load()
359 c.Assert(err, check.IsNil)
360 if !c.Check(got, check.DeepEquals, expected) {
361 cmd := exec.Command("diff", "-u", "--label", "expected", "--label", "got", "/dev/fd/3", "/dev/fd/4")
362 for _, obj := range []interface{}{expected, got} {
363 y, _ := yaml.Marshal(obj)
364 pr, pw, err := os.Pipe()
365 c.Assert(err, check.IsNil)
368 io.Copy(pw, bytes.NewBuffer(y))
371 cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
373 diff, err := cmd.CombinedOutput()
375 c.Check(err, check.IsNil)
379 func checkListKeys(path string, x interface{}) (err error) {
380 v := reflect.Indirect(reflect.ValueOf(x))
386 if k.Kind() == reflect.String {
387 if err = checkListKeys(path+"."+k.String(), iter.Value().Interface()); err != nil {
395 for i := 0; i < v.NumField(); i++ {
397 structField := v.Type().Field(i)
398 fieldname := structField.Name
399 endsWithList := strings.HasSuffix(fieldname, "List")
400 isAnArray := structField.Type.Kind() == reflect.Slice
401 if endsWithList != isAnArray {
403 err = fmt.Errorf("%s.%s ends with 'List' but field is not an array (type %v)", path, fieldname, val.Kind())
406 if isAnArray && structField.Type.Elem().Kind() != reflect.Uint8 {
407 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())
411 if val.CanInterface() {
412 checkListKeys(path+"."+fieldname, val.Interface())
419 func (s *LoadSuite) TestListKeys(c *check.C) {
422 }{[]string{"a", "b"}}
423 var m1 = make(map[string]interface{})
425 if err := checkListKeys("", m1); err != nil {
431 }{[]string{"a", "b"}}
432 var m2 = make(map[string]interface{})
434 if err := checkListKeys("", m2); err == nil {
435 c.Errorf("Should have produced an error")
441 var m3 = make(map[string]interface{})
443 if err := checkListKeys("", m3); err == nil {
444 c.Errorf("Should have produced an error")
447 var logbuf bytes.Buffer
448 loader := testLoader(c, string(DefaultYAML), &logbuf)
449 cfg, err := loader.Load()
450 c.Assert(err, check.IsNil)
451 if err := checkListKeys("", cfg); err != nil {