istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/bootstrap/config_compare_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 bootstrap 16 17 import ( 18 "testing" 19 20 networking "istio.io/api/networking/v1alpha3" 21 "istio.io/istio/pkg/config" 22 "istio.io/istio/pkg/config/constants" 23 "istio.io/istio/pkg/config/schema/gvk" 24 ) 25 26 func TestNeedsPush(t *testing.T) { 27 cases := []struct { 28 name string 29 prev config.Config 30 curr config.Config 31 expected bool 32 }{ 33 { 34 name: "different gvk", 35 prev: config.Config{ 36 Meta: config.Meta{ 37 GroupVersionKind: gvk.VirtualService, 38 Name: "acme2-v1", 39 Namespace: "not-default", 40 }, 41 Spec: &networking.VirtualService{}, 42 }, 43 curr: config.Config{ 44 Meta: config.Meta{ 45 GroupVersionKind: gvk.DestinationRule, 46 Name: "acme2-v1", 47 Namespace: "not-default", 48 }, 49 Spec: &networking.VirtualService{}, 50 }, 51 expected: true, 52 }, 53 { 54 name: "same gvk label change", 55 prev: config.Config{ 56 Meta: config.Meta{ 57 GroupVersionKind: gvk.VirtualService, 58 Name: "acme2-v1", 59 Namespace: "not-default", 60 Labels: map[string]string{"test": "test-v1"}, 61 }, 62 Spec: &networking.VirtualService{}, 63 }, 64 curr: config.Config{ 65 Meta: config.Meta{ 66 GroupVersionKind: gvk.VirtualService, 67 Name: "acme2-v1", 68 Namespace: "not-default", 69 Labels: map[string]string{"test": "test-v2"}, 70 }, 71 Spec: &networking.VirtualService{}, 72 }, 73 expected: false, 74 }, 75 { 76 name: "same gvk spec change", 77 prev: config.Config{ 78 Meta: config.Meta{ 79 GroupVersionKind: gvk.VirtualService, 80 Name: "acme2-v1", 81 Namespace: "not-default", 82 Labels: map[string]string{"test": "test-v1"}, 83 }, 84 Spec: &networking.VirtualService{Hosts: []string{"test-host"}}, 85 }, 86 curr: config.Config{ 87 Meta: config.Meta{ 88 GroupVersionKind: gvk.VirtualService, 89 Name: "acme2-v1", 90 Namespace: "not-default", 91 Labels: map[string]string{"test": "test-v1"}, 92 }, 93 Spec: &networking.VirtualService{Hosts: []string{"test-host", "test-host2"}}, 94 }, 95 expected: true, 96 }, 97 { 98 name: "current config with istio.io label", 99 prev: config.Config{ 100 Meta: config.Meta{ 101 GroupVersionKind: gvk.Ingress, 102 Name: "acme2-v1", 103 Namespace: "not-default", 104 }, 105 }, 106 curr: config.Config{ 107 Meta: config.Meta{ 108 GroupVersionKind: gvk.Ingress, 109 Name: "acme2-v1", 110 Namespace: "not-default", 111 Labels: map[string]string{constants.AlwaysPushLabel: "true"}, 112 }, 113 }, 114 expected: true, 115 }, 116 { 117 name: "previous config with istio.io label", 118 prev: config.Config{ 119 Meta: config.Meta{ 120 GroupVersionKind: gvk.Ingress, 121 Name: "acme2-v1", 122 Namespace: "not-default", 123 Labels: map[string]string{constants.AlwaysPushLabel: "true"}, 124 }, 125 }, 126 curr: config.Config{ 127 Meta: config.Meta{ 128 GroupVersionKind: gvk.Ingress, 129 Name: "acme2-v1", 130 Namespace: "not-default", 131 }, 132 }, 133 expected: true, 134 }, 135 { 136 name: "current config with istio.io annotation", 137 prev: config.Config{ 138 Meta: config.Meta{ 139 GroupVersionKind: gvk.Ingress, 140 Name: "acme2-v1", 141 Namespace: "not-default", 142 }, 143 }, 144 curr: config.Config{ 145 Meta: config.Meta{ 146 GroupVersionKind: gvk.Ingress, 147 Name: "acme2-v1", 148 Namespace: "not-default", 149 Annotations: map[string]string{constants.AlwaysPushLabel: "true"}, 150 }, 151 }, 152 expected: true, 153 }, 154 { 155 name: "previous config with istio.io annotation", 156 prev: config.Config{ 157 Meta: config.Meta{ 158 GroupVersionKind: gvk.Ingress, 159 Name: "acme2-v1", 160 Namespace: "not-default", 161 Annotations: map[string]string{constants.AlwaysPushLabel: "true"}, 162 }, 163 }, 164 curr: config.Config{ 165 Meta: config.Meta{ 166 GroupVersionKind: gvk.Ingress, 167 Name: "acme2-v1", 168 Namespace: "not-default", 169 }, 170 }, 171 expected: true, 172 }, 173 { 174 name: "non istio resources", 175 prev: config.Config{ 176 Meta: config.Meta{ 177 GroupVersionKind: gvk.TCPRoute, 178 Name: "acme2-v1", 179 Namespace: "not-default", 180 Labels: map[string]string{"test": "test-v1"}, 181 }, 182 Spec: &networking.VirtualService{}, 183 }, 184 curr: config.Config{ 185 Meta: config.Meta{ 186 GroupVersionKind: gvk.TCPRoute, 187 Name: "acme2-v1", 188 Namespace: "not-default", 189 Labels: map[string]string{"test": "test-v2"}, 190 }, 191 Spec: &networking.VirtualService{}, 192 }, 193 expected: true, 194 }, 195 } 196 197 for _, c := range cases { 198 t.Run(c.name, func(t *testing.T) { 199 if got := needsPush(c.prev, c.curr); got != c.expected { 200 t.Errorf("unexpected needsPush result. expected: %v got: %v", c.expected, got) 201 } 202 }) 203 } 204 }