github.com/verrazzano/verrazzano@v1.7.0/platform-operator/internal/vzconfig/external_service_test.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package vzconfig 5 6 import ( 7 "testing" 8 9 asserts "github.com/stretchr/testify/assert" 10 vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1" 11 v1 "k8s.io/api/core/v1" 12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 13 k8scheme "k8s.io/client-go/kubernetes/scheme" 14 fakes "sigs.k8s.io/controller-runtime/pkg/client/fake" 15 ) 16 17 // TestGetExternalIP tests retrieving the external IP from a given service 18 // GIVEN a call to TestGetExternalIP 19 // WHEN the service has an external IP 20 // THEN no error and the external IP address is returned 21 func TestGetExternalIP(t *testing.T) { 22 assert := asserts.New(t) 23 24 svcName := "foo" 25 svcNamespace := "bar" 26 ipaddr := "0.0.0.0" 27 svcNoIP := &v1.Service{ 28 ObjectMeta: metav1.ObjectMeta{ 29 Name: svcName, 30 Namespace: svcNamespace, 31 }, 32 } 33 34 svcIPExt := svcNoIP.DeepCopy() 35 svcIPExt.Spec.ExternalIPs = []string{ipaddr} 36 37 svcIPStatus := svcNoIP.DeepCopy() 38 svcIPStatus.Status.LoadBalancer.Ingress = []v1.LoadBalancerIngress{ 39 { 40 IP: ipaddr, 41 }, 42 } 43 44 tests := []struct { 45 name string 46 ingType vzapi.IngressType 47 svcName string 48 svcNamespace string 49 service *v1.Service 50 expectError bool 51 }{ 52 { 53 name: "test empty", 54 ingType: vzapi.LoadBalancer, 55 svcName: svcName, 56 svcNamespace: svcNamespace, 57 service: &v1.Service{}, 58 expectError: true, 59 }, 60 { 61 name: "test standard service", 62 ingType: vzapi.LoadBalancer, 63 svcName: svcName, 64 svcNamespace: svcNamespace, 65 service: svcNoIP, 66 expectError: true, 67 }, 68 { 69 name: "test service external IP", 70 ingType: vzapi.LoadBalancer, 71 svcName: svcName, 72 svcNamespace: svcNamespace, 73 service: svcIPExt, 74 expectError: false, 75 }, 76 { 77 name: "test service status IP", 78 ingType: vzapi.NodePort, 79 svcName: svcName, 80 svcNamespace: svcNamespace, 81 service: svcIPStatus, 82 expectError: false, 83 }, 84 } 85 86 for _, tt := range tests { 87 t.Run(tt.name, func(_ *testing.T) { 88 cli := fakes.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(tt.service).Build() 89 ip, err := GetExternalIP(cli, tt.ingType, tt.svcName, tt.svcNamespace) 90 if tt.expectError { 91 assert.Error(err) 92 return 93 } 94 assert.NoError(err) 95 assert.Equal(ip, ipaddr) 96 }) 97 } 98 }