Repository navigation
Expand file tree
/
Copy pathconfigcommand_test.go
More file actions
203 lines (165 loc) · 6.25 KB
/
Copy pathconfigcommand_test.go
File metadata and controls
203 lines (165 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package cli_test
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/dioad/cli"
)
type tlsBlock struct {
CertFile string `desc:"path to the certificate" mapstructure:"cert-file"`
}
type serviceConfig struct {
ListenAddress string `desc:"address to listen on" mapstructure:"listen-address"`
Workers int `desc:"number of workers" mapstructure:"workers"`
DrainDelay time.Duration `desc:"how long to keep serving after a stop request" mapstructure:"drain-delay"`
Origins []string `desc:"origins allowed by CORS" mapstructure:"cors-allowed-origins"`
TLS tlsBlock `mapstructure:"tls-server"`
Labels map[string]string `desc:"free-form labels" mapstructure:"labels"`
Ignored string `mapstructure:"-"`
}
func newServiceConfig() cli.Validator {
return &serviceConfig{ListenAddress: ":8080", Workers: 4, DrainDelay: 5 * time.Second}
}
func (c *serviceConfig) Validate() error {
if c.Workers < 1 {
return errors.New("workers must be at least 1")
}
return nil
}
func runConfigCommand(t *testing.T, args ...string) (string, error) {
t.Helper()
root := &cobra.Command{Use: "svc"}
root.AddCommand(cli.NewConfigCommand("svc", newServiceConfig))
var out bytes.Buffer
root.SetOut(&out)
root.SetErr(&out)
root.SetArgs(append([]string{"config"}, args...))
err := root.Execute()
return out.String(), err
}
func writeConfig(t *testing.T, content string) string {
t.Helper()
path := t.TempDir() + "/svc.yaml"
require.NoError(t, os.WriteFile(path, []byte(content), 0o600))
return path
}
func TestConfigValidate_AcceptsAGoodFile(t *testing.T) {
t.Parallel()
path := writeConfig(t, "listen-address: ':9000'\nworkers: 2\n")
out, err := runConfigCommand(t, "validate", "--config", path)
require.NoError(t, err)
assert.Equal(t, path+" is valid\n", out)
}
func TestConfigValidate_ReportsEveryProblemTogether(t *testing.T) {
t.Parallel()
path := writeConfig(t, "workers: 0\nlisten-adress: ':1'\ntls-server:\n cert: x\n")
out, err := runConfigCommand(t, "validate", "--config", path)
require.ErrorContains(t, err, "3 configuration problem(s) found")
assert.Contains(t, out, "listen-adress", "a misspelt key is named")
assert.Contains(t, out, "tls-server.cert", "an unknown nested key is reported with its full path")
assert.Contains(t, out, "workers must be at least 1", "a setting Validate rejects is reported with them")
}
func TestConfigValidate_ReportsAFileThatCannotBeRead(t *testing.T) {
t.Parallel()
_, err := runConfigCommand(t, "validate", "--config", t.TempDir()+"/missing.yaml")
require.ErrorContains(t, err, "reading config file")
}
func TestConfigValidate_UsesTheDefaultFile(t *testing.T) {
t.Parallel()
path := writeConfig(t, "workers: 3\n")
root := &cobra.Command{Use: "svc"}
root.AddCommand(cli.NewConfigCommand("svc", newServiceConfig, cli.WithConfigCommandFile(path)))
var out bytes.Buffer
root.SetOut(&out)
root.SetArgs([]string{"config", "validate"})
require.NoError(t, root.Execute())
assert.Equal(t, path+" is valid\n", out.String())
}
func TestConfigExample_ListsEverySettingWithItsDescriptionAndDefault(t *testing.T) {
t.Parallel()
out, err := runConfigCommand(t, "example")
require.NoError(t, err)
assert.Equal(t, `# address to listen on
listen-address: :8080
# number of workers
workers: 4
# how long to keep serving after a stop request
drain-delay: 5s
# origins allowed by CORS
cors-allowed-origins: []
tls-server:
# path to the certificate
cert-file: ""
# free-form labels
labels: {}
`, out)
}
func TestConfigExample_IsAValidConfigFile(t *testing.T) {
t.Parallel()
out, err := runConfigCommand(t, "example")
require.NoError(t, err)
validated, err := runConfigCommand(t, "validate", "--config", writeConfig(t, out))
require.NoError(t, err, validated)
}
func TestConfigSchema_DescribesEachKeyAndRejectsOthers(t *testing.T) {
t.Parallel()
out, err := runConfigCommand(t, "schema")
require.NoError(t, err)
var schema struct {
Type string `json:"type"`
AdditionalProperties bool `json:"additionalProperties"`
Properties map[string]map[string]any `json:"properties"`
}
require.NoError(t, json.Unmarshal([]byte(out), &schema))
assert.Equal(t, "object", schema.Type)
assert.False(t, schema.AdditionalProperties, "an unknown key is rejected")
assert.Equal(t, "string", schema.Properties["listen-address"]["type"])
assert.Equal(t, "address to listen on", schema.Properties["listen-address"]["description"])
assert.Equal(t, "integer", schema.Properties["workers"]["type"])
assert.Equal(t, "string", schema.Properties["drain-delay"]["type"], "a duration is written as a string")
assert.Equal(t, "array", schema.Properties["cors-allowed-origins"]["type"])
assert.Equal(t, "object", schema.Properties["tls-server"]["type"])
assert.NotContains(t, schema.Properties, "Ignored")
}
func TestConfigCommand_ExampleAndSchemaCanBeSuppliedByTheService(t *testing.T) {
t.Parallel()
root := &cobra.Command{Use: "svc"}
root.AddCommand(cli.NewConfigCommand("svc", newServiceConfig,
cli.WithConfigExample(func() ([]byte, error) { return []byte("custom example\n"), nil }),
cli.WithConfigSchema(func() ([]byte, error) { return []byte("{}\n"), nil }),
))
for args, want := range map[string]string{"example": "custom example\n", "schema": "{}\n"} {
var out bytes.Buffer
root.SetOut(&out)
root.SetArgs([]string{"config", args})
require.NoError(t, root.Execute())
assert.Equal(t, want, out.String())
}
}
func ExampleNewConfigCommand() {
root := &cobra.Command{Use: "svc"}
root.AddCommand(cli.NewConfigCommand("svc", newServiceConfig))
file, _ := os.CreateTemp("", "svc-*.yaml")
defer func() { _ = os.Remove(file.Name()) }()
_, _ = file.WriteString("workers: 0\nlisten-adress: ':1'\n")
_ = file.Close()
var out bytes.Buffer
root.SetOut(&out)
root.SetErr(&out)
root.SetArgs([]string{"config", "validate", "--config", file.Name()})
_ = root.Execute()
fmt.Print(strings.ReplaceAll(out.String(), file.Name(), "svc.yaml"))
// Output:
// svc.yaml:
// listen-adress: unknown key
// workers must be at least 1
// Error: svc.yaml: 2 configuration problem(s) found
}