launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/utils/network_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package utils_test
     5  
     6  import (
     7  	gc "launchpad.net/gocheck"
     8  	"net"
     9  
    10  	"launchpad.net/juju-core/utils"
    11  )
    12  
    13  type networkSuite struct {
    14  }
    15  
    16  var _ = gc.Suite(&networkSuite{})
    17  
    18  type fakeAddress struct {
    19  	address string
    20  }
    21  
    22  func (fake fakeAddress) Network() string {
    23  	return "ignored"
    24  }
    25  
    26  func (fake fakeAddress) String() string {
    27  	return fake.address
    28  }
    29  
    30  func makeAddresses(values ...string) (result []net.Addr) {
    31  	for _, v := range values {
    32  		result = append(result, &fakeAddress{v})
    33  	}
    34  	return
    35  }
    36  
    37  func (*networkSuite) TestGetIPv4Address(c *gc.C) {
    38  	for _, test := range []struct {
    39  		addresses   []net.Addr
    40  		expected    string
    41  		errorString string
    42  	}{{
    43  		addresses: makeAddresses(
    44  			"complete",
    45  			"nonsense"),
    46  		errorString: "invalid CIDR address: complete",
    47  	}, {
    48  		addresses: makeAddresses(
    49  			"fe80::90cf:9dff:fe6e:ece/64",
    50  		),
    51  		errorString: "no addresses match",
    52  	}, {
    53  		addresses: makeAddresses(
    54  			"fe80::90cf:9dff:fe6e:ece/64",
    55  			"10.0.3.1/24",
    56  		),
    57  		expected: "10.0.3.1",
    58  	}, {
    59  		addresses: makeAddresses(
    60  			"10.0.3.1/24",
    61  			"fe80::90cf:9dff:fe6e:ece/64",
    62  		),
    63  		expected: "10.0.3.1",
    64  	}} {
    65  		ip, err := utils.GetIPv4Address(test.addresses)
    66  		if test.errorString == "" {
    67  			c.Assert(err, gc.IsNil)
    68  			c.Assert(ip, gc.Equals, test.expected)
    69  		} else {
    70  			c.Assert(err, gc.ErrorMatches, test.errorString)
    71  			c.Assert(ip, gc.Equals, "")
    72  		}
    73  	}
    74  }