@@ -4,12 +4,30 @@ 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+ func (c mockIPTablesClient ) InsertIptableRule (_ , _ , _ , _ , _ string ) error {
16+ return nil
17+ }
18+
19+ func (c mockIPTablesClient ) AppendIptableRule (_ , _ , _ , _ , _ string ) error {
20+ return nil
21+ }
22+
23+ func (c mockIPTablesClient ) DeleteIptableRule (_ , _ , _ , _ , _ string ) error {
24+ return nil
25+ }
26+
27+ func (c mockIPTablesClient ) CreateChain (_ , _ , _ string ) error {
28+ return nil
29+ }
30+
1331func TestMain (m * testing.M ) {
1432 exitCode := m .Run ()
1533
@@ -18,16 +36,22 @@ func TestMain(m *testing.M) {
1836 os .Exit (exitCode )
1937}
2038
21- func TestAllowInboundFromHostToNC (t * testing.T ) {
22- nl := netlink .NewNetlink ()
23- iptc := iptables .NewClient ()
24- client := & Client {
39+ func GetTestClient (nl netlink.NetlinkInterface , iptc ipTablesClient , nio netio.NetIOInterface ) * Client {
40+ return & Client {
2541 SnatBridgeIP : "169.254.0.1/16" ,
2642 localIP : "169.254.0.4/16" ,
2743 containerSnatVethName : anyInterface ,
2844 netlink : nl ,
2945 ipTablesClient : iptc ,
46+ netioClient : nio ,
3047 }
48+ }
49+
50+ func TestAllowInboundFromHostToNC (t * testing.T ) {
51+ nl := netlink .NewMockNetlink (false , "" )
52+ iptc := & mockIPTablesClient {}
53+ nio := netio .NewMockNetIO (false , 0 )
54+ client := GetTestClient (nl , iptc , nio )
3155
3256 if err := nl .AddLink (& netlink.DummyLink {
3357 LinkInfo : netlink.LinkInfo {
@@ -65,18 +89,18 @@ func TestAllowInboundFromHostToNC(t *testing.T) {
6589 if err := nl .DeleteLink (SnatBridgeName ); err != nil {
6690 t .Errorf ("Error removing snat bridge: %v" , err )
6791 }
92+
93+ client .netioClient = netio .NewMockNetIO (true , 1 )
94+ if err := client .AllowInboundFromHostToNC (); err == nil {
95+ t .Errorf ("Expected error when interface not found in allow host to nc but got nil" )
96+ }
6897}
6998
7099func 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- }
100+ nl := netlink .NewMockNetlink (false , "" )
101+ iptc := & mockIPTablesClient {}
102+ nio := netio .NewMockNetIO (false , 0 )
103+ client := GetTestClient (nl , iptc , nio )
80104
81105 if err := nl .AddLink (& netlink.DummyLink {
82106 LinkInfo : netlink.LinkInfo {
@@ -114,4 +138,9 @@ func TestAllowInboundFromNCToHost(t *testing.T) {
114138 if err := nl .DeleteLink (SnatBridgeName ); err != nil {
115139 t .Errorf ("Error removing snat bridge: %v" , err )
116140 }
141+
142+ client .netioClient = netio .NewMockNetIO (true , 1 )
143+ if err := client .AllowInboundFromNCToHost (); err == nil {
144+ t .Errorf ("Expected error when interface not found in allow nc to host but got nil" )
145+ }
117146}
0 commit comments