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