github.com/verrazzano/verrazzano@v1.7.0/tools/oam-converter/pkg/resources/destinationrule/destinationrule_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 destinationrule 5 6 import ( 7 "github.com/stretchr/testify/assert" 8 vzapi "github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1" 9 istio "istio.io/api/networking/v1beta1" 10 istioclient "istio.io/client-go/pkg/apis/networking/v1alpha3" 11 corev1 "k8s.io/api/core/v1" 12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 13 "testing" 14 ) 15 16 func TestCreateDestinationFromRule(t *testing.T) { 17 testCases := []struct { 18 name string 19 rule vzapi.IngressRule 20 expectedDest *istio.HTTPRouteDestination 21 expectedError error 22 }{ 23 // Test case 1: Rule with host and port 24 { 25 name: "Test case 1", 26 rule: vzapi.IngressRule{ 27 Destination: vzapi.IngressDestination{ 28 Host: "example.com", 29 Port: 8080, 30 }, 31 }, 32 expectedDest: &istio.HTTPRouteDestination{ 33 Destination: &istio.Destination{ 34 Host: "example.com", 35 Port: &istio.PortSelector{Number: 8080}, 36 }, 37 }, 38 expectedError: nil, 39 }, 40 // Test case 2: Rule with host only 41 { 42 name: "Test case 2", 43 rule: vzapi.IngressRule{ 44 Destination: vzapi.IngressDestination{ 45 Host: "example2.com", 46 }, 47 }, 48 expectedDest: &istio.HTTPRouteDestination{ 49 Destination: &istio.Destination{ 50 Host: "example2.com", 51 }, 52 }, 53 expectedError: nil, 54 }, 55 // Test case 3: Rule without host or port 56 { 57 name: "Test case 3", 58 rule: vzapi.IngressRule{}, 59 expectedDest: &istio.HTTPRouteDestination{Destination: &istio.Destination{}}, 60 expectedError: nil, 61 }, 62 } 63 64 for _, tc := range testCases { 65 t.Run(tc.name, func(t *testing.T) { 66 dest, err := CreateDestinationFromRule(tc.rule) 67 assert.Equal(t, tc.expectedDest, dest, "Unexpected destination") 68 assert.Equal(t, tc.expectedError, err, "Unexpected error") 69 }) 70 } 71 } 72 73 func TestCreateDestinationRule(t *testing.T) { 74 75 // Test case 1: Rule with HTTPCookie 76 trait := &vzapi.IngressTrait{ 77 ObjectMeta: metav1.ObjectMeta{ 78 Namespace: "my-namespace", 79 }, 80 } 81 rule1 := vzapi.IngressRule{ 82 Destination: vzapi.IngressDestination{ 83 HTTPCookie: &vzapi.IngressDestinationHTTPCookie{ 84 Name: "my-cookie", 85 Path: "/", 86 TTL: 60, 87 }, 88 }, 89 } 90 name := "my-destination-rule" 91 destRule1, err1 := CreateDestinationRule(trait, rule1, name) 92 assert.NoError(t, err1, "Error was not expected for test case 1") 93 assert.NotNil(t, destRule1, "DestinationRule should not be nil for test case 1") 94 95 // Test case 2: Rule without HTTPCookie 96 rule2 := vzapi.IngressRule{} 97 destRule2, err2 := CreateDestinationRule(trait, rule2, name) 98 assert.NoError(t, err2, "Error was not expected for test case 2") 99 assert.Nil(t, destRule2, "DestinationRule should be nil for test case 2") 100 } 101 102 func TestMutateDestinationRule(t *testing.T) { 103 // Test case 1: Namespace with istio-injection enabled 104 namespace1 := &corev1.Namespace{ 105 ObjectMeta: metav1.ObjectMeta{ 106 Labels: map[string]string{"istio-injection": "enabled"}, 107 }, 108 } 109 rule1 := vzapi.IngressRule{ 110 Destination: vzapi.IngressDestination{ 111 112 Host: "example.com", 113 HTTPCookie: &vzapi.IngressDestinationHTTPCookie{ 114 Name: "my-cookie", 115 Path: "/", 116 TTL: 60, 117 }, 118 }, 119 } 120 destinationRule1 := &istioclient.DestinationRule{ 121 TypeMeta: metav1.TypeMeta{}, 122 ObjectMeta: metav1.ObjectMeta{ 123 Namespace: "my-namespace", 124 Name: "my-destination-rule", 125 }, 126 } 127 destRule1, err1 := mutateDestinationRule(destinationRule1, rule1, namespace1) 128 assert.NoError(t, err1, "Error was not expected for test case 1") 129 assert.NotNil(t, destRule1, "DestinationRule should not be nil for test case 1") 130 131 }