|
| 1 | +package network |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/Azure/azure-container-networking/iptables" |
| 8 | + "github.com/Azure/azure-container-networking/netio" |
| 9 | + "github.com/Azure/azure-container-networking/netlink" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// mockIPTablesClientWithRunCmd extends mock to track RunCmd calls. |
| 15 | +type mockIPTablesClientWithRunCmd struct { |
| 16 | + mockIPTablesClient |
| 17 | + runCmdCalls []string |
| 18 | +} |
| 19 | + |
| 20 | +func (c *mockIPTablesClientWithRunCmd) RunCmd(version, params string) error { |
| 21 | + c.runCmdCalls = append(c.runCmdCalls, version+" "+params) |
| 22 | + return nil |
| 23 | +} |
| 24 | + |
| 25 | +func TestHandleCommonOptions_AppliesRoutes(t *testing.T) { |
| 26 | + routeCalled := false |
| 27 | + nl := netlink.NewMockNetlink(false, "") |
| 28 | + nl.SetAddRouteValidationFn(func(_ *netlink.Route) error { |
| 29 | + routeCalled = true |
| 30 | + return nil |
| 31 | + }) |
| 32 | + |
| 33 | + nm := &networkManager{ |
| 34 | + netlink: nl, |
| 35 | + netio: netio.NewMockNetIO(false, 0), |
| 36 | + iptablesClient: &mockIPTablesClient{}, |
| 37 | + } |
| 38 | + |
| 39 | + nwInfo := &EndpointInfo{ |
| 40 | + Options: map[string]interface{}{ |
| 41 | + RoutesKey: []RouteInfo{ |
| 42 | + { |
| 43 | + Dst: net.IPNet{IP: net.ParseIP("10.1.0.0"), Mask: net.CIDRMask(16, 32)}, |
| 44 | + Gw: net.ParseIP("10.0.0.1"), |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + err := nm.handleCommonOptions("eth0", nwInfo) |
| 51 | + require.NoError(t, err) |
| 52 | + assert.True(t, routeCalled, "expected route to be added when RoutesKey is present in options") |
| 53 | +} |
| 54 | + |
| 55 | +func TestHandleCommonOptions_AppliesIPTables(t *testing.T) { |
| 56 | + iptc := &mockIPTablesClientWithRunCmd{} |
| 57 | + |
| 58 | + nm := &networkManager{ |
| 59 | + netlink: netlink.NewMockNetlink(false, ""), |
| 60 | + netio: netio.NewMockNetIO(false, 0), |
| 61 | + iptablesClient: iptc, |
| 62 | + } |
| 63 | + |
| 64 | + nwInfo := &EndpointInfo{ |
| 65 | + Options: map[string]interface{}{ |
| 66 | + IPTablesKey: []iptables.IPTableEntry{ |
| 67 | + {Version: iptables.V4, Params: "-A FORWARD -j ACCEPT"}, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + err := nm.handleCommonOptions("eth0", nwInfo) |
| 73 | + require.NoError(t, err) |
| 74 | + assert.NotEmpty(t, iptc.runCmdCalls, "expected iptables RunCmd to be called when IPTablesKey is present") |
| 75 | +} |
| 76 | + |
| 77 | +func TestHandleCommonOptions_NoOptionsNoError(t *testing.T) { |
| 78 | + nm := &networkManager{ |
| 79 | + netlink: netlink.NewMockNetlink(false, ""), |
| 80 | + netio: netio.NewMockNetIO(false, 0), |
| 81 | + iptablesClient: &mockIPTablesClient{}, |
| 82 | + } |
| 83 | + |
| 84 | + nwInfo := &EndpointInfo{ |
| 85 | + Options: map[string]interface{}{}, |
| 86 | + } |
| 87 | + |
| 88 | + err := nm.handleCommonOptions("eth0", nwInfo) |
| 89 | + require.NoError(t, err) |
| 90 | +} |
| 91 | + |
| 92 | +func TestHandleCommonOptions_NilOptionsNoError(t *testing.T) { |
| 93 | + nm := &networkManager{ |
| 94 | + netlink: netlink.NewMockNetlink(false, ""), |
| 95 | + netio: netio.NewMockNetIO(false, 0), |
| 96 | + iptablesClient: &mockIPTablesClient{}, |
| 97 | + } |
| 98 | + |
| 99 | + nwInfo := &EndpointInfo{ |
| 100 | + Options: nil, |
| 101 | + } |
| 102 | + |
| 103 | + err := nm.handleCommonOptions("eth0", nwInfo) |
| 104 | + require.NoError(t, err) |
| 105 | +} |
0 commit comments