1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
gc "launchpad.net/gocheck"
9
"launchpad.net/juju-core/environs"
10
"launchpad.net/juju-core/state"
11
"launchpad.net/juju-core/state/api/params"
12
"launchpad.net/juju-core/state/apiserver/common"
13
statetesting "launchpad.net/juju-core/state/testing"
14
jc "launchpad.net/juju-core/testing/checkers"
22
type EnvironmentWatcher interface {
23
WatchForEnvironConfigChanges() (params.NotifyWatchResult, error)
24
EnvironConfig() (params.EnvironConfigResult, error)
27
type EnvironWatcherTest struct {
28
envWatcher EnvironmentWatcher
30
resources *common.Resources
34
func NewEnvironWatcherTest(
35
envWatcher EnvironmentWatcher,
37
resources *common.Resources,
38
hasSecrets bool) *EnvironWatcherTest {
39
return &EnvironWatcherTest{envWatcher, st, resources, hasSecrets}
42
// AssertEnvironConfig provides a method to test the config from the
43
// envWatcher. This allows other tests that embed this type to have
44
// more than just the default test.
45
func (s *EnvironWatcherTest) AssertEnvironConfig(c *gc.C, envWatcher EnvironmentWatcher, hasSecrets bool) {
46
envConfig, err := s.st.EnvironConfig()
47
c.Assert(err, gc.IsNil)
49
result, err := envWatcher.EnvironConfig()
50
c.Assert(err, gc.IsNil)
51
c.Assert(result.Error, gc.IsNil)
53
configAttributes := envConfig.AllAttrs()
54
// If the implementor doesn't provide secrets, we need to replace the config
55
// values in our environment to compare against with the secrets replaced.
57
env, err := environs.New(envConfig)
58
c.Assert(err, gc.IsNil)
59
secretAttrs, err := env.Provider().SecretAttrs(envConfig)
60
c.Assert(err, gc.IsNil)
61
for key := range secretAttrs {
62
configAttributes[key] = "not available"
66
c.Assert(result.Config, jc.DeepEquals, params.EnvironConfig(configAttributes))
69
func (s *EnvironWatcherTest) TestEnvironConfig(c *gc.C) {
70
s.AssertEnvironConfig(c, s.envWatcher, s.hasSecrets)
73
func (s *EnvironWatcherTest) TestWatchForEnvironConfigChanges(c *gc.C) {
74
c.Assert(s.resources.Count(), gc.Equals, 0)
76
result, err := s.envWatcher.WatchForEnvironConfigChanges()
77
c.Assert(err, gc.IsNil)
78
c.Assert(result, gc.DeepEquals, params.NotifyWatchResult{
82
// Verify the resources were registered and stop them when done.
83
c.Assert(s.resources.Count(), gc.Equals, 1)
84
resource := s.resources.Get("1")
85
defer statetesting.AssertStop(c, resource)
87
// Check that the Watch has consumed the initial event ("returned"
89
wc := statetesting.NewNotifyWatcherC(c, s.st, resource.(state.NotifyWatcher))