-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathprofiles_test.go
More file actions
272 lines (256 loc) · 6.34 KB
/
profiles_test.go
File metadata and controls
272 lines (256 loc) · 6.34 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
package config
import (
_ "embed"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
)
//go:embed template/test_profile.json
var templateConfig string
func TestValidateProfile(t *testing.T) {
tests := []struct {
description string
profile string
isValid bool
}{
{
description: "valid profile with letters",
profile: "myprofile",
isValid: true,
},
{
description: "valid with letters and hyphen",
profile: "my-profile",
isValid: true,
},
{
description: "valid with letters, numbers, and hyphen",
profile: "my-profile-123",
isValid: true,
},
{
description: "valid with letters, numbers, and ending with hyphen",
profile: "my-profile123-",
isValid: true,
},
{
description: "invalid empty",
profile: "",
isValid: false,
},
{
description: "invalid with special characters",
profile: "my_profile",
isValid: false,
},
{
description: "invalid with spaces",
profile: "my profile",
isValid: false,
},
{
description: "invalid starting with -",
profile: "-my-profile",
isValid: false,
},
{
description: "invalid profile with uppercase letters",
profile: "myProfile",
isValid: false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
err := ValidateProfile(tt.profile)
if tt.isValid && err != nil {
t.Errorf("expected profile to be valid but got error: %v", err)
}
if !tt.isValid && err == nil {
t.Errorf("expected profile to be invalid but got no error")
}
})
}
}
func TestGetProfileFolderPath(t *testing.T) {
tests := []struct {
description string
defaultConfigFolderNotSet bool
profile string
expected string
}{
{
description: "default profile",
profile: DefaultProfileName,
expected: getInitialConfigDir(),
},
{
description: "default profile, default config folder not set",
defaultConfigFolderNotSet: true,
profile: DefaultProfileName,
expected: getInitialConfigDir(),
},
{
description: "custom profile",
profile: "my-profile",
expected: filepath.Join(getInitialConfigDir(), profileRootFolder, "my-profile"),
},
{
description: "custom profile, default config folder not set",
defaultConfigFolderNotSet: true,
profile: "my-profile",
expected: filepath.Join(getInitialConfigDir(), profileRootFolder, "my-profile"),
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
defaultConfigFolderPath = getInitialConfigDir()
if tt.defaultConfigFolderNotSet {
defaultConfigFolderPath = ""
}
actual := GetProfileFolderPath(tt.profile)
if actual != tt.expected {
t.Errorf("expected profile folder path to be %q but got %q", tt.expected, actual)
}
})
}
}
func TestImportProfile(t *testing.T) {
tests := []struct {
description string
profile string
config string
setAsActive bool
isValid bool
}{
{
description: "valid profile",
profile: "profile-name",
config: templateConfig,
setAsActive: false,
isValid: true,
},
{
description: "invalid profile name",
profile: "invalid-profile-&",
config: templateConfig,
setAsActive: false,
isValid: false,
},
{
description: "invalid config",
profile: "my-profile",
config: `{ "invalid": "json }`,
setAsActive: false,
isValid: false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
p := print.NewPrinter()
err := ImportProfile(p, tt.profile, tt.config, tt.setAsActive)
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("profile should be valid but got error: %v\n", err)
}
if !tt.isValid {
t.Fatalf("profile should be invalid but got no error\n")
}
})
t.Cleanup(func() {
p := print.NewPrinter()
err := DeleteProfile(p, tt.profile)
if err != nil {
if !tt.isValid {
return
}
fmt.Printf("could not clean up imported profile: %v\n", err)
}
})
}
}
func TestExportProfile(t *testing.T) {
// Create directory where the export configs should be stored
testDir, err := os.MkdirTemp(os.TempDir(), "stackit-cli-test")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
func(path string) {
err := os.RemoveAll(path)
if err != nil {
fmt.Printf("could not clean up temp dir: %v\n", err)
}
}(testDir)
})
// Create test config directory
testConfigFolderPath := filepath.Join(testDir, "config")
initConfig(testConfigFolderPath)
err = Write()
if err != nil {
t.Fatalf("could not write profile, %v", err)
}
// Create prerequisite profile
p := print.NewPrinter()
profileName := "export-profile-test"
err = CreateProfile(p, profileName, true, false, false)
if err != nil {
t.Fatalf("could not create prerequisite profile, %v", err)
}
t.Cleanup(func() {
func(p *print.Printer, profile string) {
err := DeleteProfile(p, profile)
if err != nil {
fmt.Printf("could not clean up prerequisite profile %q, %v", profileName, err)
}
}(p, profileName)
})
tests := []struct {
description string
profile string
filePath string
isValid bool
}{
{
description: "valid profile",
profile: profileName,
filePath: filepath.Join(testDir, fmt.Sprintf("custom-name.%s", configFileExtension)),
isValid: true,
},
{
description: "invalid profile",
profile: "invalid-my-profile",
isValid: false,
},
{
description: "not existing path",
profile: profileName,
filePath: filepath.Join(testDir, "invalid", "path", fmt.Sprintf("custom-name.%s", configFileExtension)),
isValid: false,
},
{
description: "export without file extension",
profile: profileName,
filePath: filepath.Join(testDir, "file-without-extension"),
isValid: true,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
p := print.NewPrinter()
err := ExportProfile(p, tt.profile, tt.filePath)
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("export should be valid but got error: %v\n", err)
}
if !tt.isValid {
t.Fatalf("export should be invalid but got no error\n")
}
})
}
}