1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/sdk/go/arvados"
16 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
17 "github.com/ghodss/yaml"
18 "github.com/sirupsen/logrus"
19 check "gopkg.in/check.v1"
22 // Gocheck boilerplate
23 func Test(t *testing.T) {
27 var _ = check.Suite(&LoadSuite{})
29 type LoadSuite struct{}
31 func (s *LoadSuite) TestEmpty(c *check.C) {
32 cfg, err := NewLoader(&bytes.Buffer{}, ctxlog.TestLogger(c)).Load()
33 c.Check(cfg, check.IsNil)
34 c.Assert(err, check.ErrorMatches, `config does not define any clusters`)
37 func (s *LoadSuite) TestNoConfigs(c *check.C) {
38 cfg, err := NewLoader(bytes.NewBufferString(`Clusters: {"z1111": {}}`), ctxlog.TestLogger(c)).Load()
39 c.Assert(err, check.IsNil)
40 c.Assert(cfg.Clusters, check.HasLen, 1)
41 cc, err := cfg.GetCluster("z1111")
42 c.Assert(err, check.IsNil)
43 c.Check(cc.ClusterID, check.Equals, "z1111")
44 c.Check(cc.API.MaxRequestAmplification, check.Equals, 4)
45 c.Check(cc.API.MaxItemsPerResponse, check.Equals, 1000)
48 func (s *LoadSuite) TestSampleKeys(c *check.C) {
49 for _, yaml := range []string{
50 `{"Clusters":{"z1111":{}}}`,
51 `{"Clusters":{"z1111":{"InstanceTypes":{"Foo":{"RAM": "12345M"}}}}}`,
53 cfg, err := NewLoader(bytes.NewBufferString(yaml), ctxlog.TestLogger(c)).Load()
54 c.Assert(err, check.IsNil)
55 cc, err := cfg.GetCluster("z1111")
56 _, hasSample := cc.InstanceTypes["SAMPLE"]
57 c.Check(hasSample, check.Equals, false)
58 if strings.Contains(yaml, "Foo") {
59 c.Check(cc.InstanceTypes["Foo"].RAM, check.Equals, arvados.ByteSize(12345000000))
60 c.Check(cc.InstanceTypes["Foo"].Price, check.Equals, 0.0)
65 func (s *LoadSuite) TestMultipleClusters(c *check.C) {
66 cfg, err := NewLoader(bytes.NewBufferString(`{"Clusters":{"z1111":{},"z2222":{}}}`), ctxlog.TestLogger(c)).Load()
67 c.Assert(err, check.IsNil)
68 c1, err := cfg.GetCluster("z1111")
69 c.Assert(err, check.IsNil)
70 c.Check(c1.ClusterID, check.Equals, "z1111")
71 c2, err := cfg.GetCluster("z2222")
72 c.Assert(err, check.IsNil)
73 c.Check(c2.ClusterID, check.Equals, "z2222")
76 func (s *LoadSuite) TestDeprecatedOrUnknownWarning(c *check.C) {
77 var logbuf bytes.Buffer
78 logger := logrus.New()
80 _, err := NewLoader(bytes.NewBufferString(`
88 Host: z2222.arvadosapi.com
92 c.Assert(err, check.IsNil)
93 logs := strings.Split(strings.TrimSuffix(logbuf.String(), "\n"), "\n")
94 for _, log := range logs {
95 c.Check(log, check.Matches, `.*deprecated or unknown config entry:.*BadKey.*`)
97 c.Check(logs, check.HasLen, 2)
100 func (s *LoadSuite) TestNoUnrecognizedKeysInDefaultConfig(c *check.C) {
101 var logbuf bytes.Buffer
102 logger := logrus.New()
104 var supplied map[string]interface{}
105 yaml.Unmarshal(DefaultYAML, &supplied)
107 loader := NewLoader(bytes.NewBuffer(DefaultYAML), logger)
108 cfg, err := loader.Load()
109 c.Assert(err, check.IsNil)
110 var loaded map[string]interface{}
111 buf, err := yaml.Marshal(cfg)
112 c.Assert(err, check.IsNil)
113 err = yaml.Unmarshal(buf, &loaded)
114 c.Assert(err, check.IsNil)
116 loader.logExtraKeys(loaded, supplied, "")
117 c.Check(logbuf.String(), check.Equals, "")
120 func (s *LoadSuite) TestNoWarningsForDumpedConfig(c *check.C) {
121 var logbuf bytes.Buffer
122 logger := logrus.New()
124 cfg, err := NewLoader(bytes.NewBufferString(`{"Clusters":{"zzzzz":{}}}`), logger).Load()
125 c.Assert(err, check.IsNil)
126 yaml, err := yaml.Marshal(cfg)
127 c.Assert(err, check.IsNil)
128 cfgDumped, err := NewLoader(bytes.NewBuffer(yaml), logger).Load()
129 c.Assert(err, check.IsNil)
130 c.Check(cfg, check.DeepEquals, cfgDumped)
131 c.Check(logbuf.String(), check.Equals, "")
134 func (s *LoadSuite) TestPostgreSQLKeyConflict(c *check.C) {
135 _, err := NewLoader(bytes.NewBufferString(`
142 `), ctxlog.TestLogger(c)).Load()
143 c.Check(err, check.ErrorMatches, `Clusters.zzzzz.PostgreSQL.Connection: multiple entries for "(dbname|host)".*`)
146 func (s *LoadSuite) TestBadType(c *check.C) {
147 for _, data := range []string{`
160 ConnectionPool: "foo"
170 ConnectionPool: [] # {foo: bar} isn't caught here; we rely on config-check
174 v, err := NewLoader(bytes.NewBufferString(data), ctxlog.TestLogger(c)).Load()
176 c.Logf("%#v", v.Clusters["zzzzz"].PostgreSQL.ConnectionPool)
178 c.Check(err, check.ErrorMatches, `.*cannot unmarshal .*PostgreSQL.*`)
182 func (s *LoadSuite) TestMovedKeys(c *check.C) {
183 s.checkEquivalent(c, `# config has old keys only
187 MultiClusterRequestConcurrency: 3
188 MaxItemsPerResponse: 999
193 MaxRequestAmplification: 3
194 MaxItemsPerResponse: 999
196 s.checkEquivalent(c, `# config has both old and new keys; old values win
200 MultiClusterRequestConcurrency: 0
201 MaxItemsPerResponse: 555
203 MaxRequestAmplification: 3
204 MaxItemsPerResponse: 999
209 MaxRequestAmplification: 0
210 MaxItemsPerResponse: 555
214 func (s *LoadSuite) checkEquivalent(c *check.C, goty, expectedy string) {
215 got, err := NewLoader(bytes.NewBufferString(goty), ctxlog.TestLogger(c)).Load()
216 c.Assert(err, check.IsNil)
217 expected, err := NewLoader(bytes.NewBufferString(expectedy), ctxlog.TestLogger(c)).Load()
218 c.Assert(err, check.IsNil)
219 if !c.Check(got, check.DeepEquals, expected) {
220 cmd := exec.Command("diff", "-u", "--label", "expected", "--label", "got", "/dev/fd/3", "/dev/fd/4")
221 for _, obj := range []interface{}{expected, got} {
222 y, _ := yaml.Marshal(obj)
223 pr, pw, err := os.Pipe()
224 c.Assert(err, check.IsNil)
227 io.Copy(pw, bytes.NewBuffer(y))
230 cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
232 diff, err := cmd.CombinedOutput()
234 c.Check(err, check.IsNil)