@@ -450,6 +450,7 @@ func TestPortsEqualIgnoreOrder(t *testing.T) {
450
450
func TestCreateServiceImportStruct (t * testing.T ) {
451
451
type args struct {
452
452
servicePorts []* model.Port
453
+ endpoints []* model.Endpoint
453
454
}
454
455
tests := []struct {
455
456
name string
@@ -461,7 +462,12 @@ func TestCreateServiceImportStruct(t *testing.T) {
461
462
args : args {
462
463
servicePorts : []* model.Port {
463
464
{Name : test .PortName1 , Protocol : test .Protocol1 , Port : test .Port1 },
464
- {Name : test .PortName2 , Protocol : test .Protocol1 , Port : test .Port2 },
465
+ {Name : test .PortName2 , Protocol : test .Protocol2 , Port : test .Port2 },
466
+ },
467
+ endpoints : []* model.Endpoint {
468
+ {
469
+ ServiceType : model .ClusterSetIPType ,
470
+ },
465
471
},
466
472
},
467
473
want : multiclusterv1alpha1.ServiceImport {
@@ -475,15 +481,15 @@ func TestCreateServiceImportStruct(t *testing.T) {
475
481
Type : multiclusterv1alpha1 .ClusterSetIP ,
476
482
Ports : []multiclusterv1alpha1.ServicePort {
477
483
{Name : test .PortName1 , Protocol : v1 .ProtocolTCP , Port : test .Port1 },
478
- {Name : test .PortName2 , Protocol : v1 .ProtocolTCP , Port : test .Port2 },
484
+ {Name : test .PortName2 , Protocol : v1 .ProtocolUDP , Port : test .Port2 },
479
485
},
480
486
},
481
487
},
482
488
},
483
489
}
484
490
for _ , tt := range tests {
485
491
t .Run (tt .name , func (t * testing.T ) {
486
- if got := CreateServiceImportStruct (test .HttpNsName , test . SvcName , tt .args .servicePorts ); ! reflect .DeepEqual (* got , tt .want ) {
492
+ if got := CreateServiceImportStruct (test .GetTestServiceWithEndpoint ( tt . args . endpoints ) , tt .args .servicePorts ); ! reflect .DeepEqual (* got , tt .want ) {
487
493
t .Errorf ("CreateServiceImportStruct() = %v, want %v" , got , tt .want )
488
494
}
489
495
})
@@ -547,3 +553,104 @@ func TestExtractServiceType(t *testing.T) {
547
553
})
548
554
}
549
555
}
556
+
557
+ func TestServiceTypetoServiceImportType (t * testing.T ) {
558
+ tests := []struct {
559
+ name string
560
+ svcType model.ServiceType
561
+ want multiclusterv1alpha1.ServiceImportType
562
+ }{
563
+ {
564
+ name : "cluster ip type" ,
565
+ svcType : model .ClusterSetIPType ,
566
+ want : multiclusterv1alpha1 .ClusterSetIP ,
567
+ },
568
+ {
569
+ name : "headless type" ,
570
+ svcType : model .HeadlessType ,
571
+ want : multiclusterv1alpha1 .Headless ,
572
+ },
573
+ }
574
+ for _ , tt := range tests {
575
+ t .Run (tt .name , func (t * testing.T ) {
576
+ if got := ServiceTypetoServiceImportType (tt .svcType ); got != tt .want {
577
+ t .Errorf ("ServiceTypetoServiceImportType() = %v, want %v" , got , tt .want )
578
+ }
579
+ })
580
+ }
581
+ }
582
+
583
+ func TestCreateDerivedServiceStruct (t * testing.T ) {
584
+ type args struct {
585
+ servicePorts []* model.Port
586
+ svcImport * multiclusterv1alpha1.ServiceImport
587
+ }
588
+ tests := []struct {
589
+ name string
590
+ args args
591
+ want * v1.ServiceSpec
592
+ }{
593
+ {
594
+ name : "cluster ip case" ,
595
+ args : args {
596
+ servicePorts : []* model.Port {
597
+ {Name : test .PortName1 , Protocol : test .Protocol1 , Port : test .Port1 , TargetPort : "8080" },
598
+ {Name : test .PortName2 , Protocol : test .Protocol2 , Port : test .Port2 , TargetPort : "8080" },
599
+ },
600
+ svcImport : & multiclusterv1alpha1.ServiceImport {
601
+ ObjectMeta : metav1.ObjectMeta {
602
+ Namespace : test .HttpNsName ,
603
+ Name : test .SvcName ,
604
+ Annotations : map [string ]string {DerivedServiceAnnotation : DerivedName (test .HttpNsName , test .SvcName )},
605
+ },
606
+ Spec : multiclusterv1alpha1.ServiceImportSpec {
607
+ IPs : []string {},
608
+ Type : multiclusterv1alpha1 .ClusterSetIP ,
609
+ },
610
+ },
611
+ },
612
+ want : & v1.ServiceSpec {
613
+ Type : v1 .ServiceTypeClusterIP ,
614
+ Ports : []v1.ServicePort {
615
+ {Name : test .PortName1 , Protocol : test .Protocol1 , Port : test .Port1 , TargetPort : intstr.IntOrString {Type : intstr .Int , IntVal : 8080 }},
616
+ {Name : test .PortName2 , Protocol : test .Protocol2 , Port : test .Port2 , TargetPort : intstr.IntOrString {Type : intstr .Int , IntVal : 8080 }},
617
+ },
618
+ },
619
+ },
620
+ {
621
+ name : "headless case" ,
622
+ args : args {
623
+ servicePorts : []* model.Port {
624
+ {Name : test .PortName1 , Protocol : test .Protocol1 , Port : test .Port1 , TargetPort : "8080" },
625
+ {Name : test .PortName2 , Protocol : test .Protocol2 , Port : test .Port2 , TargetPort : "8080" },
626
+ },
627
+ svcImport : & multiclusterv1alpha1.ServiceImport {
628
+ ObjectMeta : metav1.ObjectMeta {
629
+ Namespace : test .HttpNsName ,
630
+ Name : test .SvcName ,
631
+ Annotations : map [string ]string {DerivedServiceAnnotation : DerivedName (test .HttpNsName , test .SvcName )},
632
+ },
633
+ Spec : multiclusterv1alpha1.ServiceImportSpec {
634
+ IPs : []string {},
635
+ Type : multiclusterv1alpha1 .Headless ,
636
+ },
637
+ },
638
+ },
639
+ want : & v1.ServiceSpec {
640
+ Type : v1 .ServiceTypeClusterIP ,
641
+ Ports : []v1.ServicePort {
642
+ {Name : test .PortName1 , Protocol : test .Protocol1 , Port : test .Port1 , TargetPort : intstr.IntOrString {Type : intstr .Int , IntVal : 8080 }},
643
+ {Name : test .PortName2 , Protocol : test .Protocol2 , Port : test .Port2 , TargetPort : intstr.IntOrString {Type : intstr .Int , IntVal : 8080 }},
644
+ },
645
+ ClusterIP : "None" ,
646
+ },
647
+ },
648
+ }
649
+ for _ , tt := range tests {
650
+ t .Run (tt .name , func (t * testing.T ) {
651
+ if got := & CreateDerivedServiceStruct (tt .args .svcImport , tt .args .servicePorts ).Spec ; ! reflect .DeepEqual (got , tt .want ) {
652
+ t .Errorf ("CreateDerivedServiceStruct() = %v, want %v" , got , tt .want )
653
+ }
654
+ })
655
+ }
656
+ }
0 commit comments