github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/azure/internal/iputils/iputils_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package iputils_test 5 6 import ( 7 "fmt" 8 "net" 9 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/provider/azure/internal/iputils" 14 "github.com/juju/juju/testing" 15 ) 16 17 type iputilsSuite struct { 18 testing.BaseSuite 19 } 20 21 var _ = gc.Suite(&iputilsSuite{}) 22 23 func (*iputilsSuite) TestNextSubnetIP(c *gc.C) { 24 assertNextSubnetIP(c, "10.0.0.0/8", nil, "10.0.0.4") 25 assertNextSubnetIP(c, "10.0.0.0/8", []string{"10.0.0.1"}, "10.0.0.4") 26 assertNextSubnetIP(c, "10.0.0.0/8", []string{"10.0.0.1", "10.0.0.4"}, "10.0.0.5") 27 } 28 29 func (*iputilsSuite) TestNextSubnetIPErrors(c *gc.C) { 30 // The subnet is too small to have any non-reserved addresses. 31 assertNextSubnetIPError( 32 c, 33 "10.1.2.0/30", 34 nil, 35 "no addresses available in 10.1.2.0/30", 36 ) 37 38 // All addresses in use. 39 var addresses []string 40 for i := 1; i < 255; i++ { 41 addr := fmt.Sprintf("10.0.0.%d", i) 42 addresses = append(addresses, addr) 43 } 44 assertNextSubnetIPError( 45 c, "10.0.0.0/24", addresses, 46 "no addresses available in 10.0.0.0/24", 47 ) 48 } 49 50 func (*iputilsSuite) TestNthSubnetIP(c *gc.C) { 51 assertNthSubnetIP(c, "10.0.0.0/8", 0, "10.0.0.4") 52 assertNthSubnetIP(c, "10.0.0.0/8", 1, "10.0.0.5") 53 assertNthSubnetIP(c, "10.0.0.0/29", 0, "10.0.0.4") 54 assertNthSubnetIP(c, "10.0.0.0/29", 1, "10.0.0.5") 55 assertNthSubnetIP(c, "10.0.0.0/29", 2, "10.0.0.6") 56 assertNthSubnetIP(c, "10.0.0.0/29", 3, "") // all bits set, broadcast 57 assertNthSubnetIP(c, "10.1.2.0/30", 0, "") 58 } 59 60 func assertNextSubnetIP(c *gc.C, ipnetString string, inuseStrings []string, expectedString string) { 61 ipnet := parseIPNet(c, ipnetString) 62 inuse := parseIPs(c, inuseStrings...) 63 next, err := iputils.NextSubnetIP(ipnet, inuse) 64 c.Assert(err, jc.ErrorIsNil) 65 c.Assert(next.String(), gc.Equals, expectedString) 66 } 67 68 func assertNextSubnetIPError(c *gc.C, ipnetString string, inuseStrings []string, expect string) { 69 ipnet := parseIPNet(c, ipnetString) 70 inuse := parseIPs(c, inuseStrings...) 71 _, err := iputils.NextSubnetIP(ipnet, inuse) 72 c.Assert(err, gc.ErrorMatches, expect) 73 } 74 75 func assertNthSubnetIP(c *gc.C, ipnetString string, n int, expectedString string) { 76 ipnet := parseIPNet(c, ipnetString) 77 ip := iputils.NthSubnetIP(ipnet, n) 78 if expectedString == "" { 79 c.Assert(ip, gc.IsNil) 80 } else { 81 c.Assert(ip, gc.NotNil) 82 c.Assert(ip.String(), gc.Equals, expectedString) 83 } 84 } 85 86 func parseIPs(c *gc.C, ipStrings ...string) []net.IP { 87 ips := make([]net.IP, len(ipStrings)) 88 for i, ipString := range ipStrings { 89 ip := net.ParseIP(ipString) 90 c.Assert(ip, gc.NotNil, gc.Commentf("failed to parse IP %q", ipString)) 91 ips[i] = ip 92 } 93 return ips 94 } 95 96 func parseIPNet(c *gc.C, cidr string) *net.IPNet { 97 _, ipnet, err := net.ParseCIDR(cidr) 98 c.Assert(err, jc.ErrorIsNil) 99 return ipnet 100 }