@@ -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
1111var 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+
1329func 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
7097func 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}
0 commit comments