15107: Get additional email addresses from Google account.
[arvados.git] / lib / config / cmd_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         "io/ioutil"
11         "os"
12
13         "git.curoverse.com/arvados.git/lib/cmd"
14         check "gopkg.in/check.v1"
15 )
16
17 var _ = check.Suite(&CommandSuite{})
18
19 var (
20         // Commands must satisfy cmd.Handler interface
21         _ cmd.Handler = dumpCommand{}
22         _ cmd.Handler = checkCommand{}
23 )
24
25 type CommandSuite struct{}
26
27 func (s *CommandSuite) SetUpSuite(c *check.C) {
28         os.Unsetenv("ARVADOS_API_HOST")
29         os.Unsetenv("ARVADOS_API_HOST_INSECURE")
30         os.Unsetenv("ARVADOS_API_TOKEN")
31 }
32
33 func (s *CommandSuite) TestBadArg(c *check.C) {
34         var stderr bytes.Buffer
35         code := DumpCommand.RunCommand("arvados config-dump", []string{"-badarg"}, bytes.NewBuffer(nil), bytes.NewBuffer(nil), &stderr)
36         c.Check(code, check.Equals, 2)
37         c.Check(stderr.String(), check.Matches, `(?ms)flag provided but not defined: -badarg\nUsage:\n.*`)
38 }
39
40 func (s *CommandSuite) TestEmptyInput(c *check.C) {
41         var stdout, stderr bytes.Buffer
42         code := DumpCommand.RunCommand("arvados config-dump", []string{"-config", "-"}, &bytes.Buffer{}, &stdout, &stderr)
43         c.Check(code, check.Equals, 1)
44         c.Check(stderr.String(), check.Matches, `config does not define any clusters\n`)
45 }
46
47 func (s *CommandSuite) TestCheckNoDeprecatedKeys(c *check.C) {
48         var stdout, stderr bytes.Buffer
49         in := `
50 Clusters:
51  z1234:
52   API:
53     MaxItemsPerResponse: 1234
54   PostgreSQL:
55     Connection:
56       sslmode: require
57   Services:
58     RailsAPI:
59       InternalURLs:
60         "http://0.0.0.0:8000": {}
61   Workbench:
62     UserProfileFormFields:
63       color:
64         Type: select
65         Options:
66           fuchsia: {}
67     ApplicationMimetypesWithViewIcon:
68       whitespace: {}
69 `
70         code := CheckCommand.RunCommand("arvados config-check", []string{"-config", "-"}, bytes.NewBufferString(in), &stdout, &stderr)
71         c.Check(code, check.Equals, 0)
72         c.Check(stdout.String(), check.Equals, "")
73         c.Check(stderr.String(), check.Equals, "")
74 }
75
76 func (s *CommandSuite) TestCheckDeprecatedKeys(c *check.C) {
77         var stdout, stderr bytes.Buffer
78         in := `
79 Clusters:
80  z1234:
81   RequestLimits:
82     MaxItemsPerResponse: 1234
83 `
84         code := CheckCommand.RunCommand("arvados config-check", []string{"-config", "-"}, bytes.NewBufferString(in), &stdout, &stderr)
85         c.Check(code, check.Equals, 1)
86         c.Check(stdout.String(), check.Matches, `(?ms).*\n\- +.*MaxItemsPerResponse: 1000\n\+ +MaxItemsPerResponse: 1234\n.*`)
87 }
88
89 func (s *CommandSuite) TestCheckOldKeepstoreConfigFile(c *check.C) {
90         f, err := ioutil.TempFile("", "")
91         c.Assert(err, check.IsNil)
92         defer os.Remove(f.Name())
93
94         io.WriteString(f, "Listen: :12345\nDebug: true\n")
95
96         var stdout, stderr bytes.Buffer
97         in := `
98 Clusters:
99  z1234:
100   SystemLogs:
101     LogLevel: info
102 `
103         code := CheckCommand.RunCommand("arvados config-check", []string{"-config", "-", "-legacy-keepstore-config", f.Name()}, bytes.NewBufferString(in), &stdout, &stderr)
104         c.Check(code, check.Equals, 1)
105         c.Check(stdout.String(), check.Matches, `(?ms).*\n\- +.*LogLevel: info\n\+ +LogLevel: debug\n.*`)
106         c.Check(stderr.String(), check.Matches, `(?ms).*you should remove the legacy keepstore config file.*\n`)
107 }
108
109 func (s *CommandSuite) TestCheckUnknownKey(c *check.C) {
110         var stdout, stderr bytes.Buffer
111         in := `
112 Clusters:
113  z1234:
114   Bogus1: foo
115   BogusSection:
116     Bogus2: foo
117   API:
118     Bogus3:
119      Bogus4: true
120   PostgreSQL:
121     ConnectionPool:
122       {Bogus5: true}
123 `
124         code := CheckCommand.RunCommand("arvados config-check", []string{"-config", "-"}, bytes.NewBufferString(in), &stdout, &stderr)
125         c.Log(stderr.String())
126         c.Check(code, check.Equals, 1)
127         c.Check(stderr.String(), check.Matches, `(?ms).*deprecated or unknown config entry: Clusters.z1234.Bogus1"\n.*`)
128         c.Check(stderr.String(), check.Matches, `(?ms).*deprecated or unknown config entry: Clusters.z1234.BogusSection"\n.*`)
129         c.Check(stderr.String(), check.Matches, `(?ms).*deprecated or unknown config entry: Clusters.z1234.API.Bogus3"\n.*`)
130         c.Check(stderr.String(), check.Matches, `(?ms).*unexpected object in config entry: Clusters.z1234.PostgreSQL.ConnectionPool"\n.*`)
131 }
132
133 func (s *CommandSuite) TestDumpFormatting(c *check.C) {
134         var stdout, stderr bytes.Buffer
135         in := `
136 Clusters:
137  z1234:
138   Containers:
139    CloudVMs:
140     TimeoutBooting: 600s
141   Services:
142    Controller:
143     InternalURLs:
144      http://localhost:12345: {}
145 `
146         code := DumpCommand.RunCommand("arvados config-dump", []string{"-config", "-"}, bytes.NewBufferString(in), &stdout, &stderr)
147         c.Check(code, check.Equals, 0)
148         c.Check(stdout.String(), check.Matches, `(?ms).*TimeoutBooting: 10m\n.*`)
149         c.Check(stdout.String(), check.Matches, `(?ms).*http://localhost:12345: {}\n.*`)
150 }
151
152 func (s *CommandSuite) TestDumpUnknownKey(c *check.C) {
153         var stdout, stderr bytes.Buffer
154         in := `
155 Clusters:
156  z1234:
157   UnknownKey: foobar
158   ManagementToken: secret
159 `
160         code := DumpCommand.RunCommand("arvados config-dump", []string{"-config", "-"}, bytes.NewBufferString(in), &stdout, &stderr)
161         c.Check(code, check.Equals, 0)
162         c.Check(stderr.String(), check.Matches, `(?ms).*deprecated or unknown config entry: Clusters.z1234.UnknownKey.*`)
163         c.Check(stdout.String(), check.Matches, `(?ms)Clusters:\n  z1234:\n.*`)
164         c.Check(stdout.String(), check.Matches, `(?ms).*\n *ManagementToken: secret\n.*`)
165         c.Check(stdout.String(), check.Not(check.Matches), `(?ms).*UnknownKey.*`)
166 }