1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
jc "github.com/juju/testing/checkers"
10
gc "gopkg.in/check.v1"
12
"github.com/juju/juju/environs/manual"
13
"github.com/juju/juju/network"
14
"github.com/juju/juju/testing"
18
invalidHost = "testing.invalid"
19
validHost = "testing.valid"
22
type addressesSuite struct {
24
netLookupHostCalled int
27
var _ = gc.Suite(&addressesSuite{})
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)
36
return []string{"127.0.0.1"}, nil
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{
46
Type: network.HostName,
47
Scope: network.ScopePublic,
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{})
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{
64
Type: network.IPv4Address,
65
Scope: network.ScopePublic,
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{
75
Type: network.IPv6Address,
76
Scope: network.ScopePublic,