forked from fluid-cloudnative/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
1241 lines (1194 loc) · 33.4 KB
/
utils_test.go
File metadata and controls
1241 lines (1194 loc) · 33.4 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 2021 The Fluid Authors.
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 alluxio
import (
"context"
"fmt"
"os"
"reflect"
"testing"
. "github.com/agiledragon/gomonkey/v2"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
"github.com/fluid-cloudnative/fluid/pkg/utils/kubeclient"
)
func TestIsFluidNativeScheme(t *testing.T) {
var tests = []struct {
mountPoint string
expect bool
}{
{"local:///test",
true},
{
"pvc://test",
true,
}, {
"oss://test",
false,
},
}
for _, test := range tests {
result := common.IsFluidNativeScheme(test.mountPoint)
if result != test.expect {
t.Errorf("expect %v for %s, but got %v", test.expect, test.mountPoint, result)
}
}
}
func TestAlluxioEngine_getInitUserDir(t *testing.T) {
type fields struct {
runtime *datav1alpha1.AlluxioRuntime
name string
namespace string
runtimeType string
Log logr.Logger
Client client.Client
retryShutdown int32
}
tests := []struct {
name string
fields fields
want string
}{
{name: "test",
fields: fields{runtime: &datav1alpha1.AlluxioRuntime{
TypeMeta: v1.TypeMeta{},
ObjectMeta: v1.ObjectMeta{},
Spec: datav1alpha1.AlluxioRuntimeSpec{},
Status: datav1alpha1.RuntimeStatus{},
}, name: "test", namespace: "default", runtimeType: "alluxio", Log: fake.NullLogger()},
want: fmt.Sprintf("/tmp/fluid/%s/%s", "default", "test"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &AlluxioEngine{
runtime: tt.fields.runtime,
name: tt.fields.name,
namespace: tt.fields.namespace,
runtimeType: tt.fields.runtimeType,
Log: tt.fields.Log,
Client: tt.fields.Client,
retryShutdown: tt.fields.retryShutdown,
}
if got := e.getInitUserDir(); got != tt.want {
t.Errorf("AlluxioEngine.getInitUserDir() = %v, want %v", got, tt.want)
}
})
}
}
func TestAlluxioEngine_getInitUsersArgs(t *testing.T) {
type fields struct {
runtime *datav1alpha1.AlluxioRuntime
Log logr.Logger
Client client.Client
}
f := func(s int64) *int64 {
return &s
}
tests := []struct {
name string
fields fields
want []string
}{
{name: "test",
fields: fields{
runtime: &datav1alpha1.AlluxioRuntime{
TypeMeta: v1.TypeMeta{},
ObjectMeta: v1.ObjectMeta{},
Spec: datav1alpha1.AlluxioRuntimeSpec{RunAs: &datav1alpha1.User{UID: f(int64(1000)), GID: f(int64(1000)),
UserName: "test", GroupName: "a"}},
Status: datav1alpha1.RuntimeStatus{},
},
},
want: []string{"1000:test:1000", "1000:a"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := utils.GetInitUsersArgs(tt.fields.runtime.Spec.RunAs)
var ne bool
for i, src := range got {
if src != tt.want[i] {
ne = false
}
}
if ne {
t.Errorf("AlluxioEngine.getInitUsersArgs() = %v, want %v", got, tt.want)
}
})
}
}
func TestMountRootWithEnvSet(t *testing.T) {
var testCases = []struct {
input string
expected string
}{
{"/var/lib/mymount", "/var/lib/mymount/alluxio"},
}
for _, tc := range testCases {
t.Setenv(utils.MountRoot, tc.input)
if tc.expected != getMountRoot() {
t.Errorf("expected %#v, got %#v",
tc.expected, getMountRoot())
}
}
}
func TestMountRootWithoutEnvSet(t *testing.T) {
var testCases = []struct {
input string
expected string
}{
{"/var/lib/mymount", "/alluxio"},
}
for _, tc := range testCases {
os.Unsetenv(utils.MountRoot)
if tc.expected != getMountRoot() {
t.Errorf("expected %#v, got %#v",
tc.expected, getMountRoot())
}
}
}
// Test_isPortInUsed tests the functionality of the isPortInUsed function.
// This function checks whether a specified port is in the list of used ports.
// Test cases include:
// - Checking if a port is in the list of used ports.
//
// Each test case calls the isPortInUsed function and verifies if the returned value matches the expected result.
// If the returned value does not match the expected result, the test fails and outputs an error message.
func Test_isPortInUsed(t *testing.T) {
type args struct {
port int
usedPorts []int
}
tests := []struct {
name string
args args
want bool
}{
{name: "test",
args: args{
port: 20000,
usedPorts: []int{20000, 20001, 20002, 20003, 20004, 20005, 20006, 20007, 20008},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isPortInUsed(tt.args.port, tt.args.usedPorts); got != tt.want {
t.Errorf("isPortInUsed() = %v, want %v", got, tt.want)
}
})
}
}
// Test_lookUpUsedCapacity verifies the functionality of lookUpUsedCapacity in retrieving used capacity values based on node identifiers.
// This test validates two key scenarios:
// 1. Capacity lookup using the node's internal IP address (NodeInternalIP type).
// 2. Capacity lookup using the node's internal DNS hostname (NodeInternalDNS type).
// For each case, it checks if the returned value matches the expected capacity from the provided map.
//
// Parameters:
// - t (testing.T): The test object to run the test case.
//
// Returns:
// - None.
func Test_lookUpUsedCapacity(t *testing.T) {
type args struct {
node corev1.Node
usedCapacityMap map[string]int64
}
internalIP := "192.168.1.147"
var usageForInternalIP int64 = 1024
internalHost := "slave001"
var usageForInternalHost int64 = 4096
usedCapacityMap := map[string]int64{}
usedCapacityMap[internalIP] = usageForInternalIP
usedCapacityMap[internalHost] = usageForInternalHost
tests := []struct {
name string
args args
want int64
}{
{
name: "test_lookUpUsedCapacity_ip",
args: args{
node: corev1.Node{
Status: corev1.NodeStatus{
Addresses: []corev1.NodeAddress{
{
Type: corev1.NodeInternalIP,
Address: internalIP,
},
},
},
},
usedCapacityMap: usedCapacityMap,
},
want: usageForInternalIP,
},
{
name: "test_lookUpUsedCapacity_hostname",
args: args{
node: corev1.Node{
Status: corev1.NodeStatus{
Addresses: []corev1.NodeAddress{
{
Type: corev1.NodeInternalDNS,
Address: internalHost,
},
},
},
},
usedCapacityMap: usedCapacityMap,
},
want: usageForInternalHost,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := lookUpUsedCapacity(tt.args.node, tt.args.usedCapacityMap); got != tt.want {
t.Errorf("lookUpUsedCapacity() = %v, want %v", got, tt.want)
}
})
}
}
func mockExecCommandInContainerForGetFileCount() (stdout string, stderr string, err error) {
r := `Master.FilesCompleted (Type: COUNTER, Value: 1,000)`
return r, "", nil
}
func mockExecCommandInContainerForWorkerUsedCapacity() (stdout string, stderr string, err error) {
r := `Capacity information for all workers:
Total Capacity: 4096.00MB
Tier: MEM Size: 4096.00MB
Used Capacity: 443.89MB
Tier: MEM Size: 443.89MB
Used Percentage: 10%
Free Percentage: 90%
Worker Name Last Heartbeat Storage MEM
192.168.1.147 0 capacity 2048.00MB
used 443.89MB (21%)
192.168.1.146 0 capacity 2048.00MB
used 0B (0%)`
return r, "", nil
}
func TestGetDataSetFileNum(t *testing.T) {
type fields struct {
runtime *datav1alpha1.AlluxioRuntime
name string
namespace string
Log logr.Logger
}
tests := []struct {
name string
fields fields
want string
wantErr bool
}{
{
name: "test0",
fields: fields{
runtime: &datav1alpha1.AlluxioRuntime{
ObjectMeta: v1.ObjectMeta{
Name: "spark",
Namespace: "default",
},
},
name: "spark",
namespace: "default",
Log: fake.NullLogger(),
},
want: "1000",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &AlluxioEngine{
runtime: tt.fields.runtime,
name: tt.fields.name,
namespace: tt.fields.namespace,
Log: tt.fields.Log,
}
patch1 := ApplyFunc(kubeclient.ExecCommandInContainerWithFullOutput, func(ctx context.Context, podName string, containerName string, namespace string, cmd []string) (string, string, error) {
stdout, stderr, err := mockExecCommandInContainerForGetFileCount()
return stdout, stderr, err
})
defer patch1.Reset()
got, err := e.getDataSetFileNum()
if (err != nil) != tt.wantErr {
t.Errorf("AlluxioEngine.getDataSetFileNum() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("AlluxioEngine.getDataSetFileNum() = %v, want %v", got, tt.want)
}
})
}
}
// TestGetRuntime tests the AlluxioEngine.getRuntime() method to verify it correctly retrieves
// the AlluxioRuntime custom resource from the Kubernetes cluster.
//
// Test Structure:
// - Defines a test table with multiple test cases (though currently only one example exists).
// - Each test case specifies:
// - Input fields: Simulated AlluxioEngine instance configuration.
// - Expected output: The AlluxioRuntime object that should be returned.
// - Error expectation: Whether an error is expected during retrieval.
//
// Key Testing Components:
// - Uses Kubernetes client-go testing utilities (fake client, scheme registration) to mock
// API server interactions, avoiding real cluster dependencies.
// - Validates both successful retrieval and error conditions.
// - Checks deep equality between retrieved and expected objects to ensure metadata accuracy.
//
// Test Workflow for Each Case:
// 1. Register required Kubernetes resource types (AlluxioRuntime, core v1) into the scheme.
// 2. Initialize a fake client preloaded with the expected AlluxioRuntime object.
// 3. Instantiate the AlluxioEngine with test-specific configurations and the fake client.
// 4. Execute getRuntime() and validate:
// - Error behavior matches expectations
// - Retrieved object matches the expected object structure exactly
//
// Edge Cases Covered (via additional test cases in TODO):
// - Non-existent runtime
// - Invalid namespace/name configurations
// - API version/kind mismatches
// - Cluster connection failures (simulated via client misconfiguration)
func TestGetRuntime(t *testing.T) {
type fields struct {
runtime *datav1alpha1.AlluxioRuntime
name string
namespace string
}
tests := []struct {
name string
fields fields
want *datav1alpha1.AlluxioRuntime
wantErr bool
}{
// TODO: Add test cases.
{
name: "test",
fields: fields{
runtime: &datav1alpha1.AlluxioRuntime{
ObjectMeta: v1.ObjectMeta{
Name: "spark",
Namespace: "default",
},
},
name: "spark",
namespace: "default",
},
want: &datav1alpha1.AlluxioRuntime{
TypeMeta: v1.TypeMeta{
Kind: "AlluxioRuntime",
APIVersion: "data.fluid.io/v1alpha1",
},
ObjectMeta: v1.ObjectMeta{
Name: "spark",
Namespace: "default",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := runtime.NewScheme()
s.AddKnownTypes(datav1alpha1.GroupVersion, tt.fields.runtime)
_ = corev1.AddToScheme(s)
mockClient := fake.NewFakeClientWithScheme(s, tt.want)
e := &AlluxioEngine{
runtime: tt.fields.runtime,
name: tt.fields.name,
namespace: tt.fields.namespace,
Client: mockClient,
}
got, err := e.getRuntime()
if (err != nil) != tt.wantErr {
t.Errorf("AlluxioEngine.getRuntime() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AlluxioEngine.getRuntime() = %#v, want %#v", got, tt.want)
}
})
}
}
// TestGetMasterPod verifies the correct retrieval of the master Pod for an Alluxio runtime.
// This test validates whether the AlluxioEngine's getMasterPod method accurately fetches
// the expected Pod resource from Kubernetes based on the provided runtime configuration.
//
// Test Cases:
// - Standard scenario: Checks if the master Pod is correctly retrieved when valid runtime metadata is provided.
//
// Parameters:
// - t *testing.T: Testing framework handle for assertion and logging
//
// Test Logic:
// 1. Defines test structures with mock AlluxioRuntime configurations and expected Pod results
// 2. Initializes a fake Kubernetes client with test-specific schemas and objects
// 3. Executes getMasterPod with different test configurations
// 4. Compares actual results against expected outcomes using deep equality checks
// 5. Reports discrepancies between actual and expected results through testing.T
func TestGetMasterPod(t *testing.T) {
type fields struct {
runtime *datav1alpha1.AlluxioRuntime
name string
namespace string
}
tests := []struct {
name string
fields fields
want *corev1.Pod
wantErr bool
}{
{
name: "test",
fields: fields{
runtime: &datav1alpha1.AlluxioRuntime{
ObjectMeta: v1.ObjectMeta{
Name: "spark-master",
Namespace: "default",
},
},
name: "spark-master",
namespace: "default",
},
want: &corev1.Pod{
ObjectMeta: v1.ObjectMeta{
Name: "spark-master",
Namespace: "default",
},
TypeMeta: v1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := runtime.NewScheme()
s.AddKnownTypes(datav1alpha1.GroupVersion, tt.fields.runtime)
s.AddKnownTypes(corev1.SchemeGroupVersion, &corev1.Pod{})
_ = corev1.AddToScheme(s)
mockClient := fake.NewFakeClientWithScheme(s, tt.fields.runtime, tt.want)
e := &AlluxioEngine{
runtime: tt.fields.runtime,
name: tt.fields.name,
namespace: tt.fields.namespace,
Client: mockClient,
}
gotMaster, err := e.getMasterPod(tt.fields.name, tt.fields.namespace)
if (err != nil) != tt.wantErr {
t.Errorf("AlluxioEngine.getMasterPod() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotMaster, tt.want) {
t.Errorf("AlluxioEngine.getMasterPod() = %#v, want %#v", gotMaster, tt.want)
}
})
}
}
// TestGetMasterStatefulset tests the getMasterStatefulset method of the AlluxioEngine struct.
// It verifies that the method correctly retrieves the expected StatefulSet based on the provided
// AlluxioRuntime, name, and namespace. The test includes a sample runtime and expected
// StatefulSet, checking for both successful retrieval and error scenarios.
//
// Parameters:
// - t: The test framework's context, which provides methods for logging and error reporting.
//
// Returns:
// - The test does not return any value, but it reports errors using the t.Error and
// t.Errorf methods to indicate whether the test passed or failed.
func TestGetMasterStatefulset(t *testing.T) {
type fields struct {
runtime *datav1alpha1.AlluxioRuntime
name string
namespace string
}
tests := []struct {
name string
fields fields
want *appsv1.StatefulSet
wantErr bool
}{
{
name: "test",
fields: fields{
runtime: &datav1alpha1.AlluxioRuntime{
ObjectMeta: v1.ObjectMeta{
Name: "spark-master",
Namespace: "default",
},
},
name: "spark-master",
namespace: "default",
},
want: &appsv1.StatefulSet{
ObjectMeta: v1.ObjectMeta{
Name: "spark-master",
Namespace: "default",
},
TypeMeta: v1.TypeMeta{
Kind: "StatefulSet",
APIVersion: "apps/v1",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := runtime.NewScheme()
s.AddKnownTypes(datav1alpha1.GroupVersion, tt.fields.runtime)
s.AddKnownTypes(appsv1.SchemeGroupVersion, &appsv1.StatefulSet{})
_ = corev1.AddToScheme(s)
mockClient := fake.NewFakeClientWithScheme(s, tt.fields.runtime, tt.want)
e := &AlluxioEngine{
runtime: tt.fields.runtime,
name: tt.fields.name,
namespace: tt.fields.namespace,
Client: mockClient,
}
gotMaster, err := e.getMasterStatefulset(tt.fields.name, tt.fields.namespace)
if (err != nil) != tt.wantErr {
t.Errorf("AlluxioEngine.getMasterStatefulset() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotMaster, tt.want) {
t.Errorf("AlluxioEngine.getMasterStatefulset() = %#v, want %#v", gotMaster, tt.want)
}
})
}
}
func TestGetDaemonset(t *testing.T) {
type fields struct {
runtime *datav1alpha1.AlluxioRuntime
name string
namespace string
Client client.Client
}
tests := []struct {
name string
fields fields
wantDaemonset *appsv1.DaemonSet
wantErr bool
}{
{
name: "test",
fields: fields{
runtime: &datav1alpha1.AlluxioRuntime{
ObjectMeta: v1.ObjectMeta{
Name: "spark-master",
Namespace: "default",
},
},
name: "spark-master",
namespace: "default",
},
wantDaemonset: &appsv1.DaemonSet{
ObjectMeta: v1.ObjectMeta{
Name: "spark-master",
Namespace: "default",
},
TypeMeta: v1.TypeMeta{
Kind: "DaemonSet",
APIVersion: "apps/v1",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := runtime.NewScheme()
s.AddKnownTypes(datav1alpha1.GroupVersion, tt.fields.runtime)
s.AddKnownTypes(appsv1.SchemeGroupVersion, &appsv1.DaemonSet{})
_ = corev1.AddToScheme(s)
mockClient := fake.NewFakeClientWithScheme(s, tt.fields.runtime, tt.wantDaemonset)
e := &AlluxioEngine{
runtime: tt.fields.runtime,
name: tt.fields.name,
namespace: tt.fields.namespace,
Client: mockClient,
}
gotDaemonset, err := e.getDaemonset(tt.fields.name, tt.fields.namespace)
if (err != nil) != tt.wantErr {
t.Errorf("AlluxioEngine.getDaemonset() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotDaemonset, tt.wantDaemonset) {
t.Errorf("AlluxioEngine.getDaemonset() = %#v, want %#v", gotDaemonset, tt.wantDaemonset)
}
})
}
}
// TestGetMasterPodInfo tests the getMasterPodInfo function of the AlluxioEngine struct.
// It defines a set of test cases with expected pod and container names based on the engine's name.
// The function iterates through the test cases, initializes an AlluxioEngine instance,
// and verifies whether the returned pod name and container name match the expected values.
func TestGetMasterPodInfo(t *testing.T) {
type fields struct {
name string
}
tests := []struct {
name string
fields fields
wantPodName string
wantContainerName string
}{
{
name: "test",
fields: fields{
name: "spark",
},
wantPodName: "spark-master-0",
wantContainerName: "alluxio-master",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &AlluxioEngine{
name: tt.fields.name,
}
gotPodName, gotContainerName := e.getMasterPodInfo()
if gotPodName != tt.wantPodName {
t.Errorf("AlluxioEngine.getMasterPodInfo() gotPodName = %v, want %v", gotPodName, tt.wantPodName)
}
if gotContainerName != tt.wantContainerName {
t.Errorf("AlluxioEngine.getMasterPodInfo() gotContainerName = %v, want %v", gotContainerName, tt.wantContainerName)
}
})
}
}
func TestGetMasterStatefulsetName(t *testing.T) {
type fields struct {
name string
}
tests := []struct {
name string
fields fields
wantDsName string
}{
{
name: "test",
fields: fields{
name: "spark",
},
wantDsName: "spark-master",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &AlluxioEngine{
name: tt.fields.name,
}
if gotDsName := e.getMasterName(); gotDsName != tt.wantDsName {
t.Errorf("AlluxioEngine.getMasterStatefulsetName() = %v, want %v", gotDsName, tt.wantDsName)
}
})
}
}
func TestGetWorkerDaemonsetName(t *testing.T) {
type fields struct {
name string
}
tests := []struct {
name string
fields fields
wantDsName string
}{
{
name: "test",
fields: fields{
name: "spark",
},
wantDsName: "spark-worker",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &AlluxioEngine{
name: tt.fields.name,
}
if gotDsName := e.getWorkerName(); gotDsName != tt.wantDsName {
t.Errorf("AlluxioEngine.getWorkerDaemonsetName() = %v, want %v", gotDsName, tt.wantDsName)
}
})
}
}
// TestGetFuseDaemonsetName is a unit test for the getFuseName method of the AlluxioEngine struct.
// This test verifies that the method correctly constructs the expected daemonset name
// based on the given engine name.
// The test defines a struct `fields` containing the engine name and a test case struct
// that includes the test case name, input fields, and the expected daemonset name.
// The test case used:
// - When the engine name is "spark", the expected daemonset name should be "spark-fuse".
// The test iterates through all defined cases, creates an instance of AlluxioEngine with
// the given name, calls the `getFuseName` method, and checks if the returned result matches
// the expected value. If the result differs, an error message is reported.
func TestGetFuseDaemonsetName(t *testing.T) {
type fields struct {
name string
}
tests := []struct {
name string
fields fields
wantDsName string
}{
{
name: "test",
fields: fields{
name: "spark",
},
wantDsName: "spark-fuse",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &AlluxioEngine{
name: tt.fields.name,
}
if gotDsName := e.getFuseName(); gotDsName != tt.wantDsName {
t.Errorf("AlluxioEngine.getFuseName() = %v, want %v", gotDsName, tt.wantDsName)
}
})
}
}
// TestGetMountPoint tests the AlluxioEngine.getMountPoint method to ensure it correctly constructs
// the mount point path. The test verifies the path concatenation logic using configured MountRoot,
// namespace, and engine name parameters to validate the resulting filesystem path.
//
// Parameters:
// - t : *testing.T
// Testing framework handle for managing test state and reporting failures
//
// Returns:
// - None
// Failures are reported through t.Errorf
func TestGetMountPoint(t *testing.T) {
type fields struct {
name string
namespace string
Log logr.Logger
MountRoot string
}
tests := []struct {
name string
fields fields
}{
{
name: "test",
fields: fields{
name: "spark",
namespace: "default",
Log: fake.NullLogger(),
MountRoot: "/tmp",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &AlluxioEngine{
Log: tt.fields.Log,
name: tt.fields.name,
namespace: tt.fields.namespace,
}
t.Setenv("MOUNT_ROOT", tt.fields.MountRoot)
wantMountPath := fmt.Sprintf("%s/%s/%s/alluxio-fuse", tt.fields.MountRoot+"/alluxio", tt.fields.namespace, e.name)
if gotMountPath := e.getMountPoint(); gotMountPath != wantMountPath {
t.Errorf("AlluxioEngine.getMountPoint() = %v, want %v", gotMountPath, wantMountPath)
}
})
}
}
func TestGetInitTierPathsEnv(t *testing.T) {
type fields struct {
runtime *datav1alpha1.AlluxioRuntime
}
tests := []struct {
name string
fields fields
want string
}{
// TODO: Add test cases.
{
name: "test",
fields: fields{
&datav1alpha1.AlluxioRuntime{
Spec: datav1alpha1.AlluxioRuntimeSpec{
TieredStore: datav1alpha1.TieredStore{
Levels: []datav1alpha1.Level{
{
Path: "/mnt/alluxio0",
},
{
Path: "/mnt/alluxio1",
},
},
},
},
},
},
want: "/mnt/alluxio0:/mnt/alluxio1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &AlluxioEngine{
runtime: tt.fields.runtime,
}
if got := e.getInitTierPathsEnv(tt.fields.runtime); got != tt.want {
t.Errorf("AlluxioEngine.getInitTierPathsEnv() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetMountRoot(t *testing.T) {
tests := []struct {
name string
wantPath string
}{
{
name: "test",
wantPath: "/tmp/alluxio",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("MOUNT_ROOT", "/tmp")
if gotPath := getMountRoot(); gotPath != tt.wantPath {
t.Errorf("getMountRoot() = %v, want %v", gotPath, tt.wantPath)
}
})
}
}
func TestParseRuntimeImage(t *testing.T) {
type args struct {
image string
tag string
imagePullPolicy string
imagePullSecrets []corev1.LocalObjectReference
}
type envs map[string]string
tests := []struct {
name string
args args
envs envs
want string
want1 string
want2 string
want3 []corev1.LocalObjectReference
}{
{
name: "test0",
args: args{
image: "registry.cn-huhehaote.aliyuncs.com/alluxio/alluxio",
tag: "2.3.0-SNAPSHOT-2c41226",
imagePullPolicy: "IfNotPresent",
imagePullSecrets: []corev1.LocalObjectReference{{Name: "test"}},
},
envs: map[string]string{
common.AlluxioRuntimeImageEnv: "registry.cn-huhehaote.aliyuncs.com/alluxio/alluxio:2.3.0-SNAPSHOT-2c41226",
},
want: "registry.cn-huhehaote.aliyuncs.com/alluxio/alluxio",
want1: "2.3.0-SNAPSHOT-2c41226",
want2: "IfNotPresent",
want3: []corev1.LocalObjectReference{{Name: "test"}},
},
{
name: "test1",
args: args{
image: "",
tag: "",
imagePullPolicy: "IfNotPresent",
imagePullSecrets: []corev1.LocalObjectReference{},
},
envs: map[string]string{
common.AlluxioRuntimeImageEnv: "registry.cn-huhehaote.aliyuncs.com/alluxio/alluxio:2.3.0-SNAPSHOT-2c41226",
common.EnvImagePullSecretsKey: "secret1,secret2",
},
want: "registry.cn-huhehaote.aliyuncs.com/alluxio/alluxio",
want1: "2.3.0-SNAPSHOT-2c41226",
want2: "IfNotPresent",
want3: []corev1.LocalObjectReference{{Name: "secret1"}, {Name: "secret2"}},
},
{
name: "test2",
args: args{
image: "",
tag: "",
imagePullPolicy: "IfNotPresent",
imagePullSecrets: []corev1.LocalObjectReference{{Name: "test"}},
},
envs: map[string]string{
common.AlluxioRuntimeImageEnv: "registry.cn-huhehaote.aliyuncs.com/alluxio/alluxio:2.3.0-SNAPSHOT-2c41226",
common.EnvImagePullSecretsKey: "secret1,secret2",
},
want: "registry.cn-huhehaote.aliyuncs.com/alluxio/alluxio",
want1: "2.3.0-SNAPSHOT-2c41226",
want2: "IfNotPresent",
want3: []corev1.LocalObjectReference{{Name: "test"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &AlluxioEngine{}
for k, v := range tt.envs {
// mock env
t.Setenv(k, v)