1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
package addressupdater_test
11
gc "launchpad.net/gocheck"
13
"launchpad.net/juju-core/instance"
14
"launchpad.net/juju-core/juju/testing"
15
"launchpad.net/juju-core/provider/dummy"
16
"launchpad.net/juju-core/state"
17
coretesting "launchpad.net/juju-core/testing"
18
"launchpad.net/juju-core/testing/testbase"
19
"launchpad.net/juju-core/worker"
20
"launchpad.net/juju-core/worker/addressupdater"
23
var _ = gc.Suite(&workerSuite{})
25
type workerSuite struct {
29
func (*workerSuite) instId(i int) instance.Id {
30
return instance.Id(fmt.Sprint(i))
33
func (*workerSuite) addressesForIndex(i int) []instance.Address {
34
return []instance.Address{
35
instance.NewAddress(fmt.Sprintf("127.0.0.%d", i)),
39
func (s *workerSuite) TestWorker(c *gc.C) {
40
// Most functionality is already tested in detail - we
41
// just need to test that things are wired together
43
defer testbase.PatchValue(addressupdater.ShortPoll, 10*time.Millisecond).Restore()
44
defer testbase.PatchValue(addressupdater.LongPoll, 10*time.Millisecond).Restore()
45
machines, insts := s.setupScenario(c)
47
w := addressupdater.NewWorker(s.State)
49
c.Assert(worker.Stop(w), gc.IsNil)
52
// Wait for the odd numbered machines in the
53
// first half of the machine slice to be given their
55
for a := coretesting.LongAttempt.Start(); a.Next(); {
57
c.Fatalf("timed out waiting for machine addresses")
59
if machinesSatisfy(c, machines, func(i int, m *state.Machine) bool {
60
if i < len(machines)/2 && i%2 == 1 {
61
return reflect.DeepEqual(m.Addresses(), s.addressesForIndex(i))
63
return len(m.Addresses()) == 0
68
// Now provision the second half of the machines and
69
// watch them get addresses.
70
for i := len(insts) / 2; i < len(insts); i++ {
71
dummy.SetInstanceAddresses(insts[i], s.addressesForIndex(i))
73
for a := coretesting.LongAttempt.Start(); a.Next(); {
75
c.Fatalf("timed out waiting for machine addresses")
77
if machinesSatisfy(c, machines, func(i int, m *state.Machine) bool {
78
return reflect.DeepEqual(m.Addresses(), s.addressesForIndex(i))
86
// - check that the environment observer is actually hooked up.
87
// - check that the environment observer is stopped.
88
// - check that the errors propagate correctly.
90
func machinesSatisfy(c *gc.C, machines []*state.Machine, f func(i int, m *state.Machine) bool) bool {
91
for i, m := range machines {
93
c.Assert(err, gc.IsNil)
101
func (s *workerSuite) setupScenario(c *gc.C) ([]*state.Machine, []instance.Instance) {
102
var machines []*state.Machine
103
var insts []instance.Instance
104
for i := 0; i < 10; i++ {
105
m, err := s.State.AddMachine("series", state.JobHostUnits)
106
c.Assert(err, gc.IsNil)
107
machines = append(machines, m)
108
inst, _ := testing.AssertStartInstance(c, s.Conn.Environ, m.Id())
109
insts = append(insts, inst)
111
// Associate the odd-numbered machines with an instance.
112
for i := 1; i < len(machines); i += 2 {
114
err := m.SetProvisioned(insts[i].Id(), "nonce", nil)
115
c.Assert(err, gc.IsNil)
117
// Associate the first half of the instances with an address.
118
for i := 0; i < len(machines)/2; i++ {
119
dummy.SetInstanceAddresses(insts[i], s.addressesForIndex(i))
121
return machines, insts