github.com/verrazzano/verrazzano@v1.7.1/tools/oam-converter/pkg/resources/virtualservice/virtualservice_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 virtualservice
     5  
     6  import (
     7  	"github.com/stretchr/testify/assert"
     8  	vzapi "github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1"
     9  	consts "github.com/verrazzano/verrazzano/tools/oam-converter/pkg/constants"
    10  	istio "istio.io/api/networking/v1beta1"
    11  	vsapi "istio.io/client-go/pkg/apis/networking/v1beta1"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  	"testing"
    14  )
    15  
    16  func TestGetPathsFromRule(t *testing.T) {
    17  	// Create a sample IngressRule with paths
    18  	ruleWithPaths := vzapi.IngressRule{
    19  		Paths: []vzapi.IngressPath{
    20  			{Path: "/api/v1", PathType: "prefix"},
    21  			{Path: "/app", PathType: "exact"},
    22  		},
    23  	}
    24  
    25  	// Call the function with the sample IngressRule
    26  	pathsWithRule := GetPathsFromRule(ruleWithPaths)
    27  
    28  	// Check if the function returns the correct paths
    29  	assert.Equal(t, ruleWithPaths.Paths, pathsWithRule, "Unexpected paths returned when rule has paths")
    30  
    31  	// Create a sample IngressRule with no paths
    32  	ruleWithoutPaths := vzapi.IngressRule{}
    33  
    34  	// Call the function with the sample IngressRule
    35  	pathsWithoutRule := GetPathsFromRule(ruleWithoutPaths)
    36  
    37  	// Check if the function returns the default path when there are no paths in the rule
    38  	expectedDefaultPath := []vzapi.IngressPath{{Path: "/", PathType: "prefix"}}
    39  	assert.Equal(t, expectedDefaultPath, pathsWithoutRule, "Unexpected paths returned when rule has no paths")
    40  }
    41  
    42  func TestCreateVirtualServiceMatchURIFromIngressTraitPath(t *testing.T) {
    43  	// Create a sample IngressPath with path set to "/api/v1" and pathType set to "prefix"
    44  	path := vzapi.IngressPath{
    45  		Path:     "/api/v1",
    46  		PathType: "prefix",
    47  	}
    48  
    49  	// Call the function with the sample IngressPath
    50  	match := CreateVirtualServiceMatchURIFromIngressTraitPath(path)
    51  
    52  	// Check if the function returns the correct string match for the given path and pathType
    53  	expectedMatch := &istio.StringMatch{MatchType: &istio.StringMatch_Prefix{Prefix: "/api/v1"}}
    54  	assert.Equal(t, expectedMatch, match, "Unexpected StringMatch for path with prefix")
    55  
    56  	// Create a sample IngressPath with path set to "/app" and pathType set to "exact"
    57  	path = vzapi.IngressPath{
    58  		Path:     "/app",
    59  		PathType: "exact",
    60  	}
    61  
    62  	// Call the function with the sample IngressPath
    63  	match = CreateVirtualServiceMatchURIFromIngressTraitPath(path)
    64  
    65  	// Check if the function returns the correct string match for the given path and pathType
    66  	expectedMatch = &istio.StringMatch{MatchType: &istio.StringMatch_Exact{Exact: "/app"}}
    67  	assert.Equal(t, expectedMatch, match, "Unexpected StringMatch for path with exact")
    68  
    69  	// Create a sample IngressPath with an empty path and pathType not specified
    70  	path = vzapi.IngressPath{
    71  		Path: "",
    72  	}
    73  
    74  	// Call the function with the sample IngressPath
    75  	match = CreateVirtualServiceMatchURIFromIngressTraitPath(path)
    76  
    77  	// Check if the function returns the default StringMatch for an empty path (prefix type)
    78  	expectedMatch = &istio.StringMatch{MatchType: &istio.StringMatch_Prefix{Prefix: "/"}}
    79  	assert.Equal(t, expectedMatch, match, "Unexpected StringMatch for empty path (prefix)")
    80  
    81  	// Create a sample IngressPath with path set to "/" and pathType set to "regex"
    82  	path = vzapi.IngressPath{
    83  		Path:     "/",
    84  		PathType: "regex",
    85  	}
    86  
    87  	// Call the function with the sample IngressPath
    88  	match = CreateVirtualServiceMatchURIFromIngressTraitPath(path)
    89  
    90  	// Check if the function returns the correct string match for the given path and pathType
    91  	expectedMatch = &istio.StringMatch{MatchType: &istio.StringMatch_Regex{Regex: "/"}}
    92  	assert.Equal(t, expectedMatch, match, "Unexpected StringMatch for path with regex")
    93  }
    94  
    95  func TestCreateVirtualService(t *testing.T) {
    96  	// Create a sample IngressTrait, IngressRule, allHostsForTrait, name, and Gateway
    97  	ingressTrait := &vzapi.IngressTrait{
    98  		ObjectMeta: metav1.ObjectMeta{
    99  			Namespace: "test-namespace",
   100  		},
   101  	}
   102  	rule := vzapi.IngressRule{
   103  		Paths: []vzapi.IngressPath{
   104  			{Path: "/api/v1", PathType: "prefix"},
   105  		},
   106  	}
   107  	allHostsForTrait := []string{"example.com"}
   108  	name := "test-virtualservice"
   109  	gateway := &vsapi.Gateway{
   110  		ObjectMeta: metav1.ObjectMeta{
   111  			Name: "test-gateway",
   112  		},
   113  	}
   114  
   115  	// Call the function with the sample inputs
   116  	virtualService, err := CreateVirtualService(ingressTrait, rule, allHostsForTrait, name, gateway)
   117  
   118  	// Check if the function returns the VirtualService with the correct settings
   119  	expectedVirtualService := &vsapi.VirtualService{
   120  		TypeMeta: metav1.TypeMeta{
   121  			APIVersion: consts.VirtualServiceAPIVersion,
   122  			Kind:       "VirtualService",
   123  		},
   124  		ObjectMeta: metav1.ObjectMeta{
   125  			Namespace: "test-namespace",
   126  			Name:      "test-virtualservice",
   127  		},
   128  	}
   129  
   130  	matches := []*istio.HTTPMatchRequest{
   131  		{
   132  			Uri: &istio.StringMatch{MatchType: &istio.StringMatch_Prefix{Prefix: "/api/v1"}},
   133  		},
   134  	}
   135  	route := []*istio.HTTPRoute{
   136  		{
   137  			Match: matches,
   138  			Route: []*istio.HTTPRouteDestination{
   139  				{
   140  
   141  					Destination: &istio.Destination{
   142  						Host: "",
   143  					},
   144  				},
   145  			},
   146  		},
   147  	}
   148  	expectedVirtualService.Spec.Gateways = []string{"test-gateway"}
   149  	expectedVirtualService.Spec.Hosts = []string{"example.com"}
   150  	expectedVirtualService.Spec.Http = route
   151  	assert.Nil(t, err, "Unexpected error returned from CreateVirtualService")
   152  	assert.Equal(t, expectedVirtualService, virtualService, "Unexpected VirtualService returned")
   153  }