github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/net/port_range_test.go (about) 1 /* 2 Copyright 2015 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 net 18 19 import ( 20 "testing" 21 22 flag "github.com/spf13/pflag" 23 ) 24 25 func TestPortRange(t *testing.T) { 26 testCases := []struct { 27 input string 28 success bool 29 expected string 30 included int 31 excluded int 32 }{ 33 {"100-200", true, "100-200", 200, 201}, 34 {" 100-200 ", true, "100-200", 200, 201}, 35 {"0-0", true, "0-0", 0, 1}, 36 {"", true, "", -1, 0}, 37 {"100", true, "100-100", 100, 101}, 38 {"100 - 200", false, "", -1, -1}, 39 {"-100", false, "", -1, -1}, 40 {"100-", false, "", -1, -1}, 41 {"200-100", false, "", -1, -1}, 42 {"60000-70000", false, "", -1, -1}, 43 {"70000-80000", false, "", -1, -1}, 44 {"70000+80000", false, "", -1, -1}, 45 {"1+0", true, "1-1", 1, 2}, 46 {"0+0", true, "0-0", 0, 1}, 47 {"1+-1", false, "", -1, -1}, 48 {"1-+1", false, "", -1, -1}, 49 {"100+200", true, "100-300", 300, 301}, 50 {"1+65535", false, "", -1, -1}, 51 {"0+65535", true, "0-65535", 65535, 65536}, 52 } 53 54 for i := range testCases { 55 tc := &testCases[i] 56 pr := &PortRange{} 57 var f flag.Value = pr 58 err := f.Set(tc.input) 59 if err != nil && tc.success == true { 60 t.Errorf("expected success, got %q", err) 61 continue 62 } else if err == nil && tc.success == false { 63 t.Errorf("expected failure %#v", testCases[i]) 64 continue 65 } else if tc.success { 66 if f.String() != tc.expected { 67 t.Errorf("expected %q, got %q", tc.expected, f.String()) 68 } 69 if tc.included >= 0 && !pr.Contains(tc.included) { 70 t.Errorf("expected %q to include %d", f.String(), tc.included) 71 } 72 if tc.excluded >= 0 && pr.Contains(tc.excluded) { 73 t.Errorf("expected %q to exclude %d", f.String(), tc.excluded) 74 } 75 } 76 } 77 }