Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/e2e/handler/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,34 @@ func vrfAbsent(vrfID string) nmstate.State {
`, vrfID))
}

// vrfRouteWithVrfName creates a VRF interface and a route using vrf-name instead of table-id.
func vrfRouteWithVrfName(vrfID, vrfName, iface, ipAddress, destIPAddress, prefixLen, nextHopIPAddress string) nmstate.State {
return nmstate.NewState(fmt.Sprintf(`interfaces:
- name: %s
type: vrf
state: up
vrf:
port: [%s]
route-table-id: %s
- name: %s
type: ethernet
state: up
ipv4:
address:
- ip: %s
prefix-length: %s
dhcp: false
enabled: true
routes:
config:
- destination: %s
metric: 150
next-hop-address: %s
next-hop-interface: %s
vrf-name: %s
Comment on lines +260 to +264

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The YAML for the route configuration is incorrectly indented. The list item under config (the - on line 260) should be indented to be a child of config. Currently, it's at the same level, which is invalid YAML and will cause a parsing error when the state is applied.

Suggested change
- destination: %s
metric: 150
next-hop-address: %s
next-hop-interface: %s
vrf-name: %s
- destination: %s
metric: 150
next-hop-address: %s
next-hop-interface: %s
vrf-name: %s

`, vrfName, iface, vrfID, iface, ipAddress, prefixLen, destIPAddress, nextHopIPAddress, iface, vrfName))
}

func interfaceAbsent(iface string) nmstate.State {
return nmstate.NewState(fmt.Sprintf(`interfaces:
- name: %s
Expand Down
48 changes: 48 additions & 0 deletions test/e2e/handler/vrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package handler

import (
"github.com/nmstate/kubernetes-nmstate/test/e2e/policy"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -59,4 +60,51 @@ var _ = Describe("NodeNetworkState", func() {
routeNextHopInterfaceWithTableID(node, destIPAddress, vrfID).Should(Equal(firstSecondaryNic))
})
})

Context("when VRF route configured using vrf-name", func() {
var (
vrfID = "103"
vrfName = "vrf103"
ipAddress = "192.0.2.252"
destIPAddress = "1.2.3.0/24"
prefixLen = "24"
nextHopIPAddress = "192.0.2.2"
)

BeforeEach(func() {
updateDesiredStateAtNodeAndWait(
node,
vrfRouteWithVrfName(vrfID, vrfName, firstSecondaryNic, ipAddress, destIPAddress, prefixLen, nextHopIPAddress),
)
})

AfterEach(func() {
updateDesiredStateAtNodeAndWait(node, vrfAbsent(vrfID))
resetDesiredStateForNodes()
})

It("should have the VRF interface and route configured", func() {
vrfForNodeInterfaceEventually(node, vrfID).Should(Equal(vrfID))
ipAddressForNodeInterfaceEventually(node, firstSecondaryNic).Should(Equal(ipAddress))
routeNextHopInterfaceWithTableID(node, destIPAddress, vrfID).Should(Equal(firstSecondaryNic))
})

It("should persist VRF route after node reboot", func() {
Byf("Verifying VRF interface and route before reboot")
vrfForNodeInterfaceEventually(node, vrfID).Should(Equal(vrfID))
ipAddressForNodeInterfaceEventually(node, firstSecondaryNic).Should(Equal(ipAddress))
routeNextHopInterfaceWithTableID(node, destIPAddress, vrfID).Should(Equal(firstSecondaryNic))

restartNodeWithoutWaiting(node)

By("Wait for policy re-reconciled after node reboot")
policy.WaitForPolicyTransitionUpdate(TestPolicy)
policy.WaitForAvailablePolicy(TestPolicy)

Byf("Node %s was rebooted, verifying VRF route still exists", node)
vrfForNodeInterfaceEventually(node, vrfID).Should(Equal(vrfID))
ipAddressForNodeInterfaceEventually(node, firstSecondaryNic).Should(Equal(ipAddress))
routeNextHopInterfaceWithTableID(node, destIPAddress, vrfID).Should(Equal(firstSecondaryNic))
})
Comment on lines +86 to +108

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and reduce code duplication, you can extract the repeated verification logic into a helper function. The assertions on lines 87-89, 94-96, and 105-107 are identical. A helper function will make the tests cleaner and easier to read.

		verifyVrfRouteIsConfigured := func() {
			vrfForNodeInterfaceEventually(node, vrfID).Should(Equal(vrfID))
			ipAddressForNodeInterfaceEventually(node, firstSecondaryNic).Should(Equal(ipAddress))
			routeNextHopInterfaceWithTableID(node, destIPAddress, vrfID).Should(Equal(firstSecondaryNic))
		}

		It("should have the VRF interface and route configured", func() {
			verifyVrfRouteIsConfigured()
		})

		It("should persist VRF route after node reboot", func() {
			Byf("Verifying VRF interface and route before reboot")
			verifyVrfRouteIsConfigured()

			restartNodeWithoutWaiting(node)

			By("Wait for policy re-reconciled after node reboot")
			policy.WaitForPolicyTransitionUpdate(TestPolicy)
			policy.WaitForAvailablePolicy(TestPolicy)

			Byf("Node %s was rebooted, verifying VRF route still exists", node)
			verifyVrfRouteIsConfigured()
		})

})
})