func (s *ClientKeysSuite) TestPrivateKeyFiles(c *gc.C) {
	// Create/load client keys. They will be cached in memory:
	// any files added to the directory will not be considered
	// unless LoadClientKeys is called again.
	err := ssh.LoadClientKeys("~/.juju/ssh")
	c.Assert(err, jc.ErrorIsNil)
	checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")
	priv, pub, err := ssh.GenerateKey("whatever")
	c.Assert(err, jc.ErrorIsNil)
	err = ioutil.WriteFile(gitjujutesting.HomePath(".juju", "ssh", "whatever"), []byte(priv), 0600)
	c.Assert(err, jc.ErrorIsNil)
	err = ssh.LoadClientKeys("~/.juju/ssh")
	c.Assert(err, jc.ErrorIsNil)
	// The new private key won't be observed until the
	// corresponding public key exists.
	checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")
	err = ioutil.WriteFile(gitjujutesting.HomePath(".juju", "ssh", "whatever.pub"), []byte(pub), 0600)
	c.Assert(err, jc.ErrorIsNil)
	// new keys won't be reported until we call LoadClientKeys again
	checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub")
	checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")
	err = ssh.LoadClientKeys("~/.juju/ssh")
	c.Assert(err, jc.ErrorIsNil)
	checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub", "~/.juju/ssh/whatever.pub")
	checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa", "~/.juju/ssh/whatever")
}
Exemple #2
0
// The boilerplate is sent to stdout with --show, and the environments.yaml
// is not created.
func (*InitSuite) TestBoilerPlatePrinted(c *gc.C) {
	envPath := gitjujutesting.HomePath(".juju", "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.HomePath(".juju", "environments.yaml")
	_, err = ioutil.ReadFile(environpath)
	c.Assert(err, gc.NotNil)
}
Exemple #3
0
func (suite *PluginSuite) makeFullPlugin(params PluginParams) {
	// Create a new template and parse the plugin into it.
	t := template.Must(template.New("plugin").Parse(pluginTemplate))
	content := &bytes.Buffer{}
	filename := gitjujutesting.HomePath("juju-" + params.Name)
	// Create the files in the temp dirs, so we don't pollute the working space
	if params.Creates != "" {
		params.Creates = gitjujutesting.HomePath(params.Creates)
	}
	if params.DependsOn != "" {
		params.DependsOn = gitjujutesting.HomePath(params.DependsOn)
	}
	t.Execute(content, params)
	ioutil.WriteFile(filename, content.Bytes(), 0755)
}
func (s *ClientKeysSuite) TestLoadClientKeysDirExists(c *gc.C) {
	err := os.MkdirAll(gitjujutesting.HomePath(".juju", "ssh"), 0755)
	c.Assert(err, jc.ErrorIsNil)
	err = ssh.LoadClientKeys("~/.juju/ssh")
	c.Assert(err, jc.ErrorIsNil)
	checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")
}
Exemple #5
0
// In the case where we cannot examine an environment, we want the
// error to propagate back up to the user.
func (s *BootstrapSuite) TestBootstrapPropagatesEnvErrors(c *gc.C) {

	env := resetJujuHome(c)
	defaultSeriesVersion := version.Current
	defaultSeriesVersion.Series = config.PreferredSeries(env.Config())
	// Force a dev version by having a non zero build number.
	// This is because we have not uploaded any tools and auto
	// upload is only enabled for dev versions.
	defaultSeriesVersion.Build = 1234
	s.PatchValue(&version.Current, defaultSeriesVersion)

	const envName = "peckham"
	_, err := coretesting.RunCommand(c, envcmd.Wrap(&BootstrapCommand{}), "-e", envName)
	c.Assert(err, gc.IsNil)

	// Change permissions on the jenv file to simulate some kind of
	// unexpected error when trying to read info from the environment
	jenvFile := gitjujutesting.HomePath(".juju", "environments", envName+".jenv")
	err = os.Chmod(jenvFile, os.FileMode(0200))
	c.Assert(err, gc.IsNil)

	// The second bootstrap should fail b/c of the propogated error
	_, err = coretesting.RunCommand(c, envcmd.Wrap(&BootstrapCommand{}), "-e", envName)
	c.Assert(err, gc.ErrorMatches, "there was an issue examining the environment: .*")
}
Exemple #6
0
func (s *suite) TestConfigPerm(c *gc.C) {
	testing.MakeSampleJujuHome(c)

	path := gitjujutesting.HomePath(".juju")
	info, err := os.Lstat(path)
	c.Assert(err, gc.IsNil)
	oldPerm := info.Mode().Perm()
	env := `
environments:
    only:
        type: dummy
        state-server: false
        authorized-keys: i-am-a-key
`
	outfile, err := environs.WriteEnvirons("", env)
	c.Assert(err, gc.IsNil)

	info, err = os.Lstat(outfile)
	c.Assert(err, gc.IsNil)
	c.Assert(info.Mode().Perm(), gc.Equals, os.FileMode(0600))

	info, err = os.Lstat(filepath.Dir(outfile))
	c.Assert(err, gc.IsNil)
	c.Assert(info.Mode().Perm(), gc.Equals, oldPerm)

}
Exemple #7
0
// The environments.yaml is created by default if it
// does not already exist.
func (*InitSuite) TestBoilerPlateEnvironment(c *gc.C) {
	envPath := gitjujutesting.HomePath(".juju", "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 environment configuration file has been written.*")
	environpath := gitjujutesting.HomePath(".juju", "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.*")
}
Exemple #8
0
func (*SwitchSimpleSuite) TestNoEnvironment(c *gc.C) {
	envPath := gitjujutesting.HomePath(".juju", "environments.yaml")
	err := os.Remove(envPath)
	c.Assert(err, jc.ErrorIsNil)
	_, err = testing.RunCommand(c, &SwitchCommand{})
	c.Assert(err, gc.ErrorMatches, "couldn't read the environment")
}
Exemple #9
0
func (s *suite) TestConfigPerm(c *gc.C) {
	testing.MakeSampleJujuHome(c)

	path := gitjujutesting.HomePath(".juju")
	info, err := os.Lstat(path)
	c.Assert(err, jc.ErrorIsNil)
	oldPerm := info.Mode().Perm()
	env := `
environments:
    only:
        type: dummy
        state-server: 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)
	}

}
Exemple #10
0
// In the case where we cannot examine an environment, we want the
// error to propagate back up to the user.
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"
	env := resetJujuHome(c, envName)
	defaultSeriesVersion := version.Current
	defaultSeriesVersion.Series = config.PreferredSeries(env.Config())
	// Force a dev version by having a non zero build number.
	// This is because we have not uploaded any tools and auto
	// upload is only enabled for dev versions.
	defaultSeriesVersion.Build = 1234
	s.PatchValue(&version.Current, defaultSeriesVersion)
	s.PatchValue(&environType, func(string) (string, error) { return "", nil })

	_, err := coretesting.RunCommand(c, envcmd.Wrap(&BootstrapCommand{}), "-e", envName)
	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 := gitjujutesting.HomePath(".juju", "environments", envName+".jenv")
	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, envcmd.Wrap(&BootstrapCommand{}), "-e", envName)
	c.Assert(err, gc.ErrorMatches, "there was an issue examining the environment: .*")
}
Exemple #11
0
func (*SwitchSimpleSuite) TestNoDefaultNoEnvironmentsFile(c *gc.C) {
	envPath := gitjujutesting.HomePath(".juju", "environments.yaml")
	err := os.Remove(envPath)
	c.Assert(err, jc.ErrorIsNil)
	_, err = testing.RunCommand(c, newSwitchCommand())
	c.Assert(err, gc.ErrorMatches, "no currently specified environment")
}
Exemple #12
0
func (s *ModelCommandSuite) TestGetDefaultModelNothingSet(c *gc.C) {
	envPath := gitjujutesting.HomePath(".juju", "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 (s *SystemCommandSuite) TestSystemCommandInitNoEnvFile(c *gc.C) {
	// Since we ignore the environments.yaml file, we don't care if it isn't
	// there.
	envPath := gitjujutesting.HomePath(".juju", "environments.yaml")
	err := os.Remove(envPath)
	_, err = initTestSystemCommand(c)
	c.Assert(err, gc.ErrorMatches, "no system specified")
}
func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentNothingSet(c *gc.C) {
	envPath := gitjujutesting.HomePath(".juju", "environments.yaml")
	err := os.Remove(envPath)
	c.Assert(err, gc.IsNil)
	env, err := envcmd.GetDefaultEnvironment()
	c.Assert(env, gc.Equals, "")
	c.Assert(err, jc.Satisfies, environs.IsNoEnv)
}
Exemple #15
0
func (*suite) TestNoEnv(c *gc.C) {
	envPath := gitjujutesting.HomePath(".juju", "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)
}
Exemple #16
0
func (s *SwitchSimpleSuite) TestNoEnvironmentReadsConfigStore(c *gc.C) {
	envPath := gitjujutesting.HomePath(".juju", "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")
}
Exemple #17
0
func (suite *PluginSuite) SetUpTest(c *gc.C) {
	//TODO(bogdanteleaga): Fix bash tests
	if runtime.GOOS == "windows" {
		c.Skip("bug 1403084: tests use bash scrips, will be rewritten for windows")
	}
	suite.FakeJujuXDGDataHomeSuite.SetUpTest(c)
	suite.oldPath = os.Getenv("PATH")
	os.Setenv("PATH", "/bin:"+gitjujutesting.HomePath())
}
Exemple #18
0
func (s *FakeJujuHomeSuite) SetUpTest(c *gc.C) {
	s.JujuOSEnvSuite.SetUpTest(c)
	s.FakeHomeSuite.SetUpTest(c)
	jujuHome := gitjujutesting.HomePath(".juju")
	err := os.Mkdir(jujuHome, 0700)
	c.Assert(err, gc.IsNil)
	s.oldJujuHome = osenv.SetJujuHome(jujuHome)
	WriteEnvironments(c, SingleEnvConfig, SampleCertName)
}
Exemple #19
0
func (s *fakeHomeSuite) TestSshDirSetUp(c *gc.C) {
	// The SSH directory is properly created and set up.
	s.fakeHomeSuite.SetUpTest(c)
	sshDir := testing.HomePath(".ssh")
	c.Assert(sshDir, jc.IsDirectory)
	PrivKeyFile := filepath.Join(sshDir, "id_rsa")
	c.Assert(PrivKeyFile, jc.IsNonEmptyFile)
	PubKeyFile := filepath.Join(sshDir, "id_rsa.pub")
	c.Assert(PubKeyFile, jc.IsNonEmptyFile)
	s.fakeHomeSuite.TearDownTest(c)
}
Exemple #20
0
// resetJujuHome restores an new, clean Juju home environment without tools.
func resetJujuHome(c *gc.C, envName string) environs.Environ {
	jenvDir := gitjujutesting.HomePath(".juju", "environments")
	err := os.RemoveAll(jenvDir)
	c.Assert(err, jc.ErrorIsNil)
	coretesting.WriteEnvironments(c, envConfig)
	dummy.Reset()
	store, err := configstore.Default()
	c.Assert(err, jc.ErrorIsNil)
	env, err := environs.PrepareFromName(envName, envcmd.BootstrapContext(cmdtesting.NullContext(c)), store)
	c.Assert(err, jc.ErrorIsNil)
	return env
}
Exemple #21
0
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.HomePath(".juju", "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 environment: open .*: permission denied")
}
Exemple #22
0
// resetJujuHome restores an new, clean Juju home environment without tools.
func resetJujuHome(c *gc.C) environs.Environ {
	jenvDir := gitjujutesting.HomePath(".juju", "environments")
	err := os.RemoveAll(jenvDir)
	c.Assert(err, gc.IsNil)
	coretesting.WriteEnvironments(c, envConfig)
	dummy.Reset()
	store, err := configstore.Default()
	c.Assert(err, gc.IsNil)
	env, err := environs.PrepareFromName("peckham", cmdtesting.NullContext(c), store)
	c.Assert(err, gc.IsNil)
	envtesting.RemoveAllTools(c, env)
	return env
}
Exemple #23
0
func (s *ClientKeysSuite) TestPublicKeyFiles(c *gc.C) {
	// LoadClientKeys will create the specified directory
	// and populate it with a key pair.
	err := ssh.LoadClientKeys("~/.juju/ssh")
	c.Assert(err, jc.ErrorIsNil)
	checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub")
	// All files ending with .pub in the client key dir get picked up.
	priv, pub, err := ssh.GenerateKey("whatever")
	c.Assert(err, jc.ErrorIsNil)
	err = ioutil.WriteFile(gitjujutesting.HomePath(".juju", "ssh", "whatever.pub"), []byte(pub), 0600)
	c.Assert(err, jc.ErrorIsNil)
	err = ssh.LoadClientKeys("~/.juju/ssh")
	c.Assert(err, jc.ErrorIsNil)
	// The new public key won't be observed until the
	// corresponding private key exists.
	checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub")
	err = ioutil.WriteFile(gitjujutesting.HomePath(".juju", "ssh", "whatever"), []byte(priv), 0600)
	c.Assert(err, jc.ErrorIsNil)
	err = ssh.LoadClientKeys("~/.juju/ssh")
	c.Assert(err, jc.ErrorIsNil)
	checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub", "~/.juju/ssh/whatever.pub")
}
Exemple #24
0
// 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 environment configuration already exists.*")
	environpath := gitjujutesting.HomePath(".juju", "environments.yaml")
	data, err := ioutil.ReadFile(environpath)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(string(data), gc.Equals, existingEnv)
}
func (s *EnvironmentCommandSuite) TestEnvironCommandInitErrors(c *gc.C) {
	envPath := gitjujutesting.HomePath(".juju", "environments.yaml")
	err := os.Remove(envPath)
	c.Assert(err, gc.IsNil)
	cmd := envcmd.Wrap(new(testCommand))
	err = cmdtesting.InitCommand(cmd, nil)
	c.Assert(err, jc.Satisfies, environs.IsNoEnv)

	// If there are multiple environments but no default,
	// an error should be returned.
	coretesting.WriteEnvironments(c, coretesting.MultipleEnvConfigNoDefault)
	err = cmdtesting.InitCommand(cmd, nil)
	c.Assert(err, gc.Equals, envcmd.ErrNoEnvironmentSpecified)
}
Exemple #26
0
// An existing environments.yaml will be overwritten when -f is
// given explicitly.
func (*InitSuite) TestExistingEnvironmentOverwritten(c *gc.C) {
	testing.WriteEnvironments(c, existingEnv)

	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 := gitjujutesting.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.*")
}
Exemple #27
0
// 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.HomePath(".juju", "environments", 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)
}
Exemple #28
0
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.HomePath(".juju", "environments")
	err := os.Mkdir(envsDir, 0500)
	c.Assert(err, jc.ErrorIsNil)

	jenvCmd := &environment.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, "")
}
Exemple #29
0
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.HomePath(".juju", "environments.yaml")
	err := os.Chmod(envPath, 0200)
	c.Assert(err, jc.ErrorIsNil)

	jenvCmd := &environment.JenvCommand{}
	ctx, err := testing.RunCommand(c, jenvCmd, f.Name())
	c.Assert(err, gc.ErrorMatches, `cannot switch to the new environment "testing": cannot get the default environment: .*: permission denied`)
	c.Assert(testing.Stdout(ctx), gc.Equals, "")
}
Exemple #30
0
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.HomePath(".juju", "environments.yaml")
	err := os.Remove(envPath)
	c.Assert(err, jc.ErrorIsNil)

	// Importing a jenv does not require having environments already defined.
	jenvCmd := &environment.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")
}