github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/initialize_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state_test 5 6 import ( 7 "github.com/juju/names" 8 gitjujutesting "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/constraints" 13 "github.com/juju/juju/environs/config" 14 "github.com/juju/juju/state" 15 statetesting "github.com/juju/juju/state/testing" 16 "github.com/juju/juju/testing" 17 ) 18 19 type InitializeSuite struct { 20 gitjujutesting.MgoSuite 21 testing.BaseSuite 22 State *state.State 23 } 24 25 var _ = gc.Suite(&InitializeSuite{}) 26 27 func (s *InitializeSuite) SetUpSuite(c *gc.C) { 28 s.BaseSuite.SetUpSuite(c) 29 s.MgoSuite.SetUpSuite(c) 30 } 31 32 func (s *InitializeSuite) TearDownSuite(c *gc.C) { 33 s.MgoSuite.TearDownSuite(c) 34 s.BaseSuite.TearDownSuite(c) 35 } 36 37 func (s *InitializeSuite) SetUpTest(c *gc.C) { 38 s.BaseSuite.SetUpTest(c) 39 s.MgoSuite.SetUpTest(c) 40 } 41 42 func (s *InitializeSuite) openState(c *gc.C, modelTag names.ModelTag) { 43 st, err := state.Open( 44 modelTag, 45 statetesting.NewMongoInfo(), 46 statetesting.NewDialOpts(), 47 state.Policy(nil), 48 ) 49 c.Assert(err, jc.ErrorIsNil) 50 s.State = st 51 } 52 53 func (s *InitializeSuite) TearDownTest(c *gc.C) { 54 if s.State != nil { 55 err := s.State.Close() 56 c.Check(err, jc.ErrorIsNil) 57 } 58 s.MgoSuite.TearDownTest(c) 59 s.BaseSuite.TearDownTest(c) 60 } 61 62 func (s *InitializeSuite) TestInitialize(c *gc.C) { 63 cfg := testing.ModelConfig(c) 64 uuid := cfg.UUID() 65 initial := cfg.AllAttrs() 66 owner := names.NewLocalUserTag("initialize-admin") 67 st, err := state.Initialize(owner, statetesting.NewMongoInfo(), cfg, statetesting.NewDialOpts(), nil) 68 c.Assert(err, jc.ErrorIsNil) 69 c.Assert(st, gc.NotNil) 70 modelTag := st.ModelTag() 71 c.Assert(modelTag.Id(), gc.Equals, uuid) 72 err = st.Close() 73 c.Assert(err, jc.ErrorIsNil) 74 75 s.openState(c, modelTag) 76 77 cfg, err = s.State.ModelConfig() 78 c.Assert(err, jc.ErrorIsNil) 79 c.Assert(cfg.AllAttrs(), gc.DeepEquals, initial) 80 // Check that the model has been created. 81 env, err := s.State.Model() 82 c.Assert(err, jc.ErrorIsNil) 83 c.Assert(env.Tag(), gc.Equals, modelTag) 84 // Check that the owner has been created. 85 c.Assert(env.Owner(), gc.Equals, owner) 86 // Check that the owner can be retrieved by the tag. 87 entity, err := s.State.FindEntity(env.Owner()) 88 c.Assert(err, jc.ErrorIsNil) 89 c.Assert(entity.Tag(), gc.Equals, owner) 90 // Check that the owner has an ModelUser created for the bootstrapped model. 91 modelUser, err := s.State.ModelUser(env.Owner()) 92 c.Assert(err, jc.ErrorIsNil) 93 c.Assert(modelUser.UserTag(), gc.Equals, owner) 94 c.Assert(modelUser.ModelTag(), gc.Equals, env.Tag()) 95 96 // Check that the model can be found through the tag. 97 entity, err = s.State.FindEntity(modelTag) 98 c.Assert(err, jc.ErrorIsNil) 99 cons, err := s.State.ModelConstraints() 100 c.Assert(err, jc.ErrorIsNil) 101 c.Assert(&cons, jc.Satisfies, constraints.IsEmpty) 102 103 addrs, err := s.State.APIHostPorts() 104 c.Assert(err, jc.ErrorIsNil) 105 c.Assert(addrs, gc.HasLen, 0) 106 107 info, err := s.State.ControllerInfo() 108 c.Assert(err, jc.ErrorIsNil) 109 c.Assert(info, jc.DeepEquals, &state.ControllerInfo{ModelTag: modelTag}) 110 } 111 112 func (s *InitializeSuite) TestDoubleInitializeConfig(c *gc.C) { 113 cfg := testing.ModelConfig(c) 114 owner := names.NewLocalUserTag("initialize-admin") 115 116 mgoInfo := statetesting.NewMongoInfo() 117 dialOpts := statetesting.NewDialOpts() 118 st, err := state.Initialize(owner, mgoInfo, cfg, dialOpts, state.Policy(nil)) 119 c.Assert(err, jc.ErrorIsNil) 120 err = st.Close() 121 c.Check(err, jc.ErrorIsNil) 122 123 st, err = state.Initialize(owner, mgoInfo, cfg, dialOpts, state.Policy(nil)) 124 c.Check(err, gc.ErrorMatches, "already initialized") 125 if !c.Check(st, gc.IsNil) { 126 err = st.Close() 127 c.Check(err, jc.ErrorIsNil) 128 } 129 } 130 131 func (s *InitializeSuite) TestModelConfigWithAdminSecret(c *gc.C) { 132 // admin-secret blocks Initialize. 133 good := testing.ModelConfig(c) 134 badUpdateAttrs := map[string]interface{}{"admin-secret": "foo"} 135 bad, err := good.Apply(badUpdateAttrs) 136 owner := names.NewLocalUserTag("initialize-admin") 137 138 _, err = state.Initialize(owner, statetesting.NewMongoInfo(), bad, statetesting.NewDialOpts(), state.Policy(nil)) 139 c.Assert(err, gc.ErrorMatches, "admin-secret should never be written to the state") 140 141 // admin-secret blocks UpdateModelConfig. 142 st := statetesting.Initialize(c, owner, good, nil) 143 st.Close() 144 145 s.openState(c, st.ModelTag()) 146 err = s.State.UpdateModelConfig(badUpdateAttrs, nil, nil) 147 c.Assert(err, gc.ErrorMatches, "admin-secret should never be written to the state") 148 149 // ModelConfig remains inviolate. 150 cfg, err := s.State.ModelConfig() 151 c.Assert(err, jc.ErrorIsNil) 152 c.Assert(cfg.AllAttrs(), gc.DeepEquals, good.AllAttrs()) 153 } 154 155 func (s *InitializeSuite) TestModelConfigWithoutAgentVersion(c *gc.C) { 156 // admin-secret blocks Initialize. 157 good := testing.ModelConfig(c) 158 attrs := good.AllAttrs() 159 delete(attrs, "agent-version") 160 bad, err := config.New(config.NoDefaults, attrs) 161 c.Assert(err, jc.ErrorIsNil) 162 owner := names.NewLocalUserTag("initialize-admin") 163 164 _, err = state.Initialize(owner, statetesting.NewMongoInfo(), bad, statetesting.NewDialOpts(), state.Policy(nil)) 165 c.Assert(err, gc.ErrorMatches, "agent-version must always be set in state") 166 167 st := statetesting.Initialize(c, owner, good, nil) 168 // yay side effects 169 st.Close() 170 171 s.openState(c, st.ModelTag()) 172 err = s.State.UpdateModelConfig(map[string]interface{}{}, []string{"agent-version"}, nil) 173 c.Assert(err, gc.ErrorMatches, "agent-version must always be set in state") 174 175 // ModelConfig remains inviolate. 176 cfg, err := s.State.ModelConfig() 177 c.Assert(err, jc.ErrorIsNil) 178 c.Assert(cfg.AllAttrs(), gc.DeepEquals, good.AllAttrs()) 179 }