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

« back to all changes in this revision

Viewing changes to worker/addressupdater/observer_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
 
5
 
 
6
import (
 
7
        "strings"
 
8
        "time"
 
9
 
 
10
        gc "launchpad.net/gocheck"
 
11
        "launchpad.net/loggo"
 
12
 
 
13
        "launchpad.net/juju-core/juju/testing"
 
14
        coretesting "launchpad.net/juju-core/testing"
 
15
)
 
16
 
 
17
var _ = gc.Suite(&observerSuite{})
 
18
 
 
19
type observerSuite struct {
 
20
        testing.JujuConnSuite
 
21
}
 
22
 
 
23
func (s *observerSuite) TestWaitsForValidEnviron(c *gc.C) {
 
24
        obs, err := newEnvironObserver(s.State, nil)
 
25
        c.Assert(err, gc.IsNil)
 
26
        env := obs.Environ()
 
27
        stateConfig, err := s.State.EnvironConfig()
 
28
        c.Assert(err, gc.IsNil)
 
29
        c.Assert(env.Config().AllAttrs(), gc.DeepEquals, stateConfig.AllAttrs())
 
30
}
 
31
 
 
32
func (s *observerSuite) TestEnvironmentChanges(c *gc.C) {
 
33
        originalConfig, err := s.State.EnvironConfig()
 
34
        c.Assert(err, gc.IsNil)
 
35
 
 
36
        logc := make(logChan, 1009)
 
37
        c.Assert(loggo.RegisterWriter("testing", logc, loggo.WARNING), gc.IsNil)
 
38
        defer loggo.RemoveWriter("testing")
 
39
 
 
40
        obs, err := newEnvironObserver(s.State, nil)
 
41
        c.Assert(err, gc.IsNil)
 
42
 
 
43
        env := obs.Environ()
 
44
        c.Assert(env.Config().AllAttrs(), gc.DeepEquals, originalConfig.AllAttrs())
 
45
 
 
46
        var oldType string
 
47
        // Change to an invalid configuration and check
 
48
        // that the observer's environment remains the same.
 
49
        testing.ChangeEnvironConfig(c, s.State, func(attrs coretesting.Attrs) coretesting.Attrs {
 
50
                oldType = attrs["type"].(string)
 
51
                return attrs.Merge(coretesting.Attrs{
 
52
                        "type": "invalid",
 
53
                })
 
54
        })
 
55
        s.State.StartSync()
 
56
 
 
57
        // Wait for the observer to register the invalid environment
 
58
        timeout := time.After(coretesting.LongWait)
 
59
loop:
 
60
        for {
 
61
                select {
 
62
                case msg := <-logc:
 
63
                        if strings.Contains(msg, "error creating Environ") {
 
64
                                break loop
 
65
                        }
 
66
                case <-timeout:
 
67
                        c.Fatalf("timed out waiting to see broken environment")
 
68
                }
 
69
        }
 
70
        // Check that the returned environ is still the same.
 
71
        env = obs.Environ()
 
72
        c.Assert(env.Config().AllAttrs(), gc.DeepEquals, originalConfig.AllAttrs())
 
73
 
 
74
        // Change the environment back to a valid configuration
 
75
        // with a different name and check that we see it.
 
76
        testing.ChangeEnvironConfig(c, s.State, func(attrs coretesting.Attrs) coretesting.Attrs {
 
77
                return attrs.Merge(coretesting.Attrs{
 
78
                        "type": oldType,
 
79
                        "name": "a-new-name",
 
80
                })
 
81
        })
 
82
        s.State.StartSync()
 
83
 
 
84
        for a := coretesting.LongAttempt.Start(); a.Next(); {
 
85
                env := obs.Environ()
 
86
                if !a.HasNext() {
 
87
                        c.Fatalf("timed out waiting for new environ")
 
88
                }
 
89
                if env.Config().Name() == "a-new-name" {
 
90
                        break
 
91
                }
 
92
        }
 
93
}
 
94
 
 
95
type logChan chan string
 
96
 
 
97
func (logc logChan) Write(level loggo.Level, name, filename string, line int, timestamp time.Time, message string) {
 
98
        logc <- message
 
99
}