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

     1  package serviceentries
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/kiali/kiali/models"
    10  	"github.com/kiali/kiali/tests/testutils/validations"
    11  )
    12  
    13  // ServiceEntry points to ratings.internal.cloud
    14  // and has workloadSelector: app=ratings
    15  // WorkloadEntry points to ratings.internal.cloud
    16  // and has labels app=ratings,version=v1
    17  func TestServiceEntryAddressMatch(t *testing.T) {
    18  	noValidationsShown(t, "ratings", "bookinfo")
    19  }
    20  
    21  // ServiceEntry points to an external host
    22  // and its location is MESH_EXTERNAL
    23  func TestExternalServiceEntry(t *testing.T) {
    24  	noValidationsShown(t, "ratings-external", "bookinfo")
    25  }
    26  
    27  // ServiceEntry hasn't got any workload matching
    28  func TestServiceEntryUnmatchingAddress(t *testing.T) {
    29  	noValidationsShown(t, "ratings-wrong-labels", "bookinfo")
    30  }
    31  
    32  // ServiceEntry points to ADDR1
    33  // and has workloadSelector: app=ratings-unmatching
    34  // WorkloadEntry points to ADDR2 (!= ADDR1)
    35  // and has labels app=ratings-unmatching,version=v1
    36  func TestServiceEntryWithNoMatchingWorkloads(t *testing.T) {
    37  	validationsShown(t, "ratings-unmatching-address", "bookinfo")
    38  }
    39  
    40  // ServiceEntry only has one address
    41  // There are two WorkloadEntries pointing to 2 different addresses
    42  func TestServiceEntryMissesWorkloadAddress(t *testing.T) {
    43  	validationsShown(t, "ratings-missing", "bookinfo")
    44  }
    45  
    46  // ServiceEntry points to 2 addresses
    47  // There is one WorkloadEntry. It's IP is in the ServiceEntry addresses list
    48  func TestServiceEntryMoreAddresses(t *testing.T) {
    49  	noValidationsShown(t, "ratings-surplus", "bookinfo")
    50  }
    51  
    52  func noValidationsShown(t *testing.T, serviceEntryName, namespace string) {
    53  	asserter := assert.New(t)
    54  	checks, valid := checksFor(serviceEntryName, namespace, t)
    55  	asserter.Empty(checks)
    56  	asserter.True(valid)
    57  }
    58  
    59  func validationsShown(t *testing.T, serviceEntryName, namespace string) {
    60  	asserter := assert.New(t)
    61  	checks, valid := checksFor(serviceEntryName, namespace, t)
    62  
    63  	asserter.NotEmpty(checks)
    64  	asserter.True(valid)
    65  	asserter.Equal(models.WarningSeverity, checks[0].Severity)
    66  	asserter.NoError(validations.ConfirmIstioCheckMessage("serviceentries.workloadentries.addressmatch", checks[0]))
    67  	asserter.Equal("spec/addresses", checks[0].Path)
    68  }
    69  
    70  func checksFor(serviceEntryName, namespace string, t *testing.T) ([]*models.IstioCheck, bool) {
    71  	loader := yamlFixtureLoaderFor("workload_entry_address_match.yaml")
    72  	if err := loader.Load(); err != nil {
    73  		t.Fatalf("Error loading tests manifests")
    74  	}
    75  
    76  	serviceEntry := loader.FindServiceEntry(serviceEntryName, namespace)
    77  	if serviceEntry == nil {
    78  		t.Fatalf("ServieEntry not found: %s/%s", serviceEntryName, namespace)
    79  		return []*models.IstioCheck{}, true
    80  	}
    81  
    82  	return HasMatchingWorkloadEntryAddress{
    83  		ServiceEntry:    serviceEntry,
    84  		WorkloadEntries: GroupWorkloadEntriesByLabels(loader.GetResources().WorkloadEntries),
    85  	}.Check()
    86  }
    87  
    88  func yamlFixtureLoaderFor(file string) *validations.YamlFixtureLoader {
    89  	path := fmt.Sprintf("../../../tests/data/validations/serviceentries/%s", file)
    90  	return &validations.YamlFixtureLoader{Filename: path}
    91  }