// MakeConfig creates a functional environConfig for a test. func MakeConfig(c *gc.C, attrs testing.Attrs) *environConfig { cfg, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) env, err := environs.Prepare(cfg, testing.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) return env.(*joyentEnviron).Ecfg() }
func (s *AgentToolsSuite) TestUpdateToolsAvailabilityNoMatches(c *gc.C) { fakeModelConfig := func(_ *state.Model) (*config.Config, error) { sConfig := coretesting.FakeConfig() sConfig = sConfig.Merge(coretesting.Attrs{ "agent-version": "2.5.0", }) return config.New(config.NoDefaults, sConfig) } s.PatchValue(&modelConfig, fakeModelConfig) // No new tools available. fakeToolFinder := func(_ environs.Environ, _ int, _ int, _ string, _ coretools.Filter) (coretools.List, error) { return nil, errors.NotFoundf("tools") } // Update should never be called. fakeUpdate := func(_ *state.Model, v version.Number) error { c.Fail() return nil } cfg, err := config.New(config.NoDefaults, coretesting.FakeConfig()) c.Assert(err, jc.ErrorIsNil) err = updateToolsAvailability(&mockState{configGetter{cfg}}, getDummyEnviron, fakeToolFinder, fakeUpdate) c.Assert(err, jc.ErrorIsNil) }
func (s *AgentToolsSuite) TestUpdateToolsAvailability(c *gc.C) { fakeModelConfig := func(_ *state.Model) (*config.Config, error) { sConfig := coretesting.FakeConfig() sConfig = sConfig.Merge(coretesting.Attrs{ "agent-version": "2.5.0", }) return config.New(config.NoDefaults, sConfig) } s.PatchValue(&modelConfig, fakeModelConfig) fakeToolFinder := func(_ environs.Environ, _ int, _ int, _ string, _ coretools.Filter) (coretools.List, error) { ver := version.Binary{Number: version.Number{Major: 2, Minor: 5, Patch: 2}} olderVer := version.Binary{Number: version.Number{Major: 2, Minor: 5, Patch: 1}} t := coretools.Tools{Version: ver, URL: "http://example.com", Size: 1} tOld := coretools.Tools{Version: olderVer, URL: "http://example.com", Size: 1} return coretools.List{&t, &tOld}, nil } var ver version.Number fakeUpdate := func(_ *state.Model, v version.Number) error { ver = v return nil } cfg, err := config.New(config.NoDefaults, coretesting.FakeConfig()) c.Assert(err, jc.ErrorIsNil) err = updateToolsAvailability(&mockState{configGetter{cfg}}, getDummyEnviron, fakeToolFinder, fakeUpdate) c.Assert(err, jc.ErrorIsNil) c.Assert(ver, gc.Not(gc.Equals), version.Zero) c.Assert(ver, gc.Equals, version.Number{Major: 2, Minor: 5, Patch: 2}) }
func (t *LiveTests) TestBootstrapWithDefaultSeries(c *gc.C) { if !t.HasProvisioner { c.Skip("HasProvisioner is false; cannot test deployment") } current := version.Current other := current other.Series = "quantal" if current == other { other.Series = "precise" } dummyCfg, err := config.New(config.NoDefaults, dummy.SampleConfig().Merge(coretesting.Attrs{ "state-server": false, "name": "dummy storage", })) dummyenv, err := environs.Prepare(dummyCfg, coretesting.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) defer dummyenv.Destroy() t.Destroy(c) attrs := t.TestConfig.Merge(coretesting.Attrs{"default-series": other.Series}) cfg, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) env, err := environs.Prepare(cfg, coretesting.Context(c), t.ConfigStore) c.Assert(err, gc.IsNil) defer environs.Destroy(env, t.ConfigStore) currentName := envtools.StorageName(current) otherName := envtools.StorageName(other) envStorage := env.Storage() dummyStorage := dummyenv.Storage() defer envStorage.Remove(otherName) _, err = sync.Upload(dummyStorage, ¤t.Number) c.Assert(err, gc.IsNil) // This will only work while cross-compiling across releases is safe, // which depends on external elements. Tends to be safe for the last // few releases, but we may have to refactor some day. err = storageCopy(dummyStorage, currentName, envStorage, otherName) c.Assert(err, gc.IsNil) err = bootstrap.Bootstrap(coretesting.Context(c), env, environs.BootstrapParams{}) c.Assert(err, gc.IsNil) st := t.Env.(testing.GetStater).GetStateInAPIServer() // Wait for machine agent to come up on the bootstrap // machine and ensure it deployed the proper series. m0, err := st.Machine("0") c.Assert(err, gc.IsNil) mw0 := newMachineToolWaiter(m0) defer mw0.Stop() waitAgentTools(c, mw0, other) }
func (*configSuite) TestValidateAcceptsUnchangedConfig(c *gc.C) { attrs := makeAzureConfigMap(c) provider := azureEnvironProvider{} oldConfig, err := config.New(config.NoDefaults, attrs) c.Assert(err, jc.ErrorIsNil) newConfig, err := config.New(config.NoDefaults, attrs) c.Assert(err, jc.ErrorIsNil) result, err := provider.Validate(newConfig, oldConfig) c.Assert(err, jc.ErrorIsNil) c.Check(result.Name(), gc.Equals, attrs["name"]) }
func (*configSuite) TestValidateChecksConfigChanges(c *gc.C) { provider := azureEnvironProvider{} oldConfig, err := config.New(config.NoDefaults, makeConfigMap(nil)) c.Assert(err, jc.ErrorIsNil) newAttrs := makeConfigMap(map[string]interface{}{ "name": "different-name", }) newConfig, err := config.New(config.NoDefaults, newAttrs) c.Assert(err, jc.ErrorIsNil) _, err = provider.Validate(newConfig, oldConfig) c.Check(err, gc.NotNil) }
func (s *providerSuite) TestPrepareUseSSHStorage(c *gc.C) { minimal := manual.MinimalConfigValues() minimal["use-sshstorage"] = false testConfig, err := config.New(config.UseDefaults, minimal) c.Assert(err, jc.ErrorIsNil) _, err = manual.ProviderInstance.PrepareForBootstrap(envtesting.BootstrapContext(c), testConfig) c.Assert(err, gc.ErrorMatches, "use-sshstorage must not be specified") minimal["use-sshstorage"] = true testConfig, err = config.New(config.UseDefaults, minimal) c.Assert(err, jc.ErrorIsNil) _, err = manual.ProviderInstance.PrepareForBootstrap(envtesting.BootstrapContext(c), testConfig) c.Assert(err, jc.ErrorIsNil) }
func (s *localServerSuite) assertGetImageMetadataSources(c *gc.C, stream, officialSourcePath string) { // Create a config that matches s.TestConfig but with the specified stream. envAttrs := s.TestConfig if stream != "" { envAttrs = envAttrs.Merge(coretesting.Attrs{"image-stream": stream}) } cfg, err := config.New(config.NoDefaults, envAttrs) c.Assert(err, gc.IsNil) env, err := environs.New(cfg) c.Assert(err, gc.IsNil) sources, err := imagemetadata.GetMetadataSources(env) c.Assert(err, gc.IsNil) c.Assert(sources, gc.HasLen, 4) var urls = make([]string, len(sources)) for i, source := range sources { url, err := source.URL("") c.Assert(err, gc.IsNil) urls[i] = url } // The image-metadata-url ends with "/juju-dist-test/". c.Check(strings.HasSuffix(urls[0], "/juju-dist-test/"), jc.IsTrue) // The control bucket URL contains the bucket name. c.Check(strings.Contains(urls[1], openstack.ControlBucketName(env)+"/images"), jc.IsTrue) // The product-streams URL ends with "/imagemetadata". c.Check(strings.HasSuffix(urls[2], "/imagemetadata/"), jc.IsTrue) c.Assert(urls[3], gc.Equals, fmt.Sprintf("http://cloud-images.ubuntu.com/%s/", officialSourcePath)) }
func (s *ToolsMetadataSuite) SetUpTest(c *gc.C) { s.FakeJujuXDGDataHomeSuite.SetUpTest(c) s.AddCleanup(dummy.Reset) cfg, err := config.New(config.UseDefaults, map[string]interface{}{ "name": "erewhemos", "type": "dummy", "uuid": coretesting.ModelTag.Id(), "controller-uuid": coretesting.ControllerTag.Id(), "conroller": true, }) c.Assert(err, jc.ErrorIsNil) env, err := bootstrap.Prepare( modelcmd.BootstrapContextNoVerify(coretesting.Context(c)), jujuclienttesting.NewMemStore(), bootstrap.PrepareParams{ ControllerConfig: coretesting.FakeControllerConfig(), ControllerName: cfg.Name(), ModelConfig: cfg.AllAttrs(), Cloud: dummy.SampleCloudSpec(), AdminSecret: "admin-secret", }, ) c.Assert(err, jc.ErrorIsNil) s.env = env loggo.GetLogger("").SetLogLevel(loggo.INFO) // Switch the default tools location. s.publicStorageDir = c.MkDir() s.PatchValue(&tools.DefaultBaseURL, s.publicStorageDir) }
func (cs *NewAPIStateSuite) TestNewAPIState(c *gc.C) { cfg, err := config.New(config.NoDefaults, dummy.SampleConfig()) c.Assert(err, jc.ErrorIsNil) ctx := envtesting.BootstrapContext(c) env, err := environs.Prepare(cfg, ctx, configstore.NewMem()) c.Assert(err, jc.ErrorIsNil) storageDir := c.MkDir() cs.PatchValue(&envtools.DefaultBaseURL, storageDir) stor, err := filestorage.NewFileStorageWriter(storageDir) c.Assert(err, jc.ErrorIsNil) envtesting.UploadFakeTools(c, stor, "released", "released") err = bootstrap.Bootstrap(ctx, env, bootstrap.BootstrapParams{}) c.Assert(err, jc.ErrorIsNil) cfg = env.Config() cfg, err = cfg.Apply(map[string]interface{}{ "secret": "fnord", }) c.Assert(err, jc.ErrorIsNil) err = env.SetConfig(cfg) c.Assert(err, jc.ErrorIsNil) st, err := juju.NewAPIState(dummy.AdminUserTag(), env, api.DialOpts{}) c.Assert(st, gc.NotNil) // the secrets will not be updated, as they already exist attrs, err := st.Client().EnvironmentGet() c.Assert(attrs["secret"], gc.Equals, "pork") c.Assert(st.Close(), gc.IsNil) }
func getEnvironConfig(c *gc.C, attrs map[string]interface{}) *environConfig { testConfig, err := config.New(config.UseDefaults, attrs) c.Assert(err, jc.ErrorIsNil) envConfig, err := manualProvider{}.validate(testConfig, nil) c.Assert(err, jc.ErrorIsNil) return envConfig }
func (*OpenSuite) TestPrepareGeneratesDifferentAdminSecrets(c *gc.C) { baselineAttrs := dummy.SampleConfig().Merge(testing.Attrs{ "state-server": false, "name": "erewhemos", }).Delete( "admin-secret", ) cfg, err := config.New(config.NoDefaults, baselineAttrs) c.Assert(err, gc.IsNil) ctx := testing.Context(c) env0, err := environs.Prepare(cfg, ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) adminSecret0 := env0.Config().AdminSecret() c.Assert(adminSecret0, gc.HasLen, 32) c.Assert(adminSecret0, gc.Matches, "^[0-9a-f]*$") env1, err := environs.Prepare(cfg, ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) adminSecret1 := env1.Config().AdminSecret() c.Assert(adminSecret1, gc.HasLen, 32) c.Assert(adminSecret1, gc.Matches, "^[0-9a-f]*$") c.Assert(adminSecret1, gc.Not(gc.Equals), adminSecret0) }
// mockTestingEnvConfig prepares an environment configuration using // the mock provider which does not support networking. func mockTestingEnvConfig(c *gc.C) *config.Config { cfg, err := config.New(config.NoDefaults, mockConfig()) c.Assert(err, jc.ErrorIsNil) env, err := environs.Prepare(cfg, modelcmd.BootstrapContext(coretesting.Context(c)), configstore.NewMem()) c.Assert(err, jc.ErrorIsNil) return env.Config() }
// testingEnvConfig prepares an environment configuration using // the dummy provider. func testingEnvConfig(c *gc.C) *config.Config { cfg, err := config.New(config.NoDefaults, dummy.SampleConfig()) c.Assert(err, jc.ErrorIsNil) env, err := environs.Prepare(cfg, envcmd.BootstrapContext(testing.Context(c)), configstore.NewMem()) c.Assert(err, jc.ErrorIsNil) return env.Config() }
func (s *ConfigSuite) TestFirewallMode(c *gc.C) { for i, test := range firewallModeTests { c.Logf("test %d: %s", i, test.configFirewallMode) attrs := dummy.SampleConfig() if test.configFirewallMode != "" { attrs = attrs.Merge(testing.Attrs{ "firewall-mode": test.configFirewallMode, }) } cfg, err := config.New(config.NoDefaults, attrs) if err != nil { c.Assert(err, gc.ErrorMatches, test.errorMsg) continue } ctx := testing.Context(c) env, err := environs.Prepare(cfg, ctx, configstore.NewMem()) if test.errorMsg != "" { c.Assert(err, gc.ErrorMatches, test.errorMsg) continue } c.Assert(err, gc.IsNil) defer env.Destroy() firewallMode := env.Config().FirewallMode() c.Assert(firewallMode, gc.Equals, test.firewallMode) s.TearDownTest(c) } }
// Prepare prepares a new environment based on the provided configuration. // If the environment is already prepared, it behaves like New. func Prepare(cfg *config.Config, ctx BootstrapContext, store configstore.Storage) (Environ, error) { if p, err := Provider(cfg.Type()); err != nil { return nil, err } else if info, err := store.ReadInfo(cfg.Name()); errors.IsNotFound(errors.Cause(err)) { info = store.CreateInfo(cfg.Name()) if env, err := prepare(ctx, cfg, info, p); err == nil { return env, decorateAndWriteInfo(info, env.Config()) } else { if err := info.Destroy(); err != nil { logger.Warningf("cannot destroy newly created environment info: %v", err) } return nil, err } } else if err != nil { return nil, errors.Annotatef(err, "error reading environment info %q", cfg.Name()) } else if !info.Initialized() { return nil, errors.Errorf( "found uninitialized environment info for %q; environment preparation probably in progress or interrupted", cfg.Name(), ) } else if len(info.BootstrapConfig()) == 0 { return nil, errors.New("found environment info but no bootstrap config") } else { cfg, err = config.New(config.NoDefaults, info.BootstrapConfig()) if err != nil { return nil, errors.Annotate(err, "cannot parse bootstrap config") } return New(cfg) } }
func (s *BootstrapSuite) makeTestEnv(c *gc.C) { attrs := dummy.SampleConfig().Merge( testing.Attrs{ "agent-version": version.Current.Number.String(), "bootstrap-timeout": "123", }, ).Delete("admin-secret", "ca-private-key") cfg, err := config.New(config.NoDefaults, attrs) c.Assert(err, jc.ErrorIsNil) provider, err := environs.Provider(cfg.Type()) c.Assert(err, jc.ErrorIsNil) env, err := provider.PrepareForBootstrap(nullContext(), cfg) c.Assert(err, jc.ErrorIsNil) envtesting.MustUploadFakeTools(s.toolsStorage, cfg.AgentStream(), cfg.AgentStream()) inst, _, _, err := jujutesting.StartInstance(env, "0") c.Assert(err, jc.ErrorIsNil) s.instanceId = inst.Id() addresses, err := inst.Addresses() c.Assert(err, jc.ErrorIsNil) s.bootstrapName = network.SelectPublicAddress(addresses) s.envcfg = env.Config() s.b64yamlEnvcfg = b64yaml(s.envcfg.AllAttrs()).encode() }
// ConfigForName returns the configuration for the environment with // the given name from the default environments file. If the name is // blank, the default environment will be used. If the configuration // is not found, an errors.NotFoundError is returned. If the given // store contains an entry for the environment and it has associated // bootstrap config, that configuration will be returned. // ConfigForName also returns where the configuration was sourced from // (this is also valid even when there is an error. func ConfigForName(name string, store configstore.Storage) (*config.Config, ConfigSource, error) { envs, err := ReadEnvirons("") if err != nil { return nil, ConfigFromNowhere, err } if name == "" { name = envs.Default } // TODO(rog) 2013-10-04 https://bugs.github.com/juju/juju/+bug/1235217 // Don't fall back to reading from environments.yaml // when we can be sure that everyone has a // .jenv file for their currently bootstrapped environments. if name != "" { info, err := store.ReadInfo(name) if err == nil { if len(info.BootstrapConfig()) == 0 { return nil, ConfigFromNowhere, EmptyConfig{fmt.Errorf("environment has no bootstrap configuration data")} } logger.Debugf("ConfigForName found bootstrap config %#v", info.BootstrapConfig()) cfg, err := config.New(config.NoDefaults, info.BootstrapConfig()) return cfg, ConfigFromInfo, err } if err != nil && !errors.IsNotFound(err) { return nil, ConfigFromInfo, fmt.Errorf("cannot read environment info for %q: %v", name, err) } } cfg, err := envs.Config(name) return cfg, ConfigFromEnvirons, err }
func (*OpenSuite) TestDestroy(c *gc.C) { cfg, err := config.New(config.NoDefaults, dummy.SampleConfig().Merge( testing.Attrs{ "state-server": false, "name": "erewhemos", }, )) c.Assert(err, gc.IsNil) store := configstore.NewMem() // Prepare the environment and sanity-check that // the config storage info has been made. ctx := testing.Context(c) e, err := environs.Prepare(cfg, ctx, store) c.Assert(err, gc.IsNil) _, err = store.ReadInfo(e.Config().Name()) c.Assert(err, gc.IsNil) err = environs.Destroy(e, store) c.Assert(err, gc.IsNil) // Check that the environment has actually been destroyed // and that the config info has been destroyed too. _, err = e.StateServerInstances() c.Assert(err, gc.ErrorMatches, "environment has been destroyed") _, err = store.ReadInfo(e.Config().Name()) c.Assert(err, jc.Satisfies, errors.IsNotFound) }
// ModelConfig returns the complete config for the model represented // by this state. func (st *State) ModelConfig() (*config.Config, error) { modelSettings, err := readSettings(st, settingsC, modelGlobalKey) if err != nil { return nil, errors.Trace(err) } return config.New(config.NoDefaults, modelSettings.Map()) }
func (s *configSuite) TestValidateConfig(c *gc.C) { testConfig := MinimalConfig(c) testConfig, err := testConfig.Apply(map[string]interface{}{"bootstrap-host": ""}) c.Assert(err, jc.ErrorIsNil) _, err = manualProvider{}.Validate(testConfig, nil) c.Assert(err, gc.ErrorMatches, "bootstrap-host must be specified") testConfig, err = testConfig.Apply(map[string]interface{}{"storage-auth-key": nil}) c.Assert(err, jc.ErrorIsNil) _, err = manualProvider{}.Validate(testConfig, nil) c.Assert(err, gc.ErrorMatches, "storage-auth-key: expected string, got nothing") values := MinimalConfigValues() delete(values, "bootstrap-user") delete(values, "storage-listen-ip") delete(values, "storage-port") testConfig, err = config.New(config.UseDefaults, values) c.Assert(err, jc.ErrorIsNil) valid, err := manualProvider{}.Validate(testConfig, nil) c.Assert(err, jc.ErrorIsNil) unknownAttrs := valid.UnknownAttrs() c.Assert(unknownAttrs["bootstrap-host"], gc.Equals, "hostname") c.Assert(unknownAttrs["bootstrap-user"], gc.Equals, "") c.Assert(unknownAttrs["storage-listen-ip"], gc.Equals, "") c.Assert(unknownAttrs["storage-port"], gc.Equals, int(8040)) }
func (suite *maas2Suite) makeEnviron(c *gc.C, controller gomaasapi.Controller) *maasEnviron { if controller != nil { suite.injectController(controller) } testAttrs := coretesting.Attrs{} for k, v := range maasEnvAttrs { testAttrs[k] = v } testAttrs["agent-version"] = version.Current.String() cred := cloud.NewCredential(cloud.OAuth1AuthType, map[string]string{ "maas-oauth": "a:b:c", }) cloud := environs.CloudSpec{ Type: "maas", Name: "maas", Endpoint: "http://any-old-junk.invalid/", Credential: &cred, } attrs := coretesting.FakeConfig().Merge(testAttrs) suite.controllerUUID = coretesting.FakeControllerConfig().ControllerUUID() cfg, err := config.New(config.NoDefaults, attrs) c.Assert(err, jc.ErrorIsNil) env, err := NewEnviron(cloud, cfg) c.Assert(err, jc.ErrorIsNil) c.Assert(env, gc.NotNil) return env }
func testConfig() *config.Config { attrs := dummy.SampleConfig().Merge(coretesting.Attrs{ "type": "nonex", }) cfg, _ := config.New(config.NoDefaults, attrs) return cfg }
func (s *ConfigSuite) TestPrepareSetsDefaultBlockSource(c *gc.C) { s.PatchValue(&verifyCredentials, func(*environ) error { return nil }) attrs := testing.FakeConfig().Merge(testing.Attrs{ "type": "ec2", }) config, err := config.New(config.NoDefaults, attrs) c.Assert(err, jc.ErrorIsNil) cfg, err := providerInstance.BootstrapConfig(environs.BootstrapConfigParams{ Config: config, CloudRegion: "test", Credentials: cloud.NewCredential( cloud.AccessKeyAuthType, map[string]string{ "access-key": "x", "secret-key": "y", }, ), }) c.Assert(err, jc.ErrorIsNil) source, ok := cfg.StorageDefaultBlockSource() c.Assert(ok, jc.IsTrue) c.Assert(source, gc.Equals, "ebs") }
// If the environment is configured not to require a public IP address for nodes, // bootstrapping and starting an instance should occur without any attempt to // allocate a public address. func (s *localServerSuite) TestStartInstanceWithoutPublicIP(c *gc.C) { cleanup := s.srv.Service.Nova.RegisterControlPoint( "addFloatingIP", func(sc hook.ServiceControl, args ...interface{}) error { return fmt.Errorf("add floating IP should not have been called") }, ) defer cleanup() cleanup = s.srv.Service.Nova.RegisterControlPoint( "addServerFloatingIP", func(sc hook.ServiceControl, args ...interface{}) error { return fmt.Errorf("add server floating IP should not have been called") }, ) defer cleanup() cfg, err := config.New(config.NoDefaults, s.TestConfig.Merge(coretesting.Attrs{ "use-floating-ip": false, })) c.Assert(err, gc.IsNil) env, err := environs.Prepare(cfg, coretesting.Context(c), s.ConfigStore) c.Assert(err, gc.IsNil) err = bootstrap.Bootstrap(coretesting.Context(c), env, environs.BootstrapParams{}) c.Assert(err, gc.IsNil) inst, _ := testing.AssertStartInstance(c, env, "100") err = env.StopInstances(inst.Id()) c.Assert(err, gc.IsNil) }
func (s *AgentToolsSuite) TestCheckToolsNonReleasedStream(c *gc.C) { sConfig := coretesting.FakeConfig() sConfig = sConfig.Merge(coretesting.Attrs{ "agent-version": "2.5-alpha1", "agent-stream": "proposed", }) cfg, err := config.New(config.NoDefaults, sConfig) c.Assert(err, jc.ErrorIsNil) fakeNewEnvirons := func(*config.Config) (environs.Environ, error) { return dummyEnviron{}, nil } s.PatchValue(&newEnvirons, fakeNewEnvirons) var ( calledWithMajor, calledWithMinor int calledWithStreams []string ) fakeToolFinder := func(e environs.Environ, maj int, min int, stream string, filter coretools.Filter) (coretools.List, error) { calledWithMajor = maj calledWithMinor = min calledWithStreams = append(calledWithStreams, stream) if stream == "released" { return nil, coretools.ErrNoMatches } ver := version.Binary{Number: version.Number{Major: maj, Minor: min}} t := coretools.Tools{Version: ver, URL: "http://example.com", Size: 1} c.Assert(calledWithMajor, gc.Equals, 2) c.Assert(calledWithMinor, gc.Equals, 5) return coretools.List{&t}, nil } ver, err := checkToolsAvailability(cfg, fakeToolFinder) c.Assert(err, jc.ErrorIsNil) c.Assert(calledWithStreams, gc.DeepEquals, []string{"released", "proposed"}) c.Assert(ver, gc.Not(gc.Equals), version.Zero) c.Assert(ver, gc.Equals, version.Number{Major: 2, Minor: 5, Patch: 0}) }
func (e mockEnviron) Config() *config.Config { cfg, err := config.New(config.NoDefaults, mockConfig()) if err != nil { panic("invalid configuration for testing") } return cfg }
func (s *updaterSuite) TestCheckTools(c *gc.C) { sConfig := coretesting.FakeConfig() sConfig = sConfig.Merge(coretesting.Attrs{ "agent-version": "2.5.0", }) cfg, err := config.New(config.NoDefaults, sConfig) c.Assert(err, jc.ErrorIsNil) fakeNewEnvirons := func(*config.Config) (environs.Environ, error) { return dummyEnviron{}, nil } s.PatchValue(&newEnvirons, fakeNewEnvirons) var ( calledWithEnviron environs.Environ calledWithMajor, calledWithMinor int calledWithFilter coretools.Filter ) fakeToolFinder := func(e environs.Environ, maj int, min int, _ string, filter coretools.Filter) (coretools.List, error) { calledWithEnviron = e calledWithMajor = maj calledWithMinor = min calledWithFilter = filter ver := version.Binary{Number: version.Number{Major: maj, Minor: min}} t := coretools.Tools{Version: ver, URL: "http://example.com", Size: 1} c.Assert(calledWithMajor, gc.Equals, 2) c.Assert(calledWithMinor, gc.Equals, 5) return coretools.List{&t}, nil } ver, err := checkToolsAvailability(cfg, fakeToolFinder) c.Assert(err, jc.ErrorIsNil) c.Assert(ver, gc.Not(gc.Equals), version.Zero) c.Assert(ver, gc.Equals, version.Number{Major: 2, Minor: 5, Patch: 0}) }
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()) }
// NewFromAttrs returns a new environment based on the provided configuration // attributes. // TODO(rog) remove this function - it's almost always wrong to use it. func NewFromAttrs(attrs map[string]interface{}) (Environ, error) { cfg, err := config.New(config.NoDefaults, attrs) if err != nil { return nil, err } return New(cfg) }