15003: Load config over defaults.
[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         "io"
10         "os"
11         "os/exec"
12         "testing"
13
14         "git.curoverse.com/arvados.git/sdk/go/ctxlog"
15         "github.com/ghodss/yaml"
16         check "gopkg.in/check.v1"
17 )
18
19 // Gocheck boilerplate
20 func Test(t *testing.T) {
21         check.TestingT(t)
22 }
23
24 var _ = check.Suite(&LoadSuite{})
25
26 type LoadSuite struct{}
27
28 func (s *LoadSuite) TestEmpty(c *check.C) {
29         cfg, err := Load(&bytes.Buffer{}, ctxlog.TestLogger(c))
30         c.Check(cfg, check.IsNil)
31         c.Assert(err, check.ErrorMatches, `config does not define any clusters`)
32 }
33
34 func (s *LoadSuite) TestNoConfigs(c *check.C) {
35         cfg, err := Load(bytes.NewBufferString(`Clusters: {"z1111": {}}`), ctxlog.TestLogger(c))
36         c.Assert(err, check.IsNil)
37         c.Assert(cfg.Clusters, check.HasLen, 1)
38         cc, err := cfg.GetCluster("z1111")
39         c.Assert(err, check.IsNil)
40         c.Check(cc.ClusterID, check.Equals, "z1111")
41 }
42
43 func (s *LoadSuite) TestMultipleClusters(c *check.C) {
44         cfg, err := Load(bytes.NewBufferString(`{"Clusters":{"z1111":{},"z2222":{}}}`), ctxlog.TestLogger(c))
45         c.Assert(err, check.IsNil)
46         c1, err := cfg.GetCluster("z1111")
47         c.Assert(err, check.IsNil)
48         c.Check(c1.ClusterID, check.Equals, "z1111")
49         c2, err := cfg.GetCluster("z2222")
50         c.Assert(err, check.IsNil)
51         c.Check(c2.ClusterID, check.Equals, "z2222")
52 }
53
54 func (s *LoadSuite) TestNodeProfilesToServices(c *check.C) {
55         hostname, err := os.Hostname()
56         c.Assert(err, check.IsNil)
57         s.checkEquivalent(c, `
58 Clusters:
59  z1111:
60   NodeProfiles:
61    "*":
62     arvados-dispatch-cloud:
63      listen: ":9006"
64     arvados-controller:
65      listen: ":9004"
66    `+hostname+`:
67     arvados-api-server:
68      listen: ":8000"
69 `, `
70 Clusters:
71  z1111:
72   Services:
73    RailsAPI:
74     InternalURLs:
75      "http://`+hostname+`:8000": {}
76    Controller:
77     InternalURLs:
78      "http://`+hostname+`:9004": {}
79    DispatchCloud:
80     InternalURLs:
81      "http://`+hostname+`:9006": {}
82   NodeProfiles:
83    "*":
84     arvados-dispatch-cloud:
85      listen: ":9006"
86     arvados-controller:
87      listen: ":9004"
88    `+hostname+`:
89     arvados-api-server:
90      listen: ":8000"
91 `)
92 }
93
94 func (s *LoadSuite) checkEquivalent(c *check.C, goty, expectedy string) {
95         got, err := Load(bytes.NewBufferString(goty), ctxlog.TestLogger(c))
96         c.Assert(err, check.IsNil)
97         expected, err := Load(bytes.NewBufferString(expectedy), ctxlog.TestLogger(c))
98         c.Assert(err, check.IsNil)
99         if !c.Check(got, check.DeepEquals, expected) {
100                 cmd := exec.Command("diff", "-u", "--label", "got", "--label", "expected", "/dev/fd/3", "/dev/fd/4")
101                 for _, obj := range []interface{}{got, expected} {
102                         y, _ := yaml.Marshal(obj)
103                         pr, pw, err := os.Pipe()
104                         c.Assert(err, check.IsNil)
105                         defer pr.Close()
106                         go func() {
107                                 io.Copy(pw, bytes.NewBuffer(y))
108                                 pw.Close()
109                         }()
110                         cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
111                 }
112                 diff, err := cmd.CombinedOutput()
113                 c.Log(string(diff))
114                 c.Check(err, check.IsNil)
115         }
116 }