// The boilerplate is sent to stdout with --show, and the environments.yaml // is not created. func (*InitSuite) TestBoilerPlatePrinted(c *gc.C) { envPath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") err := os.Remove(envPath) c.Assert(err, jc.ErrorIsNil) ctx := testing.Context(c) code := cmd.Main(&initCommand{}, ctx, []string{"--show"}) c.Check(code, gc.Equals, 0) outStr := ctx.Stdout.(*bytes.Buffer).String() strippedOut := strings.Replace(outStr, "\n", "", -1) c.Check(strippedOut, gc.Matches, ".*# This is the Juju config file, which you can use.*") environpath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") _, err = ioutil.ReadFile(environpath) c.Assert(err, gc.NotNil) }
func (s *suite) TestConfigPerm(c *gc.C) { testing.MakeSampleJujuHome(c) path := gitjujutesting.JujuXDGDataHomePath() info, err := os.Lstat(path) c.Assert(err, jc.ErrorIsNil) oldPerm := info.Mode().Perm() env := ` environments: only: type: dummy controller: false authorized-keys: i-am-a-key ` outfile, err := environs.WriteEnvirons("", env) c.Assert(err, jc.ErrorIsNil) info, err = os.Lstat(outfile) c.Assert(err, jc.ErrorIsNil) // Windows is not fully POSIX compliant. Normal permission // checking will yield unexpected results if runtime.GOOS != "windows" { c.Assert(info.Mode().Perm(), gc.Equals, os.FileMode(0600)) } info, err = os.Lstat(filepath.Dir(outfile)) c.Assert(err, jc.ErrorIsNil) if runtime.GOOS != "windows" { c.Assert(info.Mode().Perm(), gc.Equals, oldPerm) } }
func (*SwitchSimpleSuite) TestNoDefaultNoEnvironmentsFile(c *gc.C) { envPath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") err := os.Remove(envPath) c.Assert(err, jc.ErrorIsNil) _, err = testing.RunCommand(c, newSwitchCommand()) c.Assert(err, gc.ErrorMatches, "no currently specified model") }
// The environments.yaml is created by default if it // does not already exist. func (*InitSuite) TestBoilerPlateEnvironment(c *gc.C) { envPath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") err := os.Remove(envPath) c.Assert(err, jc.ErrorIsNil) ctx := testing.Context(c) code := cmd.Main(&initCommand{}, ctx, nil) c.Check(code, gc.Equals, 0) outStr := ctx.Stdout.(*bytes.Buffer).String() strippedOut := strings.Replace(outStr, "\n", "", -1) c.Check(strippedOut, gc.Matches, ".*A boilerplate model configuration file has been written.*") environpath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") data, err := ioutil.ReadFile(environpath) c.Assert(err, jc.ErrorIsNil) strippedData := strings.Replace(string(data), "\n", "", -1) c.Assert(strippedData, gc.Matches, ".*# This is the Juju config file, which you can use.*") }
func (s *jenvSuite) SetUpTest(c *gc.C) { s.FakeJujuXDGDataHomeSuite.SetUpTest(c) dir := gitjujutesting.JujuXDGDataHomePath() err := os.MkdirAll(dir, 0600) c.Check(err, jc.ErrorIsNil) }
func (s *FakeJujuXDGDataHomeSuite) SetUpTest(c *gc.C) { s.JujuOSEnvSuite.SetUpTest(c) s.FakeHomeSuite.SetUpTest(c) jujuXDGDataHome := gitjujutesting.JujuXDGDataHomePath() err := os.MkdirAll(jujuXDGDataHome, 0700) c.Assert(err, jc.ErrorIsNil) s.oldJujuXDGDataHome = osenv.SetJujuXDGDataHome(jujuXDGDataHome) }
func (s *ModelCommandSuite) TestGetDefaultModelNothingSet(c *gc.C) { envPath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") err := os.Remove(envPath) c.Assert(err, jc.ErrorIsNil) env, err := modelcmd.GetDefaultModel() c.Assert(env, gc.Equals, "") c.Assert(err, jc.ErrorIsNil) }
func (*suite) TestNoEnv(c *gc.C) { envPath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") err := os.Remove(envPath) c.Assert(err, jc.ErrorIsNil) es, err := environs.ReadEnvirons("") c.Assert(es, gc.IsNil) c.Assert(err, jc.Satisfies, environs.IsNoEnv) }
func (s *ControllerCommandSuite) TestControllerCommandInitNoEnvFile(c *gc.C) { // Since we ignore the environments.yaml file, we don't care if it isn't // there. envPath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") err := os.Remove(envPath) _, err = initTestControllerCommand(c) c.Assert(err, gc.ErrorMatches, "no controller specified") }
func (s *FakeJujuXDGDataHomeSuite) SetUpTest(c *gc.C) { s.JujuOSEnvSuite.SetUpTest(c) s.FakeHomeSuite.SetUpTest(c) jujuXDGDataHome := gitjujutesting.JujuXDGDataHomePath() err := os.MkdirAll(jujuXDGDataHome, 0700) c.Assert(err, jc.ErrorIsNil) s.oldJujuXDGDataHome = osenv.SetJujuXDGDataHome(jujuXDGDataHome) WriteEnvironments(c, SingleEnvConfig, SampleCertName) }
func (s *SwitchSimpleSuite) TestNoEnvironmentReadsConfigStore(c *gc.C) { envPath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") err := os.Remove(envPath) c.Assert(err, jc.ErrorIsNil) s.addTestController(c) context, err := testing.RunCommand(c, newSwitchCommand(), "--list") c.Assert(err, jc.ErrorIsNil) c.Assert(testing.Stdout(context), gc.Equals, "a-controller (controller)\n") }
func (s *SwitchSimpleSuite) TestErrorReadingEnvironmentsFile(c *gc.C) { if runtime.GOOS == "windows" { c.Skip("bug 1496997: os.Chmod doesn't exist on windows, checking this on one platform is sufficent to test this case") } envPath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") err := os.Chmod(envPath, 0) c.Assert(err, jc.ErrorIsNil) s.addTestController(c) _, err = testing.RunCommand(c, newSwitchCommand(), "--list") c.Assert(err, gc.ErrorMatches, "couldn't read the model: open .*: permission denied") }
// resetJujuXDGDataHome restores an new, clean Juju home environment without tools. func resetJujuXDGDataHome(c *gc.C, envName string) environs.Environ { jenvDir := testing.JujuXDGDataHomePath("models") err := os.RemoveAll(jenvDir) c.Assert(err, jc.ErrorIsNil) coretesting.WriteEnvironments(c, modelConfig) dummy.Reset() store, err := configstore.Default() c.Assert(err, jc.ErrorIsNil) env, err := environs.PrepareFromName(envName, modelcmd.BootstrapContext(cmdtesting.NullContext(c)), store) c.Assert(err, jc.ErrorIsNil) return env }
// An existing environments.yaml will not be overwritten without // the explicit -f option. func (*InitSuite) TestExistingEnvironmentNotOverwritten(c *gc.C) { testing.WriteEnvironments(c, existingEnv) 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 model configuration already exists.*") environpath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") data, err := ioutil.ReadFile(environpath) c.Assert(err, jc.ErrorIsNil) c.Assert(string(data), gc.Equals, existingEnv) }
// assertJenvContents checks that the jenv file corresponding to the given // envName is correctly present in the Juju Home and has the given contents. func assertJenvContents(c *gc.C, contents []byte, envName string) { path := gitjujutesting.JujuXDGDataHomePath("models", envName+".jenv") // Ensure the jenv file has been created. c.Assert(path, jc.IsNonEmptyFile) // Retrieve the jenv file contents. b, err := ioutil.ReadFile(path) c.Assert(err, jc.ErrorIsNil) // Ensure the jenv file reflects the source contents. var data map[string]interface{} err = yaml.Unmarshal(contents, &data) c.Assert(err, jc.ErrorIsNil) c.Assert(string(b), jc.YAMLEquals, data) }
// resetJujuXDGDataHome restores an new, clean Juju home environment without tools. func resetJujuXDGDataHome(c *gc.C) { jenvDir := testing.JujuXDGDataHomePath("models") err := os.RemoveAll(jenvDir) c.Assert(err, jc.ErrorIsNil) cloudsPath := cloud.JujuPersonalCloudsPath() err = ioutil.WriteFile(cloudsPath, []byte(` clouds: dummy-cloud: type: dummy regions: region-1: region-2: dummy-cloud-without-regions: type: dummy `[1:]), 0644) c.Assert(err, jc.ErrorIsNil) }
func (*jenvSuite) TestSuccessNoJujuEnvironments(c *gc.C) { // Create a jenv file. contents := makeValidJenvContents() f := openJenvFile(c, contents) defer f.Close() // Remove the Juju environments.yaml file. envPath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") err := os.Remove(envPath) c.Assert(err, jc.ErrorIsNil) // Importing a jenv does not require having environments already defined. jenvCmd := &model.JenvCommand{} ctx, err := testing.RunCommand(c, jenvCmd, f.Name()) c.Assert(err, jc.ErrorIsNil) assertJenvContents(c, contents, "testing") c.Assert(testing.Stdout(ctx), gc.Equals, "-> testing\n") }
func (*jenvSuite) TestSwitchErrorEnvironmentsNotReadable(c *gc.C) { if runtime.GOOS == "windows" { c.Skip("Cannot test on windows because it uses chmod") } // Create a jenv file. f := openJenvFile(c, makeValidJenvContents()) defer f.Close() // Remove write permissions to the environments.yaml file. envPath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") err := os.Chmod(envPath, 0200) c.Assert(err, jc.ErrorIsNil) jenvCmd := &model.JenvCommand{} ctx, err := testing.RunCommand(c, jenvCmd, f.Name()) c.Assert(err, gc.ErrorMatches, `cannot switch to the new model "testing": cannot get the default model: .*: permission denied`) c.Assert(testing.Stdout(ctx), gc.Equals, "") }
func (*jenvSuite) TestWriteError(c *gc.C) { if runtime.GOOS == "windows" { c.Skip("Cannot test on windows because it uses chmod") } // Create a jenv file. f := openJenvFile(c, makeValidJenvContents()) defer f.Close() // Create the environments dir without write permissions. envsDir := gitjujutesting.JujuXDGDataHomePath("models") err := os.Mkdir(envsDir, 0500) c.Assert(err, jc.ErrorIsNil) jenvCmd := &model.JenvCommand{} ctx, err := testing.RunCommand(c, jenvCmd, f.Name()) c.Assert(err, gc.ErrorMatches, "cannot write the jenv file: cannot write info: .*: permission denied") c.Assert(testing.Stdout(ctx), gc.Equals, "") }
func (*jenvSuite) TestConfigStoreError(c *gc.C) { if runtime.GOOS == "windows" { c.Skip("Cannot test on windows because it uses chmod") } // Create a jenv file. f := openJenvFile(c, nil) defer f.Close() // Remove Juju home read permissions. home := gitjujutesting.JujuXDGDataHomePath() err := os.Chmod(home, 0) c.Assert(err, jc.ErrorIsNil) defer os.Chmod(home, 0700) jenvCmd := &model.JenvCommand{} ctx, err := testing.RunCommand(c, jenvCmd, f.Name()) c.Assert(err, gc.ErrorMatches, "cannot get config store: .*: permission denied") c.Assert(testing.Stdout(ctx), gc.Equals, "") }
func (*suite) TestDefaultConfigFile(c *gc.C) { env := ` environments: only: type: dummy controller: false authorized-keys: i-am-a-key ` outfile, err := environs.WriteEnvirons("", env) c.Assert(err, jc.ErrorIsNil) path := gitjujutesting.JujuXDGDataHomePath("environments.yaml") c.Assert(path, gc.Equals, outfile) envs, err := environs.ReadEnvirons("") c.Assert(err, jc.ErrorIsNil) cfg, err := envs.Config("") c.Assert(err, jc.ErrorIsNil) c.Assert(cfg.Name(), gc.Equals, "only") }
func (*jenvSuite) TestSwitchErrorCannotWriteCurrentModel(c *gc.C) { if runtime.GOOS == "windows" { c.Skip("Cannot test on windows because it uses chmod") } // Create a jenv file. f := openJenvFile(c, makeValidJenvContents()) defer f.Close() // Create the current environment file without write permissions. currentEnvPath := gitjujutesting.JujuXDGDataHomePath(modelcmd.CurrentModelFilename) currentEnvFile, err := os.Create(currentEnvPath) c.Assert(err, jc.ErrorIsNil) defer currentEnvFile.Close() err = currentEnvFile.Chmod(0500) c.Assert(err, jc.ErrorIsNil) jenvCmd := &model.JenvCommand{} ctx, err := testing.RunCommand(c, jenvCmd, f.Name()) c.Assert(err, gc.ErrorMatches, `cannot switch to the new model "testing": unable to write to the model file: .*: permission denied`) c.Assert(testing.Stdout(ctx), gc.Equals, "") }
func (s *BootstrapSuite) TestBootstrapPropagatesEnvErrors(c *gc.C) { //TODO(bogdanteleaga): fix this for windows once permissions are fixed if runtime.GOOS == "windows" { c.Skip("bug 1403084: this is very platform specific. When/if we will support windows state machine, this will probably be rewritten.") } const envName = "devenv" s.patchVersionAndSeries(c, envName) s.PatchValue(&environType, func(string) (string, error) { return "", nil }) _, err := coretesting.RunCommand(c, newBootstrapCommand(), "-m", envName, "--auto-upgrade") c.Assert(err, jc.ErrorIsNil) // Change permissions on the jenv file to simulate some kind of // unexpected error when trying to read info from the environment jenvFile := testing.JujuXDGDataHomePath("models", "cache.yaml") err = os.Chmod(jenvFile, os.FileMode(0200)) c.Assert(err, jc.ErrorIsNil) // The second bootstrap should fail b/c of the propogated error _, err = coretesting.RunCommand(c, newBootstrapCommand(), "-m", envName) c.Assert(err, gc.ErrorMatches, "there was an issue examining the model: .*") }
func (s *fakeHomeSuite) TestFakeHomeSetsConfigJujuXDGDataHome(c *gc.C) { s.PatchEnvironment(osenv.XDGDataHome, "") expected := gitjujutesting.JujuXDGDataHomePath() c.Assert(osenv.JujuXDGDataHome(), gc.Equals, expected) }
func (s *fakeHomeSuite) TestFakeHomeSetsUpJujuXDGDataHome(c *gc.C) { jujuDir := gitjujutesting.JujuXDGDataHomePath() c.Assert(jujuDir, jc.IsDirectory) }
func (s *ModelCommandSuite) TestModelCommandInitNoEnvFile(c *gc.C) { envPath := gitjujutesting.JujuXDGDataHomePath("environments.yaml") err := os.Remove(envPath) c.Assert(err, jc.ErrorIsNil) testEnsureModelName(c, "") }
func (s *fakeHomeSuite) TestFakeHomeSetsUpJujuXDGDataHome(c *gc.C) { jujuDir := gitjujutesting.JujuXDGDataHomePath() c.Assert(jujuDir, jc.IsDirectory) envFile := gitjujutesting.JujuXDGDataHomePath("environments.yaml") c.Assert(envFile, jc.IsNonEmptyFile) }