github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/swarm/ipnet_slice_test.go (about) 1 package swarm 2 3 import ( 4 "fmt" 5 "net" 6 "strings" 7 "testing" 8 9 "github.com/spf13/pflag" 10 ) 11 12 // Helper function to set static slices 13 func getCIDR(ip net.IP, cidr *net.IPNet, err error) net.IPNet { 14 return *cidr 15 } 16 17 func equalCIDR(c1 net.IPNet, c2 net.IPNet) bool { 18 return c1.String() == c2.String() 19 } 20 21 func setUpIPNetFlagSet(ipsp *[]net.IPNet) *pflag.FlagSet { 22 f := pflag.NewFlagSet("test", pflag.ContinueOnError) 23 f.VarP(newIPNetSliceValue([]net.IPNet{}, ipsp), "cidrs", "", "Command separated list!") 24 return f 25 } 26 27 func TestIPNets(t *testing.T) { 28 var ips []net.IPNet 29 f := setUpIPNetFlagSet(&ips) 30 31 vals := []string{"192.168.1.1/24", "10.0.0.1/16", "fd00:0:0:0:0:0:0:2/64"} 32 arg := fmt.Sprintf("--cidrs=%s", strings.Join(vals, ",")) 33 err := f.Parse([]string{arg}) 34 if err != nil { 35 t.Fatal("expected no error; got", err) 36 } 37 for i, v := range ips { 38 if _, cidr, _ := net.ParseCIDR(vals[i]); cidr == nil { 39 t.Fatalf("invalid string being converted to CIDR: %s", vals[i]) 40 } else if !equalCIDR(*cidr, v) { 41 t.Fatalf("expected ips[%d] to be %s but got: %s from GetIPSlice", i, vals[i], v) 42 } 43 } 44 } 45 46 func TestIPNetCalledTwice(t *testing.T) { 47 var cidrs []net.IPNet 48 f := setUpIPNetFlagSet(&cidrs) 49 50 in := []string{"192.168.1.2/16,fd00::/64", "10.0.0.1/24"} 51 52 expected := []net.IPNet{ 53 getCIDR(net.ParseCIDR("192.168.1.2/16")), 54 getCIDR(net.ParseCIDR("fd00::/64")), 55 getCIDR(net.ParseCIDR("10.0.0.1/24")), 56 } 57 argfmt := "--cidrs=%s" 58 arg1 := fmt.Sprintf(argfmt, in[0]) 59 arg2 := fmt.Sprintf(argfmt, in[1]) 60 err := f.Parse([]string{arg1, arg2}) 61 if err != nil { 62 t.Fatal("expected no error; got", err) 63 } 64 for i, v := range cidrs { 65 if !equalCIDR(expected[i], v) { 66 t.Fatalf("expected cidrs[%d] to be %s but got: %s", i, expected[i], v) 67 } 68 } 69 } 70 71 func TestIPNetBadQuoting(t *testing.T) { 72 73 tests := []struct { 74 Want []net.IPNet 75 FlagArg []string 76 }{ 77 { 78 Want: []net.IPNet{ 79 getCIDR(net.ParseCIDR("a4ab:61d:f03e:5d7d:fad7:d4c2:a1a5:568/128")), 80 getCIDR(net.ParseCIDR("203.107.49.208/32")), 81 getCIDR(net.ParseCIDR("14.57.204.90/32")), 82 }, 83 FlagArg: []string{ 84 "a4ab:61d:f03e:5d7d:fad7:d4c2:a1a5:568/128", 85 "203.107.49.208/32", 86 "14.57.204.90/32", 87 }, 88 }, 89 { 90 Want: []net.IPNet{ 91 getCIDR(net.ParseCIDR("204.228.73.195/32")), 92 getCIDR(net.ParseCIDR("86.141.15.94/32")), 93 }, 94 FlagArg: []string{ 95 "204.228.73.195/32", 96 "86.141.15.94/32", 97 }, 98 }, 99 { 100 Want: []net.IPNet{ 101 getCIDR(net.ParseCIDR("c70c:db36:3001:890f:c6ea:3f9b:7a39:cc3f/128")), 102 getCIDR(net.ParseCIDR("4d17:1d6e:e699:bd7a:88c5:5e7e:ac6a:4472/128")), 103 }, 104 FlagArg: []string{ 105 "c70c:db36:3001:890f:c6ea:3f9b:7a39:cc3f/128", 106 "4d17:1d6e:e699:bd7a:88c5:5e7e:ac6a:4472/128", 107 }, 108 }, 109 { 110 Want: []net.IPNet{ 111 getCIDR(net.ParseCIDR("5170:f971:cfac:7be3:512a:af37:952c:bc33/128")), 112 getCIDR(net.ParseCIDR("93.21.145.140/32")), 113 getCIDR(net.ParseCIDR("2cac:61d3:c5ff:6caf:73e0:1b1a:c336:c1ca/128")), 114 }, 115 FlagArg: []string{ 116 " 5170:f971:cfac:7be3:512a:af37:952c:bc33/128 , 93.21.145.140/32 ", 117 "2cac:61d3:c5ff:6caf:73e0:1b1a:c336:c1ca/128", 118 }, 119 }, 120 { 121 Want: []net.IPNet{ 122 getCIDR(net.ParseCIDR("2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128")), 123 getCIDR(net.ParseCIDR("2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128")), 124 getCIDR(net.ParseCIDR("2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128")), 125 getCIDR(net.ParseCIDR("2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128")), 126 }, 127 FlagArg: []string{ 128 `"2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128, 2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128,2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128 "`, 129 " 2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128"}, 130 }, 131 } 132 133 for i, test := range tests { 134 135 var cidrs []net.IPNet 136 f := setUpIPNetFlagSet(&cidrs) 137 138 if err := f.Parse([]string{fmt.Sprintf("--cidrs=%s", strings.Join(test.FlagArg, ","))}); err != nil { 139 t.Fatalf("flag parsing failed with error: %s\nparsing:\t%#v\nwant:\t\t%s", 140 err, test.FlagArg, test.Want[i]) 141 } 142 143 for j, b := range cidrs { 144 if !equalCIDR(b, test.Want[j]) { 145 t.Fatalf("bad value parsed for test %d on net.IP %d:\nwant:\t%s\ngot:\t%s", i, j, test.Want[j], b) 146 } 147 } 148 } 149 }