~rogpeppe/juju-core/438-local-instance-Addresses

« back to all changes in this revision

Viewing changes to worker/addressupdater/worker_test.go

[r=rogpeppe] worker/addressupdater: wire up

It could do with some more comprehensive
testing, but it might be OK to land anyway.

https://codereview.appspot.com/14306043/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package addressupdater_test
 
5
 
 
6
import (
 
7
        "fmt"
 
8
        "reflect"
 
9
        "time"
 
10
 
 
11
        gc "launchpad.net/gocheck"
 
12
 
 
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"
 
21
)
 
22
 
 
23
var _ = gc.Suite(&workerSuite{})
 
24
 
 
25
type workerSuite struct {
 
26
        testing.JujuConnSuite
 
27
}
 
28
 
 
29
func (*workerSuite) instId(i int) instance.Id {
 
30
        return instance.Id(fmt.Sprint(i))
 
31
}
 
32
 
 
33
func (*workerSuite) addressesForIndex(i int) []instance.Address {
 
34
        return []instance.Address{
 
35
                instance.NewAddress(fmt.Sprintf("127.0.0.%d", i)),
 
36
        }
 
37
}
 
38
 
 
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
 
42
        // correctly.
 
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)
 
46
        s.State.StartSync()
 
47
        w := addressupdater.NewWorker(s.State)
 
48
        defer func() {
 
49
                c.Assert(worker.Stop(w), gc.IsNil)
 
50
        }()
 
51
 
 
52
        // Wait for the odd numbered machines in the
 
53
        // first half of the machine slice to be given their
 
54
        // addresses.
 
55
        for a := coretesting.LongAttempt.Start(); a.Next(); {
 
56
                if !a.HasNext() {
 
57
                        c.Fatalf("timed out waiting for machine addresses")
 
58
                }
 
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))
 
62
                        }
 
63
                        return len(m.Addresses()) == 0
 
64
                }) {
 
65
                        break
 
66
                }
 
67
        }
 
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))
 
72
        }
 
73
        for a := coretesting.LongAttempt.Start(); a.Next(); {
 
74
                if !a.HasNext() {
 
75
                        c.Fatalf("timed out waiting for machine addresses")
 
76
                }
 
77
                if machinesSatisfy(c, machines, func(i int, m *state.Machine) bool {
 
78
                        return reflect.DeepEqual(m.Addresses(), s.addressesForIndex(i))
 
79
                }) {
 
80
                        break
 
81
                }
 
82
        }
 
83
}
 
84
 
 
85
// TODO(rog)
 
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.
 
89
 
 
90
func machinesSatisfy(c *gc.C, machines []*state.Machine, f func(i int, m *state.Machine) bool) bool {
 
91
        for i, m := range machines {
 
92
                err := m.Refresh()
 
93
                c.Assert(err, gc.IsNil)
 
94
                if !f(i, m) {
 
95
                        return false
 
96
                }
 
97
        }
 
98
        return true
 
99
}
 
100
 
 
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)
 
110
        }
 
111
        // Associate the odd-numbered machines with an instance.
 
112
        for i := 1; i < len(machines); i += 2 {
 
113
                m := machines[i]
 
114
                err := m.SetProvisioned(insts[i].Id(), "nonce", nil)
 
115
                c.Assert(err, gc.IsNil)
 
116
        }
 
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))
 
120
        }
 
121
        return machines, insts
 
122
}