-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathmain_test.go
More file actions
1246 lines (1107 loc) · 37.5 KB
/
main_test.go
File metadata and controls
1246 lines (1107 loc) · 37.5 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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022 NetApp, Inc. All Rights Reserved.
package main
import (
"context"
"flag"
"io"
"math"
"math/rand"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"testing"
"time"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/utils/pointer"
"github.com/netapp/trident/config"
)
func TestMain(m *testing.M) {
// Disable any standard log output
log.SetOutput(io.Discard)
os.Exit(m.Run())
}
func TestMain_processCommandLineArgs_QPS(t *testing.T) {
type parameters struct {
k8sApiQPS float64
expectedK8sApiQPS float32
}
tests := map[string]parameters{
"QPS is set to 100.0": {
k8sApiQPS: 100.0,
expectedK8sApiQPS: 100.0,
},
"QPS is set to maxFloat32": {
k8sApiQPS: math.MaxFloat32,
expectedK8sApiQPS: math.MaxFloat32,
},
"QPS is set to maxFloat64 + 1": {
k8sApiQPS: math.MaxFloat64 + 1,
expectedK8sApiQPS: config.DefaultK8sAPIQPS,
},
}
for name, params := range tests {
t.Run(name, func(t *testing.T) {
csiEndpoint = pointer.String("true")
useInMemory = pointer.Bool(true)
k8sApiQPS = ¶ms.k8sApiQPS
processCmdLineArgs(context.TODO())
assert.Equal(t, params.expectedK8sApiQPS, config.K8sAPIQPS)
})
}
}
func TestMain_processCommandLineArgs_TagriTimeout(t *testing.T) {
originalTagriTimeout := tagriTimeout
originalConfigTagriTimeout := config.TagriTimeout
defer func() {
tagriTimeout = originalTagriTimeout
config.TagriTimeout = originalConfigTagriTimeout
}()
tests := []struct {
name string
tagriTimeoutSecs int
expectedDuration time.Duration
}{
{
name: "default tagri_timeout (5 minutes)",
tagriTimeoutSecs: 300,
expectedDuration: 300 * time.Second,
},
{
name: "custom tagri_timeout (10 minutes)",
tagriTimeoutSecs: 600,
expectedDuration: 600 * time.Second,
},
{
name: "custom tagri_timeout (1 minute)",
tagriTimeoutSecs: 60,
expectedDuration: 60 * time.Second,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
customTimeout := time.Duration(tt.tagriTimeoutSecs) * time.Second
tagriTimeout = &customTimeout
csiEndpoint = pointer.String("true")
useInMemory = pointer.Bool(true)
k8sApiQPS = pointer.Float64(config.DefaultK8sAPIQPS)
k8sApiBurst = pointer.Int(config.DefaultK8sAPIBurst)
processCmdLineArgs(context.TODO())
assert.Equal(t, tt.expectedDuration, config.TagriTimeout,
"config.TagriTimeout should be set from --tagri_timeout flag")
})
}
}
func TestGetenvAsPointerToBool(t *testing.T) {
ctx := context.Background()
testKey := "TEST_BOOL_VAR"
// Clean up environment variable before and after test
defer func() {
os.Unsetenv(testKey)
}()
tests := []struct {
name string
envValue string
setEnv bool
expected bool
description string
}{
{
name: "Environment variable not set",
setEnv: false,
expected: false,
description: "Should return false when environment variable is not set",
},
{
name: "Environment variable set to empty string",
envValue: "",
setEnv: true,
expected: false,
description: "Should return false when environment variable is empty",
},
{
name: "Environment variable set to 'true'",
envValue: "true",
setEnv: true,
expected: true,
description: "Should return true when environment variable is 'true'",
},
{
name: "Environment variable set to 'TRUE'",
envValue: "TRUE",
setEnv: true,
expected: true,
description: "Should return true when environment variable is 'TRUE' (case insensitive)",
},
{
name: "Environment variable set to 'True'",
envValue: "True",
setEnv: true,
expected: true,
description: "Should return true when environment variable is 'True' (mixed case)",
},
{
name: "Environment variable set to 'false'",
envValue: "false",
setEnv: true,
expected: false,
description: "Should return false when environment variable is 'false'",
},
{
name: "Environment variable set to 'FALSE'",
envValue: "FALSE",
setEnv: true,
expected: false,
description: "Should return false when environment variable is 'FALSE'",
},
{
name: "Environment variable set to random string",
envValue: "random",
setEnv: true,
expected: false,
description: "Should return false when environment variable is not 'true'",
},
{
name: "Environment variable set to '1'",
envValue: "1",
setEnv: true,
expected: false,
description: "Should return false when environment variable is '1' (not 'true')",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clean up before each test
os.Unsetenv(testKey)
if tt.setEnv {
os.Setenv(testKey, tt.envValue)
}
result := getenvAsPointerToBool(ctx, testKey)
require.NotNil(t, result, "Result should not be nil")
assert.Equal(t, tt.expected, *result, tt.description)
})
}
}
func TestEnsureDockerPluginExecPath(t *testing.T) {
ctx := context.Background()
originalPath := os.Getenv("PATH")
// Restore original PATH after test
defer func() {
os.Setenv("PATH", originalPath)
}()
tests := []struct {
name string
initialPath string
expected string
description string
}{
{
name: "PATH does not contain /netapp",
initialPath: "/usr/bin:/bin:/usr/local/bin",
expected: "/netapp:/usr/bin:/bin:/usr/local/bin",
description: "Should prepend /netapp to PATH when not present",
},
{
name: "PATH already contains /netapp at the beginning",
initialPath: "/netapp:/usr/bin:/bin",
expected: "/netapp:/usr/bin:/bin",
description: "Should not modify PATH when /netapp is already present",
},
{
name: "PATH contains /netapp in the middle",
initialPath: "/usr/bin:/netapp:/bin",
expected: "/usr/bin:/netapp:/bin",
description: "Should not modify PATH when /netapp is already present in middle",
},
{
name: "PATH contains /netapp at the end",
initialPath: "/usr/bin:/bin:/netapp",
expected: "/usr/bin:/bin:/netapp",
description: "Should not modify PATH when /netapp is already present at end",
},
{
name: "Empty PATH",
initialPath: "",
expected: "/netapp:",
description: "Should prepend /netapp to empty PATH",
},
{
name: "PATH with similar but different paths",
initialPath: "/usr/bin:/bin:/opt/netapp/bin",
expected: "/usr/bin:/bin:/opt/netapp/bin",
description: "Should not modify PATH when /netapp is already present in another path",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set initial PATH
os.Setenv("PATH", tt.initialPath)
// Call the function
ensureDockerPluginExecPath(ctx)
// Check the result
actualPath := os.Getenv("PATH")
assert.Equal(t, tt.expected, actualPath, tt.description)
})
}
}
func TestProcessDockerPluginArgs(t *testing.T) {
ctx := context.Background()
// Save original values
originalEnableREST := enableREST
originalConfigPath := configPath
originalPath := os.Getenv("PATH")
defer func() {
// Restore original values
enableREST = originalEnableREST
configPath = originalConfigPath
os.Setenv("PATH", originalPath)
os.Unsetenv(config.DockerPluginModeEnvVariable)
os.Unsetenv("rest")
os.Unsetenv("config")
}()
t.Run("Not in docker plugin mode", func(t *testing.T) {
// Ensure we're not in docker plugin mode
os.Unsetenv(config.DockerPluginModeEnvVariable)
err := processDockerPluginArgs(ctx)
assert.NoError(t, err, "Should return no error when not in docker plugin mode")
})
t.Run("Docker plugin mode with rest environment variable true", func(t *testing.T) {
os.Setenv(config.DockerPluginModeEnvVariable, "1")
os.Setenv("rest", "true")
err := processDockerPluginArgs(ctx)
assert.NoError(t, err, "Should return no error when processing valid docker plugin args")
assert.True(t, *enableREST, "enableREST should be set to true")
})
t.Run("Docker plugin mode with rest environment variable false", func(t *testing.T) {
os.Setenv(config.DockerPluginModeEnvVariable, "1")
os.Setenv("rest", "false")
err := processDockerPluginArgs(ctx)
assert.NoError(t, err, "Should return no error when processing valid docker plugin args")
assert.False(t, *enableREST, "enableREST should be set to false")
})
t.Run("Docker plugin mode with rest environment variable empty", func(t *testing.T) {
os.Setenv(config.DockerPluginModeEnvVariable, "1")
os.Setenv("rest", "")
err := processDockerPluginArgs(ctx)
assert.NoError(t, err, "Should return no error when processing valid docker plugin args")
assert.False(t, *enableREST, "enableREST should be set to false when rest env var is empty")
})
t.Run("Docker plugin mode with non-existent config file", func(t *testing.T) {
os.Setenv(config.DockerPluginModeEnvVariable, "1")
os.Setenv("config", "/non/existent/config.json")
err := processDockerPluginArgs(ctx)
assert.Error(t, err, "Should return error when config file does not exist")
assert.Contains(t, err.Error(), "does not exist", "Error should mention file does not exist")
})
t.Run("Docker plugin mode ensures PATH contains /netapp", func(t *testing.T) {
// Set a PATH that doesn't contain /netapp
initialPath := "/usr/bin:/bin"
os.Setenv("PATH", initialPath)
os.Setenv(config.DockerPluginModeEnvVariable, "1")
// Explicitly unset config env var to avoid config file validation
os.Unsetenv("config")
err := processDockerPluginArgs(ctx)
assert.NoError(t, err, "Should return no error")
updatedPath := os.Getenv("PATH")
assert.True(t, strings.Contains(updatedPath, "/netapp"), "PATH should contain /netapp after processing docker plugin args")
})
}
func TestProcessDockerPluginArgsWithMocking(t *testing.T) {
ctx := context.Background()
// Save original values
originalEnableREST := enableREST
originalConfigPath := configPath
originalPath := os.Getenv("PATH")
defer func() {
// Restore original values
enableREST = originalEnableREST
configPath = originalConfigPath
os.Setenv("PATH", originalPath)
os.Unsetenv(config.DockerPluginModeEnvVariable)
os.Unsetenv("rest")
os.Unsetenv("config")
}()
t.Run("Docker plugin mode with config file in plugin location", func(t *testing.T) {
os.Setenv(config.DockerPluginModeEnvVariable, "1")
// Create a config file with absolute path that starts with the plugin location
tempConfig := filepath.Join(config.DockerPluginConfigLocation, "test-config.json")
// Set config env to absolute path that includes plugin location
os.Setenv("config", tempConfig)
err := processDockerPluginArgs(ctx)
// This will fail because the file doesn't actually exist, but we'll get the right error
assert.Error(t, err, "Should return error for non-existent file")
assert.Contains(t, err.Error(), "does not exist", "Should be 'does not exist' error")
})
t.Run("Docker plugin mode with permission denied error", func(t *testing.T) {
os.Setenv(config.DockerPluginModeEnvVariable, "1")
// Just test with a nonexistent file path that doesn't trigger the specific "does not exist" logic
// The function checks !strings.HasPrefix(configFile, config.DockerPluginConfigLocation)
// So we'll use an absolute path to bypass that logic and test the stat error path
nonExistentFile := "/tmp/nonexistent_test_config_dir_12345/config.json"
os.Setenv("config", nonExistentFile)
err := processDockerPluginArgs(ctx)
assert.Error(t, err, "Should return error for nonexistent file")
assert.Contains(t, err.Error(), "does not exist", "Should be 'does not exist' error for this path")
})
}
func TestPrintFlag(t *testing.T) {
tests := []struct {
name string
flagName string
flagValue string
description string
}{
{
name: "String flag",
flagName: "test-string",
flagValue: "test-value",
description: "Should call printFlag without error",
},
{
name: "Boolean flag",
flagName: "test-bool",
flagValue: "true",
description: "Should call printFlag without error",
},
{
name: "Empty value flag",
flagName: "test-empty",
flagValue: "",
description: "Should call printFlag without error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a flag for testing
testFlag := &flag.Flag{
Name: tt.flagName,
Value: &testFlagValue{value: tt.flagValue},
}
// Call printFlag - this should not panic or error
assert.NotPanics(t, func() {
printFlag(testFlag)
}, "printFlag should not panic")
})
}
}
// testFlagValue is a helper struct to mock flag.Value interface for testing
type testFlagValue struct {
value string
}
func (t *testFlagValue) String() string {
return t.value
}
func (t *testFlagValue) Set(s string) error {
t.value = s
return nil
}
func TestMain_processCommandLineArgs_Comprehensive(t *testing.T) {
// Save original values
originalStoreClient := storeClient
originalUseInMemory := useInMemory
originalUsePassthrough := usePassthrough
originalUseCRD := useCRD
originalCsiEndpoint := csiEndpoint
originalConfigPath := configPath
originalDockerPluginMode := dockerPluginMode
originalK8sApiQPS := k8sApiQPS
originalK8sApiBurst := k8sApiBurst
defer func() {
// Restore original values
storeClient = originalStoreClient
useInMemory = originalUseInMemory
usePassthrough = originalUsePassthrough
useCRD = originalUseCRD
csiEndpoint = originalCsiEndpoint
configPath = originalConfigPath
dockerPluginMode = originalDockerPluginMode
k8sApiQPS = originalK8sApiQPS
k8sApiBurst = originalK8sApiBurst
}()
tests := []struct {
name string
setupFunc func()
expectedK8sQPS float32
expectedK8sBurst int
expectedEnableCSI bool
expectedEnableDocker bool
description string
}{
{
name: "CSI frontend with in-memory store",
setupFunc: func() {
csiEndpoint = pointer.String("/tmp/csi.sock")
useInMemory = pointer.Bool(true)
usePassthrough = pointer.Bool(false)
useCRD = pointer.Bool(false)
k8sApiQPS = pointer.Float64(50.0)
k8sApiBurst = pointer.Int(100)
},
expectedK8sQPS: 50.0,
expectedK8sBurst: 100,
expectedEnableCSI: true,
expectedEnableDocker: false,
description: "Should configure CSI frontend with in-memory store",
},
{
name: "In-memory store only (for testing)",
setupFunc: func() {
csiEndpoint = pointer.String("")
dockerPluginMode = pointer.Bool(false)
configPath = pointer.String("")
useInMemory = pointer.Bool(true)
usePassthrough = pointer.Bool(false)
useCRD = pointer.Bool(false)
k8sApiQPS = pointer.Float64(config.DefaultK8sAPIQPS)
k8sApiBurst = pointer.Int(config.DefaultK8sAPIBurst)
},
expectedK8sQPS: config.DefaultK8sAPIQPS,
expectedK8sBurst: config.DefaultK8sAPIBurst,
expectedEnableCSI: false,
expectedEnableDocker: false,
description: "Should configure in-memory store for testing",
},
{
name: "K8s API Burst configuration",
setupFunc: func() {
csiEndpoint = pointer.String("/tmp/csi.sock")
useInMemory = pointer.Bool(true)
k8sApiQPS = pointer.Float64(75.5)
k8sApiBurst = pointer.Int(150)
},
expectedK8sQPS: 75.5,
expectedK8sBurst: 150,
expectedEnableCSI: true,
expectedEnableDocker: false,
description: "Should properly configure K8s API burst settings",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Reset to defaults
config.K8sAPIQPS = config.DefaultK8sAPIQPS
config.K8sAPIBurst = config.DefaultK8sAPIBurst
storeClient = nil
// Setup test conditions
tt.setupFunc()
// Call the function
processCmdLineArgs(context.Background())
// Verify K8s API settings
assert.Equal(t, tt.expectedK8sQPS, config.K8sAPIQPS, "K8s API QPS should match expected value")
assert.Equal(t, tt.expectedK8sBurst, config.K8sAPIBurst, "K8s API Burst should match expected value")
// Verify frontend configuration
actualEnableCSI := *csiEndpoint != ""
actualEnableDocker := (*dockerPluginMode || *configPath != "") && !actualEnableCSI
assert.Equal(t, tt.expectedEnableCSI, actualEnableCSI, "CSI enablement should match expected")
assert.Equal(t, tt.expectedEnableDocker, actualEnableDocker, "Docker enablement should match expected")
// Verify store client is created
assert.NotNil(t, storeClient, "Store client should be created")
// For in-memory store, we can verify UsingPassthroughStore is false
assert.False(t, config.UsingPassthroughStore, "Should not be using passthrough store for in-memory")
})
}
}
func TestMainFunction(t *testing.T) {
// Test the main function by running it as a subprocess
// This is the standard way to test main functions that call os.Exit()
if os.Getenv("TEST_MAIN_FUNCTION") == "1" {
// This block runs in the subprocess
// Set up minimal valid configuration to avoid immediate exit
// Reset command line args to minimal set
os.Args = []string{"trident", "--no_persistence", "--csi_endpoint=/tmp/test.sock", "--csi_node_name=test-node", "--csi_role=node"}
// Set required environment to avoid early exits
os.Setenv("PATH", "/usr/bin:/bin")
// Override global variables for testing
useInMemory = pointer.Bool(true)
csiEndpoint = pointer.String("/tmp/test.sock")
csiNodeName = pointer.String("test-node")
csiRole = pointer.String("node")
enableREST = pointer.Bool(false)
enableHTTPSREST = pointer.Bool(false)
enableMetrics = pointer.Bool(false)
// Call main - this will eventually exit, but we want to test the early initialization
main()
return
}
tests := []struct {
name string
args []string
env map[string]string
expectExit bool
expectedCode int
description string
}{
{
name: "Invalid log level",
args: []string{"trident", "--log_level=invalid", "--no_persistence"},
env: map[string]string{},
expectExit: true,
expectedCode: 1,
description: "Should exit with code 1 for invalid log level",
},
{
name: "Invalid log format",
args: []string{"trident", "--log_format=invalid", "--no_persistence"},
env: map[string]string{},
expectExit: true,
expectedCode: 1,
description: "Should exit with code 1 for invalid log format",
},
{
name: "Help flag",
args: []string{"trident", "--help"},
env: map[string]string{},
expectExit: true,
expectedCode: 2, // flag package exits with 2 for help
description: "Should exit with code 2 for help flag",
},
{
name: "Version info with minimal config",
args: []string{"trident", "--no_persistence"},
env: map[string]string{"TEST_MAIN_FUNCTION": "1"},
expectExit: true,
expectedCode: 1, // Will eventually fail trying to create orchestrator, but tests early init
description: "Should run early initialization before failing on orchestrator creation",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command(os.Args[0], "-test.run=TestMainFunction")
cmd.Env = append(os.Environ(), "TEST_MAIN_FUNCTION=1")
// Set custom args through environment
for k, v := range tt.env {
cmd.Env = append(cmd.Env, k+"="+v)
}
// Set the args for the subprocess
if len(tt.args) > 1 {
cmd.Args = append(cmd.Args, tt.args[1:]...)
}
err := cmd.Run()
if tt.expectExit {
// Check that the process exited with expected code
if exitError, ok := err.(*exec.ExitError); ok {
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
actualCode := status.ExitStatus()
if tt.expectedCode != 0 {
assert.Equal(t, tt.expectedCode, actualCode, tt.description)
} else {
assert.NotEqual(t, 0, actualCode, "Process should have exited with non-zero code")
}
}
} else if err == nil && tt.expectedCode == 0 {
// Process exited successfully
assert.NoError(t, err, "Process should have exited successfully")
} else if err != nil {
// Some other error occurred
t.Logf("Process exited with error: %v", err)
}
} else {
assert.NoError(t, err, "Process should not have failed")
}
})
}
}
// TestMainFunctionInitialization tests the early initialization parts of main
func TestMainFunctionInitialization(t *testing.T) {
// Test aspects of main function that can be tested without running the full function
t.Run("GOMAXPROCS behavior", func(t *testing.T) {
originalMaxProcs := runtime.GOMAXPROCS(0) // Get current value
defer runtime.GOMAXPROCS(originalMaxProcs) // Restore after test
// Test that GOMAXPROCS is set to number of CPUs
expectedProcs := runtime.NumCPU()
actualProcs := runtime.GOMAXPROCS(expectedProcs)
assert.Equal(t, expectedProcs, runtime.GOMAXPROCS(0), "GOMAXPROCS should be set to number of CPUs")
// Restore the previous value
runtime.GOMAXPROCS(actualProcs)
})
t.Run("Force detach flag creation on Linux", func(t *testing.T) {
// This test verifies the OS-specific flag creation logic
if runtime.GOOS == "linux" {
// On Linux, enableForceDetach should be available
assert.NotNil(t, enableForceDetach, "enableForceDetach flag should be created on Linux")
} else {
// On non-Linux platforms, enableForceDetach might not be initialized in tests
// but the logic should handle this gracefully
t.Logf("Running on %s, force detach flag is Linux-specific", runtime.GOOS)
}
})
t.Run("Docker plugin mode environment setup", func(t *testing.T) {
originalMode := dockerPluginMode
originalEnv := os.Getenv("DOCKER_PLUGIN_MODE")
defer func() {
dockerPluginMode = originalMode
if originalEnv == "" {
os.Unsetenv("DOCKER_PLUGIN_MODE")
} else {
os.Setenv("DOCKER_PLUGIN_MODE", originalEnv)
}
}()
// Test docker plugin mode environment variable setting
dockerPluginMode = pointer.Bool(true)
// Simulate the logic from main function
if *dockerPluginMode {
os.Setenv("DOCKER_PLUGIN_MODE", "1")
}
assert.Equal(t, "1", os.Getenv("DOCKER_PLUGIN_MODE"), "Docker plugin mode environment should be set")
})
t.Run("Random seed initialization", func(t *testing.T) {
// Test that random seed can be set without error
// This simulates the rand.Seed(time.Now().UnixNano()) call in main
beforeTime := time.Now().UnixNano()
time.Sleep(1 * time.Millisecond) // Ensure time difference
afterTime := time.Now().UnixNano()
assert.True(t, afterTime > beforeTime, "Time should advance for random seed")
// Test that we can set the seed (this is what main does)
assert.NotPanics(t, func() {
rand.Seed(time.Now().UnixNano())
}, "Setting random seed should not panic")
})
}
func TestProcessCmdLineArgsErrorPaths(t *testing.T) {
// Test the error paths in processCmdLineArgs using subprocess testing
// since the function calls Fatal which exits the process
if os.Getenv("TEST_PROCESSCMDLINEARGS_ERRORS") == "1" {
// This block runs in the subprocess
ctx := context.Background()
// Set up conditions based on environment variables
testCase := os.Getenv("TEST_CASE")
switch testCase {
case "insufficient_args":
// No frontends and no in-memory
csiEndpoint = pointer.String("")
dockerPluginMode = pointer.Bool(false)
configPath = pointer.String("")
useInMemory = pointer.Bool(false)
case "multiple_stores":
// Multiple store types
csiEndpoint = pointer.String("/tmp/csi.sock")
useInMemory = pointer.Bool(true)
usePassthrough = pointer.Bool(true)
useCRD = pointer.Bool(false)
case "passthrough_error":
// Docker mode with invalid config path for passthrough store
csiEndpoint = pointer.String("")
dockerPluginMode = pointer.Bool(true)
configPath = pointer.String("/invalid/path/config.json")
useInMemory = pointer.Bool(false)
usePassthrough = pointer.Bool(false) // Will be inferred to true
useCRD = pointer.Bool(false)
}
// Call processCmdLineArgs - this will eventually call Fatal and exit
processCmdLineArgs(ctx)
return
}
tests := []struct {
name string
testCase string
expectExit bool
expectedCode int
description string
}{
{
name: "Insufficient arguments",
testCase: "insufficient_args",
expectExit: true,
expectedCode: 1,
description: "Should exit with code 1 when no frontends and no in-memory mode",
},
{
name: "Multiple store types",
testCase: "multiple_stores",
expectExit: true,
expectedCode: 1,
description: "Should exit with code 1 when multiple store types configured",
},
{
name: "Passthrough store creation error",
testCase: "passthrough_error",
expectExit: true,
expectedCode: 1,
description: "Should exit with code 1 when passthrough store creation fails",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command(os.Args[0], "-test.run=TestProcessCmdLineArgsErrorPaths")
cmd.Env = append(os.Environ(),
"TEST_PROCESSCMDLINEARGS_ERRORS=1",
"TEST_CASE="+tt.testCase,
)
err := cmd.Run()
if tt.expectExit {
// Check that the process exited with expected code
if exitError, ok := err.(*exec.ExitError); ok {
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
actualCode := status.ExitStatus()
assert.Equal(t, tt.expectedCode, actualCode, tt.description)
}
} else if err != nil {
// Some other error occurred - still indicates Fatal was called
t.Logf("Process exited with error (expected): %v", err)
}
} else {
assert.NoError(t, err, "Process should not have failed")
}
})
}
}
func TestProcessCmdLineArgsAdvancedCoverage(t *testing.T) {
// Test additional paths in processCmdLineArgs for better coverage
// Save original values
originalStoreClient := storeClient
originalUseInMemory := useInMemory
originalConfigPath := configPath
defer func() {
// Restore original values
storeClient = originalStoreClient
useInMemory = originalUseInMemory
configPath = originalConfigPath
}()
t.Run("In-memory store simple test", func(t *testing.T) {
// Reset state
storeClient = nil
useInMemory = pointer.Bool(true)
// This should complete successfully
assert.NotPanics(t, func() {
processCmdLineArgs(context.Background())
}, "Should complete successfully with in-memory store")
assert.NotNil(t, storeClient, "Store client should be created")
})
}
func TestMainFunctionAdditionalPaths(t *testing.T) {
// Test additional paths in main function using subprocess testing
if os.Getenv("TEST_MAIN_ADDITIONAL") == "1" {
// This block runs in the subprocess
testCase := os.Getenv("TEST_CASE")
switch testCase {
case "docker_plugin_args_error":
// Set up a condition that will cause processDockerPluginArgs to fail
os.Setenv(config.DockerPluginModeEnvVariable, "1")
os.Setenv("config", "/invalid/nonexistent/config.json")
dockerPluginMode = pointer.Bool(true)
case "init_log_level_error":
// Set up invalid log level
debug = pointer.Bool(false)
logLevel = pointer.String("invalid-level")
case "init_log_format_error":
// Set up invalid log format
logFormat = pointer.String("invalid-format")
}
// Call main - this will exit with an error
main()
return
}
tests := []struct {
name string
testCase string
expectExit bool
expectedCode int
description string
}{
{
name: "Docker plugin args error",
testCase: "docker_plugin_args_error",
expectExit: true,
expectedCode: 1,
description: "Should exit with code 1 when processDockerPluginArgs fails",
},
{
name: "Init log level error",
testCase: "init_log_level_error",
expectExit: true,
expectedCode: 1,
description: "Should exit with code 1 when InitLogLevel fails",
},
{
name: "Init log format error",
testCase: "init_log_format_error",
expectExit: true,
expectedCode: 1,
description: "Should exit with code 1 when InitLogFormat fails",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command(os.Args[0], "-test.run=TestMainFunctionAdditionalPaths")
cmd.Env = append(os.Environ(),
"TEST_MAIN_ADDITIONAL=1",
"TEST_CASE="+tt.testCase,
)
err := cmd.Run()
if tt.expectExit {
// Check that the process exited with expected code
if exitError, ok := err.(*exec.ExitError); ok {
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
actualCode := status.ExitStatus()
assert.Equal(t, tt.expectedCode, actualCode, tt.description)
}
} else if err != nil {
// Some other error occurred - still indicates exit was called
t.Logf("Process exited with error (expected): %v", err)
}
} else {
assert.NoError(t, err, "Process should not have failed")
}
})
}
}
func TestMainFunctionAdvancedPaths(t *testing.T) {
// Test additional main function paths using subprocess testing
if os.Getenv("TEST_MAIN_ADVANCED") == "1" {
// This block runs in the subprocess
testCase := os.Getenv("TEST_CASE")
switch testCase {
case "debug_flag_coverage":
// Test debug flag path - but fail early to avoid long initialization
debug = pointer.Bool(true)
logLevel = pointer.String("info") // Will be overridden to debug
// Don't use in-memory store to force early exit due to missing k8s config
case "concurrency_enabled":
// Test concurrency path - but fail early
enableConcurrency = pointer.Bool(true)
// Don't use in-memory store to force early exit due to missing k8s config
case "metrics_enabled_with_port":
// Test metrics enabled with port - but fail early
enableMetrics = pointer.Bool(true)
metricsPort = pointer.String("8080")
metricsAddress = pointer.String("localhost")
// Don't use in-memory store to force early exit due to missing k8s config
case "metrics_enabled_no_port":
// Test metrics enabled without port (should warn) - but fail early
enableMetrics = pointer.Bool(true)
metricsPort = pointer.String("")
// Don't use in-memory store to force early exit due to missing k8s config