k8s.io/kubernetes@v1.29.3/test/e2e/framework/providers/gce/firewall_test.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package gce
    18  
    19  import "testing"
    20  
    21  func TestIsPortsSubset(t *testing.T) {
    22  	tc := map[string]struct {
    23  		required  []string
    24  		coverage  []string
    25  		expectErr bool
    26  	}{
    27  		"Single port coverage": {
    28  			required: []string{"tcp/50"},
    29  			coverage: []string{"tcp/50", "tcp/60", "tcp/70"},
    30  		},
    31  		"Port range coverage": {
    32  			required: []string{"tcp/50"},
    33  			coverage: []string{"tcp/20-30", "tcp/45-60"},
    34  		},
    35  		"Multiple Port range coverage": {
    36  			required: []string{"tcp/50", "tcp/29", "tcp/46"},
    37  			coverage: []string{"tcp/20-30", "tcp/45-60"},
    38  		},
    39  		"Not covered": {
    40  			required:  []string{"tcp/50"},
    41  			coverage:  []string{"udp/50", "tcp/49", "tcp/51-60"},
    42  			expectErr: true,
    43  		},
    44  	}
    45  
    46  	for name, c := range tc {
    47  		t.Run(name, func(t *testing.T) {
    48  			gotErr := isPortsSubset(c.required, c.coverage)
    49  			if c.expectErr != (gotErr != nil) {
    50  				t.Errorf("isPortsSubset(%v, %v) = %v, wanted err? %v", c.required, c.coverage, gotErr, c.expectErr)
    51  			}
    52  		})
    53  	}
    54  }