1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
10
gc "launchpad.net/gocheck"
12
"launchpad.net/juju-core/environs"
13
"launchpad.net/juju-core/instance"
14
"launchpad.net/juju-core/utils"
17
type pollingSuite struct {
18
originalLongAttempt utils.AttemptStrategy
21
var _ = gc.Suite(&pollingSuite{})
23
func (s *pollingSuite) SetUpSuite(c *gc.C) {
24
s.originalLongAttempt = environs.LongAttempt
25
// The implementation of AttemptStrategy does not yield at all for a
26
// delay that's already expired. So while this setting must be short
27
// to avoid blocking tests, it must also allow enough time to convince
28
// AttemptStrategy to sleep. Otherwise a polling loop would just run
29
// uninterrupted and a concurrent goroutine that it was waiting for
30
// might never actually get to do its work.
31
environs.LongAttempt = utils.AttemptStrategy{
32
Total: 10 * time.Millisecond,
33
Delay: 1 * time.Millisecond,
37
func (s *pollingSuite) TearDownSuite(c *gc.C) {
38
environs.LongAttempt = s.originalLongAttempt
41
// dnsNameFakeInstance is a fake environs.Instance implementation where
42
// DNSName returns whatever you tell it to, and WaitDNSName delegates to the
43
// shared WaitDNSName implementation. All the other methods are empty stubs.
44
type dnsNameFakeInstance struct {
45
// embed a nil Instance to panic on unimplemented method
51
var _ instance.Instance = (*dnsNameFakeInstance)(nil)
53
func (inst *dnsNameFakeInstance) DNSName() (string, error) {
54
return inst.name, inst.err
57
func (inst *dnsNameFakeInstance) WaitDNSName() (string, error) {
58
return environs.WaitDNSName(inst)
61
func (*dnsNameFakeInstance) Id() instance.Id { return "" }
63
func (pollingSuite) TestWaitDNSNameReturnsDNSNameIfAvailable(c *gc.C) {
64
inst := dnsNameFakeInstance{name: "anansi"}
65
name, err := environs.WaitDNSName(&inst)
66
c.Assert(err, gc.IsNil)
67
c.Check(name, gc.Equals, "anansi")
70
func (pollingSuite) TestWaitDNSNamePollsOnErrNoDNSName(c *gc.C) {
71
inst := dnsNameFakeInstance{err: instance.ErrNoDNSName}
72
_, err := environs.WaitDNSName(&inst)
73
c.Assert(err, gc.NotNil)
74
c.Check(err, gc.ErrorMatches, ".*timed out trying to get DNS address.*")
77
func (pollingSuite) TestWaitDNSNamePropagatesFailure(c *gc.C) {
78
failure := errors.New("deliberate failure")
79
inst := dnsNameFakeInstance{err: failure}
80
_, err := environs.WaitDNSName(&inst)
81
c.Assert(err, gc.NotNil)
82
c.Check(err, gc.Equals, failure)
85
func (pollingSuite) TestInstanceWaitDNSDelegatesToSharedWaitDNS(c *gc.C) {
86
inst := dnsNameFakeInstance{name: "anansi"}
87
name, err := inst.WaitDNSName()
88
c.Assert(err, gc.IsNil)
89
c.Check(name, gc.Equals, "anansi")