Skip to content

Commit 7676d8a

Browse files
authored
cnf-network: Added ip forwarding tests (#1333)
1 parent 5d7fc53 commit 7676d8a

3 files changed

Lines changed: 992 additions & 0 deletions

File tree

tests/cnf/core/network/internal/netnmstate/netnmstate.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,100 @@ func CheckThatWorkersDeployedWithBondVfs(
340340
return bondName, bondSlaves, nil
341341
}
342342

343+
// WithInterfaceIPv4Forwarding returns a function that sets IPv4 forwarding on the specified interface.
344+
func WithInterfaceIPv4Forwarding(
345+
interfaceName string, forwarding bool) func(*nmstate.PolicyBuilder) (*nmstate.PolicyBuilder, error) {
346+
klog.V(90).Infof("Setting IPv4 forwarding=%v on interface %s", forwarding, interfaceName)
347+
348+
return withInterfaceForwardingMutator(func(iface *nmstate.NetworkInterface) {
349+
iface.Ipv4.Forwarding = &forwarding
350+
},
351+
interfaceName,
352+
)
353+
}
354+
355+
// WithInterfaceIPv6Forwarding returns a function that sets IPv6 forwarding on the specified interface.
356+
func WithInterfaceIPv6Forwarding(
357+
interfaceName string, forwarding bool) func(*nmstate.PolicyBuilder) (*nmstate.PolicyBuilder, error) {
358+
klog.V(90).Infof("Setting IPv6 forwarding=%v on interface %s", forwarding, interfaceName)
359+
360+
return withInterfaceForwardingMutator(func(iface *nmstate.NetworkInterface) {
361+
iface.Ipv6.Forwarding = &forwarding
362+
},
363+
interfaceName,
364+
)
365+
}
366+
367+
// withInterfaceForwardingMutator returns a function that mutates IP forwarding on a specific interface.
368+
func withInterfaceForwardingMutator(
369+
mutateFunc func(*nmstate.NetworkInterface),
370+
interfaceName string) func(*nmstate.PolicyBuilder) (*nmstate.PolicyBuilder, error) {
371+
return func(builder *nmstate.PolicyBuilder) (*nmstate.PolicyBuilder, error) {
372+
klog.V(90).Infof("Mutating the interface %s forwarding configuration", interfaceName)
373+
374+
if interfaceName == "" {
375+
klog.V(90).Infof("The interfaceName cannot be an empty string")
376+
377+
return builder, fmt.Errorf("the interfaceName is an empty string")
378+
}
379+
380+
var currentState nmstate.DesiredState
381+
382+
err := yaml.Unmarshal(builder.Definition.Spec.DesiredState.Raw, &currentState)
383+
if err != nil {
384+
klog.V(90).Infof("Failed to unmarshal DesiredState")
385+
386+
return builder, fmt.Errorf("failed to unmarshal DesiredState: %w", err)
387+
}
388+
389+
var foundInterface bool
390+
391+
for i, networkInterface := range currentState.Interfaces {
392+
if networkInterface.Name == interfaceName {
393+
mutateFunc(&currentState.Interfaces[i])
394+
395+
foundInterface = true
396+
397+
break
398+
}
399+
}
400+
401+
if !foundInterface {
402+
klog.V(90).Infof("Failed to find the given interface")
403+
404+
return builder, fmt.Errorf("failed to find interface %s", interfaceName)
405+
}
406+
407+
desiredStateYaml, err := yaml.Marshal(currentState)
408+
if err != nil {
409+
klog.V(90).Infof("Failed to marshal DesiredState")
410+
411+
return builder, fmt.Errorf("failed to marshal a new Desired state: %w", err)
412+
}
413+
414+
builder.Definition.Spec.DesiredState = nmstateShared.NewState(string(desiredStateYaml))
415+
416+
return builder, nil
417+
}
418+
}
419+
420+
// UpdatePolicyAndWaitUntilItsDegraded updates a NodeNetworkConfigurationPolicy and waits until
421+
// it reaches the Degraded condition.
422+
func UpdatePolicyAndWaitUntilItsDegraded(
423+
timeout time.Duration, nmstatePolicy *nmstate.PolicyBuilder) (*nmstate.PolicyBuilder, error) {
424+
klog.V(90).Infof("Updating an NMState policy and waiting for it to become Degraded.")
425+
426+
nmstatePolicy, err := nmstatePolicy.Update(true)
427+
if err != nil {
428+
return nmstatePolicy, err
429+
}
430+
431+
err = nmstatePolicy.WaitUntilCondition(
432+
nmstateShared.NodeNetworkConfigurationPolicyConditionDegraded, timeout)
433+
434+
return nmstatePolicy, err
435+
}
436+
343437
// withBondOptionMutator returns a function that mutates a specific option for a bond interface.
344438
func withBondOptionMutator(
345439
mutateFunc func(*nmstate.OptionsLinkAggregation),

tests/cnf/core/network/metallb/internal/tsparams/consts.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const (
4848
MetallbServiceName2 = "service-2"
4949
// BGPAdvAndAddressPoolName BGPAdvAndAdressPoolName is the name of the BgpAdvertisement and IPAddressPool.
5050
BGPAdvAndAddressPoolName = "bgp-test"
51+
// BGPAdvAndAddressPoolName2 is the name of the second BgpAdvertisement and IPAddressPool.
52+
BGPAdvAndAddressPoolName2 = "bgp-test-2"
5153
// LabelValue1 is the value name for the label1.
5254
LabelValue1 = "nginx1"
5355
// LabelValue2 is the value name for the label2.
@@ -75,6 +77,8 @@ bfd
7577
`
7678
// FRRDefaultConfigMapName represents default FRR configMap name.
7779
FRRDefaultConfigMapName = "frr-config"
80+
// FRRDefaultConfigMapName2 represents the second default FRR configMap name.
81+
FRRDefaultConfigMapName2 = "frr-config-2"
7882
// LocalBGPASN represents local BGP AS number.
7983
LocalBGPASN = 64500
8084
// RemoteBGPASN represents remote BGP AS number.

0 commit comments

Comments
 (0)