func (*OpenSuite) TestConfigForNameDefault(c *gc.C) { defer testing.MakeFakeHome(c, testing.SingleEnvConfig, testing.SampleCertName).Restore() cfg, source, err := environs.ConfigForName("", configstore.NewMem()) c.Assert(err, gc.IsNil) c.Assert(cfg.Name(), gc.Equals, "erewhemos") c.Assert(source, gc.Equals, environs.ConfigFromEnvirons) }
func (*SwitchSimpleSuite) TestListEnvironmentsOSJujuEnvSet(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfig).Restore() os.Setenv("JUJU_ENV", "using-env") context, err := testing.RunCommand(c, &SwitchCommand{}, []string{"--list"}) c.Assert(err, gc.IsNil) c.Assert(testing.Stdout(context), gc.Equals, expectedEnvironments) }
func (*OpenSuite) TestConfigForNameNoDefault(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfigNoDefault, testing.SampleCertName).Restore() cfg, source, err := environs.ConfigForName("", configstore.NewMem()) c.Assert(err, gc.ErrorMatches, "no default environment found") c.Assert(cfg, gc.IsNil) c.Assert(source, gc.Equals, environs.ConfigFromEnvirons) }
func (s *ValidateToolsMetadataSuite) SetUpTest(c *gc.C) { s.LoggingSuite.SetUpTest(c) s.home = coretesting.MakeFakeHome(c, metadataTestEnvConfig) s.metadataDir = c.MkDir() s.PatchEnvironment("AWS_ACCESS_KEY_ID", "access") s.PatchEnvironment("AWS_SECRET_ACCESS_KEY", "secret") }
func (*SwitchSimpleSuite) TestSettingWritesFile(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfig).Restore() context, err := testing.RunCommand(c, &SwitchCommand{}, []string{"erewhemos-2"}) c.Assert(err, gc.IsNil) c.Assert(testing.Stdout(context), gc.Equals, "erewhemos -> erewhemos-2\n") c.Assert(envcmd.ReadCurrentEnvironment(), gc.Equals, "erewhemos-2") }
func (*SwitchSimpleSuite) TestShowsJujuEnv(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfig).Restore() os.Setenv("JUJU_ENV", "using-env") context, err := testing.RunCommand(c, &SwitchCommand{}, nil) c.Assert(err, gc.IsNil) c.Assert(testing.Stdout(context), gc.Equals, "using-env\n") }
func (*SwitchSimpleSuite) TestCurrentEnvironmentHasPrecidence(c *gc.C) { home := testing.MakeFakeHome(c, testing.MultipleEnvConfig) defer home.Restore() home.AddFiles(c, []testing.TestFile{{".juju/current-environment", "fubar"}}) context, err := testing.RunCommand(c, &SwitchCommand{}, nil) c.Assert(err, gc.IsNil) c.Assert(testing.Stdout(context), gc.Equals, "fubar\n") }
func (s *datasourceSuite) SetUpTest(c *gc.C) { s.home = testing.MakeFakeHome(c, existingEnv, "existing") environ, err := environs.PrepareFromName("test", testing.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) s.stor = environ.Storage() s.baseURL, err = s.stor.URL("") c.Assert(err, gc.IsNil) }
func (*OpenSuite) TestNewFromNameWithInvalidEnvironConfig(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfigNoDefault, testing.SampleCertName).Restore() store := configstore.NewMem() e, err := environs.NewFromName("erewhemos", store) c.Assert(err, gc.Equals, environs.ErrNotBootstrapped) c.Assert(e, gc.IsNil) }
func (*OpenSuite) TestPrepareFromName(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfigNoDefault, testing.SampleCertName).Restore() ctx := testing.Context(c) e, err := environs.PrepareFromName("erewhemos", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) c.Assert(e.Name(), gc.Equals, "erewhemos") // Check we can access storage ok, which implies the environment has been prepared. c.Assert(e.Storage(), gc.NotNil) }
func (*SwitchSimpleSuite) TestJujuEnvOverCurrentEnvironment(c *gc.C) { home := testing.MakeFakeHome(c, testing.MultipleEnvConfig) defer home.Restore() home.AddFiles(c, []testing.TestFile{{".juju/current-environment", "fubar"}}) os.Setenv("JUJU_ENV", "using-env") context, err := testing.RunCommand(c, &SwitchCommand{}, nil) c.Assert(err, gc.IsNil) c.Assert(testing.Stdout(context), gc.Equals, "using-env\n") }
func (s *EnvironmentCommandSuite) TestEnsureEnvNameErrors(c *gc.C) { err := initEnvCommandBase(c, "").EnsureEnvName() c.Assert(err, jc.Satisfies, environs.IsNoEnv) // If there are multiple environments but no default, // an error should be returned. defer coretesting.MakeFakeHome(c, coretesting.MultipleEnvConfigNoDefault).Restore() err = initEnvCommandBase(c, "").EnsureEnvName() c.Assert(err, gc.Equals, envcmd.ErrNoEnvironmentSpecified) }
// makeEmptyFakeHome creates a faked home without envtools. func makeEmptyFakeHome(c *gc.C) (environs.Environ, *coretesting.FakeHome) { fake := coretesting.MakeFakeHome(c, envConfig) dummy.Reset() store, err := configstore.Default() c.Assert(err, gc.IsNil) env, err := environs.PrepareFromName("peckham", nullContext(c), store) c.Assert(err, gc.IsNil) envtesting.RemoveAllTools(c, env) return env, fake }
func (s *EnvironmentCommandSuite) TestEnsureEnvName(c *gc.C) { // Take environment name from command line arg. cmd := initEnvCommandBase(c, "explicit") err := cmd.EnsureEnvName() c.Assert(err, gc.IsNil) c.Assert(cmd.EnvName, gc.Equals, "explicit") // Take environment name from the default. defer coretesting.MakeFakeHome(c, coretesting.MultipleEnvConfig).Restore() testEnsureEnvName(c, coretesting.SampleEnvName) // Take environment name from the one and only environment, // even if it is not explicitly marked as default. defer coretesting.MakeFakeHome(c, coretesting.SingleEnvConfigNoDefault).Restore() testEnsureEnvName(c, coretesting.SampleEnvName) // If there is a current-environment file, use that. err = envcmd.WriteCurrentEnvironment("fubar") testEnsureEnvName(c, "fubar") }
func (*OpenSuite) TestNewFromName(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfigNoDefault, testing.SampleCertName).Restore() store := configstore.NewMem() ctx := testing.Context(c) e, err := environs.PrepareFromName("erewhemos", ctx, store) c.Assert(err, gc.IsNil) e, err = environs.NewFromName("erewhemos", store) c.Assert(err, gc.IsNil) c.Assert(e.Name(), gc.Equals, "erewhemos") }
func (s *verifyStorageSuite) TestVerifyStorageFails(c *gc.C) { defer testing.MakeFakeHome(c, existingEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) stor := environ.Storage() someError := errors.Unauthorizedf("you shall not pass") dummy.Poison(stor, environs.VerificationFilename, someError) err = environs.VerifyStorage(stor) c.Assert(err, gc.Equals, environs.VerifyStorageError) }
// An existing environments.yaml will not be overwritten without // the explicit -f option. func (*InitSuite) TestExistingEnvironmentNotOverwritten(c *gc.C) { defer testing.MakeFakeHome(c, existingEnv, "existing").Restore() ctx := testing.Context(c) code := cmd.Main(&InitCommand{}, ctx, nil) c.Check(code, gc.Equals, 1) errOut := ctx.Stderr.(*bytes.Buffer).String() strippedOut := strings.Replace(errOut, "\n", "", -1) c.Check(strippedOut, gc.Matches, ".*A juju environment configuration already exists.*") environpath := testing.HomePath(".juju", "environments.yaml") data, err := ioutil.ReadFile(environpath) c.Assert(err, gc.IsNil) c.Assert(string(data), gc.Equals, existingEnv) }
func (s *ImageMetadataSuite) SetUpTest(c *gc.C) { s.LoggingSuite.SetUpTest(c) os.Clearenv() s.dir = c.MkDir() // Create a fake certificate so azure test environment can be opened. certfile, err := ioutil.TempFile(s.dir, "") c.Assert(err, gc.IsNil) filename := certfile.Name() err = ioutil.WriteFile(filename, []byte("test certificate"), 0644) c.Assert(err, gc.IsNil) envConfig := strings.Replace(metadataTestEnvConfig, "/home/me/azure.pem", filename, -1) s.home = testing.MakeFakeHome(c, envConfig) s.PatchEnvironment("AWS_ACCESS_KEY_ID", "access") s.PatchEnvironment("AWS_SECRET_ACCESS_KEY", "secret") }
// An existing environments.yaml will be overwritten when -f is // given explicitly. func (*InitSuite) TestExistingEnvironmentOverwritten(c *gc.C) { defer testing.MakeFakeHome(c, existingEnv, "existing").Restore() ctx := testing.Context(c) code := cmd.Main(&InitCommand{}, ctx, []string{"-f"}) c.Check(code, gc.Equals, 0) stdOut := ctx.Stdout.(*bytes.Buffer).String() strippedOut := strings.Replace(stdOut, "\n", "", -1) c.Check(strippedOut, gc.Matches, ".*A boilerplate environment configuration file has been written.*") environpath := testing.HomePath(".juju", "environments.yaml") data, err := ioutil.ReadFile(environpath) c.Assert(err, gc.IsNil) strippedData := strings.Replace(string(data), "\n", "", -1) c.Assert(strippedData, gc.Matches, ".*# This is the Juju config file, which you can use.*") }
func (s *checkEnvironmentSuite) TestCheckEnvironment(c *gc.C) { defer testing.MakeFakeHome(c, checkEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) // VerifyStorage is sufficient for our tests and much simpler // than Bootstrap which calls it. stor := environ.Storage() err = environs.VerifyStorage(stor) c.Assert(err, gc.IsNil) err = environs.CheckEnvironment(environ) c.Assert(err, gc.IsNil) }
func (s *syncToolsSuite) SetUpTest(c *gc.C) { s.LoggingSuite.SetUpTest(c) // Create a target environments.yaml and make sure its environment is empty. s.home = coretesting.MakeFakeHome(c, ` environments: test-target: type: dummy state-server: false authorized-keys: "not-really-one" `) var err error s.configStore, err = configstore.Default() c.Assert(err, gc.IsNil) s.origSyncTools = syncTools }
func (s *verifyStorageSuite) TestVerifyStorage(c *gc.C) { defer testing.MakeFakeHome(c, existingEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) stor := environ.Storage() err = environs.VerifyStorage(stor) c.Assert(err, gc.IsNil) reader, err := storage.Get(stor, environs.VerificationFilename) c.Assert(err, gc.IsNil) defer reader.Close() contents, err := ioutil.ReadAll(reader) c.Assert(err, gc.IsNil) c.Check(string(contents), gc.Equals, "juju-core storage writing verified: ok\n") }
func (s *checkEnvironmentSuite) TestCheckEnvironmentBadContent(c *gc.C) { defer testing.MakeFakeHome(c, checkEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) // We mock a bad (eg. from a Python-juju environment) bootstrap-verify. stor := environ.Storage() content := "bad verification content" reader := strings.NewReader(content) err = stor.Put(environs.VerificationFilename, reader, int64(len(content))) c.Assert(err, gc.IsNil) // When the bootstrap-verify file contains unexpected content, // we get an InvalidEnvironmentError. err = environs.CheckEnvironment(environ) c.Assert(err, gc.Equals, environs.InvalidEnvironmentError) }
func (s *syncSuite) setUpTest(c *gc.C) { s.LoggingSuite.SetUpTest(c) s.ToolsFixture.SetUpTest(c) s.origVersion = version.Current // It's important that this be v1.8.x to match the test data. version.Current.Number = version.MustParse("1.8.3") // Create a target environments.yaml. fakeHome := coretesting.MakeFakeHome(c, ` environments: test-target: type: dummy state-server: false authorized-keys: "not-really-one" `) s.AddCleanup(func(*gc.C) { fakeHome.Restore() }) var err error s.targetEnv, err = environs.PrepareFromName("test-target", coretesting.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) envtesting.RemoveAllTools(c, s.targetEnv) // Create a source storage. baseDir := c.MkDir() stor, err := filestorage.NewFileStorageWriter(baseDir) c.Assert(err, gc.IsNil) s.storage = stor // Create a local tools directory. s.localStorage = c.MkDir() // Populate both local and default tools locations with the public tools. versionStrings := make([]string, len(vAll)) for i, vers := range vAll { versionStrings[i] = vers.String() } ttesting.MakeTools(c, baseDir, "releases", versionStrings) ttesting.MakeTools(c, s.localStorage, "releases", versionStrings) // Switch the default tools location. baseURL, err := s.storage.URL(storage.BaseToolsPath) c.Assert(err, gc.IsNil) s.PatchValue(&envtools.DefaultBaseURL, baseURL) }
func (*OpenSuite) TestNewFromNameWithInvalidInfo(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfigNoDefault, testing.SampleCertName).Restore() store := configstore.NewMem() cfg, _, err := environs.ConfigForName("erewhemos", store) c.Assert(err, gc.IsNil) info, err := store.CreateInfo("erewhemos") c.Assert(err, gc.IsNil) // The configuration from environments.yaml is invalid // because it doesn't contain the state-id attribute which // the dummy environment adds at Prepare time. info.SetBootstrapConfig(cfg.AllAttrs()) err = info.Write() c.Assert(err, gc.IsNil) e, err := environs.NewFromName("erewhemos", store) c.Assert(err, gc.ErrorMatches, "environment is not prepared") c.Assert(e, gc.IsNil) }
func (s *checkEnvironmentSuite) TestCheckEnvironmentGetFails(c *gc.C) { defer testing.MakeFakeHome(c, checkEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) // VerifyStorage is sufficient for our tests and much simpler // than Bootstrap which calls it. stor := environ.Storage() err = environs.VerifyStorage(stor) c.Assert(err, gc.IsNil) // When fetching the verification file from storage fails, // we get an InvalidEnvironmentError. someError := errors.Unauthorizedf("you shall not pass") dummy.Poison(stor, environs.VerificationFilename, someError) err = environs.CheckEnvironment(environ) c.Assert(err, gc.Equals, someError) }
func (s *checkEnvironmentSuite) TestCheckEnvironmentFileNotFound(c *gc.C) { defer testing.MakeFakeHome(c, checkEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) // VerifyStorage is sufficient for our tests and much simpler // than Bootstrap which calls it. stor := environ.Storage() err = environs.VerifyStorage(stor) c.Assert(err, gc.IsNil) // When the bootstrap-verify file does not exist, it still believes // the environment is a juju-core one because earlier versions // did not create that file. err = stor.Remove(environs.VerificationFilename) c.Assert(err, gc.IsNil) err = environs.CheckEnvironment(environ) c.Assert(err, gc.IsNil) }
func (*OpenSuite) TestConfigForNameFromInfo(c *gc.C) { defer testing.MakeFakeHome(c, testing.SingleEnvConfig, testing.SampleCertName).Restore() store := configstore.NewMem() cfg, source, err := environs.ConfigForName("", store) c.Assert(err, gc.IsNil) c.Assert(source, gc.Equals, environs.ConfigFromEnvirons) info, err := store.CreateInfo("test-config") c.Assert(err, gc.IsNil) var attrs testing.Attrs = cfg.AllAttrs() attrs = attrs.Merge(testing.Attrs{ "name": "test-config", }) info.SetBootstrapConfig(attrs) err = info.Write() c.Assert(err, gc.IsNil) cfg, source, err = environs.ConfigForName("test-config", store) c.Assert(err, gc.IsNil) c.Assert(source, gc.Equals, environs.ConfigFromInfo) c.Assert(testing.Attrs(cfg.AllAttrs()), gc.DeepEquals, attrs) }
func (*SwitchSimpleSuite) TestSettingWhenJujuEnvSet(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfig).Restore() os.Setenv("JUJU_ENV", "using-env") _, err := testing.RunCommand(c, &SwitchCommand{}, []string{"erewhemos-2"}) c.Assert(err, gc.ErrorMatches, `cannot switch when JUJU_ENV is overriding the environment \(set to "using-env"\)`) }
func (*SwitchSimpleSuite) TestSettingToUnknown(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfig).Restore() _, err := testing.RunCommand(c, &SwitchCommand{}, []string{"unknown"}) c.Assert(err, gc.ErrorMatches, `"unknown" is not a name of an existing defined environment`) }