func (s *InitializeSuite) TestInitialize(c *C) { _, err := s.State.EnvironConfig() c.Assert(err, checkers.Satisfies, errors.IsNotFoundError) _, err = s.State.Annotator("environment-foo") c.Assert(err, checkers.Satisfies, errors.IsNotFoundError) _, err = s.State.EnvironConstraints() c.Assert(err, checkers.Satisfies, errors.IsNotFoundError) cfg := testing.EnvironConfig(c) initial := cfg.AllAttrs() st, err := state.Initialize(state.TestingStateInfo(), cfg, state.TestingDialOpts()) c.Assert(err, IsNil) c.Assert(st, NotNil) err = st.Close() c.Assert(err, IsNil) cfg, err = s.State.EnvironConfig() c.Assert(err, IsNil) c.Assert(cfg.AllAttrs(), DeepEquals, initial) env, err := s.State.Annotator("environment-" + cfg.Name()) c.Assert(err, IsNil) annotations, err := env.Annotations() c.Assert(err, IsNil) c.Assert(annotations, HasLen, 0) cons, err := s.State.EnvironConstraints() c.Assert(err, IsNil) c.Assert(cons, DeepEquals, constraints.Value{}) }
func (s *InitializeSuite) SetUpTest(c *C) { s.LoggingSuite.SetUpTest(c) s.MgoSuite.SetUpTest(c) var err error s.State, err = state.Open(state.TestingStateInfo(), state.TestingDialOpts()) c.Assert(err, IsNil) }
func (s *InitializeSuite) TestEnvironConfigWithAdminSecret(c *C) { // admin-secret blocks Initialize. good := testing.EnvironConfig(c) bad, err := good.Apply(map[string]interface{}{"admin-secret": "foo"}) _, err = state.Initialize(state.TestingStateInfo(), bad, state.TestingDialOpts()) c.Assert(err, ErrorMatches, "admin-secret should never be written to the state") // admin-secret blocks SetEnvironConfig. st := state.TestingInitialize(c, good) st.Close() err = s.State.SetEnvironConfig(bad) c.Assert(err, ErrorMatches, "admin-secret should never be written to the state") // EnvironConfig remains inviolate. cfg, err := s.State.EnvironConfig() c.Assert(err, IsNil) c.Assert(cfg.AllAttrs(), DeepEquals, good.AllAttrs()) }
func (s *InitializeSuite) TestDoubleInitializeConfig(c *C) { cfg := testing.EnvironConfig(c) initial := cfg.AllAttrs() st := state.TestingInitialize(c, cfg) st.Close() // A second initialize returns an open *State, but ignores its params. // TODO(fwereade) I think this is crazy, but it's what we were testing // for originally... cfg, err := cfg.Apply(map[string]interface{}{"authorized-keys": "something-else"}) c.Assert(err, IsNil) st, err = state.Initialize(state.TestingStateInfo(), cfg, state.TestingDialOpts()) c.Assert(err, IsNil) c.Assert(st, NotNil) st.Close() cfg, err = s.State.EnvironConfig() c.Assert(err, IsNil) c.Assert(cfg.AllAttrs(), DeepEquals, initial) }
func (s *InitializeSuite) TestEnvironConfigWithoutAgentVersion(c *C) { // admin-secret blocks Initialize. good := testing.EnvironConfig(c) attrs := good.AllAttrs() delete(attrs, "agent-version") bad, err := config.New(attrs) c.Assert(err, IsNil) _, err = state.Initialize(state.TestingStateInfo(), bad, state.TestingDialOpts()) c.Assert(err, ErrorMatches, "agent-version must always be set in state") // Bad agent-version blocks SetEnvironConfig. st := state.TestingInitialize(c, good) st.Close() err = s.State.SetEnvironConfig(bad) c.Assert(err, ErrorMatches, "agent-version must always be set in state") // EnvironConfig remains inviolate. cfg, err := s.State.EnvironConfig() c.Assert(err, IsNil) c.Assert(cfg.AllAttrs(), DeepEquals, good.AllAttrs()) }
func (s *UnitSuite) TestSetMongoPasswordOnUnitAfterConnectingAsMachineEntity(c *C) { // Make a second unit to use later. (Subordinate units can only be created // as a side-effect of a principal entering relation scope.) subCharm := s.AddTestingCharm(c, "logging") _, err := s.State.AddService("logging", subCharm) c.Assert(err, IsNil) eps, err := s.State.InferEndpoints([]string{"wordpress", "logging"}) c.Assert(err, IsNil) rel, err := s.State.AddRelation(eps...) c.Assert(err, IsNil) ru, err := rel.Unit(s.unit) c.Assert(err, IsNil) err = ru.EnterScope(nil) c.Assert(err, IsNil) subUnit, err := s.State.Unit("logging/0") c.Assert(err, IsNil) info := state.TestingStateInfo() st, err := state.Open(info, state.TestingDialOpts()) c.Assert(err, IsNil) defer st.Close() // Turn on fully-authenticated mode. err = st.SetAdminMongoPassword("admin-secret") c.Assert(err, IsNil) // Add a new machine, assign the units to it // and set its password. m, err := st.AddMachine("series", state.JobHostUnits) c.Assert(err, IsNil) unit, err := st.Unit(s.unit.Name()) c.Assert(err, IsNil) subUnit, err = st.Unit(subUnit.Name()) c.Assert(err, IsNil) err = unit.AssignToMachine(m) c.Assert(err, IsNil) err = m.SetMongoPassword("foo") c.Assert(err, IsNil) // Sanity check that we cannot connect with the wrong // password info.Tag = m.Tag() info.Password = "******" err = tryOpenState(info) c.Assert(err, checkers.Satisfies, errors.IsUnauthorizedError) // Connect as the machine entity. info.Tag = m.Tag() info.Password = "******" st1, err := state.Open(info, state.TestingDialOpts()) c.Assert(err, IsNil) defer st1.Close() // Change the password for a unit derived from // the machine entity's state. unit, err = st1.Unit(s.unit.Name()) c.Assert(err, IsNil) err = unit.SetMongoPassword("bar") c.Assert(err, IsNil) // Now connect as the unit entity and, as that // that entity, change the password for a new unit. info.Tag = unit.Tag() info.Password = "******" st2, err := state.Open(info, state.TestingDialOpts()) c.Assert(err, IsNil) defer st2.Close() // Check that we can set its password. unit, err = st2.Unit(subUnit.Name()) c.Assert(err, IsNil) err = unit.SetMongoPassword("bar2") c.Assert(err, IsNil) // Clear the admin password, so tests can reset the db. err = st.SetAdminMongoPassword("") c.Assert(err, IsNil) }