github.com/kiali/kiali@v1.84.0/kubernetes/host_test.go (about)

     1  package kubernetes
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	networking_v1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
     8  	"k8s.io/apimachinery/pkg/util/yaml"
     9  	k8s_networking_v1 "sigs.k8s.io/gateway-api/apis/v1"
    10  	k8s_networking_v1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
    11  
    12  	"github.com/kiali/kiali/config"
    13  	"github.com/kiali/kiali/log"
    14  )
    15  
    16  func TestGatewayAsHost(t *testing.T) {
    17  	assert := assert.New(t)
    18  
    19  	conf := config.NewConfig()
    20  	config.Set(conf)
    21  
    22  	assert.Equal("mygateway.bookinfo.svc.cluster.local", ParseGatewayAsHost("mygateway", "bookinfo").String())
    23  	assert.Equal("mygateway.bookinfo.svc.cluster.local", ParseGatewayAsHost("bookinfo/mygateway", "bookinfo").String())
    24  	assert.Equal("mygateway.istio-system.svc.cluster.local", ParseGatewayAsHost("istio-system/mygateway", "bookinfo").String())
    25  	assert.Equal("mygateway.bookinfo.svc.cluster.local", ParseGatewayAsHost("mygateway.bookinfo", "bookinfo").String())
    26  	assert.Equal("mygateway.bookinfo.svc.cluster.local", ParseGatewayAsHost("mygateway.bookinfo", "bookinfo").String())
    27  	assert.Equal("mygateway.bookinfo.svc.cluster.local", ParseGatewayAsHost("mygateway.bookinfo.svc.cluster.local", "bookinfo").String())
    28  }
    29  
    30  func TestHasMatchingVirtualServices(t *testing.T) {
    31  	assert := assert.New(t)
    32  
    33  	conf := config.NewConfig()
    34  	config.Set(conf)
    35  
    36  	// Short name service
    37  	assert.True(HasMatchingVirtualServices(Host{Service: "reviews", Namespace: "bookinfo", Cluster: "svc.cluster.local"}, []*networking_v1beta1.VirtualService{createVirtualService("bookinfo", []string{"reviews"})}))
    38  	assert.False(HasMatchingVirtualServices(Host{Service: "reviews", Namespace: "bookinfo", Cluster: "svc.cluster.local"}, []*networking_v1beta1.VirtualService{createVirtualService("bogus", []string{"reviews"})}))
    39  
    40  	// Half-FQDN
    41  	assert.True(HasMatchingVirtualServices(Host{Service: "reviews", Namespace: "bookinfo", Cluster: "svc.cluster.local"}, []*networking_v1beta1.VirtualService{createVirtualService("bookinfo", []string{"reviews.bookinfo"})}))
    42  	assert.False(HasMatchingVirtualServices(Host{Service: "reviews", Namespace: "bookinfo", Cluster: "svc.cluster.local"}, []*networking_v1beta1.VirtualService{createVirtualService("bogus", []string{"reviews.bogus"})}))
    43  
    44  	// FQDN
    45  	assert.True(HasMatchingVirtualServices(Host{Service: "reviews", Namespace: "bookinfo", Cluster: "svc.cluster.local"}, []*networking_v1beta1.VirtualService{createVirtualService("bookinfo", []string{"reviews.bookinfo.svc.cluster.local"})}))
    46  	assert.False(HasMatchingVirtualServices(Host{Service: "reviews", Namespace: "bookinfo", Cluster: "svc.cluster.local"}, []*networking_v1beta1.VirtualService{createVirtualService("bogus", []string{"reviews.bogus.svc.cluster.local"})}))
    47  
    48  	// Wildcard
    49  	assert.True(HasMatchingVirtualServices(Host{Service: "reviews", Namespace: "bookinfo", Cluster: "svc.cluster.local"}, []*networking_v1beta1.VirtualService{createVirtualService("bookinfo", []string{"*.bookinfo.svc.cluster.local"})}))
    50  	assert.True(HasMatchingVirtualServices(Host{Service: "reviews", Namespace: "bookinfo", Cluster: "svc.cluster.local"}, []*networking_v1beta1.VirtualService{createVirtualService("bookinfo", []string{"*"})}))
    51  	assert.False(HasMatchingVirtualServices(Host{Service: "reviews", Namespace: "bookinfo", Cluster: "svc.cluster.local"}, []*networking_v1beta1.VirtualService{createVirtualService("bogus", []string{"*.bogus.svc.cluster.local"})}))
    52  
    53  	// External host
    54  	assert.False(HasMatchingVirtualServices(Host{Service: "reviews", Namespace: "bookinfo", Cluster: "svc.cluster.local"}, []*networking_v1beta1.VirtualService{createVirtualService("bookinfo", []string{"foo.example.com"})}))
    55  	assert.False(HasMatchingVirtualServices(Host{Service: "reviews", Namespace: "bookinfo", Cluster: "svc.cluster.local"}, []*networking_v1beta1.VirtualService{createVirtualService("bookinfo", []string{"*.foo.example.com"})}))
    56  
    57  	assert.True(HasMatchingVirtualServices(Host{Service: "foo.example.com", Namespace: "", Cluster: ""}, []*networking_v1beta1.VirtualService{createVirtualService("bookinfo", []string{"foo.example.com"})}))
    58  	assert.True(HasMatchingVirtualServices(Host{Service: "new.foo.example.com", Namespace: "", Cluster: ""}, []*networking_v1beta1.VirtualService{createVirtualService("bookinfo", []string{"*.foo.example.com"})}))
    59  	assert.True(HasMatchingVirtualServices(Host{Service: "foo.example.com", Namespace: "", Cluster: ""}, []*networking_v1beta1.VirtualService{createVirtualService("bookinfo", []string{"*"})}))
    60  }
    61  
    62  func TestHasMatchingReferenceGrant(t *testing.T) {
    63  	assert := assert.New(t)
    64  
    65  	conf := config.NewConfig()
    66  	config.Set(conf)
    67  
    68  	assert.True(HasMatchingReferenceGrant("bookinfo", "default", K8sActualHTTPRouteType, ServiceType, []*k8s_networking_v1beta1.ReferenceGrant{createReferenceGrant("test", "default", "bookinfo"), createReferenceGrant("test", "bookinfo2", "bookinfo")}))
    69  	assert.False(HasMatchingReferenceGrant("bookinfo", "test", K8sActualHTTPRouteType, ServiceType, []*k8s_networking_v1beta1.ReferenceGrant{createReferenceGrant("test", "default", "bookinfo"), createReferenceGrant("test", "bookinfo2", "bookinfo")}))
    70  	assert.False(HasMatchingReferenceGrant("default", "bookinfo", K8sActualHTTPRouteType, ServiceType, []*k8s_networking_v1beta1.ReferenceGrant{createReferenceGrant("test", "default", "bookinfo"), createReferenceGrant("test", "bookinfo2", "bookinfo")}))
    71  }
    72  
    73  func createVirtualService(namespace string, hosts []string) *networking_v1beta1.VirtualService {
    74  	vsYaml := []byte(`
    75  apiVersion: networking.istio.io/v1beta1
    76  kind: VirtualService
    77  metadata:
    78    name: virtual-service 
    79  spec:
    80    hosts:
    81    - reviews
    82    http:
    83    - route:
    84      - destination:
    85          host: reviews
    86          subset: v2
    87      timeout: 0.5s
    88  `)
    89  	var vs networking_v1beta1.VirtualService
    90  	err := yaml.Unmarshal(vsYaml, &vs)
    91  	if err != nil {
    92  		log.Error(err)
    93  	}
    94  	vs.Namespace = namespace
    95  	vs.Spec.Hosts = hosts
    96  	return &vs
    97  }
    98  
    99  func createReferenceGrant(name string, namespace string, fromNamespace string) *k8s_networking_v1beta1.ReferenceGrant {
   100  	rg := k8s_networking_v1beta1.ReferenceGrant{}
   101  	rg.Name = name
   102  	rg.Namespace = namespace
   103  	rg.Spec.From = append(rg.Spec.From, k8s_networking_v1beta1.ReferenceGrantFrom{Kind: K8sActualHTTPRouteType, Group: k8s_networking_v1beta1.GroupName, Namespace: k8s_networking_v1.Namespace(fromNamespace)})
   104  	rg.Spec.To = append(rg.Spec.To, k8s_networking_v1beta1.ReferenceGrantTo{Kind: ServiceType})
   105  	return &rg
   106  }