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

     1  package gateways
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/kiali/kiali/config"
     9  	"github.com/kiali/kiali/models"
    10  	"github.com/kiali/kiali/tests/data"
    11  	"github.com/kiali/kiali/tests/testutils/validations"
    12  )
    13  
    14  func TestValidInternalSelector(t *testing.T) {
    15  	conf := config.NewConfig()
    16  	config.Set(conf)
    17  
    18  	assert := assert.New(t)
    19  
    20  	ingress := data.CreateEmptyGateway("gwingress", "test", map[string]string{"istio": "ingressgateway"})
    21  	egress := data.CreateEmptyGateway("gwegress", "test", map[string]string{"istio": "egressgateway"})
    22  
    23  	vals, valid := SelectorChecker{
    24  		Gateway: ingress,
    25  		WorkloadsPerNamespace: map[string]models.WorkloadList{
    26  			"test": data.CreateWorkloadList("istio-system",
    27  				data.CreateWorkloadListItem("istio-ingressgateway", map[string]string{"istio": "ingressgateway"})),
    28  		},
    29  		IsGatewayToNamespace: false,
    30  	}.Check()
    31  
    32  	assert.True(valid)
    33  	assert.Empty(vals)
    34  
    35  	vals, valid = SelectorChecker{
    36  		Gateway: egress,
    37  		WorkloadsPerNamespace: map[string]models.WorkloadList{
    38  			"test": data.CreateWorkloadList("istio-system",
    39  				data.CreateWorkloadListItem("istio-egressgateway", map[string]string{"istio": "egressgateway"})),
    40  		},
    41  		IsGatewayToNamespace: false,
    42  	}.Check()
    43  
    44  	assert.True(valid)
    45  	assert.Empty(vals)
    46  }
    47  
    48  func TestValidNamespaceSelector(t *testing.T) {
    49  	conf := config.NewConfig()
    50  	config.Set(conf)
    51  
    52  	assert := assert.New(t)
    53  
    54  	gw := data.CreateEmptyGateway("gwone", "test", map[string]string{"app": "proxy"})
    55  
    56  	vals, valid := SelectorChecker{
    57  		Gateway: gw,
    58  		WorkloadsPerNamespace: map[string]models.WorkloadList{
    59  			"test": data.CreateWorkloadList("test",
    60  				data.CreateWorkloadListItem("testproxy", map[string]string{"app": "proxy"})),
    61  		},
    62  		IsGatewayToNamespace: false,
    63  	}.Check()
    64  
    65  	assert.True(valid)
    66  	assert.Empty(vals)
    67  }
    68  
    69  func TestLocalNamespaceSelector(t *testing.T) {
    70  	conf := config.NewConfig()
    71  	config.Set(conf)
    72  
    73  	assert := assert.New(t)
    74  
    75  	// gateway and workload in same namespace, IsGatewayToNamespace: true, valid
    76  	gw := data.CreateEmptyGateway("gwone", "test", map[string]string{"app": "proxy"})
    77  
    78  	vals, valid := SelectorChecker{
    79  		Gateway: gw,
    80  		WorkloadsPerNamespace: map[string]models.WorkloadList{
    81  			"test": data.CreateWorkloadList("test",
    82  				data.CreateWorkloadListItem("testproxy", map[string]string{"app": "proxy"})),
    83  		},
    84  		IsGatewayToNamespace: true,
    85  	}.Check()
    86  
    87  	assert.True(valid)
    88  	assert.Empty(vals)
    89  
    90  	// gateway and workload in same namespace, IsGatewayToNamespace: false, valid
    91  	gw = data.CreateEmptyGateway("gwone", "test", map[string]string{"app": "proxy"})
    92  
    93  	vals, valid = SelectorChecker{
    94  		Gateway: gw,
    95  		WorkloadsPerNamespace: map[string]models.WorkloadList{
    96  			"test": data.CreateWorkloadList("test",
    97  				data.CreateWorkloadListItem("testproxy", map[string]string{"app": "proxy"})),
    98  		},
    99  		IsGatewayToNamespace: false,
   100  	}.Check()
   101  
   102  	assert.True(valid)
   103  	assert.Empty(vals)
   104  
   105  	// gateway and workload in different namespace, IsGatewayToNamespace: true, invalid
   106  	gw = data.CreateEmptyGateway("gwone", "other", map[string]string{"app": "proxy"})
   107  
   108  	vals, valid = SelectorChecker{
   109  		Gateway: gw,
   110  		WorkloadsPerNamespace: map[string]models.WorkloadList{
   111  			"test": data.CreateWorkloadList("test",
   112  				data.CreateWorkloadListItem("testproxy", map[string]string{"app": "proxy"})),
   113  		},
   114  		IsGatewayToNamespace: true,
   115  	}.Check()
   116  
   117  	assert.False(valid)
   118  	assert.NotEmpty(vals)
   119  
   120  	// gateway and workload in different namespace, IsGatewayToNamespace: false, valid
   121  	gw = data.CreateEmptyGateway("gwone", "other", map[string]string{"app": "proxy"})
   122  
   123  	vals, valid = SelectorChecker{
   124  		Gateway: gw,
   125  		WorkloadsPerNamespace: map[string]models.WorkloadList{
   126  			"test": data.CreateWorkloadList("test",
   127  				data.CreateWorkloadListItem("testproxy", map[string]string{"app": "proxy"})),
   128  		},
   129  		IsGatewayToNamespace: false,
   130  	}.Check()
   131  
   132  	assert.True(valid)
   133  	assert.Empty(vals)
   134  }
   135  
   136  func TestValidIstioNamespaceSelector(t *testing.T) {
   137  	conf := config.NewConfig()
   138  	config.Set(conf)
   139  
   140  	assert := assert.New(t)
   141  
   142  	gw := data.CreateEmptyGateway("gwone", "test", map[string]string{"app": "proxy"})
   143  
   144  	vals, valid := SelectorChecker{
   145  		Gateway: gw,
   146  		WorkloadsPerNamespace: map[string]models.WorkloadList{
   147  			"testproxy": data.CreateWorkloadList(conf.IstioNamespace,
   148  				data.CreateWorkloadListItem("testproxy", map[string]string{"app": "proxy"})),
   149  		},
   150  		IsGatewayToNamespace: false,
   151  	}.Check()
   152  
   153  	assert.True(valid)
   154  	assert.Empty(vals)
   155  
   156  	vals, valid = SelectorChecker{
   157  		Gateway: gw,
   158  		WorkloadsPerNamespace: map[string]models.WorkloadList{
   159  			"test": {
   160  				Namespace: "test",
   161  				Workloads: []models.WorkloadListItem{},
   162  			},
   163  		},
   164  		IsGatewayToNamespace: false,
   165  	}.Check()
   166  
   167  	assert.False(valid)
   168  	assert.NotEmpty(vals)
   169  }
   170  
   171  func TestMissingSelectorTarget(t *testing.T) {
   172  	conf := config.NewConfig()
   173  	config.Set(conf)
   174  
   175  	assert := assert.New(t)
   176  
   177  	gw := data.CreateEmptyGateway("gwone", "test", map[string]string{"app": "proxy"})
   178  
   179  	vals, valid := SelectorChecker{
   180  		Gateway: gw,
   181  		WorkloadsPerNamespace: map[string]models.WorkloadList{
   182  			"test": data.CreateWorkloadList("test"),
   183  		},
   184  		IsGatewayToNamespace: false,
   185  	}.Check()
   186  
   187  	assert.False(valid)
   188  	assert.Equal(1, len(vals))
   189  	assert.NoError(validations.ConfirmIstioCheckMessage("gateways.selector", vals[0]))
   190  	assert.Equal(models.WarningSeverity, vals[0].Severity)
   191  }