@@ -131,13 +131,30 @@ func TestFirewallPoolSyncSrcRanges(t *testing.T) {
131131func TestFirewallPoolSyncPorts (t * testing.T ) {
132132 fwp := NewFakeFirewallsProvider (false , false )
133133 fwClient := firewallclient .NewSimpleClientset ()
134- fp := NewFirewallPool (fwp , defaultNamer , srcRanges , portRanges (), klog .TODO ())
135- fcrp := NewFirewallCRPool (fwClient , fwp , defaultNamer , srcRanges , portRanges (), true , klog .TODO ())
136134 nodes := []string {"node-a" , "node-b" , "node-c" }
135+ emptyPortRanges := make ([]string , 0 )
136+
137+ // Verify empty ports' list
138+ fp := NewFirewallPool (fwp , defaultNamer , srcRanges , emptyPortRanges , klog .TODO ())
139+ fcrp := NewFirewallCRPool (fwClient , fwp , defaultNamer , srcRanges , emptyPortRanges , true , klog .TODO ())
137140
138141 if err := fp .Sync (nodes , nil , nil , true ); err != nil {
139142 t .Fatal (err )
140143 }
144+ verifyFirewallRule (fwp , ruleName , nodes , srcRanges , emptyPortRanges , t )
145+
146+ if err := fcrp .Sync (nodes , nil , nil , true ); err != nil {
147+ t .Fatal (err )
148+ }
149+ verifyFirewallCR (fwClient , ruleName , srcRanges , emptyPortRanges , true , t )
150+
151+ // Verify a preset ports' list
152+ fp = NewFirewallPool (fwp , defaultNamer , srcRanges , portRanges (), klog .TODO ())
153+ fcrp = NewFirewallCRPool (fwClient , fwp , defaultNamer , srcRanges , portRanges (), true , klog .TODO ())
154+
155+ if err := fp .Sync (nodes , nil , nil , true ); err != nil {
156+ t .Errorf ("unexpected err when syncing firewall, err: %v" , err )
157+ }
141158 verifyFirewallRule (fwp , ruleName , nodes , srcRanges , portRanges (), t )
142159
143160 if err := fcrp .Sync (nodes , nil , nil , true ); err != nil {
@@ -399,14 +416,20 @@ func verifyFirewallCR(firewallclient *firewallclient.Clientset, ruleName string,
399416 ports := sets .NewString (expectedPorts ... )
400417 srcranges := sets .NewString (sourceRanges ... )
401418
419+ // Empty ports' list would mean that all protocols are permitted
420+ // (not only TCP)
421+ if len (actualFW .Spec .Ports ) == 0 {
422+ t .Errorf ("Empty list of allowed protocols is not permited" )
423+ }
424+
402425 actualPorts := sets .NewString ()
403426 for _ , protocolports := range actualFW .Spec .Ports {
404427 if protocolports .Protocol != "TCP" {
405428 t .Errorf ("Protocol isn't TCP" )
406429 }
407430 if protocolports .EndPort != nil {
408431 actualPorts .Insert (fmt .Sprintf ("%d-%d" , * protocolports .StartPort , * protocolports .EndPort ))
409- } else {
432+ } else if protocolports . StartPort != nil {
410433 actualPorts .Insert (fmt .Sprintf ("%d" , * protocolports .StartPort ))
411434 }
412435
0 commit comments