istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/analysis/analyzers/util/hosts_test.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package util 16 17 import ( 18 "testing" 19 20 . "github.com/onsi/gomega" 21 22 "istio.io/istio/pkg/config/resource" 23 ) 24 25 func TestGetResourceNameFromHost(t *testing.T) { 26 g := NewWithT(t) 27 28 // FQDN, same namespace 29 g.Expect(GetResourceNameFromHost("default", "foo.default.svc.cluster.local")).To(Equal(resource.NewFullName("default", "foo"))) 30 // FQDN, cross namespace 31 g.Expect(GetResourceNameFromHost("default", "foo.other.svc.cluster.local")).To(Equal(resource.NewFullName("other", "foo"))) 32 // short name 33 g.Expect(GetResourceNameFromHost("default", "foo")).To(Equal(resource.NewFullName("default", "foo"))) 34 // bogus FQDN (gets treated like a short name) 35 g.Expect(GetResourceNameFromHost("default", "foo.svc.cluster.local")).To(Equal(resource.NewFullName("default", "foo.svc.cluster.local"))) 36 } 37 38 func TestGetScopedFqdnHostname(t *testing.T) { 39 g := NewWithT(t) 40 41 // FQDN, same namespace, local scope 42 g.Expect(NewScopedFqdn("default", "default", "foo.default.svc.cluster.local")).To(Equal(ScopedFqdn("default/foo.default.svc.cluster.local"))) 43 // FQDN, cross namespace, local scope 44 g.Expect(NewScopedFqdn("default", "other", "foo.default.svc.cluster.local")).To(Equal(ScopedFqdn("default/foo.default.svc.cluster.local"))) 45 // FQDN, same namespace, all namespaces scope 46 g.Expect(NewScopedFqdn("*", "default", "foo.default.svc.cluster.local")).To(Equal(ScopedFqdn("*/foo.default.svc.cluster.local"))) 47 // FQDN, cross namespace, all namespaces scope 48 g.Expect(NewScopedFqdn("*", "other", "foo.default.svc.cluster.local")).To(Equal(ScopedFqdn("*/foo.default.svc.cluster.local"))) 49 50 // short name, same namespace, local scope 51 g.Expect(NewScopedFqdn("default", "default", "foo")).To(Equal(ScopedFqdn("default/foo.default.svc.cluster.local"))) 52 // short name, same namespace, all namespaces scope 53 g.Expect(NewScopedFqdn("*", "default", "foo")).To(Equal(ScopedFqdn("*/foo.default.svc.cluster.local"))) 54 55 // wildcard, local scope 56 g.Expect(NewScopedFqdn("foo", "foo", "*")).To(Equal(ScopedFqdn("foo/*"))) 57 // wildcard sub domain, local scope 58 g.Expect(NewScopedFqdn("foo", "foo", "*.xyz.abc")).To(Equal(ScopedFqdn("foo/*.xyz.abc"))) 59 // wildcard, all namespaces scope 60 g.Expect(NewScopedFqdn("*", "foo", "*")).To(Equal(ScopedFqdn("*/*"))) 61 // wildcard sub domain, all namespaces scope 62 g.Expect(NewScopedFqdn("*", "foo", "*.xyz.abc")).To(Equal(ScopedFqdn("*/*.xyz.abc"))) 63 64 // external host, local scope 65 g.Expect(NewScopedFqdn("foo", "foo", "xyz.abc")).To(Equal(ScopedFqdn("foo/xyz.abc"))) 66 // external host, all namespaces scope 67 g.Expect(NewScopedFqdn("*", "foo", "xyz.abc")).To(Equal(ScopedFqdn("*/xyz.abc"))) 68 } 69 70 func TestScopedFqdn_GetScopeAndFqdn(t *testing.T) { 71 g := NewWithT(t) 72 73 ns, fqdn := ScopedFqdn("default/reviews.default.svc.cluster.local").GetScopeAndFqdn() 74 g.Expect(ns).To(Equal("default")) 75 g.Expect(fqdn).To(Equal("reviews.default.svc.cluster.local")) 76 77 ns, fqdn = ScopedFqdn("*/reviews.default.svc.cluster.local").GetScopeAndFqdn() 78 g.Expect(ns).To(Equal("*")) 79 g.Expect(fqdn).To(Equal("reviews.default.svc.cluster.local")) 80 81 ns, fqdn = ScopedFqdn("foo/*.xyz.abc").GetScopeAndFqdn() 82 g.Expect(ns).To(Equal("foo")) 83 g.Expect(fqdn).To(Equal("*.xyz.abc")) 84 } 85 86 func TestScopedFqdn_InScopeOf(t *testing.T) { 87 tests := []struct { 88 ScFqdn ScopedFqdn 89 Namespace string 90 Want bool 91 }{ 92 {"*/reviews.bookinfo.svc.cluster.local", "bookinfo", true}, 93 {"*/reviews.bookinfo.svc.cluster.local", "foo", true}, 94 {"./reviews.bookinfo.svc.cluster.local", "bookinfo", true}, 95 {"./reviews.bookinfo.svc.cluster.local", "foo", false}, 96 {"bookinfo/reviews.bookinfo.svc.cluster.local", "bookinfo", true}, 97 {"bookinfo/reviews.bookinfo.svc.cluster.local", "foo", false}, 98 } 99 100 for _, test := range tests { 101 if test.ScFqdn.InScopeOf(test.Namespace) != test.Want { 102 t.Errorf("%s is in the scope of %s: %t. It should be %t", test.ScFqdn, test.Namespace, !test.Want, test.Want) 103 } 104 } 105 }