github.com/kiali/kiali@v1.84.0/business/checkers/k8shttproute_checker_test.go (about)

     1  package checkers
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	k8s_networking_v1 "sigs.k8s.io/gateway-api/apis/v1"
     8  
     9  	"github.com/kiali/kiali/config"
    10  	"github.com/kiali/kiali/models"
    11  	"github.com/kiali/kiali/tests/data"
    12  	"github.com/kiali/kiali/tests/testutils/validations"
    13  )
    14  
    15  func TestNoCrashOnEmptyRoute(t *testing.T) {
    16  	assert := assert.New(t)
    17  
    18  	typeValidations := K8sHTTPRouteChecker{
    19  		K8sHTTPRoutes:    []*k8s_networking_v1.HTTPRoute{},
    20  		K8sGateways:      []*k8s_networking_v1.Gateway{},
    21  		RegistryServices: data.CreateEmptyRegistryServices(),
    22  		Namespaces:       models.Namespaces{},
    23  	}.Check()
    24  
    25  	assert.Empty(typeValidations)
    26  }
    27  
    28  func TestWithoutK8sGateway(t *testing.T) {
    29  	conf := config.NewConfig()
    30  	config.Set(conf)
    31  	assert := assert.New(t)
    32  
    33  	vals := K8sHTTPRouteChecker{
    34  		K8sHTTPRoutes: []*k8s_networking_v1.HTTPRoute{
    35  			data.CreateHTTPRoute("route1", "bookinfo", "gatewayapi", []string{"bookinfo"}),
    36  			data.CreateHTTPRoute("route2", "bookinfo", "gatewayapi2", []string{"bookinfo"})},
    37  		K8sGateways: []*k8s_networking_v1.Gateway{data.CreateEmptyK8sGateway("gatewayapiwrong", "bookinfo")},
    38  	}.Check()
    39  
    40  	assert.NotEmpty(vals)
    41  
    42  	route1 := vals[models.IstioValidationKey{ObjectType: "k8shttproute", Namespace: "bookinfo", Name: "route1"}]
    43  	assert.False(route1.Valid)
    44  	assert.NoError(validations.ConfirmIstioCheckMessage("k8shttproutes.nok8sgateway", route1.Checks[0]))
    45  	route2 := vals[models.IstioValidationKey{ObjectType: "k8shttproute", Namespace: "bookinfo", Name: "route2"}]
    46  	assert.False(route2.Valid)
    47  	assert.NoError(validations.ConfirmIstioCheckMessage("k8shttproutes.nok8sgateway", route2.Checks[0]))
    48  }
    49  
    50  func TestWithoutService(t *testing.T) {
    51  	conf := config.NewConfig()
    52  	config.Set(conf)
    53  	assert := assert.New(t)
    54  
    55  	registryService1 := data.CreateFakeRegistryServices("other.bookinfo.svc.cluster.local", "bookinfo", "*")
    56  	registryService2 := data.CreateFakeRegistryServices("details.bookinfo.svc.cluster.local", "bookinfo2", "*")
    57  
    58  	vals := K8sHTTPRouteChecker{
    59  		K8sHTTPRoutes: []*k8s_networking_v1.HTTPRoute{
    60  			data.AddBackendRefToHTTPRoute("ratings", "bookinfo", data.CreateHTTPRoute("route1", "bookinfo", "gatewayapi", []string{"bookinfo"})),
    61  			data.AddBackendRefToHTTPRoute("ratings", "bookinfo", data.CreateHTTPRoute("route2", "bookinfo2", "gatewayapi2", []string{"bookinfo2"}))},
    62  		K8sGateways:      []*k8s_networking_v1.Gateway{data.CreateEmptyK8sGateway("gatewayapi", "bookinfo"), data.CreateEmptyK8sGateway("gatewayapi2", "bookinfo2")},
    63  		RegistryServices: append(registryService1, registryService2...),
    64  		Namespaces:       models.Namespaces{models.Namespace{Name: "bookinfo"}, models.Namespace{Name: "bookinfo2"}, models.Namespace{Name: "bookinfo3"}},
    65  	}.Check()
    66  
    67  	assert.NotEmpty(vals)
    68  
    69  	route1 := vals[models.IstioValidationKey{ObjectType: "k8shttproute", Namespace: "bookinfo", Name: "route1"}]
    70  	assert.False(route1.Valid)
    71  	assert.NoError(validations.ConfirmIstioCheckMessage("k8shttproutes.nohost.namenotfound", route1.Checks[0]))
    72  	route2 := vals[models.IstioValidationKey{ObjectType: "k8shttproute", Namespace: "bookinfo2", Name: "route2"}]
    73  	assert.False(route2.Valid)
    74  	assert.NoError(validations.ConfirmIstioCheckMessage("k8shttproutes.nohost.namenotfound", route2.Checks[0]))
    75  }