func (*configSuite) TestValidateAcceptsUnchangedConfig(c *gc.C) { attrs := makeAzureConfigMap(c) provider := azureEnvironProvider{} oldConfig, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) newConfig, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) result, err := provider.Validate(newConfig, oldConfig) c.Assert(err, gc.IsNil) 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, gc.IsNil) newAttrs := makeConfigMap(map[string]interface{}{ "name": "different-name", }) newConfig, err := config.New(config.NoDefaults, newAttrs) c.Assert(err, gc.IsNil) _, err = provider.Validate(newConfig, oldConfig) c.Check(err, gc.NotNil) }
func (*configSuite) TestValidateParsesAzureConfig(c *gc.C) { location := "location" managementSubscriptionId := "subscription-id" certificate := "certificate content" storageAccountName := "account-name" forceImageName := "force-image-name" unknownFutureSetting := "preserved" azureConfig := map[string]interface{}{ "location": location, "management-subscription-id": managementSubscriptionId, "management-certificate": certificate, "storage-account-name": storageAccountName, "force-image-name": forceImageName, "unknown-future-setting": unknownFutureSetting, } attrs := makeConfigMap(azureConfig) provider := azureEnvironProvider{} config, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) azConfig, err := provider.newConfig(config) c.Assert(err, gc.IsNil) c.Check(azConfig.Name(), gc.Equals, attrs["name"]) c.Check(azConfig.location(), gc.Equals, location) c.Check(azConfig.managementSubscriptionId(), gc.Equals, managementSubscriptionId) c.Check(azConfig.managementCertificate(), gc.Equals, certificate) c.Check(azConfig.storageAccountName(), gc.Equals, storageAccountName) c.Check(azConfig.forceImageName(), gc.Equals, forceImageName) c.Check(azConfig.UnknownAttrs()["unknown-future-setting"], gc.Equals, unknownFutureSetting) }
// 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) }
func (t *ProviderSuite) assertGetImageMetadataSources(c *gc.C, stream, officialSourcePath string) { // Make an env configured with the stream. envAttrs := localConfigAttrs 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.Prepare(cfg, coretesting.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) c.Assert(env, gc.NotNil) sources, err := imagemetadata.GetMetadataSources(env) c.Assert(err, gc.IsNil) c.Assert(len(sources), gc.Equals, 2) var urls = make([]string, len(sources)) for i, source := range sources { url, err := source.URL("") c.Assert(err, gc.IsNil) urls[i] = url } // The control bucket URL contains the bucket name. c.Check(strings.Contains(urls[0], ec2.ControlBucketName(env)+"/images"), jc.IsTrue) c.Assert(urls[1], gc.Equals, fmt.Sprintf("http://cloud-images.ubuntu.com/%s/", officialSourcePath)) }
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) } }
func (*environSuite) TestSetConfigAllowsEmptyFromNilAgentName(c *gc.C) { // bug #1256179 is that when using an older version of Juju (<1.16.2) // we didn't include maas-agent-name in the database, so it was 'nil' // in the OldConfig. However, when setting an environment, we would set // it to "" (because maasEnvironConfig.Validate ensures it is a 'valid' // string). We can't create that from NewEnviron or newConfig because // both of them Validate the contents. 'cmd/juju/environment // SetEnvironmentCommand' instead uses conn.State.EnvironConfig() which // just reads the content of the database into a map, so we just create // the map ourselves. // Even though we use 'nil' here, it actually stores it as "" because // 1.16.2 already validates the value baseCfg := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": ""}) c.Check(baseCfg.UnknownAttrs()["maas-agent-name"], gc.Equals, "") env, err := maas.NewEnviron(baseCfg) c.Assert(err, gc.IsNil) provider := env.Provider() attrs := coretesting.FakeConfig() // These are attrs we need to make it a valid Config, but would usually // be set by other infrastructure attrs["type"] = "maas" nilCfg, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) validatedConfig, err := provider.Validate(baseCfg, nilCfg) c.Assert(err, gc.IsNil) c.Check(validatedConfig.UnknownAttrs()["maas-agent-name"], gc.Equals, "") // However, you can't set it to an actual value if you haven't been using a value valueCfg := getSimpleTestConfig(c, coretesting.Attrs{"maas-agent-name": "agent-name"}) _, err = provider.Validate(valueCfg, nilCfg) c.Check(err, gc.ErrorMatches, ".*cannot change maas-agent-name.*") }
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.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.StateInfo() c.Assert(err, gc.ErrorMatches, "environment has been destroyed") _, err = store.ReadInfo(e.Name()) c.Assert(err, jc.Satisfies, errors.IsNotFound) }
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 (*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) }
func (suite *environSuite) assertGetImageMetadataSources(c *gc.C, stream, officialSourcePath string) { // Make an env configured with the stream. testAttrs := maasEnvAttrs testAttrs = testAttrs.Merge(coretesting.Attrs{ "maas-server": suite.testMAASObject.TestServer.URL, }) if stream != "" { testAttrs = testAttrs.Merge(coretesting.Attrs{ "image-stream": stream, }) } attrs := coretesting.FakeConfig().Merge(testAttrs) cfg, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) env, err := NewEnviron(cfg) c.Assert(err, gc.IsNil) // Add a dummy file to storage so we can use that to check the // obtained source later. data := makeRandomBytes(10) stor := NewStorage(env) err = stor.Put("images/filename", bytes.NewBuffer([]byte(data)), int64(len(data))) c.Assert(err, gc.IsNil) sources, err := imagemetadata.GetMetadataSources(env) c.Assert(err, gc.IsNil) c.Assert(len(sources), gc.Equals, 2) assertSourceContents(c, sources[0], "filename", data) url, err := sources[1].URL("") c.Assert(err, gc.IsNil) c.Assert(url, gc.Equals, fmt.Sprintf("http://cloud-images.ubuntu.com/%s/", officialSourcePath)) }
// 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([]instance.Instance{inst}) c.Assert(err, gc.IsNil) }
func (*NewAPIConnSuite) TestNewConn(c *gc.C) { cfg, err := config.New(config.NoDefaults, dummy.SampleConfig()) c.Assert(err, gc.IsNil) ctx := coretesting.Context(c) env, err := environs.Prepare(cfg, ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) envtesting.UploadFakeTools(c, env.Storage()) err = bootstrap.Bootstrap(ctx, env, environs.BootstrapParams{}) c.Assert(err, gc.IsNil) cfg = env.Config() cfg, err = cfg.Apply(map[string]interface{}{ "secret": "fnord", }) c.Assert(err, gc.IsNil) err = env.SetConfig(cfg) c.Assert(err, gc.IsNil) conn, err := juju.NewAPIConn(env, api.DefaultDialOpts()) c.Assert(err, gc.IsNil) c.Assert(conn.Environ, gc.Equals, env) c.Assert(conn.State, gc.NotNil) // the secrets will not be updated, as they already exist attrs, err := conn.State.Client().EnvironmentGet() c.Assert(attrs["secret"], gc.Equals, "pork") c.Assert(conn.Close(), gc.IsNil) }
// 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 getEnvironConfig(c *gc.C, attrs map[string]interface{}) *environConfig { testConfig, err := config.New(config.UseDefaults, attrs) c.Assert(err, gc.IsNil) envConfig, err := manualProvider{}.validate(testConfig, nil) c.Assert(err, gc.IsNil) return envConfig }
func (w *EnvironConfigWatcher) loop() (err error) { sw := w.st.watchSettings(environGlobalKey) defer sw.Stop() out := w.out out = nil cfg := &config.Config{} for { select { case <-w.st.watcher.Dead(): return stateWatcherDeadError(w.st.watcher.Err()) case <-w.tomb.Dying(): return tomb.ErrDying case settings, ok := <-sw.Changes(): if !ok { return watcher.MustErr(sw) } cfg, err = config.New(config.NoDefaults, settings.Map()) if err == nil { out = w.out } else { out = nil } case out <- cfg: out = nil } } }
func (s *suite) TestAllocateAddress(c *gc.C) { cfg, err := config.New(config.NoDefaults, s.TestConfig) c.Assert(err, gc.IsNil) e, err := environs.Prepare(cfg, testing.Context(c), s.ConfigStore) c.Assert(err, gc.IsNil, gc.Commentf("preparing environ %#v", s.TestConfig)) c.Assert(e, gc.NotNil) envtesting.UploadFakeTools(c, e.Storage()) err = bootstrap.EnsureNotBootstrapped(e) c.Assert(err, gc.IsNil) err = bootstrap.Bootstrap(testing.Context(c), e, environs.BootstrapParams{}) c.Assert(err, gc.IsNil) inst, _ := jujutesting.AssertStartInstance(c, e, "0") c.Assert(inst, gc.NotNil) netId := network.Id("net1") opc := make(chan dummy.Operation, 200) dummy.Listen(opc) expectAddress := instance.NewAddress("0.1.2.1", instance.NetworkCloudLocal) address, err := e.AllocateAddress(inst.Id(), netId) c.Assert(err, gc.IsNil) c.Assert(address, gc.DeepEquals, expectAddress) assertAllocateAddress(c, e, opc, inst.Id(), netId, expectAddress) expectAddress = instance.NewAddress("0.1.2.2", instance.NetworkCloudLocal) address, err = e.AllocateAddress(inst.Id(), netId) c.Assert(err, gc.IsNil) c.Assert(address, gc.DeepEquals, expectAddress) assertAllocateAddress(c, e, opc, inst.Id(), netId, expectAddress) }
func (s *LiveTests) assertStartInstanceDefaultSecurityGroup(c *gc.C, useDefault bool) { attrs := s.TestConfig.Merge(coretesting.Attrs{ "name": "sample-" + randomName(), "control-bucket": "juju-test-" + randomName(), "use-default-secgroup": useDefault, }) cfg, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) // Set up a test environment. env, err := environs.New(cfg) c.Assert(err, gc.IsNil) c.Assert(env, gc.NotNil) defer env.Destroy() // Bootstrap and start an instance. err = bootstrap.Bootstrap(coretesting.Context(c), env, environs.BootstrapParams{}) c.Assert(err, gc.IsNil) inst, _ := jujutesting.AssertStartInstance(c, env, "100") // Check whether the instance has the default security group assigned. novaClient := openstack.GetNovaClient(env) groups, err := novaClient.GetServerSecurityGroups(string(inst.Id())) c.Assert(err, gc.IsNil) defaultGroupFound := false for _, group := range groups { if group.Name == "default" { defaultGroupFound = true break } } c.Assert(defaultGroupFound, gc.Equals, useDefault) }
func testingEnvConfig(c *gc.C) *config.Config { cfg, err := config.New(config.NoDefaults, dummy.SampleConfig()) c.Assert(err, gc.IsNil) env, err := environs.Prepare(cfg, testing.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) return env.Config() }
// 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/wallyworld/core/+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 (s *providerSuite) TestPrepareUseSSHStorage(c *gc.C) { minimal := manual.MinimalConfigValues() minimal["use-sshstorage"] = false testConfig, err := config.New(config.UseDefaults, minimal) c.Assert(err, gc.IsNil) _, err = manual.ProviderInstance.Prepare(coretesting.Context(c), testConfig) c.Assert(err, gc.ErrorMatches, "use-sshstorage must not be specified") s.PatchValue(manual.NewSSHStorage, func(sshHost, storageDir, storageTmpdir string) (storage.Storage, error) { return nil, fmt.Errorf("newSSHStorage failed") }) minimal["use-sshstorage"] = true testConfig, err = config.New(config.UseDefaults, minimal) c.Assert(err, gc.IsNil) _, err = manual.ProviderInstance.Prepare(coretesting.Context(c), testConfig) c.Assert(err, gc.ErrorMatches, "initialising SSH storage failed: newSSHStorage failed") }
// Prepare prepares an instance of the testing environment. func (t *Tests) Prepare(c *gc.C) environs.Environ { cfg, err := config.New(config.NoDefaults, t.TestConfig) c.Assert(err, gc.IsNil) e, err := environs.Prepare(cfg, coretesting.Context(c), t.ConfigStore) c.Assert(err, gc.IsNil, gc.Commentf("preparing environ %#v", t.TestConfig)) c.Assert(e, gc.NotNil) return e }
func minimalConfig(c *gc.C) *config.Config { minimal := minimalConfigValues() testConfig, err := config.New(config.NoDefaults, minimal) c.Assert(err, gc.IsNil) valid, err := local.Provider.Validate(testConfig, nil) c.Assert(err, gc.IsNil) return valid }
// CustomEnvironConfig returns an environment configuration with // additional specified keys added. func CustomEnvironConfig(c *gc.C, extra Attrs) *config.Config { attrs := FakeConfig().Merge(Attrs{ "agent-version": "1.2.3", }).Merge(extra).Delete("admin-secret", "ca-private-key") cfg, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) return cfg }
func (s *StorageSuite) SetUpTest(c *gc.C) { s.LoggingSuite.SetUpTest(c) cfg, err := config.New(config.NoDefaults, dummy.SampleConfig()) c.Assert(err, gc.IsNil) s.env, err = environs.Prepare(cfg, testing.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) s.dataDir = c.MkDir() }
func (st *State) EnvironConfig() (*config.Config, error) { settings, err := readSettings(st, environGlobalKey) if err != nil { return nil, err } attrs := settings.Map() return config.New(config.NoDefaults, attrs) }
func (*configSuite) TestEmptyImageStream1dot16Compat(c *gc.C) { attrs := makeAzureConfigMap(c) attrs["image-stream"] = "" provider := azureEnvironProvider{} cfg, err := config.New(config.UseDefaults, attrs) c.Assert(err, gc.IsNil) _, err = provider.Validate(cfg, nil) c.Assert(err, gc.IsNil) }
func (s *uploadSuite) SetUpTest(c *gc.C) { s.LoggingSuite.SetUpTest(c) s.ToolsFixture.SetUpTest(c) // We only want to use simplestreams to find any synced tools. cfg, err := config.New(config.NoDefaults, dummy.SampleConfig()) c.Assert(err, gc.IsNil) s.env, err = environs.Prepare(cfg, coretesting.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) }
func newTestConfig(c *gc.C, explicit testing.Attrs) *config.Config { final := testing.Attrs{"type": "my-type", "name": "my-name"} for key, value := range explicit { final[key] = value } result, err := config.New(config.UseDefaults, final) c.Assert(err, gc.IsNil) return result }
func (*configSuite) TestChecksLocationIsRequired(c *gc.C) { attrs := makeAzureConfigMap(c) attrs["location"] = "" provider := azureEnvironProvider{} newConfig, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) _, err = provider.Validate(newConfig, nil) c.Check(err, gc.ErrorMatches, ".*environment has no location.*") }