Esempio n. 1
0
func (*CloudInitSuite) TestUserData(c *C) {
	testJujuHome := c.MkDir()
	defer config.SetJujuHome(config.SetJujuHome(testJujuHome))
	tools := &tools.Tools{
		URL:    "http://foo.com/tools/juju1.2.3-linux-amd64.tgz",
		Binary: version.MustParseBinary("1.2.3-linux-amd64"),
	}
	envConfig, err := config.New(map[string]interface{}{
		"type":            "maas",
		"name":            "foo",
		"default-series":  "series",
		"authorized-keys": "keys",
		"ca-cert":         testing.CACert,
	})
	c.Assert(err, IsNil)

	cfg := &cloudinit.MachineConfig{
		MachineId:       "10",
		MachineNonce:    "5432",
		Tools:           tools,
		StateServerCert: []byte(testing.ServerCert),
		StateServerKey:  []byte(testing.ServerKey),
		StateInfo: &state.Info{
			Password: "******",
			CACert:   []byte("CA CERT\n" + testing.CACert),
		},
		APIInfo: &api.Info{
			Password: "******",
			CACert:   []byte("CA CERT\n" + testing.CACert),
		},
		DataDir:      environs.DataDir,
		Config:       envConfig,
		StatePort:    envConfig.StatePort(),
		APIPort:      envConfig.APIPort(),
		StateServer:  true,
		ProviderType: "dummy",
	}
	script1 := "script1"
	script2 := "script2"
	scripts := []string{script1, script2}
	result, err := environs.ComposeUserData(cfg, scripts...)
	c.Assert(err, IsNil)

	unzipped, err := utils.Gunzip(result)
	c.Assert(err, IsNil)

	config := make(map[interface{}]interface{})
	err = goyaml.Unmarshal(unzipped, &config)
	c.Assert(err, IsNil)

	// Just check that the cloudinit config looks good.
	c.Check(config["apt_upgrade"], Equals, true)
	// The scripts given to userData where added as the first
	// commands to be run.
	runCmd := config["runcmd"].([]interface{})
	c.Check(runCmd[0], Equals, script1)
	c.Check(runCmd[1], Equals, script2)
}
Esempio n. 2
0
func (s *TestingEnvironSuite) SetUpTest(c *C) {
	s.home = os.Getenv("HOME")
	s.jujuHome = os.Getenv("JUJU_HOME")

	os.Setenv("HOME", "/home/eric")
	os.Setenv("JUJU_HOME", "/home/eric/juju")
	config.SetJujuHome("/home/eric/juju")
}
Esempio n. 3
0
func (s *MetadataSuite) TestHelpCommands(c *gc.C) {
	// Check that we have correctly registered all the commands
	// by checking the help output.
	defer config.SetJujuHome(config.SetJujuHome(c.MkDir()))
	out := badrun(c, 0, "help", "commands")
	lines := strings.Split(out, "\n")
	var names []string
	for _, line := range lines {
		f := strings.Fields(line)
		if len(f) == 0 {
			continue
		}
		names = append(names, f[0])
	}
	// The names should be output in alphabetical order, so don't sort.
	c.Assert(names, gc.DeepEquals, commandNames)
}
Esempio n. 4
0
func (s *ConfigSuite) SetUpTest(c *C) {
	s.oldJujuHome = config.SetJujuHome(c.MkDir())
	s.savedVars = make(map[string]string)
	for v, val := range envVars {
		s.savedVars[v] = os.Getenv(v)
		os.Setenv(v, val)
	}
}
func (suite *EnvironProviderSuite) TestOpenReturnsNilInterfaceUponFailure(c *gc.C) {
	testJujuHome := c.MkDir()
	defer config.SetJujuHome(config.SetJujuHome(testJujuHome))
	const oauth = "wrongly-formatted-oauth-string"
	attrs := map[string]interface{}{
		"maas-oauth":      oauth,
		"maas-server":     "http://maas.testing.invalid/maas/",
		"name":            "wheee",
		"type":            "maas",
		"authorized-keys": "I-am-not-a-real-key",
	}
	config, err := config.New(attrs)
	c.Assert(err, gc.IsNil)
	env, err := suite.environ.Provider().Open(config)
	// When Open() fails (i.e. returns a non-nil error), it returns an
	// environs.Environ interface object with a nil value and a nil
	// type.
	c.Check(env, gc.Equals, nil)
	c.Check(err, gc.ErrorMatches, ".*malformed maas-oauth.*")
}
func (suite *EnvironProviderSuite) TestSecretAttrsReturnsSensitiveMAASAttributes(c *gc.C) {
	testJujuHome := c.MkDir()
	defer config.SetJujuHome(config.SetJujuHome(testJujuHome))
	const oauth = "aa:bb:cc"
	attrs := map[string]interface{}{
		"maas-oauth":      oauth,
		"maas-server":     "http://maas.testing.invalid/maas/",
		"name":            "wheee",
		"type":            "maas",
		"authorized-keys": "I-am-not-a-real-key",
	}
	config, err := config.New(attrs)
	c.Assert(err, gc.IsNil)

	secretAttrs, err := suite.environ.Provider().SecretAttrs(config)
	c.Assert(err, gc.IsNil)

	expectedAttrs := map[string]interface{}{"maas-oauth": oauth}
	c.Check(secretAttrs, gc.DeepEquals, expectedAttrs)
}
Esempio n. 7
0
// InitJujuHome initializes the charm and environs/config packages to use
// default paths based on the $JUJU_HOME or $HOME environment variables.
// This function should be called before calling NewConn or Conn.Deploy.
func InitJujuHome() error {
	jujuHome := os.Getenv("JUJU_HOME")
	if jujuHome == "" {
		home := os.Getenv("HOME")
		if home == "" {
			return stderrors.New("cannot determine juju home, neither $JUJU_HOME nor $HOME are set")
		}
		jujuHome = filepath.Join(home, ".juju")
	}
	config.SetJujuHome(jujuHome)
	charm.CacheDir = filepath.Join(jujuHome, "charmcache")
	return nil
}
Esempio n. 8
0
func (s *MetadataSuite) TestHelpGlobalOptions(c *gc.C) {
	// Check that we have correctly registered all the topics
	// by checking the help output.
	defer config.SetJujuHome(config.SetJujuHome(c.MkDir()))
	out := badrun(c, 0, "help", "global-options")
	c.Assert(out, gc.Matches, `Global Options

These options may be used with any command, and may appear in front of any
command\.(.|\n)*`)
	lines := strings.Split(out, "\n")
	var flags []string
	for _, line := range lines {
		f := strings.Fields(line)
		if len(f) == 0 || line[0] != '-' {
			continue
		}
		flags = append(flags, line)
	}
	c.Assert(len(flags), gc.Equals, len(globalFlags))
	for i, line := range flags {
		c.Assert(line, gc.Matches, globalFlags[i])
	}
}
Esempio n. 9
0
func MakeEmptyFakeHomeWithoutJuju(c *C) *FakeHome {
	oldHomeEnv := os.Getenv("HOME")
	oldJujuHomeEnv := os.Getenv("JUJU_HOME")
	oldJujuEnv := os.Getenv("JUJU_ENV")
	fakeHome := c.MkDir()
	os.Setenv("HOME", fakeHome)
	os.Setenv("JUJU_HOME", "")
	os.Setenv("JUJU_ENV", "")
	jujuHome := filepath.Join(fakeHome, ".juju")
	oldJujuHome := config.SetJujuHome(jujuHome)
	return &FakeHome{
		oldHomeEnv:     oldHomeEnv,
		oldJujuEnv:     oldJujuEnv,
		oldJujuHomeEnv: oldJujuHomeEnv,
		oldJujuHome:    oldJujuHome,
		files:          []TestFile{},
	}
}
Esempio n. 10
0
func (*EnvironsCertSuite) TestWriteCertAndKey(c *C) {
	defer testing.MakeEmptyFakeHome(c).Restore()

	// Ensure that the juju home path is different
	// from $HOME/.juju to check that WriteCertAndKey
	// isn't just using $HOME.
	config.SetJujuHome(c.MkDir())

	cert, key := []byte("a cert"), []byte("a key")
	err := environs.WriteCertAndKey("foo", cert, key)
	c.Assert(err, IsNil)

	// Check that the generated CA key has been written correctly.
	caCertPEM, err := ioutil.ReadFile(config.JujuHomePath("foo-cert.pem"))
	c.Assert(err, IsNil)
	c.Assert(caCertPEM, DeepEquals, cert)

	caKeyPEM, err := ioutil.ReadFile(config.JujuHomePath("foo-private-key.pem"))
	c.Assert(err, IsNil)
	c.Assert(caKeyPEM, DeepEquals, key)

}
Esempio n. 11
0
func (s *JujuConnSuite) TearDownTest(c *C) {
	s.tearDownConn(c)
	s.MgoSuite.TearDownTest(c)
	s.LoggingSuite.TearDownTest(c)
	config.SetJujuHome(s.oldJujuHome)
}
Esempio n. 12
0
func (s *JujuConnSuite) SetUpTest(c *C) {
	s.oldJujuHome = config.SetJujuHome(c.MkDir())
	s.LoggingSuite.SetUpTest(c)
	s.MgoSuite.SetUpTest(c)
	s.setUpConn(c)
}
Esempio n. 13
0
func (s *JujuHomeSuite) TestHomePath(c *C) {
	testJujuHome := c.MkDir()
	defer config.SetJujuHome(config.SetJujuHome(testJujuHome))
	envPath := config.JujuHomePath("environments.yaml")
	c.Assert(envPath, Equals, filepath.Join(testJujuHome, "environments.yaml"))
}
Esempio n. 14
0
func (s *JujuHomeSuite) TestStandardHome(c *C) {
	testJujuHome := c.MkDir()
	defer config.SetJujuHome(config.SetJujuHome(testJujuHome))
	c.Assert(config.JujuHome(), Equals, testJujuHome)
}
Esempio n. 15
0
func (s *ConfigSuite) TearDownTest(c *C) {
	for k, v := range s.savedVars {
		os.Setenv(k, v)
	}
	config.SetJujuHome(s.oldJujuHome)
}
Esempio n. 16
0
func (h *FakeHome) Restore() {
	config.SetJujuHome(h.oldJujuHome)
	os.Setenv("JUJU_ENV", h.oldJujuEnv)
	os.Setenv("JUJU_HOME", h.oldJujuHomeEnv)
	os.Setenv("HOME", h.oldHomeEnv)
}
func (*BoilerplateConfigSuite) TestBoilerPlateGeneration(c *C) {
	defer config.SetJujuHome(config.SetJujuHome(c.MkDir()))
	boilerplate_text := environs.BoilerplateConfig()
	_, err := environs.ReadEnvironsBytes([]byte(boilerplate_text))
	c.Assert(err, IsNil)
}