github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/state/apiserver/common/environwatcher_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common_test 5 6 import ( 7 "fmt" 8 9 jc "github.com/juju/testing/checkers" 10 gc "launchpad.net/gocheck" 11 12 "github.com/juju/juju/environs" 13 "github.com/juju/juju/environs/config" 14 "github.com/juju/juju/environs/configstore" 15 "github.com/juju/juju/provider/dummy" 16 "github.com/juju/juju/state" 17 "github.com/juju/juju/state/api/params" 18 "github.com/juju/juju/state/apiserver/common" 19 "github.com/juju/juju/testing" 20 ) 21 22 type environWatcherSuite struct { 23 testing.BaseSuite 24 25 testingEnvConfig *config.Config 26 } 27 28 var _ = gc.Suite(&environWatcherSuite{}) 29 30 type fakeEnvironAccessor struct { 31 envConfig *config.Config 32 envConfigError error 33 } 34 35 func (*fakeEnvironAccessor) WatchForEnvironConfigChanges() state.NotifyWatcher { 36 changes := make(chan struct{}, 1) 37 // Simulate initial event. 38 changes <- struct{}{} 39 return &fakeNotifyWatcher{changes} 40 } 41 42 func (f *fakeEnvironAccessor) EnvironConfig() (*config.Config, error) { 43 if f.envConfigError != nil { 44 return nil, f.envConfigError 45 } 46 return f.envConfig, nil 47 } 48 49 func (s *environWatcherSuite) TearDownTest(c *gc.C) { 50 dummy.Reset() 51 s.BaseSuite.TearDownTest(c) 52 } 53 54 func (*environWatcherSuite) TestWatchSuccess(c *gc.C) { 55 getCanWatch := func() (common.AuthFunc, error) { 56 return func(tag string) bool { 57 return true 58 }, nil 59 } 60 resources := common.NewResources() 61 e := common.NewEnvironWatcher( 62 &fakeEnvironAccessor{}, 63 resources, 64 getCanWatch, 65 nil, 66 ) 67 result, err := e.WatchForEnvironConfigChanges() 68 c.Assert(err, gc.IsNil) 69 c.Assert(result, gc.DeepEquals, params.NotifyWatchResult{"1", nil}) 70 c.Assert(resources.Count(), gc.Equals, 1) 71 } 72 73 func (*environWatcherSuite) TestWatchGetAuthError(c *gc.C) { 74 getCanWatch := func() (common.AuthFunc, error) { 75 return nil, fmt.Errorf("pow") 76 } 77 resources := common.NewResources() 78 e := common.NewEnvironWatcher( 79 &fakeEnvironAccessor{}, 80 resources, 81 getCanWatch, 82 nil, 83 ) 84 _, err := e.WatchForEnvironConfigChanges() 85 c.Assert(err, gc.ErrorMatches, "pow") 86 c.Assert(resources.Count(), gc.Equals, 0) 87 } 88 89 func (*environWatcherSuite) TestWatchAuthError(c *gc.C) { 90 getCanWatch := func() (common.AuthFunc, error) { 91 return func(tag string) bool { 92 return false 93 }, nil 94 } 95 resources := common.NewResources() 96 e := common.NewEnvironWatcher( 97 &fakeEnvironAccessor{}, 98 resources, 99 getCanWatch, 100 nil, 101 ) 102 result, err := e.WatchForEnvironConfigChanges() 103 c.Assert(err, gc.ErrorMatches, "permission denied") 104 c.Assert(result, gc.DeepEquals, params.NotifyWatchResult{}) 105 c.Assert(resources.Count(), gc.Equals, 0) 106 } 107 108 func (*environWatcherSuite) TestEnvironConfigSuccess(c *gc.C) { 109 getCanReadSecrets := func() (common.AuthFunc, error) { 110 return func(tag string) bool { 111 return true 112 }, nil 113 } 114 115 testingEnvConfig := testingEnvConfig(c) 116 e := common.NewEnvironWatcher( 117 &fakeEnvironAccessor{envConfig: testingEnvConfig}, 118 nil, 119 nil, 120 getCanReadSecrets, 121 ) 122 result, err := e.EnvironConfig() 123 c.Assert(err, gc.IsNil) 124 // Make sure we can read the secret attribute (i.e. it's not masked). 125 c.Check(result.Config["secret"], gc.Equals, "pork") 126 c.Check(map[string]interface{}(result.Config), jc.DeepEquals, testingEnvConfig.AllAttrs()) 127 } 128 129 func (*environWatcherSuite) TestEnvironConfigFetchError(c *gc.C) { 130 getCanReadSecrets := func() (common.AuthFunc, error) { 131 return func(tag string) bool { 132 return true 133 }, nil 134 } 135 e := common.NewEnvironWatcher( 136 &fakeEnvironAccessor{ 137 envConfigError: fmt.Errorf("pow"), 138 }, 139 nil, 140 nil, 141 getCanReadSecrets, 142 ) 143 _, err := e.EnvironConfig() 144 c.Assert(err, gc.ErrorMatches, "pow") 145 } 146 147 func (*environWatcherSuite) TestEnvironConfigGetAuthError(c *gc.C) { 148 getCanReadSecrets := func() (common.AuthFunc, error) { 149 return nil, fmt.Errorf("pow") 150 } 151 e := common.NewEnvironWatcher( 152 &fakeEnvironAccessor{envConfig: testingEnvConfig(c)}, 153 nil, 154 nil, 155 getCanReadSecrets, 156 ) 157 _, err := e.EnvironConfig() 158 c.Assert(err, gc.ErrorMatches, "pow") 159 } 160 161 func (*environWatcherSuite) TestEnvironConfigReadSecretsFalse(c *gc.C) { 162 getCanReadSecrets := func() (common.AuthFunc, error) { 163 return func(tag string) bool { 164 return false 165 }, nil 166 } 167 168 testingEnvConfig := testingEnvConfig(c) 169 e := common.NewEnvironWatcher( 170 &fakeEnvironAccessor{envConfig: testingEnvConfig}, 171 nil, 172 nil, 173 getCanReadSecrets, 174 ) 175 result, err := e.EnvironConfig() 176 c.Assert(err, gc.IsNil) 177 // Make sure the secret attribute is masked. 178 c.Check(result.Config["secret"], gc.Equals, "not available") 179 // And only that is masked. 180 result.Config["secret"] = "pork" 181 c.Check(map[string]interface{}(result.Config), jc.DeepEquals, testingEnvConfig.AllAttrs()) 182 } 183 184 func testingEnvConfig(c *gc.C) *config.Config { 185 cfg, err := config.New(config.NoDefaults, dummy.SampleConfig()) 186 c.Assert(err, gc.IsNil) 187 env, err := environs.Prepare(cfg, testing.Context(c), configstore.NewMem()) 188 c.Assert(err, gc.IsNil) 189 return env.Config() 190 }