github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/environs/manual" 13 "github.com/juju/juju/network" 14 "github.com/juju/juju/testing" 15 ) 16 17 const ( 18 invalidHost = "testing.invalid" 19 validHost = "testing.valid" 20 ) 21 22 type addressesSuite struct { 23 testing.BaseSuite 24 netLookupHostCalled int 25 } 26 27 var _ = gc.Suite(&addressesSuite{}) 28 29 func (s *addressesSuite) SetUpTest(c *gc.C) { 30 s.netLookupHostCalled = 0 31 s.PatchValue(manual.NetLookupHost, func(host string) ([]string, error) { 32 s.netLookupHostCalled++ 33 if host == invalidHost { 34 return nil, errors.New("invalid host: " + invalidHost) 35 } 36 return []string{"127.0.0.1"}, nil 37 }) 38 } 39 40 func (s *addressesSuite) TestHostAddress(c *gc.C) { 41 addr, err := manual.HostAddress(validHost) 42 c.Assert(s.netLookupHostCalled, gc.Equals, 1) 43 c.Assert(err, jc.ErrorIsNil) 44 c.Assert(addr, gc.Equals, network.Address{ 45 Value: validHost, 46 Type: network.HostName, 47 Scope: network.ScopePublic, 48 }) 49 } 50 51 func (s *addressesSuite) TestHostAddressError(c *gc.C) { 52 addr, err := manual.HostAddress(invalidHost) 53 c.Assert(s.netLookupHostCalled, gc.Equals, 1) 54 c.Assert(err, gc.ErrorMatches, "invalid host: "+invalidHost) 55 c.Assert(addr, gc.Equals, network.Address{}) 56 } 57 58 func (s *addressesSuite) TestHostAddressIPv4(c *gc.C) { 59 addr, err := manual.HostAddress("127.0.0.1") 60 c.Assert(s.netLookupHostCalled, gc.Equals, 0) 61 c.Assert(err, jc.ErrorIsNil) 62 c.Assert(addr, gc.Equals, network.Address{ 63 Value: "127.0.0.1", 64 Type: network.IPv4Address, 65 Scope: network.ScopePublic, 66 }) 67 } 68 69 func (s *addressesSuite) TestHostAddressIPv6(c *gc.C) { 70 addr, err := manual.HostAddress("::1") 71 c.Assert(s.netLookupHostCalled, gc.Equals, 0) 72 c.Assert(err, jc.ErrorIsNil) 73 c.Assert(addr, gc.Equals, network.Address{ 74 Value: "::1", 75 Type: network.IPv6Address, 76 Scope: network.ScopePublic, 77 }) 78 }