-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathinit_test.go
More file actions
291 lines (271 loc) · 8.69 KB
/
init_test.go
File metadata and controls
291 lines (271 loc) · 8.69 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package init_test
import (
"fmt"
"path"
"regexp"
"strings"
"testing"
"github.com/scaleway/scaleway-cli/v2/core"
initCLI "github.com/scaleway/scaleway-cli/v2/internal/namespaces/init" // alias required to not collide with go init func
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func checkConfig(
check func(t *testing.T, ctx *core.CheckFuncCtx, config *scw.Config),
) core.TestCheck {
return func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
homeDir := ctx.OverrideEnv["HOME"]
config, err := scw.LoadConfigFromPath(path.Join(homeDir, ".config", "scw", "config.yaml"))
require.NoError(t, err)
check(t, ctx, config)
}
}
func appendArgs(prefix string, args map[string]string) string {
var builder strings.Builder
builder.WriteString(prefix)
for k, v := range args {
fmt.Fprintf(&builder, " %s=%s", k, v)
}
return builder.String()
}
func beforeFuncSaveConfig(config *scw.Config) core.BeforeFunc {
return func(ctx *core.BeforeFuncCtx) error {
// Persist the dummy Config in the temp directory
return config.SaveTo(path.Join(ctx.OverrideEnv["HOME"], ".config", "scw", "config.yaml"))
}
}
func TestInit(t *testing.T) {
defaultArgs := map[string]string{
"access-key": "{{ .AccessKey }}",
"secret-key": "{{ .SecretKey }}",
"send-telemetry": "true",
"install-autocomplete": "false",
"with-ssh-key": "false",
"organization-id": "{{ .OrganizationID }}",
"project-id": "{{ .ProjectID }}",
}
t.Run("Simple", core.Test(&core.TestConfig{
Commands: initCLI.GetCommands(),
BeforeFunc: baseBeforeFunc(),
TmpHomeDir: true,
Cmd: appendArgs("scw init", defaultArgs),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
checkConfig(func(t *testing.T, ctx *core.CheckFuncCtx, config *scw.Config) {
t.Helper()
secretKey, _ := ctx.Client.GetSecretKey()
assert.Equal(t, secretKey, *config.SecretKey)
assert.NotEmpty(t, *config.DefaultProjectID)
}),
),
}))
t.Run("Configuration Path", core.Test(&core.TestConfig{
Commands: initCLI.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
baseBeforeFunc(),
func(ctx *core.BeforeFuncCtx) error {
ctx.Meta["CONFIG_PATH"] = path.Join(
ctx.Meta["HOME"].(string),
"new_config_path.yml",
)
return nil
},
),
TmpHomeDir: true,
Cmd: appendArgs("scw -c {{ .CONFIG_PATH }} init", defaultArgs),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
config, err := scw.LoadConfigFromPath(ctx.Meta["CONFIG_PATH"].(string))
require.NoError(t, err)
secretKey, _ := ctx.Client.GetSecretKey()
assert.Equal(t, secretKey, *config.SecretKey)
},
),
}))
t.Run("Profile", core.Test(&core.TestConfig{
Commands: initCLI.GetCommands(),
BeforeFunc: baseBeforeFunc(),
Cmd: appendArgs("scw -p foobar init", defaultArgs),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
checkConfig(func(t *testing.T, ctx *core.CheckFuncCtx, config *scw.Config) {
t.Helper()
secretKey, _ := ctx.Client.GetSecretKey()
assert.Equal(t, secretKey, *config.Profiles["foobar"].SecretKey)
}),
),
TmpHomeDir: true,
}))
t.Run("CLIv2Config", func(t *testing.T) {
dummySecretKey := "22222222-2222-2222-2222-222222222222"
dummyAccessKey := "SCW22222222222222222"
dummyConfig := &scw.Config{
Profile: scw.Profile{
AccessKey: &dummyAccessKey,
SecretKey: &dummySecretKey,
},
Profiles: map[string]*scw.Profile{
"test": {
AccessKey: &dummyAccessKey,
SecretKey: &dummySecretKey,
DefaultZone: scw.StringPtr("fr-test"), // Used to check profile override
},
},
}
t.Run("NoOverwrite", core.Test(&core.TestConfig{
Commands: initCLI.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
baseBeforeFunc(),
beforeFuncSaveConfig(dummyConfig),
),
Cmd: appendArgs("scw init", defaultArgs),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
checkConfig(func(t *testing.T, _ *core.CheckFuncCtx, config *scw.Config) {
t.Helper()
assert.Equal(t, dummyConfig.String(), config.String())
}),
),
TmpHomeDir: true,
PromptResponseMocks: []string{
// Do you want to override the current config?
"no",
},
}))
t.Run("Overwrite", core.Test(&core.TestConfig{
Commands: initCLI.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
baseBeforeFunc(),
beforeFuncSaveConfig(dummyConfig),
),
Cmd: appendArgs("scw init", defaultArgs),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
checkConfig(func(t *testing.T, ctx *core.CheckFuncCtx, config *scw.Config) {
t.Helper()
secretKey, _ := ctx.Client.GetSecretKey()
assert.Equal(t, secretKey, *config.SecretKey)
}),
),
TmpHomeDir: true,
PromptResponseMocks: []string{
// Do you want to override the current config?
"yes",
},
}))
t.Run("No Prompt Overwrite for new profile", core.Test(&core.TestConfig{
Commands: initCLI.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
baseBeforeFunc(),
beforeFuncSaveConfig(dummyConfig),
),
Cmd: appendArgs("scw -p test2 init", defaultArgs),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
checkConfig(func(t *testing.T, _ *core.CheckFuncCtx, config *scw.Config) {
t.Helper()
assert.NotNil(
t,
config.Profiles["test2"],
"new profile should have been created",
)
}),
),
TmpHomeDir: true,
PromptResponseMocks: []string{
// Do you want to override the current config? (Should not be prompted as profile is a new one)
"no",
},
}))
t.Run("Prompt Overwrite for existing profile", core.Test(&core.TestConfig{
Commands: initCLI.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
baseBeforeFunc(),
beforeFuncSaveConfig(dummyConfig),
),
Cmd: appendArgs("scw -p test init", defaultArgs),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
checkConfig(func(t *testing.T, _ *core.CheckFuncCtx, config *scw.Config) {
t.Helper()
assert.NotNil(t, config.Profiles["test"].DefaultZone)
assert.Equal(t, "fr-test", *config.Profiles["test"].DefaultZone)
}),
),
TmpHomeDir: true,
PromptResponseMocks: []string{
// Do you want to override the current config? (Should not be prompted as profile is a new one)
"no",
},
}))
t.Run("Default profile activated", core.Test(&core.TestConfig{
Commands: initCLI.GetCommands(),
BeforeFunc: baseBeforeFunc(),
TmpHomeDir: true,
Cmd: appendArgs("scw -p newprofile init", defaultArgs),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
checkConfig(func(t *testing.T, _ *core.CheckFuncCtx, config *scw.Config) {
t.Helper()
assert.NotNil(t, config.ActiveProfile)
assert.Equal(t, "newprofile", *config.ActiveProfile)
}),
),
}))
})
}
func TestInit_Prompt(t *testing.T) {
promptResponse := []string{
"secret-key", // Secret key prompt, should be replaced in BeforeFunc.
"access-key", // Access key prompt, should be replaced in BeforeFunc.
"organization-id", // Organization prompt, should be replaced in BeforeFunc.
" ", // default-project-id list prompt, space is validation, it will pick default organization project.
"", // Telemetry prompt, use default value.
"y", // Autocomplete prompt, enable it but the tests should override a SHELL variable to avoid breaking because of local configuration.
}
t.Run("Simple", core.Test(&core.TestConfig{
Commands: initCLI.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
baseBeforeFunc(),
func(ctx *core.BeforeFuncCtx) error {
promptResponse[0] = ctx.Meta["SecretKey"].(string)
promptResponse[1] = ctx.Meta["AccessKey"].(string)
promptResponse[2] = ctx.Meta["OrganizationID"].(string)
return nil
}),
TmpHomeDir: true,
Cmd: "scw init",
Check: core.TestCheckCombine(
core.TestCheckGoldenAndReplacePatterns(
core.GoldenReplacement{
Pattern: regexp.MustCompile(
"\\s\\sExcept for autocomplete: unsupported OS 'windows'\n",
),
Replacement: "",
OptionalMatch: true,
},
core.GoldenReplacement{
Pattern: regexp.MustCompile(
`Except for autocomplete: unsupported OS 'windows'\\n`,
),
Replacement: "",
OptionalMatch: true,
},
),
checkConfig(func(t *testing.T, ctx *core.CheckFuncCtx, config *scw.Config) {
t.Helper()
secretKey, _ := ctx.Client.GetSecretKey()
assert.Equal(t, secretKey, *config.SecretKey)
assert.NotEmpty(t, *config.DefaultProjectID)
}),
),
OverrideEnv: map[string]string{
"SHELL": "/bin/bash",
},
PromptResponseMocks: promptResponse,
}))
}