github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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/state" 13 statetesting "github.com/juju/juju/state/testing" 14 ) 15 16 type ModelWatcher interface { 17 WatchForModelConfigChanges() (params.NotifyWatchResult, error) 18 ModelConfig() (params.ModelConfigResult, error) 19 } 20 21 type ModelWatcherTest struct { 22 modelWatcher ModelWatcher 23 st *state.State 24 resources *common.Resources 25 } 26 27 func NewModelWatcherTest( 28 modelWatcher ModelWatcher, 29 st *state.State, 30 resources *common.Resources, 31 ) *ModelWatcherTest { 32 return &ModelWatcherTest{modelWatcher, st, resources} 33 } 34 35 // AssertModelConfig provides a method to test the config from the 36 // envWatcher. This allows other tests that embed this type to have 37 // more than just the default test. 38 func (s *ModelWatcherTest) AssertModelConfig(c *gc.C, envWatcher ModelWatcher) { 39 envConfig, err := s.st.ModelConfig() 40 c.Assert(err, jc.ErrorIsNil) 41 42 result, err := envWatcher.ModelConfig() 43 c.Assert(err, jc.ErrorIsNil) 44 45 configAttributes := envConfig.AllAttrs() 46 c.Assert(result.Config, jc.DeepEquals, params.ModelConfig(configAttributes)) 47 } 48 49 func (s *ModelWatcherTest) TestModelConfig(c *gc.C) { 50 s.AssertModelConfig(c, s.modelWatcher) 51 } 52 53 func (s *ModelWatcherTest) TestWatchForModelConfigChanges(c *gc.C) { 54 c.Assert(s.resources.Count(), gc.Equals, 0) 55 56 result, err := s.modelWatcher.WatchForModelConfigChanges() 57 c.Assert(err, jc.ErrorIsNil) 58 c.Assert(result, gc.DeepEquals, params.NotifyWatchResult{ 59 NotifyWatcherId: "1", 60 }) 61 62 // Verify the resources were registered and stop them when done. 63 c.Assert(s.resources.Count(), gc.Equals, 1) 64 resource := s.resources.Get("1") 65 defer statetesting.AssertStop(c, resource) 66 67 // Check that the Watch has consumed the initial event ("returned" 68 // in the Watch call) 69 wc := statetesting.NewNotifyWatcherC(c, s.st, resource.(state.NotifyWatcher)) 70 wc.AssertNoChange() 71 }