func (s *ConfigSuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) cfg, err := testing.EnvironConfig(c).Apply(vsphere.ConfigAttrs) c.Assert(err, jc.ErrorIsNil) s.config = cfg }
func (s *InitializeSuite) TestEnvironConfigWithoutAgentVersion(c *gc.C) { // admin-secret blocks Initialize. good := testing.EnvironConfig(c) attrs := good.AllAttrs() delete(attrs, "agent-version") bad, err := config.New(config.NoDefaults, attrs) c.Assert(err, jc.ErrorIsNil) owner := names.NewLocalUserTag("initialize-admin") _, err = state.Initialize(owner, statetesting.NewMongoInfo(), bad, statetesting.NewDialOpts(), state.Policy(nil)) c.Assert(err, gc.ErrorMatches, "agent-version must always be set in state") st := statetesting.Initialize(c, owner, good, nil) // yay side effects st.Close() s.openState(c, st.EnvironTag()) err = s.State.UpdateEnvironConfig(map[string]interface{}{}, []string{"agent-version"}, nil) c.Assert(err, gc.ErrorMatches, "agent-version must always be set in state") // EnvironConfig remains inviolate. cfg, err := s.State.EnvironConfig() c.Assert(err, jc.ErrorIsNil) c.Assert(cfg.AllAttrs(), gc.DeepEquals, good.AllAttrs()) }
func (s *upgradesSuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) s.MgoSuite.SetUpTest(c) var err error s.state, err = Initialize(TestingMongoInfo(), testing.EnvironConfig(c), TestingDialOpts(), Policy(nil)) c.Assert(err, gc.IsNil) }
func (s *InitializeSuite) TestInitialize(c *gc.C) { cfg := testing.EnvironConfig(c) initial := cfg.AllAttrs() st, err := state.Initialize(state.TestingStateInfo(), cfg, state.TestingDialOpts(), state.Policy(nil)) c.Assert(err, gc.IsNil) c.Assert(st, gc.NotNil) err = st.Close() c.Assert(err, gc.IsNil) s.openState(c) cfg, err = s.State.EnvironConfig() c.Assert(err, gc.IsNil) c.Assert(cfg.AllAttrs(), gc.DeepEquals, initial) env, err := s.State.Environment() c.Assert(err, gc.IsNil) entity, err := s.State.FindEntity("environment-" + env.UUID()) c.Assert(err, gc.IsNil) annotator := entity.(state.Annotator) annotations, err := annotator.Annotations() c.Assert(err, gc.IsNil) c.Assert(annotations, gc.HasLen, 0) cons, err := s.State.EnvironConstraints() c.Assert(err, gc.IsNil) c.Assert(&cons, jc.Satisfies, constraints.IsEmpty) addrs, err := s.State.APIHostPorts() c.Assert(err, gc.IsNil) c.Assert(addrs, gc.HasLen, 0) info, err := s.State.StateServerInfo() c.Assert(err, gc.IsNil) c.Assert(info, jc.DeepEquals, &state.StateServerInfo{}) }
func (s *BaseSuite) initEnv(c *gc.C) { cfg, err := testing.EnvironConfig(c).Apply(ConfigAttrs) c.Assert(err, jc.ErrorIsNil) env, err := environs.New(cfg) c.Assert(err, jc.ErrorIsNil) s.Env = env.(*environ) s.setConfig(c, cfg) }
// TestingInitialize initializes the state and returns it. If state was not // already initialized, and cfg is nil, the minimal default environment // configuration will be used. func TestingInitialize(c *gc.C, cfg *config.Config, policy Policy) *State { if cfg == nil { cfg = testing.EnvironConfig(c) } st, err := Initialize(TestingStateInfo(), cfg, TestingDialOpts(), policy) c.Assert(err, gc.IsNil) return st }
func (s *ConfigSuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) cfg, err := testing.EnvironConfig(c).Apply(gce.ConfigAttrs) c.Assert(err, jc.ErrorIsNil) s.config = cfg s.rootDir = c.MkDir() }
func (s *storeManagerStateSuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) s.MgoSuite.SetUpTest(c) st, err := Initialize(TestingMongoInfo(), testing.EnvironConfig(c), TestingDialOpts(), nil) c.Assert(err, gc.IsNil) s.State = st s.State.AddAdminUser("pass") }
func (s *BaseSuiteUnpatched) NewConfig(c *gc.C, updates testing.Attrs) *config.Config { var err error cfg := testing.EnvironConfig(c) cfg, err = cfg.Apply(ConfigAttrs) c.Assert(err, jc.ErrorIsNil) cfg, err = cfg.Apply(updates) c.Assert(err, jc.ErrorIsNil) return cfg }
func (s *storeManagerStateSuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) s.MgoSuite.SetUpTest(c) st, err := Initialize(TestingMongoInfo(), testing.EnvironConfig(c), TestingDialOpts(), nil) c.Assert(err, gc.IsNil) s.State = st env, err := st.Environment() c.Assert(err, gc.IsNil) s.owner = env.Owner() }
func (s *compatSuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) s.MgoSuite.SetUpTest(c) st, err := Initialize(TestingMongoInfo(), testing.EnvironConfig(c), TestingDialOpts(), nil) c.Assert(err, gc.IsNil) s.state = st env, err := s.state.Environment() c.Assert(err, gc.IsNil) s.env = env }
// Initialize initializes the state and returns it. If state was not // already initialized, and cfg is nil, the minimal default environment // configuration will be used. func Initialize(c *gc.C, owner names.UserTag, cfg *config.Config, policy state.Policy) *state.State { if cfg == nil { cfg = testing.EnvironConfig(c) } mgoInfo := NewMongoInfo() dialOpts := NewDialOpts() st, err := state.Initialize(owner, mgoInfo, cfg, dialOpts, policy) c.Assert(err, jc.ErrorIsNil) return st }
func (s *SettingsSuite) SetUpTest(c *gc.C) { s.MgoSuite.SetUpTest(c) s.BaseSuite.SetUpTest(c) // TODO(dfc) this logic is duplicated with the metawatcher_test. cfg := testing.EnvironConfig(c) state, err := Initialize(TestingMongoInfo(), cfg, TestingDialOpts(), Policy(nil)) c.Assert(err, gc.IsNil) s.AddCleanup(func(*gc.C) { state.Close() }) s.state = state s.key = "config" }
func (ts configTestSpec) newConfig(c *gc.C) *config.Config { filename := ts.writeAuthFile(c) attrs := ts.attrs() if filename != "" { attrs["auth-file"] = filename } cfg, err := testing.EnvironConfig(c).Apply(attrs) c.Assert(err, jc.ErrorIsNil) return cfg }
func (s *InitializeSuite) TestInitialize(c *gc.C) { cfg := testing.EnvironConfig(c) uuid, _ := cfg.UUID() initial := cfg.AllAttrs() st, err := state.Initialize(state.TestingMongoInfo(), cfg, state.TestingDialOpts(), nil) c.Assert(err, gc.IsNil) c.Assert(st, gc.NotNil) envTag := st.EnvironTag() c.Assert(envTag.Id(), gc.Equals, uuid) err = st.Close() c.Assert(err, gc.IsNil) s.openState(c) cfg, err = s.State.EnvironConfig() c.Assert(err, gc.IsNil) c.Assert(cfg.AllAttrs(), gc.DeepEquals, initial) // Check that the environment has been created. env, err := s.State.Environment() c.Assert(err, gc.IsNil) c.Assert(env.Tag(), gc.Equals, envTag) // Check that the owner has been created. owner := names.NewLocalUserTag("admin") c.Assert(env.Owner(), gc.Equals, owner) // Check that the owner can be retrieved by the tag. entity, err := s.State.FindEntity(env.Owner()) c.Assert(err, gc.IsNil) c.Assert(entity.Tag(), gc.Equals, owner) // Check that the owner has an EnvUser created for the bootstrapped environment. envUser, err := s.State.EnvironmentUser(env.Owner()) c.Assert(err, gc.IsNil) c.Assert(envUser.UserTag().Username(), gc.Equals, env.Owner().Username()) c.Assert(envUser.EnvironmentTag(), gc.Equals, env.Tag()) // Check that the environment can be found through the tag. entity, err = s.State.FindEntity(envTag) c.Assert(err, gc.IsNil) annotator := entity.(state.Annotator) annotations, err := annotator.Annotations() c.Assert(err, gc.IsNil) c.Assert(annotations, gc.HasLen, 0) cons, err := s.State.EnvironConstraints() c.Assert(err, gc.IsNil) c.Assert(&cons, jc.Satisfies, constraints.IsEmpty) addrs, err := s.State.APIHostPorts() c.Assert(err, gc.IsNil) c.Assert(addrs, gc.HasLen, 0) info, err := s.State.StateServerInfo() c.Assert(err, gc.IsNil) c.Assert(info, jc.DeepEquals, &state.StateServerInfo{EnvironmentTag: envTag}) }
func (s *AddresserSuite) TestEnvironConfigSuccess(c *gc.C) { config := coretesting.EnvironConfig(c) s.st.setConfig(c, config) result, err := s.api.EnvironConfig() c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.EnvironConfigResult{ Config: config.AllAttrs(), }) s.st.stub.CheckCallNames(c, "EnvironConfig") }
func (s *InstancePollerSuite) TestEnvironConfigSuccess(c *gc.C) { envConfig := coretesting.EnvironConfig(c) s.st.SetConfig(c, envConfig) result, err := s.api.EnvironConfig() c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.EnvironConfigResult{ Config: envConfig.AllAttrs(), }) s.st.CheckCallNames(c, "EnvironConfig") }
func (s *InstancePollerSuite) TestEnvironConfigSuccess(c *gc.C) { var called int expectedConfig := coretesting.EnvironConfig(c) expectedResults := params.EnvironConfigResult{ Config: params.EnvironConfig(expectedConfig.AllAttrs()), } apiCaller := successAPICaller(c, "EnvironConfig", nil, expectedResults, &called) api := instancepoller.NewAPI(apiCaller) cfg, err := api.EnvironConfig() c.Assert(err, jc.ErrorIsNil) c.Assert(called, gc.Equals, 1) c.Assert(cfg, jc.DeepEquals, expectedConfig) }
func (s *InitializeSuite) TestDoubleInitializeConfig(c *gc.C) { cfg := testing.EnvironConfig(c) owner := names.NewLocalUserTag("initialize-admin") mgoInfo := statetesting.NewMongoInfo() dialOpts := statetesting.NewDialOpts() st, err := state.Initialize(owner, mgoInfo, cfg, dialOpts, state.Policy(nil)) c.Assert(err, jc.ErrorIsNil) st.Close() _, err = state.Initialize(owner, mgoInfo, cfg, dialOpts, state.Policy(nil)) c.Assert(err, gc.ErrorMatches, "already initialized") }
func (s *BaseSuiteUnpatched) NewConfig(c *gc.C, updates testing.Attrs) *config.Config { if updates == nil { updates = make(testing.Attrs) } var err error cfg := testing.EnvironConfig(c) cfg, err = cfg.Apply(ConfigAttrs) c.Assert(err, jc.ErrorIsNil) if raw := updates[cfgNamespace]; raw == nil || raw.(string) == "" { updates[cfgNamespace] = cfg.Name() } cfg, err = cfg.Apply(updates) c.Assert(err, jc.ErrorIsNil) return cfg }
func NewBaseConfig(c *gc.C) *config.Config { var err error cfg := testing.EnvironConfig(c) cfg, err = cfg.Apply(ConfigAttrs) c.Assert(err, jc.ErrorIsNil) cfg, err = cfg.Apply(map[string]interface{}{ // Default the namespace to the env name. cfgNamespace: cfg.Name(), }) c.Assert(err, jc.ErrorIsNil) return cfg }
func (s *provisionerSuite) TestEnvironConfig(c *gc.C) { inputCfg := coretesting.EnvironConfig(c) apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { c.Check(objType, gc.Equals, "StorageProvisioner") c.Check(version, gc.Equals, 0) c.Check(id, gc.Equals, "") c.Check(request, gc.Equals, "EnvironConfig") c.Assert(result, gc.FitsTypeOf, ¶ms.EnvironConfigResult{}) *(result.(*params.EnvironConfigResult)) = params.EnvironConfigResult{ Config: inputCfg.AllAttrs(), } return nil }) st := storageprovisioner.NewState(apiCaller, names.NewMachineTag("123")) outputCfg, err := st.EnvironConfig() c.Assert(err, jc.ErrorIsNil) c.Assert(outputCfg.AllAttrs(), jc.DeepEquals, inputCfg.AllAttrs()) }
func (s *factorySuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) s.MgoSuite.SetUpTest(c) policy := statetesting.MockPolicy{} info := &state.Info{ Info: mongo.Info{ Addrs: []string{jtesting.MgoServer.Addr()}, CACert: testing.CACert, }, } opts := mongo.DialOpts{ Timeout: testing.LongWait, } cfg := testing.EnvironConfig(c) st, err := state.Initialize(info, cfg, opts, &policy) c.Assert(err, gc.IsNil) s.State = st s.Factory = factory.NewFactory(s.State, c) }
func (s *internalStateSuite) SetUpTest(c *gc.C) { s.MgoSuite.SetUpTest(c) s.BaseSuite.SetUpTest(c) s.owner = names.NewLocalUserTag("test-admin") // Copied from NewMongoInfo (due to import loops). info := &mongo.MongoInfo{ Info: mongo.Info{ Addrs: []string{jujutesting.MgoServer.Addr()}, CACert: testing.CACert, }, } // Copied from NewDialOpts (due to import loops). dialopts := mongo.DialOpts{ Timeout: testing.LongWait, } st, err := Initialize(s.owner, info, testing.EnvironConfig(c), dialopts, nil) c.Assert(err, jc.ErrorIsNil) s.state = st s.AddCleanup(func(*gc.C) { s.state.Close() }) }
func (s *InitializeSuite) TestDoubleInitializeConfig(c *gc.C) { cfg := testing.EnvironConfig(c) initial := cfg.AllAttrs() st := TestingInitialize(c, cfg, nil) 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, gc.IsNil) st, err = state.Initialize(state.TestingMongoInfo(), cfg, state.TestingDialOpts(), state.Policy(nil)) c.Assert(err, gc.IsNil) c.Assert(st, gc.NotNil) st.Close() s.openState(c) cfg, err = s.State.EnvironConfig() c.Assert(err, gc.IsNil) c.Assert(cfg.AllAttrs(), gc.DeepEquals, initial) }
func (s *upgradeEnvironConfigSuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) s.stub = testing.Stub{} s.cfg = coretesting.EnvironConfig(c) s.registry = &mockProviderRegistry{ providers: make(map[string]environs.EnvironProvider), } s.reader = environConfigFunc(func() (*config.Config, error) { s.stub.AddCall("EnvironConfig") return s.cfg, s.stub.NextErr() }) s.updater = updateEnvironConfigFunc(func( update map[string]interface{}, remove []string, validate state.ValidateConfigFunc, ) error { s.stub.AddCall("UpdateEnvironConfig", update, remove, validate) return s.stub.NextErr() }) }
func (cs *ConnSuite) SetUpTest(c *gc.C) { c.Log("SetUpTest") cs.BaseSuite.SetUpTest(c) cs.MgoSuite.SetUpTest(c) cs.policy = statetesting.MockPolicy{} cfg := testing.EnvironConfig(c) cs.State = TestingInitialize(c, cfg, &cs.policy) uuid, ok := cfg.UUID() c.Assert(ok, jc.IsTrue) cs.envTag = names.NewEnvironTag(uuid) cs.annotations = cs.MgoSuite.Session.DB("juju").C("annotations") cs.charms = cs.MgoSuite.Session.DB("juju").C("charms") cs.machines = cs.MgoSuite.Session.DB("juju").C("machines") cs.relations = cs.MgoSuite.Session.DB("juju").C("relations") cs.services = cs.MgoSuite.Session.DB("juju").C("services") cs.units = cs.MgoSuite.Session.DB("juju").C("units") cs.stateServers = cs.MgoSuite.Session.DB("juju").C("stateServers") cs.State.AddAdminUser("pass") cs.factory = factory.NewFactory(cs.State) c.Log("SetUpTest done") }
func (s *InitializeSuite) TestEnvironConfigWithAdminSecret(c *gc.C) { // admin-secret blocks Initialize. good := testing.EnvironConfig(c) badUpdateAttrs := map[string]interface{}{"admin-secret": "foo"} bad, err := good.Apply(badUpdateAttrs) _, err = state.Initialize(state.TestingMongoInfo(), bad, state.TestingDialOpts(), state.Policy(nil)) c.Assert(err, gc.ErrorMatches, "admin-secret should never be written to the state") // admin-secret blocks UpdateEnvironConfig. st := TestingInitialize(c, good, nil) st.Close() s.openState(c) err = s.State.UpdateEnvironConfig(badUpdateAttrs, nil, nil) c.Assert(err, gc.ErrorMatches, "admin-secret should never be written to the state") // EnvironConfig remains inviolate. cfg, err := s.State.EnvironConfig() c.Assert(err, gc.IsNil) c.Assert(cfg.AllAttrs(), gc.DeepEquals, good.AllAttrs()) }
func (s *InstancePollerSuite) TestWatchForEnvironConfigChangesSuccess(c *gc.C) { result, err := s.api.WatchForEnvironConfigChanges() c.Assert(err, jc.ErrorIsNil) c.Assert(result, jc.DeepEquals, params.NotifyWatchResult{ Error: nil, NotifyWatcherId: "1", }) // Verify the watcher resource was registered. c.Assert(s.resources.Count(), gc.Equals, 1) resource := s.resources.Get("1") defer statetesting.AssertStop(c, resource) // Check that the watcher has consumed the initial event wc := statetesting.NewNotifyWatcherC(c, s.st, resource.(state.NotifyWatcher)) wc.AssertNoChange() s.st.CheckCallNames(c, "WatchForEnvironConfigChanges") // Try changing the config to verify an event is reported. envConfig := coretesting.EnvironConfig(c) s.st.SetConfig(c, envConfig) wc.AssertOneChange() }
func newMockEnvironAccessor(c *gc.C) *mockEnvironAccessor { return &mockEnvironAccessor{ watcher: &mockNotifyWatcher{make(chan struct{}, 1)}, cfg: testing.EnvironConfig(c), } }