github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/net/util_test.go (about) 1 /* 2 Copyright 2016 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 "net" 21 "net/url" 22 "os" 23 "syscall" 24 "testing" 25 26 netutils "k8s.io/utils/net" 27 ) 28 29 func getIPNet(cidr string) *net.IPNet { 30 _, ipnet, _ := netutils.ParseCIDRSloppy(cidr) 31 return ipnet 32 } 33 34 func TestIPNetEqual(t *testing.T) { 35 testCases := []struct { 36 ipnet1 *net.IPNet 37 ipnet2 *net.IPNet 38 expect bool 39 }{ 40 // null case 41 { 42 getIPNet("10.0.0.1/24"), 43 getIPNet(""), 44 false, 45 }, 46 { 47 getIPNet("10.0.0.0/24"), 48 getIPNet("10.0.0.0/24"), 49 true, 50 }, 51 { 52 getIPNet("10.0.0.0/24"), 53 getIPNet("10.0.0.1/24"), 54 true, 55 }, 56 { 57 getIPNet("10.0.0.0/25"), 58 getIPNet("10.0.0.0/24"), 59 false, 60 }, 61 { 62 getIPNet("10.0.1.0/24"), 63 getIPNet("10.0.0.0/24"), 64 false, 65 }, 66 } 67 68 for _, tc := range testCases { 69 if tc.expect != IPNetEqual(tc.ipnet1, tc.ipnet2) { 70 t.Errorf("Expect equality of %s and %s be to %v", tc.ipnet1.String(), tc.ipnet2.String(), tc.expect) 71 } 72 } 73 } 74 75 func TestIsConnectionRefused(t *testing.T) { 76 testCases := []struct { 77 err error 78 expect bool 79 }{ 80 { 81 &url.Error{Err: &net.OpError{Err: syscall.ECONNRESET}}, 82 false, 83 }, 84 { 85 &url.Error{Err: &net.OpError{Err: syscall.ECONNREFUSED}}, 86 true, 87 }, 88 {&url.Error{Err: &net.OpError{Err: &os.SyscallError{Err: syscall.ECONNREFUSED}}}, 89 true, 90 }, 91 } 92 93 for _, tc := range testCases { 94 if result := IsConnectionRefused(tc.err); result != tc.expect { 95 t.Errorf("Expect to be %v, but actual is %v", tc.expect, result) 96 } 97 } 98 }