github.com/verrazzano/verrazzano@v1.7.0/tools/oam-converter/pkg/services/destination_test.go (about)

     1  // Copyright (c) 2023, 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 services
     5  
     6  import (
     7  	"github.com/stretchr/testify/assert"
     8  	istio "istio.io/api/networking/v1beta1"
     9  	corev1 "k8s.io/api/core/v1"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	"k8s.io/apimachinery/pkg/util/intstr"
    12  	"testing"
    13  )
    14  
    15  func TestCreateDestinationFromService(t *testing.T) {
    16  	testCases := []struct {
    17  		name          string
    18  		service       *corev1.Service
    19  		expectedDest  *istio.HTTPRouteDestination
    20  		expectedError error
    21  	}{
    22  		// Test case 1: Service with ports
    23  		{
    24  			name: "Test case 1",
    25  			service: &corev1.Service{
    26  				ObjectMeta: metav1.ObjectMeta{Name: "my-service"},
    27  				Spec: corev1.ServiceSpec{
    28  					Ports: []corev1.ServicePort{
    29  						{
    30  							Name:       "http",
    31  							Port:       8080,
    32  							TargetPort: intstr.FromInt(8080),
    33  						},
    34  					},
    35  				},
    36  			},
    37  			expectedDest: &istio.HTTPRouteDestination{
    38  				Destination: &istio.Destination{Host: "my-service",
    39  					Port: &istio.PortSelector{Number: 8080},
    40  				},
    41  			},
    42  			expectedError: nil,
    43  		},
    44  		// Test case 2: Service without ports
    45  		{
    46  			name: "Test case 2",
    47  			service: &corev1.Service{
    48  				ObjectMeta: metav1.ObjectMeta{Name: "my-service"},
    49  				Spec:       corev1.ServiceSpec{},
    50  			},
    51  			expectedDest:  &istio.HTTPRouteDestination{Destination: &istio.Destination{Host: "my-service"}},
    52  			expectedError: nil,
    53  		},
    54  	}
    55  
    56  	for _, tc := range testCases {
    57  		t.Run(tc.name, func(t *testing.T) {
    58  			dest, err := CreateDestinationFromService(tc.service)
    59  			assert.Equal(t, tc.expectedDest, dest, "Unexpected destination")
    60  			assert.Equal(t, tc.expectedError, err, "Unexpected error")
    61  		})
    62  	}
    63  }
    64  
    65  func TestSelectPortForDestination(t *testing.T) {
    66  	testCases := []struct {
    67  		name          string
    68  		service       *corev1.Service
    69  		expectedPort  corev1.ServicePort
    70  		expectedError error
    71  	}{
    72  		// Test case 1: Service with a single port
    73  		{
    74  			name: "Test case 1",
    75  			service: &corev1.Service{
    76  				Spec: corev1.ServiceSpec{
    77  					Ports: []corev1.ServicePort{
    78  						{
    79  							Name:     "http",
    80  							Port:     8080,
    81  							Protocol: corev1.ProtocolTCP,
    82  						},
    83  					},
    84  				},
    85  			},
    86  			expectedPort: corev1.ServicePort{
    87  				Name:     "http",
    88  				Port:     8080,
    89  				Protocol: corev1.ProtocolTCP,
    90  			},
    91  			expectedError: nil,
    92  		},
    93  		// Test case 2: Service with multiple ports and one http/WebLogic port
    94  		{
    95  			name: "Test case 2",
    96  			service: &corev1.Service{
    97  				Spec: corev1.ServiceSpec{
    98  					Ports: []corev1.ServicePort{
    99  						{
   100  							Name:     "http",
   101  							Port:     8080,
   102  							Protocol: corev1.ProtocolTCP,
   103  						},
   104  						{
   105  							Name:     "not-http",
   106  							Port:     9090,
   107  							Protocol: corev1.ProtocolTCP,
   108  						},
   109  					},
   110  				},
   111  			},
   112  			expectedPort: corev1.ServicePort{
   113  				Name:     "http",
   114  				Port:     8080,
   115  				Protocol: corev1.ProtocolTCP,
   116  			},
   117  			expectedError: nil,
   118  		},
   119  	}
   120  
   121  	for _, tc := range testCases {
   122  		t.Run(tc.name, func(t *testing.T) {
   123  			port, err := selectPortForDestination(tc.service)
   124  			assert.Equal(t, tc.expectedPort, port, "Unexpected service port")
   125  			assert.Equal(t, tc.expectedError, err, "Unexpected error")
   126  		})
   127  	}
   128  }