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