github.com/openshift/installer@v1.4.17/pkg/asset/manifests/capiutils/cidr/cidr_test.go (about) 1 package cidr 2 3 import ( 4 "net" 5 "testing" 6 7 . "github.com/onsi/gomega" //nolint:revive 8 ) 9 10 func TestSplitIntoSubnetsIPv4(t *testing.T) { 11 RegisterTestingT(t) 12 tests := []struct { 13 name string 14 cidrblock string 15 subnetcount int 16 expected []*net.IPNet 17 }{ 18 { 19 // https://aws.amazon.com/about-aws/whats-new/2018/10/amazon-eks-now-supports-additional-vpc-cidr-blocks/ 20 name: "default secondary cidr block configuration with primary cidr", 21 cidrblock: "100.64.0.0/10", 22 subnetcount: 3, 23 expected: []*net.IPNet{ 24 { 25 IP: net.IPv4(100, 64, 0, 0).To4(), 26 Mask: net.IPv4Mask(255, 240, 0, 0), 27 }, 28 { 29 IP: net.IPv4(100, 80, 0, 0).To4(), 30 Mask: net.IPv4Mask(255, 240, 0, 0), 31 }, 32 { 33 IP: net.IPv4(100, 96, 0, 0).To4(), 34 Mask: net.IPv4Mask(255, 240, 0, 0), 35 }, 36 }, 37 }, 38 { 39 // https://aws.amazon.com/about-aws/whats-new/2018/10/amazon-eks-now-supports-additional-vpc-cidr-blocks/ 40 name: "default secondary cidr block configuration with alternative cidr", 41 cidrblock: "198.19.0.0/16", 42 subnetcount: 3, 43 expected: []*net.IPNet{ 44 { 45 IP: net.IPv4(198, 19, 0, 0).To4(), 46 Mask: net.IPv4Mask(255, 255, 192, 0), 47 }, 48 { 49 IP: net.IPv4(198, 19, 64, 0).To4(), 50 Mask: net.IPv4Mask(255, 255, 192, 0), 51 }, 52 { 53 IP: net.IPv4(198, 19, 128, 0).To4(), 54 Mask: net.IPv4Mask(255, 255, 192, 0), 55 }, 56 }, 57 }, 58 { 59 name: "slash 16 cidr with one subnet", 60 cidrblock: "1.1.0.0/16", 61 subnetcount: 1, 62 expected: []*net.IPNet{ 63 { 64 IP: net.IPv4(1, 1, 0, 0).To4(), 65 Mask: net.IPv4Mask(255, 255, 0, 0), 66 }, 67 }, 68 }, 69 } 70 71 for _, tc := range tests { 72 t.Run(tc.name, func(t *testing.T) { 73 output, err := SplitIntoSubnetsIPv4(tc.cidrblock, tc.subnetcount) 74 Expect(err).NotTo(HaveOccurred()) 75 Expect(output).To(ConsistOf(tc.expected)) 76 }) 77 } 78 } 79 80 var ( 81 block = "2001:db8:1234:1a00::/56" 82 ) 83 84 func TestParseIPv4CIDR(t *testing.T) { 85 RegisterTestingT(t) 86 87 input := []string{ 88 "2001:0db8:85a3:0000:0000:8a2e:0370:7334/64", 89 "2001:db8::/32", 90 "193.168.3.20/7", 91 } 92 93 output, err := GetIPv4Cidrs(input) 94 Expect(err).NotTo(HaveOccurred()) 95 Expect(output).To(HaveLen(1)) 96 } 97 98 func TestParseIPv6CIDR(t *testing.T) { 99 RegisterTestingT(t) 100 101 input := []string{ 102 "2001:0db8:85a3:0000:0000:8a2e:0370:7334/64", 103 "2001:db8::/32", 104 "193.168.3.20/7", 105 } 106 107 output, err := GetIPv6Cidrs(input) 108 Expect(err).NotTo(HaveOccurred()) 109 Expect(output).To(HaveLen(2)) 110 } 111 112 func TestSplitIntoSubnetsIPv6(t *testing.T) { 113 RegisterTestingT(t) 114 ip1, _, err := net.ParseCIDR("2001:db8:1234:1a01::/64") 115 Expect(err).NotTo(HaveOccurred()) 116 ip2, _, err := net.ParseCIDR("2001:db8:1234:1a02::/64") 117 Expect(err).NotTo(HaveOccurred()) 118 ip3, _, err := net.ParseCIDR("2001:db8:1234:1a03::/64") 119 Expect(err).NotTo(HaveOccurred()) 120 ip4, _, err := net.ParseCIDR("2001:db8:1234:1a04::/64") 121 Expect(err).NotTo(HaveOccurred()) 122 output, err := SplitIntoSubnetsIPv6(block, 4) 123 Expect(err).NotTo(HaveOccurred()) 124 Expect(output).To(ConsistOf( 125 &net.IPNet{ 126 IP: ip1, 127 Mask: net.CIDRMask(64, 128), 128 }, 129 &net.IPNet{ 130 IP: ip2, 131 Mask: net.CIDRMask(64, 128), 132 }, 133 &net.IPNet{ 134 IP: ip3, 135 Mask: net.CIDRMask(64, 128), 136 }, 137 &net.IPNet{ 138 IP: ip4, 139 Mask: net.CIDRMask(64, 128), 140 }, 141 )) 142 } 143 144 func TestSplitIntoSubnetsIPv6WithFurtherSplitting(t *testing.T) { 145 RegisterTestingT(t) 146 ip1, _, err := net.ParseCIDR("2001:db8:1234:1a01::/64") 147 Expect(err).NotTo(HaveOccurred()) 148 ip2, _, err := net.ParseCIDR("2001:db8:1234:1a02::/64") 149 Expect(err).NotTo(HaveOccurred()) 150 ip3, _, err := net.ParseCIDR("2001:db8:1234:1a03::/64") 151 Expect(err).NotTo(HaveOccurred()) 152 ip4, _, err := net.ParseCIDR("2001:db8:1234:1a04::/64") 153 Expect(err).NotTo(HaveOccurred()) 154 output, err := SplitIntoSubnetsIPv6(block, 4) 155 Expect(err).NotTo(HaveOccurred()) 156 Expect(output).To(ConsistOf( 157 &net.IPNet{ 158 IP: ip1, 159 Mask: net.CIDRMask(64, 128), 160 }, 161 &net.IPNet{ 162 IP: ip2, 163 Mask: net.CIDRMask(64, 128), 164 }, 165 &net.IPNet{ 166 IP: ip3, 167 Mask: net.CIDRMask(64, 128), 168 }, 169 &net.IPNet{ 170 IP: ip4, 171 Mask: net.CIDRMask(64, 128), 172 }, 173 )) 174 output, err = SplitIntoSubnetsIPv6(output[len(output)-1].String(), 3) 175 Expect(err).NotTo(HaveOccurred()) 176 ip1, _, err = net.ParseCIDR("2001:db8:1234:1a05::/64") 177 Expect(err).NotTo(HaveOccurred()) 178 ip2, _, err = net.ParseCIDR("2001:db8:1234:1a06::/64") 179 Expect(err).NotTo(HaveOccurred()) 180 ip3, _, err = net.ParseCIDR("2001:db8:1234:1a07::/64") 181 Expect(err).NotTo(HaveOccurred()) 182 Expect(output).To(ContainElements( 183 &net.IPNet{ 184 IP: ip1, 185 Mask: net.CIDRMask(64, 128), 186 }, 187 &net.IPNet{ 188 IP: ip2, 189 Mask: net.CIDRMask(64, 128), 190 }, 191 &net.IPNet{ 192 IP: ip3, 193 Mask: net.CIDRMask(64, 128), 194 }, 195 )) 196 } 197 198 func TestSplitIntoSubnetsIPv6HigherSubnetSplitting(t *testing.T) { 199 RegisterTestingT(t) 200 output, err := SplitIntoSubnetsIPv6("2001:db8:cad:ffff::/56", 6) 201 Expect(err).NotTo(HaveOccurred()) 202 ip1, _, err := net.ParseCIDR("2001:db8:cad:ff01::/64") 203 Expect(err).NotTo(HaveOccurred()) 204 ip2, _, err := net.ParseCIDR("2001:db8:cad:ff02::/64") 205 Expect(err).NotTo(HaveOccurred()) 206 ip3, _, err := net.ParseCIDR("2001:db8:cad:ff03::/64") 207 Expect(err).NotTo(HaveOccurred()) 208 ip4, _, err := net.ParseCIDR("2001:db8:cad:ff04::/64") 209 Expect(err).NotTo(HaveOccurred()) 210 Expect(output).To(ContainElements( 211 &net.IPNet{ 212 IP: ip1, 213 Mask: net.CIDRMask(64, 128), 214 }, 215 &net.IPNet{ 216 IP: ip2, 217 Mask: net.CIDRMask(64, 128), 218 }, 219 &net.IPNet{ 220 IP: ip3, 221 Mask: net.CIDRMask(64, 128), 222 }, 223 &net.IPNet{ 224 IP: ip4, 225 Mask: net.CIDRMask(64, 128), 226 }, 227 )) 228 } 229 230 func TestSplitIntoSubnetsIPv6NoCompression(t *testing.T) { 231 RegisterTestingT(t) 232 output, err := SplitIntoSubnetsIPv6("2001:0db8:85a3:0010:1111:8a2e:0370:7334/56", 5) 233 Expect(err).NotTo(HaveOccurred()) 234 ip1, _, err := net.ParseCIDR("2001:db8:85a3:1::/64") 235 Expect(err).NotTo(HaveOccurred()) 236 ip2, _, err := net.ParseCIDR("2001:db8:85a3:2::/64") 237 Expect(err).NotTo(HaveOccurred()) 238 ip3, _, err := net.ParseCIDR("2001:db8:85a3:3::/64") 239 Expect(err).NotTo(HaveOccurred()) 240 ip4, _, err := net.ParseCIDR("2001:db8:85a3:4::/64") 241 Expect(err).NotTo(HaveOccurred()) 242 ip5, _, err := net.ParseCIDR("2001:db8:85a3:5::/64") 243 Expect(err).NotTo(HaveOccurred()) 244 Expect(output).To(ContainElements( 245 &net.IPNet{ 246 IP: ip1, 247 Mask: net.CIDRMask(64, 128), 248 }, 249 &net.IPNet{ 250 IP: ip2, 251 Mask: net.CIDRMask(64, 128), 252 }, 253 &net.IPNet{ 254 IP: ip3, 255 Mask: net.CIDRMask(64, 128), 256 }, 257 &net.IPNet{ 258 IP: ip4, 259 Mask: net.CIDRMask(64, 128), 260 }, 261 &net.IPNet{ 262 IP: ip5, 263 Mask: net.CIDRMask(64, 128), 264 }, 265 )) 266 } 267 268 func TestSplitIntoSubnetsIPv6InvalidCIDR(t *testing.T) { 269 RegisterTestingT(t) 270 _, err := SplitIntoSubnetsIPv6("2001:db8:cad::", 60) 271 Expect(err).To(MatchError(ContainSubstring("failed to parse cidr block 2001:db8:cad:: with error: invalid CIDR address: 2001:db8:cad::"))) 272 }