github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/common/modelwatcher_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 "github.com/juju/cmd/cmdtesting" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 "gopkg.in/juju/names.v2" 13 14 "github.com/juju/juju/apiserver/common" 15 "github.com/juju/juju/apiserver/params" 16 apiservertesting "github.com/juju/juju/apiserver/testing" 17 "github.com/juju/juju/cmd/modelcmd" 18 "github.com/juju/juju/environs/bootstrap" 19 "github.com/juju/juju/environs/config" 20 "github.com/juju/juju/jujuclient" 21 "github.com/juju/juju/provider/dummy" 22 "github.com/juju/juju/state" 23 "github.com/juju/juju/testing" 24 ) 25 26 type modelWatcherSuite struct { 27 testing.BaseSuite 28 29 testingEnvConfig *config.Config 30 } 31 32 var _ = gc.Suite(&modelWatcherSuite{}) 33 34 type fakeModelAccessor struct { 35 modelConfig *config.Config 36 modelConfigError error 37 } 38 39 func (*fakeModelAccessor) WatchForModelConfigChanges() state.NotifyWatcher { 40 return apiservertesting.NewFakeNotifyWatcher() 41 } 42 43 func (f *fakeModelAccessor) ModelConfig() (*config.Config, error) { 44 if f.modelConfigError != nil { 45 return nil, f.modelConfigError 46 } 47 return f.modelConfig, nil 48 } 49 50 func (s *modelWatcherSuite) TearDownTest(c *gc.C) { 51 dummy.Reset(c) 52 s.BaseSuite.TearDownTest(c) 53 } 54 55 func (s *modelWatcherSuite) TestWatchSuccess(c *gc.C) { 56 resources := common.NewResources() 57 s.AddCleanup(func(_ *gc.C) { resources.StopAll() }) 58 e := common.NewModelWatcher( 59 &fakeModelAccessor{}, 60 resources, 61 nil, 62 ) 63 result, err := e.WatchForModelConfigChanges() 64 c.Assert(err, jc.ErrorIsNil) 65 c.Assert(result, gc.DeepEquals, params.NotifyWatchResult{"1", nil}) 66 c.Assert(resources.Count(), gc.Equals, 1) 67 } 68 69 func (*modelWatcherSuite) TestModelConfigSuccess(c *gc.C) { 70 authorizer := apiservertesting.FakeAuthorizer{ 71 Tag: names.NewMachineTag("0"), 72 Controller: true, 73 } 74 testingModelConfig := testingEnvConfig(c) 75 e := common.NewModelWatcher( 76 &fakeModelAccessor{modelConfig: testingModelConfig}, 77 nil, 78 authorizer, 79 ) 80 result, err := e.ModelConfig() 81 c.Assert(err, jc.ErrorIsNil) 82 // Make sure we can read the secret attribute (i.e. it's not masked). 83 c.Check(result.Config["secret"], gc.Equals, "pork") 84 c.Check(map[string]interface{}(result.Config), jc.DeepEquals, testingModelConfig.AllAttrs()) 85 } 86 87 func (*modelWatcherSuite) TestModelConfigFetchError(c *gc.C) { 88 authorizer := apiservertesting.FakeAuthorizer{ 89 Tag: names.NewMachineTag("0"), 90 Controller: true, 91 } 92 e := common.NewModelWatcher( 93 &fakeModelAccessor{ 94 modelConfigError: fmt.Errorf("pow"), 95 }, 96 nil, 97 authorizer, 98 ) 99 _, err := e.ModelConfig() 100 c.Assert(err, gc.ErrorMatches, "pow") 101 } 102 103 func testingEnvConfig(c *gc.C) *config.Config { 104 env, err := bootstrap.PrepareController( 105 false, 106 modelcmd.BootstrapContext(cmdtesting.Context(c)), 107 jujuclient.NewMemStore(), 108 bootstrap.PrepareParams{ 109 ControllerConfig: testing.FakeControllerConfig(), 110 ControllerName: "dummycontroller", 111 ModelConfig: dummy.SampleConfig(), 112 Cloud: dummy.SampleCloudSpec(), 113 AdminSecret: "admin-secret", 114 }, 115 ) 116 c.Assert(err, jc.ErrorIsNil) 117 return env.Config() 118 }