github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/environs/manual/addresses_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package manual_test
     5  
     6  import (
     7  	"errors"
     8  
     9  	gc "launchpad.net/gocheck"
    10  
    11  	"github.com/juju/juju/environs/manual"
    12  	"github.com/juju/juju/instance"
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  const (
    17  	invalidHost = "testing.invalid"
    18  	validHost   = "testing.valid"
    19  )
    20  
    21  type addressesSuite struct {
    22  	testing.BaseSuite
    23  	netLookupHostCalled int
    24  }
    25  
    26  var _ = gc.Suite(&addressesSuite{})
    27  
    28  func (s *addressesSuite) SetUpTest(c *gc.C) {
    29  	s.netLookupHostCalled = 0
    30  	s.PatchValue(manual.NetLookupHost, func(host string) ([]string, error) {
    31  		s.netLookupHostCalled++
    32  		if host == invalidHost {
    33  			return nil, errors.New("invalid host: " + invalidHost)
    34  		}
    35  		return []string{"127.0.0.1"}, nil
    36  	})
    37  }
    38  
    39  func (s *addressesSuite) TestHostAddress(c *gc.C) {
    40  	addr, err := manual.HostAddress(validHost)
    41  	c.Assert(s.netLookupHostCalled, gc.Equals, 1)
    42  	c.Assert(err, gc.IsNil)
    43  	c.Assert(addr, gc.Equals, instance.Address{
    44  		Value:        validHost,
    45  		Type:         instance.HostName,
    46  		NetworkScope: instance.NetworkPublic,
    47  	})
    48  }
    49  
    50  func (s *addressesSuite) TestHostAddressError(c *gc.C) {
    51  	addr, err := manual.HostAddress(invalidHost)
    52  	c.Assert(s.netLookupHostCalled, gc.Equals, 1)
    53  	c.Assert(err, gc.ErrorMatches, "invalid host: "+invalidHost)
    54  	c.Assert(addr, gc.Equals, instance.Address{})
    55  }
    56  
    57  func (s *addressesSuite) TestHostAddressIPv4(c *gc.C) {
    58  	addr, err := manual.HostAddress("127.0.0.1")
    59  	c.Assert(s.netLookupHostCalled, gc.Equals, 0)
    60  	c.Assert(err, gc.IsNil)
    61  	c.Assert(addr, gc.Equals, instance.Address{
    62  		Value:        "127.0.0.1",
    63  		Type:         instance.Ipv4Address,
    64  		NetworkScope: instance.NetworkPublic,
    65  	})
    66  }
    67  
    68  func (s *addressesSuite) TestHostAddressIPv6(c *gc.C) {
    69  	addr, err := manual.HostAddress("::1")
    70  	c.Assert(s.netLookupHostCalled, gc.Equals, 0)
    71  	c.Assert(err, gc.IsNil)
    72  	c.Assert(addr, gc.Equals, instance.Address{
    73  		Value:        "::1",
    74  		Type:         instance.Ipv6Address,
    75  		NetworkScope: instance.NetworkPublic,
    76  	})
    77  }