github.com/nmstate/kubernetes-nmstate@v0.82.0/pkg/bridge/bridge_test.go (about) 1 /* 2 Copyright The Kubernetes NMState Authors. 3 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 */ 17 18 package bridge 19 20 import ( 21 . "github.com/onsi/ginkgo/v2" 22 . "github.com/onsi/gomega" 23 24 nmstate "github.com/nmstate/kubernetes-nmstate/api/shared" 25 ) 26 27 var ( 28 badYaml = nmstate.NewState("}") 29 empty = nmstate.NewState("") 30 31 noBridges = nmstate.NewState(`interfaces: 32 - name: bond1 33 type: bond 34 state: up 35 link-aggregation: 36 mode: active-backup 37 port: 38 - eth1 39 options: 40 miimon: '120' 41 `) 42 noBridgesUp = nmstate.NewState(`interfaces: 43 - name: eth1 44 type: ethernet 45 state: up 46 - name: br1 47 type: linux-bridge 48 state: down 49 - name: br2 50 type: linux-bridge 51 state: absent 52 `) 53 54 bridgeWithNoPorts = nmstate.NewState(`interfaces: 55 - name: br1 56 type: linux-bridge 57 state: up 58 `) 59 60 someBridgesUp = nmstate.NewState(`interfaces: 61 - name: br1 62 type: linux-bridge 63 state: up 64 bridge: 65 port: 66 - name: eth1 67 - name: br2 68 type: linux-bridge 69 state: up 70 bridge: 71 port: 72 - name: eth2 73 - name: eth3 74 - name: br3 75 type: linux-bridge 76 state: down 77 - name: br4 78 type: linux-bridge 79 state: absent 80 `) 81 expectedSomeBridgesUpDefaults = nmstate.NewState(`interfaces: 82 - name: br1 83 type: linux-bridge 84 state: up 85 bridge: 86 port: 87 - name: eth1 88 vlan: 89 mode: trunk 90 trunk-tags: 91 - id-range: 92 max: 4094 93 min: 2 94 - name: br2 95 type: linux-bridge 96 state: up 97 bridge: 98 port: 99 - name: eth2 100 vlan: 101 mode: trunk 102 trunk-tags: 103 - id-range: 104 max: 4094 105 min: 2 106 - name: eth3 107 vlan: 108 mode: trunk 109 trunk-tags: 110 - id-range: 111 max: 4094 112 min: 2 113 - name: br3 114 type: linux-bridge 115 state: down 116 - name: br4 117 type: linux-bridge 118 state: absent 119 `) 120 bridgeWithCustomVlan = nmstate.NewState(`interfaces: 121 - name: br1 122 type: linux-bridge 123 state: up 124 bridge: 125 port: 126 - name: eth1 127 vlan: 128 mode: trunk 129 trunk-tags: 130 - id-range: 131 max: 200 132 min: 2 133 - id: 101 134 tag: 100 135 enable-native: true 136 `) 137 bridgeWithDisabledVlan = nmstate.NewState(`interfaces: 138 - name: br1 139 type: linux-bridge 140 state: up 141 bridge: 142 port: 143 - name: eth1 144 vlan: {} 145 `) 146 someBridgesWithVlanConfiguration = nmstate.NewState(`interfaces: 147 - name: br1 148 type: linux-bridge 149 state: up 150 bridge: 151 port: 152 - name: eth1 153 - name: br2 154 type: linux-bridge 155 state: up 156 bridge: 157 port: 158 - name: eth2 159 vlan: 160 mode: trunk 161 trunk-tags: 162 - id: 101 163 - id: 102 164 tag: 100 165 enable-native: true 166 - name: eth3 167 - name: br3 168 type: linux-bridge 169 state: down 170 - name: br4 171 type: linux-bridge 172 state: absent 173 `) 174 expectedSomeBridgesWithVlanConfigurationDefaults = nmstate.NewState(`interfaces: 175 - name: br1 176 type: linux-bridge 177 state: up 178 bridge: 179 port: 180 - name: eth1 181 vlan: 182 mode: trunk 183 trunk-tags: 184 - id-range: 185 max: 4094 186 min: 2 187 - name: br2 188 type: linux-bridge 189 state: up 190 bridge: 191 port: 192 - name: eth2 193 vlan: 194 mode: trunk 195 trunk-tags: 196 - id: 101 197 - id: 102 198 tag: 100 199 enable-native: true 200 - name: eth3 201 vlan: 202 mode: trunk 203 trunk-tags: 204 - id-range: 205 max: 4094 206 min: 2 207 - name: br3 208 type: linux-bridge 209 state: down 210 - name: br4 211 type: linux-bridge 212 state: absent 213 `) 214 ) 215 216 var _ = Describe("Network desired state bridge parser", func() { 217 var ( 218 updatedDesiredState nmstate.State 219 desiredState nmstate.State 220 err error 221 ) 222 JustBeforeEach(func() { 223 updatedDesiredState, err = ApplyDefaultVlanFiltering(desiredState) 224 }) 225 Context("when desired state is not a yaml", func() { 226 BeforeEach(func() { 227 desiredState = badYaml 228 }) 229 It("should return error", func() { 230 Expect(err).To(HaveOccurred()) 231 }) 232 }) 233 Context("when desired state is empty", func() { 234 BeforeEach(func() { 235 desiredState = empty 236 }) 237 It("should not be changed", func() { 238 Expect(err).ToNot(HaveOccurred()) 239 Expect(updatedDesiredState).To(MatchYAML(desiredState)) 240 }) 241 }) 242 Context("when there are no bridges", func() { 243 BeforeEach(func() { 244 desiredState = noBridges 245 }) 246 It("should not be changed", func() { 247 Expect(err).ToNot(HaveOccurred()) 248 Expect(updatedDesiredState).To(MatchYAML(desiredState)) 249 }) 250 }) 251 Context("when there are no bridges up", func() { 252 BeforeEach(func() { 253 desiredState = noBridgesUp 254 }) 255 It("should not be changed", func() { 256 Expect(err).ToNot(HaveOccurred()) 257 Expect(updatedDesiredState).To(MatchYAML(desiredState)) 258 }) 259 }) 260 Context("when there are no ports in the bridge", func() { 261 BeforeEach(func() { 262 desiredState = bridgeWithNoPorts 263 }) 264 It("should not be changed", func() { 265 Expect(err).ToNot(HaveOccurred()) 266 Expect(updatedDesiredState).To(MatchYAML(desiredState)) 267 }) 268 }) 269 Context("when there are bridges up", func() { 270 BeforeEach(func() { 271 desiredState = someBridgesUp 272 }) 273 It("should add default vlan filtering to linux-bridge ports", func() { 274 Expect(err).ToNot(HaveOccurred()) 275 Expect(updatedDesiredState).To(MatchYAML(expectedSomeBridgesUpDefaults)) 276 }) 277 Context("when there is custom vlan configuration on linux-bridge port", func() { 278 BeforeEach(func() { 279 desiredState = bridgeWithCustomVlan 280 }) 281 It("should keep custom vlan configuration intact", func() { 282 Expect(err).ToNot(HaveOccurred()) 283 Expect(updatedDesiredState).To(MatchYAML(desiredState)) 284 }) 285 }) 286 Context("when there is empty vlan configuration", func() { 287 BeforeEach(func() { 288 desiredState = bridgeWithDisabledVlan 289 }) 290 It("should keep custom vlan configuration intact", func() { 291 Expect(err).ToNot(HaveOccurred()) 292 Expect(updatedDesiredState).To(MatchYAML(desiredState)) 293 }) 294 }) 295 Context("when some ports have vlan configuration while other do not", func() { 296 BeforeEach(func() { 297 desiredState = someBridgesWithVlanConfiguration 298 }) 299 It("should keep custom vlan configuration intact", func() { 300 Expect(err).ToNot(HaveOccurred()) 301 Expect(updatedDesiredState).To(MatchYAML(expectedSomeBridgesWithVlanConfigurationDefaults)) 302 }) 303 }) 304 }) 305 })