github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/apiserver/common/testing/environwatcher.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 gc "launchpad.net/gocheck" 8 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" 15 ) 16 17 const ( 18 HasSecrets = true 19 NoSecrets = false 20 ) 21 22 type EnvironmentWatcher interface { 23 WatchForEnvironConfigChanges() (params.NotifyWatchResult, error) 24 EnvironConfig() (params.EnvironConfigResult, error) 25 } 26 27 type EnvironWatcherTest struct { 28 envWatcher EnvironmentWatcher 29 st *state.State 30 resources *common.Resources 31 hasSecrets bool 32 } 33 34 func NewEnvironWatcherTest( 35 envWatcher EnvironmentWatcher, 36 st *state.State, 37 resources *common.Resources, 38 hasSecrets bool) *EnvironWatcherTest { 39 return &EnvironWatcherTest{envWatcher, st, resources, hasSecrets} 40 } 41 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) 48 49 result, err := envWatcher.EnvironConfig() 50 c.Assert(err, gc.IsNil) 51 c.Assert(result.Error, gc.IsNil) 52 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. 56 if !hasSecrets { 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" 63 } 64 } 65 66 c.Assert(result.Config, jc.DeepEquals, params.EnvironConfig(configAttributes)) 67 } 68 69 func (s *EnvironWatcherTest) TestEnvironConfig(c *gc.C) { 70 s.AssertEnvironConfig(c, s.envWatcher, s.hasSecrets) 71 } 72 73 func (s *EnvironWatcherTest) TestWatchForEnvironConfigChanges(c *gc.C) { 74 c.Assert(s.resources.Count(), gc.Equals, 0) 75 76 result, err := s.envWatcher.WatchForEnvironConfigChanges() 77 c.Assert(err, gc.IsNil) 78 c.Assert(result, gc.DeepEquals, params.NotifyWatchResult{ 79 NotifyWatcherId: "1", 80 }) 81 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) 86 87 // Check that the Watch has consumed the initial event ("returned" 88 // in the Watch call) 89 wc := statetesting.NewNotifyWatcherC(c, s.st, resource.(state.NotifyWatcher)) 90 wc.AssertNoChange() 91 }