forked from migtools/oadp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_test.go
More file actions
536 lines (481 loc) · 16.1 KB
/
backup_test.go
File metadata and controls
536 lines (481 loc) · 16.1 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
Copyright 2025 The OADP CLI Contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package backup
import (
"testing"
"github.com/migtools/oadp-cli/internal/testutil"
)
// TestNonAdminBackupCommands tests the non-admin backup command functionality
func TestNonAdminBackupCommands(t *testing.T) {
binaryPath := testutil.BuildCLIBinary(t)
tests := []struct {
name string
args []string
expectContains []string
}{
{
name: "nonadmin backup help",
args: []string{"nonadmin", "backup", "--help"},
expectContains: []string{
"Work with non-admin backups",
"create",
"describe",
"delete",
"get",
"logs",
},
},
{
name: "nonadmin backup create help",
args: []string{"nonadmin", "backup", "create", "--help"},
expectContains: []string{
"Create a non-admin backup",
"--storage-location",
"--include-resources",
"--exclude-resources",
"--force",
},
},
{
name: "nonadmin backup describe help",
args: []string{"nonadmin", "backup", "describe", "--help"},
expectContains: []string{
"Describe a non-admin backup",
},
},
{
name: "nonadmin backup delete help",
args: []string{"nonadmin", "backup", "delete", "--help"},
expectContains: []string{
"Delete one or more non-admin backups",
"--confirm",
},
},
{
name: "nonadmin backup get help",
args: []string{"nonadmin", "backup", "get", "--help"},
expectContains: []string{
"Get one or more non-admin backups",
},
},
{
name: "nonadmin backup logs help",
args: []string{"nonadmin", "backup", "logs", "--help"},
expectContains: []string{
"Show logs for a non-admin backup",
},
},
{
name: "na backup shorthand help",
args: []string{"na", "backup", "--help"},
expectContains: []string{
"Work with non-admin backups",
"create",
"describe",
"delete",
"get",
"logs",
},
},
// Verb-noun order help command tests
{
name: "nonadmin get backup help",
args: []string{"nonadmin", "get", "backup", "--help"},
expectContains: []string{
"Get one or more non-admin backups",
},
},
{
name: "nonadmin create backup help",
args: []string{"nonadmin", "create", "backup", "--help"},
expectContains: []string{
"Create a non-admin backup",
},
},
{
name: "nonadmin delete backup help",
args: []string{"nonadmin", "delete", "backup", "--help"},
expectContains: []string{
"Delete one or more non-admin backups",
},
},
{
name: "nonadmin describe backup help",
args: []string{"nonadmin", "describe", "backup", "--help"},
expectContains: []string{
"Describe a non-admin backup",
},
},
{
name: "nonadmin logs backup help",
args: []string{"nonadmin", "logs", "backup", "--help"},
expectContains: []string{
"Show logs for a non-admin backup",
},
},
// Shorthand verb-noun order tests
{
name: "na get backup help",
args: []string{"na", "get", "backup", "--help"},
expectContains: []string{
"Get one or more non-admin backups",
},
},
{
name: "na create backup help",
args: []string{"na", "create", "backup", "--help"},
expectContains: []string{
"Create a non-admin backup",
},
},
{
name: "na delete backup help",
args: []string{"na", "delete", "backup", "--help"},
expectContains: []string{
"Delete one or more non-admin backups",
},
},
{
name: "na describe backup help",
args: []string{"na", "describe", "backup", "--help"},
expectContains: []string{
"Describe a non-admin backup",
},
},
{
name: "na logs backup help",
args: []string{"na", "logs", "backup", "--help"},
expectContains: []string{
"Show logs for a non-admin backup",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testutil.TestHelpCommand(t, binaryPath, tt.args, tt.expectContains)
})
}
}
// TestNonAdminBackupHelpFlags tests that both --help and -h work for backup commands
func TestNonAdminBackupHelpFlags(t *testing.T) {
binaryPath := testutil.BuildCLIBinary(t)
commands := [][]string{
{"nonadmin", "backup", "--help"},
{"nonadmin", "backup", "-h"},
{"nonadmin", "backup", "create", "--help"},
{"nonadmin", "backup", "create", "-h"},
{"nonadmin", "backup", "describe", "--help"},
{"nonadmin", "backup", "describe", "-h"},
{"nonadmin", "backup", "delete", "--help"},
{"nonadmin", "backup", "delete", "-h"},
{"nonadmin", "backup", "get", "--help"},
{"nonadmin", "backup", "get", "-h"},
{"nonadmin", "backup", "logs", "--help"},
{"nonadmin", "backup", "logs", "-h"},
{"na", "backup", "--help"},
{"na", "backup", "-h"},
// Verb-noun order help flags
{"nonadmin", "get", "backup", "--help"},
{"nonadmin", "get", "backup", "-h"},
{"nonadmin", "create", "backup", "--help"},
{"nonadmin", "create", "backup", "-h"},
{"nonadmin", "delete", "backup", "--help"},
{"nonadmin", "delete", "backup", "-h"},
{"nonadmin", "describe", "backup", "--help"},
{"nonadmin", "describe", "backup", "-h"},
{"nonadmin", "logs", "backup", "--help"},
{"nonadmin", "logs", "backup", "-h"},
// Shorthand verb-noun order help flags
{"na", "get", "backup", "--help"},
{"na", "get", "backup", "-h"},
{"na", "create", "backup", "--help"},
{"na", "create", "backup", "-h"},
{"na", "delete", "backup", "--help"},
{"na", "delete", "backup", "-h"},
{"na", "describe", "backup", "--help"},
{"na", "describe", "backup", "-h"},
{"na", "logs", "backup", "--help"},
{"na", "logs", "backup", "-h"},
}
for _, cmd := range commands {
t.Run("help_flags_"+cmd[len(cmd)-1], func(t *testing.T) {
testutil.TestHelpCommand(t, binaryPath, cmd, []string{"Usage:"})
})
}
}
// TestNonAdminBackupCreateFlags tests create command specific flags
func TestNonAdminBackupCreateFlags(t *testing.T) {
binaryPath := testutil.BuildCLIBinary(t)
t.Run("create command has all expected MVP flags", func(t *testing.T) {
expectedFlags := []string{
"--include-resources",
"--exclude-resources",
"--selector",
"--or-selector",
"--include-cluster-resources",
"--ttl",
"--storage-location",
"--csi-snapshot-timeout",
"--item-operation-timeout",
"--snapshot-volumes",
"--snapshot-move-data",
"--default-volumes-to-fs-backup",
"--force",
}
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "backup", "create", "--help"},
expectedFlags)
})
}
// TestNonAdminBackupExamples tests that help text contains proper examples
func TestNonAdminBackupExamples(t *testing.T) {
binaryPath := testutil.BuildCLIBinary(t)
t.Run("create examples use correct command format", func(t *testing.T) {
expectedExamples := []string{
"kubectl oadp nonadmin backup create",
"--storage-location",
"--include-resources",
"--exclude-resources",
}
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "backup", "create", "--help"},
expectedExamples)
})
t.Run("main backup help shows subcommands", func(t *testing.T) {
expectedSubcommands := []string{
"create",
"delete",
"describe",
"get",
"logs",
}
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "backup", "--help"},
expectedSubcommands)
})
}
// TestNonAdminBackupClientConfigIntegration tests that backup commands respect client config
func TestNonAdminBackupClientConfigIntegration(t *testing.T) {
binaryPath := testutil.BuildCLIBinary(t)
_, cleanup := testutil.SetupTempHome(t)
defer cleanup()
t.Run("backup commands work with client config", func(t *testing.T) {
// Set a known namespace
_, err := testutil.RunCommand(t, binaryPath, "client", "config", "set", "namespace=user-namespace")
if err != nil {
t.Fatalf("Failed to set client config: %v", err)
}
// Test that backup commands can be invoked (they should respect the namespace)
// We test help commands since they don't require actual K8s resources
commands := [][]string{
{"nonadmin", "backup", "get", "--help"},
{"nonadmin", "backup", "create", "--help"},
{"nonadmin", "backup", "describe", "--help"},
{"nonadmin", "backup", "delete", "--help"},
{"nonadmin", "backup", "logs", "--help"},
{"na", "backup", "get", "--help"},
// Verb-noun order commands
{"nonadmin", "get", "backup", "--help"},
{"nonadmin", "create", "backup", "--help"},
{"nonadmin", "describe", "backup", "--help"},
{"nonadmin", "delete", "backup", "--help"},
{"nonadmin", "logs", "backup", "--help"},
{"na", "get", "backup", "--help"},
{"na", "create", "backup", "--help"},
}
for _, cmd := range commands {
t.Run("config_test_"+cmd[len(cmd)-2], func(t *testing.T) {
output, err := testutil.RunCommand(t, binaryPath, cmd...)
if err != nil {
t.Fatalf("Non-admin backup command should work with client config: %v", err)
}
if output == "" {
t.Errorf("Expected help output for %v", cmd)
}
})
}
})
}
// TestNonAdminBackupCommandStructure tests the overall command structure
func TestNonAdminBackupCommandStructure(t *testing.T) {
binaryPath := testutil.BuildCLIBinary(t)
t.Run("backup commands available under nonadmin", func(t *testing.T) {
_, err := testutil.RunCommand(t, binaryPath, "nonadmin", "--help")
if err != nil {
t.Fatalf("nonadmin command should exist: %v", err)
}
expectedCommands := []string{"backup"}
for _, cmd := range expectedCommands {
testutil.TestHelpCommand(t, binaryPath, []string{"nonadmin", "--help"}, []string{cmd})
}
})
t.Run("backup commands available under na shorthand", func(t *testing.T) {
_, err := testutil.RunCommand(t, binaryPath, "na", "--help")
if err != nil {
t.Fatalf("na command should exist: %v", err)
}
expectedCommands := []string{"backup"}
for _, cmd := range expectedCommands {
testutil.TestHelpCommand(t, binaryPath, []string{"na", "--help"}, []string{cmd})
}
})
}
// TestVerbNounOrderExamples tests that verb-noun order commands show proper examples
func TestVerbNounOrderExamples(t *testing.T) {
binaryPath := testutil.BuildCLIBinary(t)
t.Run("verb commands show proper examples", func(t *testing.T) {
// Test that verb commands show examples with kubectl oadp prefix
expectedExamples := []string{
"kubectl oadp nonadmin get backup",
"kubectl oadp nonadmin create backup",
"kubectl oadp nonadmin delete backup",
"kubectl oadp nonadmin describe backup",
"kubectl oadp nonadmin logs backup",
}
commands := [][]string{
{"nonadmin", "get", "--help"},
{"nonadmin", "create", "--help"},
{"nonadmin", "delete", "--help"},
{"nonadmin", "describe", "--help"},
{"nonadmin", "logs", "--help"},
}
for i, cmd := range commands {
testutil.TestHelpCommand(t, binaryPath, cmd, []string{expectedExamples[i]})
}
})
t.Run("verb commands with specific resources show proper examples", func(t *testing.T) {
// Test that verb commands with specific resources show examples (noun-first format from underlying commands)
expectedExamples := []string{
"kubectl oadp nonadmin backup get",
"kubectl oadp nonadmin backup create backup1",
"kubectl oadp nonadmin backup delete my-backup",
"kubectl oadp nonadmin backup describe my-backup",
"kubectl oadp nonadmin backup logs my-backup",
}
commands := [][]string{
{"nonadmin", "get", "backup", "--help"},
{"nonadmin", "create", "backup", "--help"},
{"nonadmin", "delete", "backup", "--help"},
{"nonadmin", "describe", "backup", "--help"},
{"nonadmin", "logs", "backup", "--help"},
}
for i, cmd := range commands {
testutil.TestHelpCommand(t, binaryPath, cmd, []string{expectedExamples[i]})
}
})
t.Run("shorthand verb commands show proper examples", func(t *testing.T) {
// Test that shorthand verb commands show examples
expectedExamples := []string{
"kubectl oadp nonadmin get backup",
"kubectl oadp nonadmin create backup",
"kubectl oadp nonadmin delete backup",
"kubectl oadp nonadmin describe backup",
"kubectl oadp nonadmin logs backup",
}
commands := [][]string{
{"na", "get", "--help"},
{"na", "create", "--help"},
{"na", "delete", "--help"},
{"na", "describe", "--help"},
{"na", "logs", "--help"},
}
for i, cmd := range commands {
testutil.TestHelpCommand(t, binaryPath, cmd, []string{expectedExamples[i]})
}
})
}
// TestNonAdminBackupDeleteAllFlag tests the --all flag functionality added in commit 6a112249d91ab5411d199b848cae1c97fccea655
// AI-generated test
func TestNonAdminBackupDeleteAllFlag(t *testing.T) {
binaryPath := testutil.BuildCLIBinary(t)
t.Run("delete help shows --all flag", func(t *testing.T) {
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "backup", "delete", "--help"},
[]string{"--all"})
})
t.Run("delete help shows --all in usage", func(t *testing.T) {
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "backup", "delete", "--help"},
[]string{"[NAME...] | --all"})
})
t.Run("delete help describes --all flag purpose", func(t *testing.T) {
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "backup", "delete", "--help"},
[]string{"Delete all backups in the current namespace"})
})
t.Run("delete --all flag description in long help", func(t *testing.T) {
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "backup", "delete", "--help"},
[]string{"Use --all to delete all backups in the current namespace"})
})
t.Run("verb-noun delete help shows --all flag", func(t *testing.T) {
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "delete", "backup", "--help"},
[]string{"--all"})
})
t.Run("shorthand na delete backup help shows --all flag", func(t *testing.T) {
testutil.TestHelpCommand(t, binaryPath,
[]string{"na", "delete", "backup", "--help"},
[]string{"--all"})
})
t.Run("shorthand na backup delete help shows --all flag", func(t *testing.T) {
testutil.TestHelpCommand(t, binaryPath,
[]string{"na", "backup", "delete", "--help"},
[]string{"--all"})
})
}
// TestNonAdminBackupDeleteAllFlagExamples tests that examples mention the --all flag
// AI-generated test
func TestNonAdminBackupDeleteAllFlagExamples(t *testing.T) {
binaryPath := testutil.BuildCLIBinary(t)
t.Run("delete help documents both usage patterns", func(t *testing.T) {
// Test that help shows both ways to use the command
expectedPatterns := []string{
"--all", // The flag itself
"--confirm", // The confirmation skip flag
}
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "backup", "delete", "--help"},
expectedPatterns)
})
t.Run("delete help has examples section", func(t *testing.T) {
// Test that examples section exists and shows various delete patterns
expectedExamples := []string{
"kubectl oadp nonadmin backup delete my-backup",
"kubectl oadp nonadmin backup delete --all",
"kubectl oadp nonadmin backup delete my-backup --confirm",
}
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "backup", "delete", "--help"},
expectedExamples)
})
}
// TestNonAdminBackupDeleteAllFlagBehavior tests the behavioral aspects of the --all flag
// AI-generated test
func TestNonAdminBackupDeleteAllFlagBehavior(t *testing.T) {
binaryPath := testutil.BuildCLIBinary(t)
t.Run("delete with --all requires no backup names", func(t *testing.T) {
// The --all flag should accept no arguments
// This is a validation that the Args function works correctly
// We expect help output to show: [NAME...] | --all
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "backup", "delete", "--help"},
[]string{"[NAME...] | --all"})
})
t.Run("delete help mentions namespace context", func(t *testing.T) {
// The --all flag deletes in current namespace, should be documented
testutil.TestHelpCommand(t, binaryPath,
[]string{"nonadmin", "backup", "delete", "--help"},
[]string{"current namespace"})
})
}