Skip to content

Commit bfd9e79

Browse files
committed
modify snat client to accept interfaces for unit testing
1 parent 964bfd6 commit bfd9e79

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

network/ovs_endpoint_snatroute_linux.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (client *OVSEndpointClient) NewSnatClient(snatBridgeIP, localIP string, epI
3333
client.netlink,
3434
client.plClient,
3535
client.iptablesClient,
36+
client.netioshim,
3637
)
3738
}
3839
}

network/snat/snat_linux.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/Azure/azure-container-networking/cni/log"
1212
"github.com/Azure/azure-container-networking/ebtables"
1313
"github.com/Azure/azure-container-networking/iptables"
14+
"github.com/Azure/azure-container-networking/netio"
1415
"github.com/Azure/azure-container-networking/netlink"
1516
"github.com/Azure/azure-container-networking/network/networkutils"
1617
"github.com/Azure/azure-container-networking/platform"
@@ -55,6 +56,7 @@ type Client struct {
5556
netlink netlink.NetlinkInterface
5657
plClient platform.ExecClient
5758
ipTablesClient ipTablesClient
59+
netioClient netio.NetIOInterface
5860
}
5961

6062
func NewSnatClient(hostIfName string,
@@ -67,6 +69,7 @@ func NewSnatClient(hostIfName string,
6769
nl netlink.NetlinkInterface,
6870
plClient platform.ExecClient,
6971
iptc ipTablesClient,
72+
nio netio.NetIOInterface,
7073
) Client {
7174
snatClient := Client{
7275
hostSnatVethName: hostIfName,
@@ -78,6 +81,7 @@ func NewSnatClient(hostIfName string,
7881
netlink: nl,
7982
plClient: plClient,
8083
ipTablesClient: iptc,
84+
netioClient: nio,
8185
}
8286

8387
snatClient.SkipAddressesFromBlock = append(snatClient.SkipAddressesFromBlock, skipAddressesFromBlock...)
@@ -223,7 +227,7 @@ func (client *Client) AllowInboundFromHostToNC() error {
223227
return newErrorSnatClient(err.Error())
224228
}
225229

226-
snatContainerVeth, err := net.InterfaceByName(client.containerSnatVethName)
230+
snatContainerVeth, err := client.netioClient.GetNetworkInterfaceByName(client.containerSnatVethName)
227231
if err != nil {
228232
logger.Info("Could not find interface", zap.String("containerSnatVethName", client.containerSnatVethName))
229233
return errors.Wrap(newErrorSnatClient(err.Error()), "could not find container snat veth name for allow host to nc")
@@ -323,7 +327,7 @@ func (client *Client) AllowInboundFromNCToHost() error {
323327
return err
324328
}
325329

326-
snatContainerVeth, err := net.InterfaceByName(client.containerSnatVethName)
330+
snatContainerVeth, err := client.netioClient.GetNetworkInterfaceByName(client.containerSnatVethName)
327331
if err != nil {
328332
logger.Info("Could not find interface", zap.String("containerSnatVethName", client.containerSnatVethName))
329333
return errors.Wrap(newErrorSnatClient(err.Error()), "could not find container snat veth name for allow nc to host")
@@ -424,7 +428,7 @@ func (client *Client) DropArpForSnatBridgeApipaRange(snatBridgeIP, azSnatVethIfN
424428

425429
// This function creates linux bridge which will be used for outbound connectivity by NCs
426430
func (client *Client) createSnatBridge(snatBridgeIP, hostPrimaryMac string) error {
427-
_, err := net.InterfaceByName(SnatBridgeName)
431+
_, err := client.netioClient.GetNetworkInterfaceByName(SnatBridgeName)
428432
if err == nil {
429433
logger.Info("Snat Bridge already exists")
430434
} else {

network/snat/snat_linux_test.go

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,28 @@ import (
44
"os"
55
"testing"
66

7-
"github.com/Azure/azure-container-networking/iptables"
7+
"github.com/Azure/azure-container-networking/netio"
88
"github.com/Azure/azure-container-networking/netlink"
99
)
1010

1111
var anyInterface = "dummy"
1212

13+
type mockIPTablesClient struct {
14+
}
15+
16+
func (c mockIPTablesClient) InsertIptableRule(version, tableName, chainName, match, target string) error {
17+
return nil
18+
}
19+
func (c mockIPTablesClient) AppendIptableRule(version, tableName, chainName, match, target string) error {
20+
return nil
21+
}
22+
func (c mockIPTablesClient) DeleteIptableRule(version, tableName, chainName, match, target string) error {
23+
return nil
24+
}
25+
func (c mockIPTablesClient) CreateChain(version, tableName, chainName string) error {
26+
return nil
27+
}
28+
1329
func TestMain(m *testing.M) {
1430
exitCode := m.Run()
1531

@@ -18,16 +34,22 @@ func TestMain(m *testing.M) {
1834
os.Exit(exitCode)
1935
}
2036

21-
func TestAllowInboundFromHostToNC(t *testing.T) {
22-
nl := netlink.NewNetlink()
23-
iptc := iptables.NewClient()
24-
client := &Client{
37+
func GetTestClient(nl netlink.NetlinkInterface, iptc ipTablesClient, nio netio.NetIOInterface) *Client {
38+
return &Client{
2539
SnatBridgeIP: "169.254.0.1/16",
2640
localIP: "169.254.0.4/16",
2741
containerSnatVethName: anyInterface,
2842
netlink: nl,
2943
ipTablesClient: iptc,
44+
netioClient: nio,
3045
}
46+
}
47+
48+
func TestAllowInboundFromHostToNC(t *testing.T) {
49+
nl := netlink.NewMockNetlink(false, "")
50+
iptc := &mockIPTablesClient{}
51+
nio := netio.NewMockNetIO(false, 0)
52+
client := GetTestClient(nl, iptc, nio)
3153

3254
if err := nl.AddLink(&netlink.DummyLink{
3355
LinkInfo: netlink.LinkInfo{
@@ -65,18 +87,18 @@ func TestAllowInboundFromHostToNC(t *testing.T) {
6587
if err := nl.DeleteLink(SnatBridgeName); err != nil {
6688
t.Errorf("Error removing snat bridge: %v", err)
6789
}
90+
91+
client.netioClient = netio.NewMockNetIO(true, 1)
92+
if err := client.AllowInboundFromHostToNC(); err == nil {
93+
t.Errorf("Expected error when interface not found in allow host to nc but got nil")
94+
}
6895
}
6996

7097
func TestAllowInboundFromNCToHost(t *testing.T) {
71-
nl := netlink.NewNetlink()
72-
iptc := iptables.NewClient()
73-
client := &Client{
74-
SnatBridgeIP: "169.254.0.1/16",
75-
localIP: "169.254.0.4/16",
76-
containerSnatVethName: anyInterface,
77-
netlink: nl,
78-
ipTablesClient: iptc,
79-
}
98+
nl := netlink.NewMockNetlink(false, "")
99+
iptc := &mockIPTablesClient{}
100+
nio := netio.NewMockNetIO(false, 0)
101+
client := GetTestClient(nl, iptc, nio)
80102

81103
if err := nl.AddLink(&netlink.DummyLink{
82104
LinkInfo: netlink.LinkInfo{
@@ -114,4 +136,9 @@ func TestAllowInboundFromNCToHost(t *testing.T) {
114136
if err := nl.DeleteLink(SnatBridgeName); err != nil {
115137
t.Errorf("Error removing snat bridge: %v", err)
116138
}
139+
140+
client.netioClient = netio.NewMockNetIO(true, 1)
141+
if err := client.AllowInboundFromNCToHost(); err == nil {
142+
t.Errorf("Expected error when interface not found in allow nc to host but got nil")
143+
}
117144
}

network/transparent_vlan_endpoint_snatroute_linux.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func (client *TransparentVlanEndpointClient) NewSnatClient(snatBridgeIP, localIP
2121
client.netlink,
2222
client.plClient,
2323
client.iptablesClient,
24+
client.netioshim,
2425
)
2526
}
2627
}

0 commit comments

Comments
 (0)