github.com/kiali/kiali@v1.84.0/business/checkers/common/export_to_namespace_checker_test.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 "github.com/kiali/kiali/config" 10 "github.com/kiali/kiali/models" 11 "github.com/kiali/kiali/tests/testutils/validations" 12 ) 13 14 func TestDRNamespaceExist(t *testing.T) { 15 assertIstioObjectValid("dr_exportto_valid.yaml", "DestinationRule", t) 16 } 17 18 func TestDRNamespaceNotFound(t *testing.T) { 19 assertIstioObjectInvalidNamespace("dr_exportto_invalid.yaml", "DestinationRule", 2, t) 20 } 21 22 func TestDRAllNamespaces(t *testing.T) { 23 assertIstioObjectValid("dr_exportto_all_valid.yaml", "DestinationRule", t) 24 } 25 26 func TestVSNamespaceExist(t *testing.T) { 27 assertIstioObjectValid("vs_exportto_valid.yaml", "VirtualService", t) 28 } 29 30 func TestVSAllExist(t *testing.T) { 31 assertIstioObjectValid("vs_exportto_all_valid.yaml", "VirtualService", t) 32 } 33 34 func TestVSNamespaceNotFound(t *testing.T) { 35 assertIstioObjectInvalidNamespace("vs_exportto_invalid.yaml", "VirtualService", 2, t) 36 } 37 38 func TestVSAllNamespaces(t *testing.T) { 39 assertIstioObjectValid("vs_exportto_all_valid.yaml", "VirtualService", t) 40 } 41 42 func TestSENamespaceExist(t *testing.T) { 43 assertIstioObjectValid("se_exportto_valid.yaml", "ServiceEntry", t) 44 } 45 46 func TestSENamespaceNotFound(t *testing.T) { 47 assertIstioObjectInvalidNamespace("se_exportto_invalid.yaml", "ServiceEntry", 2, t) 48 } 49 50 func TestSEAllNamespaces(t *testing.T) { 51 assertIstioObjectValid("se_exportto_all_valid.yaml", "ServiceEntry", t) 52 } 53 54 func assertIstioObjectValid(scenario string, objectType string, t *testing.T) { 55 assert := assert.New(t) 56 57 validations, valid := validateIstioObject(scenario, objectType, t) 58 59 // Well configured object 60 assert.True(valid) 61 assert.Empty(validations) 62 } 63 64 func assertIstioObjectInvalidNamespace(scenario string, objectType string, errorNumbers int, t *testing.T) { 65 assert := assert.New(t) 66 67 vals, valid := validateIstioObject("dr_exportto_invalid.yaml", "DestinationRule", t) 68 69 assert.False(valid) 70 assert.NotEmpty(vals) 71 assert.Len(vals, errorNumbers) 72 for i := 0; i < errorNumbers; i++ { 73 assert.NoError(validations.ConfirmIstioCheckMessage("generic.exportto.namespacenotfound", vals[i])) 74 assert.Equal(vals[i].Severity, models.ErrorSeverity) 75 assert.Equal(vals[i].Path, fmt.Sprintf("spec/exportTo[%d]", i)) 76 } 77 } 78 79 func validateIstioObject(scenario string, objectType string, t *testing.T) ([]*models.IstioCheck, bool) { 80 conf := config.NewConfig() 81 config.Set(conf) 82 83 loader := yamlFixtureLoaderFor(scenario) 84 err := loader.Load() 85 if err != nil { 86 t.Error("Error loading test data.") 87 } 88 89 exportTo := []string{} 90 switch objectType { 91 case "DestinationRule": 92 dr := loader.GetResources().DestinationRules 93 if len(dr) > 0 { 94 exportTo = dr[0].Spec.ExportTo 95 } 96 case "VirtualService": 97 vs := loader.GetResources().VirtualServices 98 if len(vs) > 0 { 99 exportTo = vs[0].Spec.ExportTo 100 } 101 case "ServiceEntry": 102 se := loader.GetResources().ServiceEntries 103 if len(se) > 0 { 104 exportTo = se[0].Spec.ExportTo 105 } 106 } 107 108 validations, valid := ExportToNamespaceChecker{ 109 ExportTo: exportTo, 110 Namespaces: models.Namespaces{ 111 models.Namespace{Name: "bookinfo"}, 112 models.Namespace{Name: "bookinfo2"}, 113 models.Namespace{Name: "default"}, 114 }, 115 }.Check() 116 117 return validations, valid 118 } 119 120 func yamlFixtureLoaderFor(file string) *validations.YamlFixtureLoader { 121 path := fmt.Sprintf("../../../tests/data/validations/exportto/%s", file) 122 return &validations.YamlFixtureLoader{Filename: path} 123 }