github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/instance/address_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package instance
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "launchpad.net/gocheck"
     9  
    10  	"github.com/juju/juju/testing"
    11  )
    12  
    13  type AddressSuite struct {
    14  	testing.BaseSuite
    15  }
    16  
    17  var _ = gc.Suite(&AddressSuite{})
    18  
    19  func (s *AddressSuite) TestNewAddressIpv4(c *gc.C) {
    20  	type test struct {
    21  		value         string
    22  		scope         NetworkScope
    23  		expectedScope NetworkScope
    24  	}
    25  
    26  	tests := []test{{
    27  		value:         "127.0.0.1",
    28  		scope:         NetworkUnknown,
    29  		expectedScope: NetworkMachineLocal,
    30  	}, {
    31  		value:         "127.0.0.1",
    32  		scope:         NetworkPublic,
    33  		expectedScope: NetworkPublic, // don't second guess != Unknown
    34  	}, {
    35  		value:         "10.0.3.1",
    36  		scope:         NetworkUnknown,
    37  		expectedScope: NetworkCloudLocal,
    38  	}, {
    39  		value:         "172.16.15.14",
    40  		scope:         NetworkUnknown,
    41  		expectedScope: NetworkCloudLocal,
    42  	}, {
    43  		value:         "192.168.0.1",
    44  		scope:         NetworkUnknown,
    45  		expectedScope: NetworkCloudLocal,
    46  	}, {
    47  		value:         "8.8.8.8",
    48  		scope:         NetworkUnknown,
    49  		expectedScope: NetworkPublic,
    50  	}}
    51  
    52  	for _, t := range tests {
    53  		c.Logf("test %s %s", t.value, t.scope)
    54  		addr := NewAddress(t.value, t.scope)
    55  		c.Check(addr.Value, gc.Equals, t.value)
    56  		c.Check(addr.Type, gc.Equals, Ipv4Address)
    57  		c.Check(addr.NetworkScope, gc.Equals, t.expectedScope)
    58  	}
    59  }
    60  
    61  func (s *AddressSuite) TestNewAddressIpv6(c *gc.C) {
    62  	addr := NewAddress("::1", NetworkUnknown)
    63  	c.Check(addr.Value, gc.Equals, "::1")
    64  	c.Check(addr.Type, gc.Equals, Ipv6Address)
    65  	c.Check(addr.NetworkScope, gc.Equals, NetworkMachineLocal)
    66  
    67  	addr = NewAddress("2001:DB8::1", NetworkUnknown)
    68  	c.Check(addr.Value, gc.Equals, "2001:DB8::1")
    69  	c.Check(addr.Type, gc.Equals, Ipv6Address)
    70  	c.Check(addr.NetworkScope, gc.Equals, NetworkUnknown)
    71  }
    72  
    73  func (s *AddressSuite) TestNewAddresses(c *gc.C) {
    74  	addresses := NewAddresses("127.0.0.1", "192.168.1.1", "192.168.178.255")
    75  	c.Assert(len(addresses), gc.Equals, 3)
    76  	c.Assert(addresses[0].Value, gc.Equals, "127.0.0.1")
    77  	c.Assert(addresses[0].NetworkScope, gc.Equals, NetworkMachineLocal)
    78  	c.Assert(addresses[1].Value, gc.Equals, "192.168.1.1")
    79  	c.Assert(addresses[1].NetworkScope, gc.Equals, NetworkCloudLocal)
    80  	c.Assert(addresses[2].Value, gc.Equals, "192.168.178.255")
    81  	c.Assert(addresses[2].NetworkScope, gc.Equals, NetworkCloudLocal)
    82  }
    83  
    84  func (s *AddressSuite) TestNewAddressHostname(c *gc.C) {
    85  	addr := NewAddress("localhost", NetworkUnknown)
    86  	c.Check(addr.Value, gc.Equals, "localhost")
    87  	c.Check(addr.Type, gc.Equals, HostName)
    88  	c.Check(addr.NetworkScope, gc.Equals, NetworkUnknown)
    89  }
    90  
    91  type selectTest struct {
    92  	about         string
    93  	addresses     []Address
    94  	expectedIndex int
    95  }
    96  
    97  // expected returns the expected address for the test.
    98  func (t selectTest) expected() string {
    99  	if t.expectedIndex == -1 {
   100  		return ""
   101  	}
   102  	return t.addresses[t.expectedIndex].Value
   103  }
   104  
   105  type hostPortTest struct {
   106  	about         string
   107  	hostPorts     []HostPort
   108  	expectedIndex int
   109  }
   110  
   111  // hostPortTest returns the HostPort equivalent test to the
   112  // receiving selectTest.
   113  func (t selectTest) hostPortTest() hostPortTest {
   114  	hps := AddressesWithPort(t.addresses, 9999)
   115  	for i := range hps {
   116  		hps[i].Port = i + 1
   117  	}
   118  	return hostPortTest{
   119  		about:         t.about,
   120  		hostPorts:     hps,
   121  		expectedIndex: t.expectedIndex,
   122  	}
   123  }
   124  
   125  // expected returns the expected host:port result
   126  // of the test.
   127  func (t hostPortTest) expected() string {
   128  	if t.expectedIndex == -1 {
   129  		return ""
   130  	}
   131  	return t.hostPorts[t.expectedIndex].NetAddr()
   132  }
   133  
   134  var selectPublicTests = []selectTest{{
   135  	"no addresses gives empty string result",
   136  	[]Address{},
   137  	-1,
   138  }, {
   139  	"a public address is selected",
   140  	[]Address{
   141  		{"8.8.8.8", Ipv4Address, "public", NetworkPublic},
   142  	},
   143  	0,
   144  }, {
   145  	"a machine local address is not selected",
   146  	[]Address{
   147  		{"127.0.0.1", Ipv4Address, "machine", NetworkMachineLocal},
   148  	},
   149  	-1,
   150  }, {
   151  	"an ipv6 address is not selected",
   152  	[]Address{
   153  		{"2001:DB8::1", Ipv6Address, "", NetworkPublic},
   154  	},
   155  	-1,
   156  }, {
   157  	"a public name is preferred to an unknown or cloud local address",
   158  	[]Address{
   159  		{"127.0.0.1", Ipv4Address, "local", NetworkUnknown},
   160  		{"10.0.0.1", Ipv4Address, "cloud", NetworkCloudLocal},
   161  		{"public.invalid.testing", HostName, "public", NetworkPublic},
   162  	},
   163  	2,
   164  }, {
   165  	"first unknown address selected",
   166  	[]Address{
   167  		{"10.0.0.1", Ipv4Address, "cloud", NetworkUnknown},
   168  		{"8.8.8.8", Ipv4Address, "floating", NetworkUnknown},
   169  	},
   170  	0,
   171  }}
   172  
   173  func (s *AddressSuite) TestSelectPublicAddress(c *gc.C) {
   174  	for i, t := range selectPublicTests {
   175  		c.Logf("test %d. %s", i, t.about)
   176  		c.Check(SelectPublicAddress(t.addresses), gc.Equals, t.expected())
   177  	}
   178  }
   179  
   180  func (s *AddressSuite) TestSelectPublicHostPort(c *gc.C) {
   181  	for i, t0 := range selectPublicTests {
   182  		t := t0.hostPortTest()
   183  		c.Logf("test %d. %s", i, t.about)
   184  		c.Assert(SelectPublicHostPort(t.hostPorts), gc.DeepEquals, t.expected())
   185  	}
   186  }
   187  
   188  var selectInternalTests = []selectTest{{
   189  	"no addresses gives empty string result",
   190  	[]Address{},
   191  	-1,
   192  }, {
   193  	"a public address is selected",
   194  	[]Address{
   195  		{"8.8.8.8", Ipv4Address, "public", NetworkPublic},
   196  	},
   197  	0,
   198  }, {
   199  	"a cloud local address is selected",
   200  	[]Address{
   201  		{"10.0.0.1", Ipv4Address, "private", NetworkCloudLocal},
   202  	},
   203  	0,
   204  }, {
   205  	"a machine local address is not selected",
   206  	[]Address{
   207  		{"127.0.0.1", Ipv4Address, "machine", NetworkMachineLocal},
   208  	},
   209  	-1,
   210  }, {
   211  	"ipv6 addresses are not selected",
   212  	[]Address{
   213  		{"::1", Ipv6Address, "", NetworkCloudLocal},
   214  	},
   215  	-1,
   216  }, {
   217  	"a cloud local address is preferred to a public address",
   218  	[]Address{
   219  		{"10.0.0.1", Ipv4Address, "cloud", NetworkCloudLocal},
   220  		{"8.8.8.8", Ipv4Address, "public", NetworkPublic},
   221  	},
   222  	0,
   223  }}
   224  
   225  func (s *AddressSuite) TestSelectInternalAddress(c *gc.C) {
   226  	for i, t := range selectInternalTests {
   227  		c.Logf("test %d. %s", i, t.about)
   228  		c.Check(SelectInternalAddress(t.addresses, false), gc.Equals, t.expected())
   229  	}
   230  }
   231  
   232  func (s *AddressSuite) TestSelectInternalHostPort(c *gc.C) {
   233  	for i, t0 := range selectInternalTests {
   234  		t := t0.hostPortTest()
   235  		c.Logf("test %d. %s", i, t.about)
   236  		c.Assert(SelectInternalHostPort(t.hostPorts, false), gc.DeepEquals, t.expected())
   237  	}
   238  }
   239  
   240  var selectInternalMachineTests = []selectTest{{
   241  	"a cloud local address is selected",
   242  	[]Address{
   243  		{"10.0.0.1", Ipv4Address, "cloud", NetworkCloudLocal},
   244  	},
   245  	0,
   246  }, {
   247  	"a machine local address is selected",
   248  	[]Address{
   249  		{"127.0.0.1", Ipv4Address, "container", NetworkMachineLocal},
   250  	},
   251  	0,
   252  }}
   253  
   254  func (s *AddressSuite) TestSelectInternalMachineAddress(c *gc.C) {
   255  	for i, t := range selectInternalMachineTests {
   256  		c.Logf("test %d. %s", i, t.about)
   257  		c.Check(SelectInternalAddress(t.addresses, true), gc.Equals, t.expected())
   258  	}
   259  }
   260  
   261  func (s *AddressSuite) TestSelectInternalMachineHostPort(c *gc.C) {
   262  	for i, t0 := range selectInternalMachineTests {
   263  		t := t0.hostPortTest()
   264  		c.Logf("test %d. %s", i, t.about)
   265  		c.Assert(SelectInternalHostPort(t.hostPorts, true), gc.DeepEquals, t.expected())
   266  	}
   267  }
   268  
   269  var stringTests = []struct {
   270  	addr Address
   271  	str  string
   272  }{{
   273  	addr: Address{
   274  		Type:  Ipv4Address,
   275  		Value: "127.0.0.1",
   276  	},
   277  	str: "127.0.0.1",
   278  }, {
   279  	addr: Address{
   280  		Type:  HostName,
   281  		Value: "foo.com",
   282  	},
   283  	str: "foo.com",
   284  }, {
   285  	addr: Address{
   286  		Type:         HostName,
   287  		Value:        "foo.com",
   288  		NetworkScope: NetworkUnknown,
   289  	},
   290  	str: "foo.com",
   291  }, {
   292  	addr: Address{
   293  		Type:         HostName,
   294  		Value:        "foo.com",
   295  		NetworkScope: NetworkPublic,
   296  	},
   297  	str: "public:foo.com",
   298  }, {
   299  	addr: Address{
   300  		Type:         HostName,
   301  		Value:        "foo.com",
   302  		NetworkScope: NetworkPublic,
   303  		NetworkName:  "netname",
   304  	},
   305  	str: "public:foo.com(netname)",
   306  }}
   307  
   308  func (s *AddressSuite) TestString(c *gc.C) {
   309  	for _, test := range stringTests {
   310  		c.Check(test.addr.String(), gc.Equals, test.str)
   311  	}
   312  }
   313  
   314  func (*AddressSuite) TestAddressesWithPort(c *gc.C) {
   315  	addrs := NewAddresses("0.1.2.3", "0.2.4.6")
   316  	hps := AddressesWithPort(addrs, 999)
   317  	c.Assert(hps, jc.DeepEquals, []HostPort{{
   318  		Address: NewAddress("0.1.2.3", NetworkUnknown),
   319  		Port:    999,
   320  	}, {
   321  		Address: NewAddress("0.2.4.6", NetworkUnknown),
   322  		Port:    999,
   323  	}})
   324  }
   325  
   326  var netAddrTests = []struct {
   327  	addr   Address
   328  	port   int
   329  	expect string
   330  }{{
   331  	addr:   NewAddress("0.1.2.3", NetworkUnknown),
   332  	port:   99,
   333  	expect: "0.1.2.3:99",
   334  }, {
   335  	addr:   NewAddress("2001:DB8::1", NetworkUnknown),
   336  	port:   100,
   337  	expect: "[2001:DB8::1]:100",
   338  }}
   339  
   340  func (*AddressSuite) TestNetAddr(c *gc.C) {
   341  	for i, test := range netAddrTests {
   342  		c.Logf("test %d: %q", i, test.addr)
   343  		hp := HostPort{
   344  			Address: test.addr,
   345  			Port:    test.port,
   346  		}
   347  		c.Assert(hp.NetAddr(), gc.Equals, test.expect)
   348  	}
   349  }