-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli_test.go
More file actions
946 lines (886 loc) · 21.8 KB
/
Copy pathcli_test.go
File metadata and controls
946 lines (886 loc) · 21.8 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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
package dewy
import (
"bytes"
"reflect"
"strings"
"testing"
"time"
"github.com/linyows/dewy/container"
)
func TestRunCLI(t *testing.T) {
tests := []struct {
name string
args []string
expectExit int
expectOutput string
expectError string
}{
{
name: "help flag",
args: []string{"--help"},
expectExit: ExitErr,
expectOutput: "Usage: dewy",
},
{
name: "version flag",
args: []string{"--version"},
expectExit: ExitOK,
expectOutput: "dewy version: test-version",
},
{
name: "no command",
args: []string{},
expectExit: ExitErr,
expectError: "Error: command is not available",
},
{
name: "invalid command",
args: []string{"invalid"},
expectExit: ExitErr,
expectError: "Error: command is not available",
},
{
name: "server command without registry",
args: []string{"server", "myapp"},
expectExit: ExitErr,
expectError: "Error: --registry is not set",
},
{
name: "assets command without registry",
args: []string{"assets"},
expectExit: ExitErr,
expectError: "Error: --registry is not set",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var outBuf, errBuf bytes.Buffer
env := Env{
Out: &outBuf,
Err: &errBuf,
Args: tt.args,
Info: &Info{
Version: "test-version",
Commit: "test-commit",
Date: "test-date",
},
}
exitCode := RunCLI(env)
if exitCode != tt.expectExit {
t.Errorf("Expected exit code %d, got %d", tt.expectExit, exitCode)
}
if tt.expectOutput != "" {
output := outBuf.String()
if !strings.Contains(output, tt.expectOutput) {
t.Errorf("Expected output to contain %q, got %q", tt.expectOutput, output)
}
}
if tt.expectError != "" {
errOutput := errBuf.String()
if !strings.Contains(errOutput, tt.expectError) {
t.Errorf("Expected error to contain %q, got %q", tt.expectError, errOutput)
}
}
})
}
}
func TestCLI_NotifierArgument(t *testing.T) {
var outBuf, errBuf bytes.Buffer
env := Env{
Out: &outBuf,
Err: &errBuf,
Args: []string{"--notifier", "slack://test", "--registry", "ghr://test/test", "assets"},
Info: &Info{
Version: "test-version",
Commit: "test-commit",
Date: "test-date",
},
}
cli := &cli{env: env, Interval: -1}
cli.Notifier = "slack://test"
cli.Registry = "ghr://test/test"
conf := DefaultConfig()
conf.Notifier = cli.Notifier
if conf.Notifier != "slack://test" {
t.Errorf("Expected notifier to be set to slack://test, got %s", conf.Notifier)
}
}
func TestCLI_ConfigurationParsing(t *testing.T) {
tests := []struct {
name string
args []string
expectInterval int
expectLogLevel string
expectPort string
expectCommand Command
}{
{
name: "default interval",
args: []string{"--registry", "ghr://test/test", "assets"},
expectInterval: 10,
expectLogLevel: "ERROR",
expectCommand: ASSETS,
},
{
name: "custom interval",
args: []string{"--interval", "30", "--registry", "ghr://test/test", "assets"},
expectInterval: 30,
expectLogLevel: "ERROR",
expectCommand: ASSETS,
},
{
name: "custom log level",
args: []string{"--log-level", "debug", "--registry", "ghr://test/test", "assets"},
expectInterval: 10,
expectLogLevel: "DEBUG",
expectCommand: ASSETS,
},
{
name: "server command with port",
args: []string{"--port", "8080", "--registry", "ghr://test/test", "server", "myapp"},
expectInterval: 10,
expectLogLevel: "ERROR",
expectPort: "8080",
expectCommand: SERVER,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var outBuf, errBuf bytes.Buffer
env := Env{
Out: &outBuf,
Err: &errBuf,
Args: tt.args,
Info: &Info{
Version: "test-version",
Commit: "test-commit",
Date: "test-date",
},
}
// Mock the CLI parsing
cli := &cli{env: env, Interval: -1}
// Parse arguments manually for testing
for i, arg := range tt.args {
switch arg {
case "--interval":
if i+1 < len(tt.args) {
if tt.args[i+1] == "30" {
cli.Interval = 30
}
}
case "--log-level":
if i+1 < len(tt.args) {
cli.LogLevel = tt.args[i+1]
}
case "--port":
if i+1 < len(tt.args) {
cli.Ports = []string{tt.args[i+1]}
}
case "--registry":
if i+1 < len(tt.args) {
cli.Registry = tt.args[i+1]
}
case "assets":
cli.command = "assets"
case "server":
cli.command = "server"
}
}
// Apply default interval logic
if cli.Interval < 0 {
cli.Interval = 10
}
// Apply default log level logic
if cli.LogLevel != "" {
cli.LogLevel = strings.ToUpper(cli.LogLevel)
} else {
cli.LogLevel = "ERROR"
}
// Test configuration
conf := DefaultConfig()
conf.Registry = cli.Registry
if cli.command == "server" {
conf.Command = SERVER
} else {
conf.Command = ASSETS
}
// Verify expectations
if cli.Interval != tt.expectInterval {
t.Errorf("Expected interval %d, got %d", tt.expectInterval, cli.Interval)
}
if cli.LogLevel != tt.expectLogLevel {
t.Errorf("Expected log level %s, got %s", tt.expectLogLevel, cli.LogLevel)
}
if len(cli.Ports) == 1 && cli.Ports[0] != tt.expectPort {
t.Errorf("Expected port %s, got %s", tt.expectPort, cli.Ports[0])
} else if len(cli.Ports) != 1 && tt.expectPort != "" {
t.Errorf("Expected single port %s, got %v", tt.expectPort, cli.Ports)
}
if conf.Command != tt.expectCommand {
t.Errorf("Expected command %v, got %v", tt.expectCommand, conf.Command)
}
})
}
}
func TestCLI_BuildHelp(t *testing.T) {
cli := &cli{}
help := cli.buildHelp([]string{"LogLevel", "Interval", "Registry"})
if len(help) != 3 {
t.Errorf("Expected 3 help lines, got %d", len(help))
}
// Check that help contains expected format
for _, line := range help {
if !strings.Contains(line, "--") {
t.Errorf("Help line should contain '--', got: %s", line)
}
}
// Test with non-existent field
helpEmpty := cli.buildHelp([]string{"NonExistentField"})
if len(helpEmpty) != 0 {
t.Errorf("Expected 0 help lines for non-existent field, got %d", len(helpEmpty))
}
}
func TestCLI_HookConfiguration(t *testing.T) {
tests := []struct {
name string
beforeDeployHook string
afterDeployHook string
expectBeforeHook string
expectAfterHook string
}{
{
name: "no hooks",
beforeDeployHook: "",
afterDeployHook: "",
expectBeforeHook: "",
expectAfterHook: "",
},
{
name: "before hook only",
beforeDeployHook: "echo before",
afterDeployHook: "",
expectBeforeHook: "echo before",
expectAfterHook: "",
},
{
name: "after hook only",
beforeDeployHook: "",
afterDeployHook: "echo after",
expectBeforeHook: "",
expectAfterHook: "echo after",
},
{
name: "both hooks",
beforeDeployHook: "echo before",
afterDeployHook: "echo after",
expectBeforeHook: "echo before",
expectAfterHook: "echo after",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cli := &cli{
BeforeDeployHook: tt.beforeDeployHook,
AfterDeployHook: tt.afterDeployHook,
}
conf := DefaultConfig()
conf.BeforeDeployHook = cli.BeforeDeployHook
conf.AfterDeployHook = cli.AfterDeployHook
if conf.BeforeDeployHook != tt.expectBeforeHook {
t.Errorf("Expected before hook %q, got %q", tt.expectBeforeHook, conf.BeforeDeployHook)
}
if conf.AfterDeployHook != tt.expectAfterHook {
t.Errorf("Expected after hook %q, got %q", tt.expectAfterHook, conf.AfterDeployHook)
}
})
}
}
func TestParsePorts(t *testing.T) {
tests := []struct {
name string
input []string
expected []string
wantErr bool
}{
{
name: "empty input",
input: []string{},
expected: nil,
wantErr: false,
},
{
name: "single port",
input: []string{"8080"},
expected: []string{"8080"},
wantErr: false,
},
{
name: "multiple single ports",
input: []string{"8080", "8081", "8082"},
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "comma-separated ports",
input: []string{"8080,8081,8082"},
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "port range",
input: []string{"8080-8082"},
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "mixed formats",
input: []string{"8080", "8085,8086", "8090-8092"},
expected: []string{"8080", "8085", "8086", "8090", "8091", "8092"},
wantErr: false,
},
{
name: "duplicates removed",
input: []string{"8080", "8081", "8080"},
expected: []string{"8080", "8081"},
wantErr: false,
},
{
name: "sorted output",
input: []string{"8082", "8080", "8081"},
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "invalid port",
input: []string{"invalid"},
wantErr: true,
},
{
name: "port out of range high",
input: []string{"70000"},
wantErr: true,
},
{
name: "port out of range low",
input: []string{"0"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parsePorts(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("parsePorts() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !reflect.DeepEqual(result, tt.expected) {
t.Errorf("parsePorts() = %v, want %v", result, tt.expected)
}
})
}
}
func TestParsePortSpec(t *testing.T) {
tests := []struct {
name string
input string
expected []string
wantErr bool
}{
{
name: "empty string",
input: "",
expected: nil,
wantErr: false,
},
{
name: "single port",
input: "8080",
expected: []string{"8080"},
wantErr: false,
},
{
name: "comma-separated ports",
input: "8080,8081,8082",
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "comma-separated with spaces",
input: "8080, 8081 , 8082",
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "port range",
input: "8080-8082",
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "mixed comma and range",
input: "8080,8085-8087,8090",
expected: []string{"8080", "8085", "8086", "8087", "8090"},
wantErr: false,
},
{
name: "invalid port in comma-separated",
input: "8080,invalid,8082",
wantErr: true,
},
{
name: "invalid range format",
input: "8080-8081-8082",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parsePortSpec(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("parsePortSpec() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !reflect.DeepEqual(result, tt.expected) {
t.Errorf("parsePortSpec() = %v, want %v", result, tt.expected)
}
})
}
}
func TestParsePortRange(t *testing.T) {
tests := []struct {
name string
input string
expected []string
wantErr bool
}{
{
name: "valid range",
input: "8080-8082",
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "single port range",
input: "8080-8080",
expected: []string{"8080"},
wantErr: false,
},
{
name: "range with spaces",
input: " 8080 - 8082 ",
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "reverse range",
input: "8082-8080",
wantErr: true,
},
{
name: "invalid format",
input: "8080-8081-8082",
wantErr: true,
},
{
name: "non-numeric start",
input: "abc-8082",
wantErr: true,
},
{
name: "non-numeric end",
input: "8080-abc",
wantErr: true,
},
{
name: "range too large",
input: "8000-8200",
wantErr: true,
},
{
name: "invalid port in range",
input: "70000-70002",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parsePortRange(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("parsePortRange() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !reflect.DeepEqual(result, tt.expected) {
t.Errorf("parsePortRange() = %v, want %v", result, tt.expected)
}
})
}
}
func TestValidatePort(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
}{
{
name: "valid port",
input: "8080",
wantErr: false,
},
{
name: "minimum valid port",
input: "1",
wantErr: false,
},
{
name: "maximum valid port",
input: "65535",
wantErr: false,
},
{
name: "non-numeric",
input: "abc",
wantErr: true,
},
{
name: "port too low",
input: "0",
wantErr: true,
},
{
name: "port too high",
input: "65536",
wantErr: true,
},
{
name: "negative port",
input: "-1",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validatePort(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("validatePort() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateAndDeduplicatePorts(t *testing.T) {
tests := []struct {
name string
input []string
expected []string
wantErr bool
}{
{
name: "empty slice",
input: []string{},
expected: []string{},
wantErr: false,
},
{
name: "no duplicates",
input: []string{"8080", "8081", "8082"},
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "with duplicates",
input: []string{"8080", "8081", "8080", "8082"},
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "unsorted input",
input: []string{"8082", "8080", "8081"},
expected: []string{"8080", "8081", "8082"},
wantErr: false,
},
{
name: "mixed order with duplicates",
input: []string{"8085", "8080", "8082", "8080", "8081"},
expected: []string{"8080", "8081", "8082", "8085"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := validateAndDeduplicatePorts(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("validateAndDeduplicatePorts() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !reflect.DeepEqual(result, tt.expected) {
t.Errorf("validateAndDeduplicatePorts() = %v, want %v", result, tt.expected)
}
})
}
}
func TestExtractAppNameFromRegistry(t *testing.T) {
tests := []struct {
name string
registry string
want string
}{
// img:// (OCI registry) - container command
{
name: "img - ghcr.io with tag",
registry: "img://ghcr.io/owner/myapp:latest",
want: "myapp",
},
{
name: "img - ghcr.io without tag",
registry: "img://ghcr.io/owner/myapp",
want: "myapp",
},
{
name: "img - docker.io library",
registry: "img://docker.io/library/nginx:1.21",
want: "nginx",
},
{
name: "img - gcr.io",
registry: "img://gcr.io/my-project/myapp",
want: "myapp",
},
{
name: "img - simple image name",
registry: "img://myapp:latest",
want: "myapp",
},
{
name: "img - with query parameters",
registry: "img://ghcr.io/owner/myapp?pre-release=true",
want: "myapp",
},
{
name: "img - with tag and query parameters",
registry: "img://ghcr.io/owner/myapp:v1.0.0?pre-release=true",
want: "myapp",
},
// ghr:// (GitHub Releases)
{
name: "ghr - owner/repo",
registry: "ghr://owner/myrepo",
want: "myrepo",
},
// s3://
{
name: "s3 - simple path",
registry: "s3://us-east-1/bucket/myapp",
want: "myapp",
},
{
name: "s3 - nested path",
registry: "s3://us-east-1/bucket/path/to/myapp",
want: "myapp",
},
// Invalid cases
{
name: "invalid format - no scheme",
registry: "invalid-url",
want: "",
},
{
name: "empty string",
registry: "",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := extractAppNameFromRegistry(tt.registry)
if got != tt.want {
t.Errorf("extractAppNameFromRegistry(%q) = %q, want %q", tt.registry, got, tt.want)
}
})
}
}
func TestCLI_ConfigureServerCommand(t *testing.T) {
tests := []struct {
name string
ports []string
args []string
logFormat string
expectPorts []string
expectCmd string
expectArgs []string
wantErr bool
}{
{
name: "with port",
ports: []string{"8080"},
args: []string{"/opt/app/current/app", "arg1", "arg2"},
logFormat: "text",
expectPorts: []string{"8080"},
expectCmd: "/opt/app/current/app",
expectArgs: []string{"arg1", "arg2"},
wantErr: false,
},
{
name: "without port (job worker)",
ports: []string{},
args: []string{"/opt/worker/current/worker"},
logFormat: "json",
expectPorts: nil,
expectCmd: "/opt/worker/current/worker",
expectArgs: nil,
wantErr: false,
},
{
name: "with multiple ports",
ports: []string{"8080,8081,8082"},
args: []string{"/opt/app/current/app"},
logFormat: "text",
expectPorts: []string{"8080", "8081", "8082"},
expectCmd: "/opt/app/current/app",
expectArgs: nil,
wantErr: false,
},
{
name: "with invalid port",
ports: []string{"invalid"},
args: []string{"/opt/app/current/app"},
logFormat: "text",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var errBuf bytes.Buffer
cli := &cli{
Ports: tt.ports,
args: tt.args,
LogFormat: tt.logFormat,
env: Env{
Err: &errBuf,
},
}
conf := DefaultConfig()
err := cli.configureServerCommand(&conf)
if (err != nil) != tt.wantErr {
t.Errorf("configureServerCommand() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if conf.Command != SERVER {
t.Errorf("Expected command SERVER, got %v", conf.Command)
}
if conf.Starter == nil {
t.Fatal("Expected Starter to be set")
}
if !reflect.DeepEqual(conf.Starter.Ports(), tt.expectPorts) {
t.Errorf("Expected ports %v, got %v", tt.expectPorts, conf.Starter.Ports())
}
if conf.Starter.Command() != tt.expectCmd {
t.Errorf("Expected command %q, got %q", tt.expectCmd, conf.Starter.Command())
}
if !reflect.DeepEqual(conf.Starter.Args(), tt.expectArgs) {
t.Errorf("Expected args %v, got %v", tt.expectArgs, conf.Starter.Args())
}
if conf.Starter.LogFormat() != tt.logFormat {
t.Errorf("Expected logformat %q, got %q", tt.logFormat, conf.Starter.LogFormat())
}
}
})
}
}
func TestDisplayContainerList(t *testing.T) {
tests := []struct {
name string
containers []*container.Info
wantOutput string
}{
{
name: "empty list",
containers: []*container.Info{},
wantOutput: "No containers found.\n",
},
{
name: "single container",
containers: []*container.Info{
{
ID: "abc123",
Name: "myapp-0",
Image: "nginx:latest",
Status: "running",
IPPort: "127.0.0.1:8080",
StartedAt: time.Date(2025, 1, 15, 10, 0, 0, 0, time.UTC),
DeployedAt: time.Date(2025, 1, 15, 10, 0, 0, 0, time.UTC),
},
},
wantOutput: "UPSTREAM",
},
{
name: "multiple containers sorted by name",
containers: []*container.Info{
{
ID: "def456",
Name: "myapp-2",
Image: "nginx:latest",
Status: "running",
IPPort: "127.0.0.1:8082",
StartedAt: time.Date(2025, 1, 15, 10, 2, 0, 0, time.UTC),
DeployedAt: time.Date(2025, 1, 15, 10, 2, 0, 0, time.UTC),
},
{
ID: "abc123",
Name: "myapp-0",
Image: "nginx:latest",
Status: "running",
IPPort: "127.0.0.1:8080",
StartedAt: time.Date(2025, 1, 15, 10, 0, 0, 0, time.UTC),
DeployedAt: time.Date(2025, 1, 15, 10, 0, 0, 0, time.UTC),
},
{
ID: "ghi789",
Name: "myapp-1",
Image: "nginx:latest",
Status: "running",
IPPort: "127.0.0.1:8081",
StartedAt: time.Date(2025, 1, 15, 10, 1, 0, 0, time.UTC),
DeployedAt: time.Date(2025, 1, 15, 10, 1, 0, 0, time.UTC),
},
},
wantOutput: "myapp-0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
c := &cli{
env: Env{
Out: &buf,
Err: &buf,
},
}
c.displayContainerList(tt.containers)
got := buf.String()
if !strings.Contains(got, tt.wantOutput) {
t.Errorf("displayContainerList() output does not contain %q\nGot:\n%s", tt.wantOutput, got)
}
// For multiple containers, verify they are sorted
if tt.name == "multiple containers sorted by name" {
lines := strings.Split(strings.TrimSpace(got), "\n")
if len(lines) < 4 {
t.Errorf("Expected at least 4 lines (header + 3 containers), got %d", len(lines))
return
}
// Check that myapp-0 comes before myapp-1, which comes before myapp-2
output := strings.Join(lines, "\n")
idx0 := strings.Index(output, "myapp-0")
idx1 := strings.Index(output, "myapp-1")
idx2 := strings.Index(output, "myapp-2")
if idx0 == -1 || idx1 == -1 || idx2 == -1 {
t.Errorf("Not all container names found in output")
return
}
if idx0 >= idx1 || idx1 >= idx2 {
t.Errorf("Containers are not sorted correctly: myapp-0 at %d, myapp-1 at %d, myapp-2 at %d",
idx0, idx1, idx2)
}
}
})
}
}