github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/common/testing/modelwatcher.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 jc "github.com/juju/testing/checkers" 8 gc "gopkg.in/check.v1" 9 10 "github.com/juju/juju/apiserver/common" 11 "github.com/juju/juju/apiserver/params" 12 "github.com/juju/juju/environs" 13 "github.com/juju/juju/state" 14 statetesting "github.com/juju/juju/state/testing" 15 ) 16 17 const ( 18 HasSecrets = true 19 NoSecrets = false 20 ) 21 22 type ModelWatcher interface { 23 WatchForModelConfigChanges() (params.NotifyWatchResult, error) 24 ModelConfig() (params.ModelConfigResult, error) 25 } 26 27 type ModelWatcherTest struct { 28 modelWatcher ModelWatcher 29 st *state.State 30 resources *common.Resources 31 hasSecrets bool 32 } 33 34 func NewModelWatcherTest( 35 modelWatcher ModelWatcher, 36 st *state.State, 37 resources *common.Resources, 38 hasSecrets bool) *ModelWatcherTest { 39 return &ModelWatcherTest{modelWatcher, st, resources, hasSecrets} 40 } 41 42 // AssertModelConfig 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 *ModelWatcherTest) AssertModelConfig(c *gc.C, envWatcher ModelWatcher, hasSecrets bool) { 46 envConfig, err := s.st.ModelConfig() 47 c.Assert(err, jc.ErrorIsNil) 48 49 result, err := envWatcher.ModelConfig() 50 c.Assert(err, jc.ErrorIsNil) 51 52 configAttributes := envConfig.AllAttrs() 53 // If the implementor doesn't provide secrets, we need to replace the config 54 // values in our environment to compare against with the secrets replaced. 55 if !hasSecrets { 56 env, err := environs.New(envConfig) 57 c.Assert(err, jc.ErrorIsNil) 58 secretAttrs, err := env.Provider().SecretAttrs(envConfig) 59 c.Assert(err, jc.ErrorIsNil) 60 for key := range secretAttrs { 61 configAttributes[key] = "not available" 62 } 63 } 64 65 c.Assert(result.Config, jc.DeepEquals, params.ModelConfig(configAttributes)) 66 } 67 68 func (s *ModelWatcherTest) TestModelConfig(c *gc.C) { 69 s.AssertModelConfig(c, s.modelWatcher, s.hasSecrets) 70 } 71 72 func (s *ModelWatcherTest) TestWatchForModelConfigChanges(c *gc.C) { 73 c.Assert(s.resources.Count(), gc.Equals, 0) 74 75 result, err := s.modelWatcher.WatchForModelConfigChanges() 76 c.Assert(err, jc.ErrorIsNil) 77 c.Assert(result, gc.DeepEquals, params.NotifyWatchResult{ 78 NotifyWatcherId: "1", 79 }) 80 81 // Verify the resources were registered and stop them when done. 82 c.Assert(s.resources.Count(), gc.Equals, 1) 83 resource := s.resources.Get("1") 84 defer statetesting.AssertStop(c, resource) 85 86 // Check that the Watch has consumed the initial event ("returned" 87 // in the Watch call) 88 wc := statetesting.NewNotifyWatcherC(c, s.st, resource.(state.NotifyWatcher)) 89 wc.AssertNoChange() 90 }