X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/3e9b9dcdcce3905fa33dde900ef99f27ba036fea..ef9107221d53f19bf848d3dca0b570f468519550:/lib/config/load_test.go diff --git a/lib/config/load_test.go b/lib/config/load_test.go index a06ad282ca..2e521ab093 100644 --- a/lib/config/load_test.go +++ b/lib/config/load_test.go @@ -46,6 +46,12 @@ func testLoader(c *check.C, configdata string, logdst io.Writer) *Loader { type LoadSuite struct{} +func (s *LoadSuite) SetUpSuite(c *check.C) { + os.Unsetenv("ARVADOS_API_HOST") + os.Unsetenv("ARVADOS_API_HOST_INSECURE") + os.Unsetenv("ARVADOS_API_TOKEN") +} + func (s *LoadSuite) TestEmpty(c *check.C) { cfg, err := testLoader(c, "", nil).Load() c.Check(cfg, check.IsNil) @@ -321,7 +327,7 @@ Clusters: } func (s *LoadSuite) TestMovedKeys(c *check.C) { - s.checkEquivalent(c, `# config has old keys only + checkEquivalent(c, `# config has old keys only Clusters: zzzzz: RequestLimits: @@ -334,7 +340,7 @@ Clusters: MaxRequestAmplification: 3 MaxItemsPerResponse: 999 `) - s.checkEquivalent(c, `# config has both old and new keys; old values win + checkEquivalent(c, `# config has both old and new keys; old values win Clusters: zzzzz: RequestLimits: @@ -352,31 +358,46 @@ Clusters: `) } -func (s *LoadSuite) checkEquivalent(c *check.C, goty, expectedy string) { - got, err := testLoader(c, goty, nil).Load() +func checkEquivalent(c *check.C, goty, expectedy string) { + gotldr := testLoader(c, goty, nil) + expectedldr := testLoader(c, expectedy, nil) + checkEquivalentLoaders(c, gotldr, expectedldr) +} + +func checkEqualYAML(c *check.C, got, expected interface{}) { + expectedyaml, err := yaml.Marshal(expected) c.Assert(err, check.IsNil) - expected, err := testLoader(c, expectedy, nil).Load() + gotyaml, err := yaml.Marshal(got) c.Assert(err, check.IsNil) - if !c.Check(got, check.DeepEquals, expected) { + if !bytes.Equal(gotyaml, expectedyaml) { cmd := exec.Command("diff", "-u", "--label", "expected", "--label", "got", "/dev/fd/3", "/dev/fd/4") - for _, obj := range []interface{}{expected, got} { - y, _ := yaml.Marshal(obj) + for _, y := range [][]byte{expectedyaml, gotyaml} { pr, pw, err := os.Pipe() c.Assert(err, check.IsNil) defer pr.Close() - go func() { - io.Copy(pw, bytes.NewBuffer(y)) + go func(data []byte) { + pw.Write(data) pw.Close() - }() + }(y) cmd.ExtraFiles = append(cmd.ExtraFiles, pr) } diff, err := cmd.CombinedOutput() + // diff should report differences and exit non-zero. + c.Check(err, check.NotNil) c.Log(string(diff)) - c.Check(err, check.IsNil) + c.Error("got != expected; see diff (-expected +got) above") } } -func (s *LoadSuite) checkListKeys(c *check.C, path string, x interface{}) { +func checkEquivalentLoaders(c *check.C, gotldr, expectedldr *Loader) { + got, err := gotldr.Load() + c.Assert(err, check.IsNil) + expected, err := expectedldr.Load() + c.Assert(err, check.IsNil) + checkEqualYAML(c, got, expected) +} + +func checkListKeys(path string, x interface{}) (err error) { v := reflect.Indirect(reflect.ValueOf(x)) switch v.Kind() { case reflect.Map: @@ -384,7 +405,9 @@ func (s *LoadSuite) checkListKeys(c *check.C, path string, x interface{}) { for iter.Next() { k := iter.Key() if k.Kind() == reflect.String { - s.checkListKeys(c, path+"."+k.String(), iter.Value().Interface()) + if err = checkListKeys(path+"."+k.String(), iter.Value().Interface()); err != nil { + return + } } } return @@ -392,28 +415,61 @@ func (s *LoadSuite) checkListKeys(c *check.C, path string, x interface{}) { case reflect.Struct: for i := 0; i < v.NumField(); i++ { val := v.Field(i) - fieldname := v.Type().Field(i).Name + structField := v.Type().Field(i) + fieldname := structField.Name endsWithList := strings.HasSuffix(fieldname, "List") - isAnArray := v.Type().Field(i).Type.Kind() == reflect.Slice + isAnArray := structField.Type.Kind() == reflect.Slice if endsWithList != isAnArray { if endsWithList { - c.Errorf("%s.%s ends with 'List' but field is not an array (type %v)", path, fieldname, val.Kind()) + err = fmt.Errorf("%s.%s ends with 'List' but field is not an array (type %v)", path, fieldname, val.Kind()) + return } - if isAnArray && v.Type().Field(i).Type.Elem().Kind() != reflect.Uint8 { - c.Errorf("%s.%s is an array but field name does not end in 'List' (slice of %v)", path, fieldname, v.Type().Field(i).Type.Elem().Kind()) + if isAnArray && structField.Type.Elem().Kind() != reflect.Uint8 { + 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()) + return } } if val.CanInterface() { - s.checkListKeys(c, path+"."+v.Type().Field(i).Name, val.Interface()) + checkListKeys(path+"."+fieldname, val.Interface()) } } } + return } func (s *LoadSuite) TestListKeys(c *check.C) { + v1 := struct { + EndInList []string + }{[]string{"a", "b"}} + var m1 = make(map[string]interface{}) + m1["c"] = &v1 + if err := checkListKeys("", m1); err != nil { + c.Error(err) + } + + v2 := struct { + DoesNot []string + }{[]string{"a", "b"}} + var m2 = make(map[string]interface{}) + m2["c"] = &v2 + if err := checkListKeys("", m2); err == nil { + c.Errorf("Should have produced an error") + } + + v3 := struct { + EndInList string + }{"a"} + var m3 = make(map[string]interface{}) + m3["c"] = &v3 + if err := checkListKeys("", m3); err == nil { + c.Errorf("Should have produced an error") + } + var logbuf bytes.Buffer loader := testLoader(c, string(DefaultYAML), &logbuf) cfg, err := loader.Load() c.Assert(err, check.IsNil) - s.checkListKeys(c, "", cfg) + if err := checkListKeys("", cfg); err != nil { + c.Error(err) + } }