44package network
55
66import (
7+ "encoding/json"
78 "fmt"
89 "net"
910 "testing"
@@ -14,6 +15,7 @@ import (
1415 "github.com/Azure/azure-container-networking/network/hnswrapper"
1516 "github.com/Azure/azure-container-networking/network/policy"
1617 "github.com/Azure/azure-container-networking/telemetry"
18+ hnsv2 "github.com/Microsoft/hcsshim/hcn"
1719 "github.com/containernetworking/cni/pkg/skel"
1820 "github.com/stretchr/testify/assert"
1921 "github.com/stretchr/testify/require"
@@ -219,8 +221,10 @@ func TestSetPoliciesFromNwCfg(t *testing.T) {
219221 name string
220222 nwCfg cni.NetworkConfig
221223 isIPv6Enabled bool
224+ expected []hnsv2.PortMappingPolicySetting
222225 }{
223226 {
227+ // ipv6 disabled, ipv4 host ip --> ipv4 host ip policy only
224228 name : "Runtime network polices" ,
225229 nwCfg : cni.NetworkConfig {
226230 RuntimeConfig : cni.RuntimeConfig {
@@ -235,9 +239,19 @@ func TestSetPoliciesFromNwCfg(t *testing.T) {
235239 },
236240 },
237241 isIPv6Enabled : false ,
242+ expected : []hnsv2.PortMappingPolicySetting {
243+ {
244+ ExternalPort : uint16 (8000 ),
245+ InternalPort : uint16 (80 ),
246+ VIP : "192.168.0.4" ,
247+ Protocol : policy .ProtocolTcp ,
248+ Flags : hnsv2 .NatFlagsLocalRoutedVip ,
249+ },
250+ },
238251 },
239252 {
240- name : "Runtime hostPort mapping polices" ,
253+ // ipv6 disabled, no host ip --> ipv4 policy only
254+ name : "Runtime hostPort mapping polices without hostIP" ,
241255 nwCfg : cni.NetworkConfig {
242256 RuntimeConfig : cni.RuntimeConfig {
243257 PortMappings : []cni.PortMapping {
@@ -250,8 +264,17 @@ func TestSetPoliciesFromNwCfg(t *testing.T) {
250264 },
251265 },
252266 isIPv6Enabled : false ,
267+ expected : []hnsv2.PortMappingPolicySetting {
268+ {
269+ ExternalPort : uint16 (44000 ),
270+ InternalPort : uint16 (80 ),
271+ Protocol : policy .ProtocolTcp ,
272+ Flags : hnsv2 .NatFlagsLocalRoutedVip ,
273+ },
274+ },
253275 },
254276 {
277+ // ipv6 enabled, ipv6 host ip --> ipv6 host ip policy only
255278 name : "Runtime hostPort mapping polices with ipv6 hostIP" ,
256279 nwCfg : cni.NetworkConfig {
257280 RuntimeConfig : cni.RuntimeConfig {
@@ -266,6 +289,99 @@ func TestSetPoliciesFromNwCfg(t *testing.T) {
266289 },
267290 },
268291 isIPv6Enabled : true ,
292+ expected : []hnsv2.PortMappingPolicySetting {
293+ {
294+ ExternalPort : uint16 (44000 ),
295+ InternalPort : uint16 (80 ),
296+ VIP : "2001:2002:2003::1" ,
297+ Protocol : policy .ProtocolTcp ,
298+ Flags : hnsv2 .NatFlagsIPv6 ,
299+ },
300+ },
301+ },
302+ {
303+ // ipv6 enabled, ipv4 host ip --> ipv4 host ip policy only
304+ name : "Runtime hostPort mapping polices with ipv4 hostIP on ipv6 enabled cluster" ,
305+ nwCfg : cni.NetworkConfig {
306+ RuntimeConfig : cni.RuntimeConfig {
307+ PortMappings : []cni.PortMapping {
308+ {
309+ Protocol : "tcp" ,
310+ HostPort : 44000 ,
311+ ContainerPort : 80 ,
312+ HostIp : "192.168.0.4" ,
313+ },
314+ },
315+ },
316+ },
317+ isIPv6Enabled : true ,
318+ expected : []hnsv2.PortMappingPolicySetting {
319+ {
320+ ExternalPort : uint16 (44000 ),
321+ InternalPort : uint16 (80 ),
322+ VIP : "192.168.0.4" ,
323+ Protocol : policy .ProtocolTcp ,
324+ Flags : hnsv2 .NatFlagsLocalRoutedVip ,
325+ },
326+ },
327+ },
328+ {
329+ // ipv6 enabled, no host ip --> ipv4 and ipv6 policies
330+ name : "Runtime hostPort mapping polices with ipv6 without hostIP" ,
331+ nwCfg : cni.NetworkConfig {
332+ RuntimeConfig : cni.RuntimeConfig {
333+ PortMappings : []cni.PortMapping {
334+ {
335+ Protocol : "tcp" ,
336+ HostPort : 44000 ,
337+ ContainerPort : 80 ,
338+ },
339+ },
340+ },
341+ },
342+ isIPv6Enabled : true ,
343+ expected : []hnsv2.PortMappingPolicySetting {
344+ {
345+ ExternalPort : uint16 (44000 ),
346+ InternalPort : uint16 (80 ),
347+ VIP : "" ,
348+ Protocol : policy .ProtocolTcp ,
349+ Flags : hnsv2 .NatFlagsLocalRoutedVip ,
350+ },
351+ {
352+ ExternalPort : uint16 (44000 ),
353+ InternalPort : uint16 (80 ),
354+ VIP : "" ,
355+ Protocol : policy .ProtocolTcp ,
356+ Flags : hnsv2 .NatFlagsIPv6 ,
357+ },
358+ },
359+ },
360+ {
361+ // ipv6 enabled, ipv6 localhost ip --> ipv6 host ip policy only
362+ name : "Runtime hostPort mapping polices with ipv6 localhost hostIP on ipv6 enabled cluster" ,
363+ nwCfg : cni.NetworkConfig {
364+ RuntimeConfig : cni.RuntimeConfig {
365+ PortMappings : []cni.PortMapping {
366+ {
367+ Protocol : "tcp" ,
368+ HostPort : 44000 ,
369+ ContainerPort : 80 ,
370+ HostIp : "::1" ,
371+ },
372+ },
373+ },
374+ },
375+ isIPv6Enabled : true ,
376+ expected : []hnsv2.PortMappingPolicySetting {
377+ {
378+ ExternalPort : uint16 (44000 ),
379+ InternalPort : uint16 (80 ),
380+ VIP : "::1" ,
381+ Protocol : policy .ProtocolTcp ,
382+ Flags : hnsv2 .NatFlagsIPv6 ,
383+ },
384+ },
269385 },
270386 }
271387 for _ , tt := range tests {
@@ -276,6 +392,18 @@ func TestSetPoliciesFromNwCfg(t *testing.T) {
276392 require .Condition (t , assert .Comparison (func () bool {
277393 return len (policies ) > 0 && policies [0 ].Type == policy .EndpointPolicy
278394 }))
395+ require .Equal (t , len (tt .expected ), len (policies ), "expected number of policies not equal to actual" )
396+ for index , policy := range policies {
397+ var hnsv2Policy hnsv2.EndpointPolicy
398+ err = json .Unmarshal (policy .Data , & hnsv2Policy )
399+ require .NoError (t , err , "failed to unmarshal hnsv2 policy" )
400+
401+ var rawPolicy hnsv2.PortMappingPolicySetting
402+ err = json .Unmarshal (hnsv2Policy .Settings , & rawPolicy )
403+ require .NoError (t , err , "failed to unmarshal hnsv2 port mapping policy" )
404+
405+ require .Equal (t , tt .expected [index ], rawPolicy , "policies are not expected" )
406+ }
279407 })
280408 }
281409}
0 commit comments