15954: Remove unused cmdArgs.
[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.arvados.org/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) TestDump_BadArg(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) TestDump_EmptyInput(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) TestCheck_NoWarnings(c *check.C) {
48         var stdout, stderr bytes.Buffer
49         in := `
50 Clusters:
51  z1234:
52   ManagementToken: xyzzy
53   SystemRootToken: xyzzy
54   API:
55     MaxItemsPerResponse: 1234
56   PostgreSQL:
57     Connection:
58       sslmode: require
59   Services:
60     RailsAPI:
61       InternalURLs:
62         "http://0.0.0.0:8000": {}
63   Workbench:
64     UserProfileFormFields:
65       color:
66         Type: select
67         Options:
68           fuchsia: {}
69     ApplicationMimetypesWithViewIcon:
70       whitespace: {}
71 `
72         code := CheckCommand.RunCommand("arvados config-check", []string{"-config", "-"}, bytes.NewBufferString(in), &stdout, &stderr)
73         c.Check(code, check.Equals, 0)
74         c.Check(stdout.String(), check.Equals, "")
75         c.Check(stderr.String(), check.Equals, "")
76 }
77
78 func (s *CommandSuite) TestCheck_DeprecatedKeys(c *check.C) {
79         var stdout, stderr bytes.Buffer
80         in := `
81 Clusters:
82  z1234:
83   RequestLimits:
84     MaxItemsPerResponse: 1234
85 `
86         code := CheckCommand.RunCommand("arvados config-check", []string{"-config", "-"}, bytes.NewBufferString(in), &stdout, &stderr)
87         c.Check(code, check.Equals, 1)
88         c.Check(stdout.String(), check.Matches, `(?ms).*\n\- +.*MaxItemsPerResponse: 1000\n\+ +MaxItemsPerResponse: 1234\n.*`)
89 }
90
91 func (s *CommandSuite) TestCheck_OldKeepstoreConfigFile(c *check.C) {
92         f, err := ioutil.TempFile("", "")
93         c.Assert(err, check.IsNil)
94         defer os.Remove(f.Name())
95
96         io.WriteString(f, "Listen: :12345\nDebug: true\n")
97
98         var stdout, stderr bytes.Buffer
99         in := `
100 Clusters:
101  z1234:
102   SystemLogs:
103     LogLevel: info
104 `
105         code := CheckCommand.RunCommand("arvados config-check", []string{"-config", "-", "-legacy-keepstore-config", f.Name()}, bytes.NewBufferString(in), &stdout, &stderr)
106         c.Check(code, check.Equals, 1)
107         c.Check(stdout.String(), check.Matches, `(?ms).*\n\- +.*LogLevel: info\n\+ +LogLevel: debug\n.*`)
108         c.Check(stderr.String(), check.Matches, `(?ms).*you should remove the legacy keepstore config file.*\n`)
109 }
110
111 func (s *CommandSuite) TestCheck_UnknownKey(c *check.C) {
112         var stdout, stderr bytes.Buffer
113         in := `
114 Clusters:
115  z1234:
116   Bogus1: foo
117   BogusSection:
118     Bogus2: foo
119   API:
120     Bogus3:
121      Bogus4: true
122   PostgreSQL:
123     ConnectionPool:
124       {Bogus5: true}
125 `
126         code := CheckCommand.RunCommand("arvados config-check", []string{"-config", "-"}, bytes.NewBufferString(in), &stdout, &stderr)
127         c.Log(stderr.String())
128         c.Check(code, check.Equals, 1)
129         c.Check(stderr.String(), check.Matches, `(?ms).*deprecated or unknown config entry: Clusters.z1234.Bogus1"\n.*`)
130         c.Check(stderr.String(), check.Matches, `(?ms).*deprecated or unknown config entry: Clusters.z1234.BogusSection"\n.*`)
131         c.Check(stderr.String(), check.Matches, `(?ms).*deprecated or unknown config entry: Clusters.z1234.API.Bogus3"\n.*`)
132         c.Check(stderr.String(), check.Matches, `(?ms).*unexpected object in config entry: Clusters.z1234.PostgreSQL.ConnectionPool"\n.*`)
133 }
134
135 func (s *CommandSuite) TestDump_Formatting(c *check.C) {
136         var stdout, stderr bytes.Buffer
137         in := `
138 Clusters:
139  z1234:
140   Containers:
141    CloudVMs:
142     TimeoutBooting: 600s
143   Services:
144    Controller:
145     InternalURLs:
146      http://localhost:12345: {}
147 `
148         code := DumpCommand.RunCommand("arvados config-dump", []string{"-config", "-"}, bytes.NewBufferString(in), &stdout, &stderr)
149         c.Check(code, check.Equals, 0)
150         c.Check(stdout.String(), check.Matches, `(?ms).*TimeoutBooting: 10m\n.*`)
151         c.Check(stdout.String(), check.Matches, `(?ms).*http://localhost:12345: {}\n.*`)
152 }
153
154 func (s *CommandSuite) TestDump_UnknownKey(c *check.C) {
155         var stdout, stderr bytes.Buffer
156         in := `
157 Clusters:
158  z1234:
159   UnknownKey: foobar
160   ManagementToken: secret
161 `
162         code := DumpCommand.RunCommand("arvados config-dump", []string{"-config", "-"}, bytes.NewBufferString(in), &stdout, &stderr)
163         c.Check(code, check.Equals, 0)
164         c.Check(stderr.String(), check.Matches, `(?ms).*deprecated or unknown config entry: Clusters.z1234.UnknownKey.*`)
165         c.Check(stdout.String(), check.Matches, `(?ms)Clusters:\n  z1234:\n.*`)
166         c.Check(stdout.String(), check.Matches, `(?ms).*\n *ManagementToken: secret\n.*`)
167         c.Check(stdout.String(), check.Not(check.Matches), `(?ms).*UnknownKey.*`)
168 }