1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
11
. "launchpad.net/gocheck"
13
"launchpad.net/juju-core/environs/config"
16
// EnvironConfig returns a default environment configuration suitable for
18
func EnvironConfig(c *C) *config.Config {
19
cfg, err := config.New(map[string]interface{}{
22
"default-series": "test-series",
23
"authorized-keys": "test-keys",
24
"agent-version": "9.9.9.9",
32
const SampleEnvName = "erewhemos"
33
const EnvDefault = "default:\n " + SampleEnvName + "\n"
35
// Environment names below are explicit as it makes them more readable.
36
const SingleEnvConfigNoDefault = `
41
authorized-keys: i-am-a-key
42
admin-secret: conn-from-name-secret
45
const SingleEnvConfig = EnvDefault + SingleEnvConfigNoDefault
47
const MultipleEnvConfigNoDefault = `
52
authorized-keys: i-am-a-key
53
admin-secret: conn-from-name-secret
57
authorized-keys: i-am-a-key
58
admin-secret: conn-from-name-secret
61
const MultipleEnvConfig = EnvDefault + MultipleEnvConfigNoDefault
63
const SampleCertName = "erewhemos"
65
type TestFile struct {
69
type FakeHome struct {
77
// MakeFakeHomeNoEnvironments creates a new temporary directory through the
78
// test checker, and overrides the HOME environment variable to point to this
79
// new temporary directory.
81
// No ~/.juju/environments.yaml exists, but CAKeys are written for each of the
82
// 'certNames' specified, and the id_rsa.pub file is written to to the .ssh
84
func MakeFakeHomeNoEnvironments(c *C, certNames ...string) *FakeHome {
85
fake := MakeEmptyFakeHome(c)
87
for _, name := range certNames {
88
err := ioutil.WriteFile(config.JujuHomePath(name+"-cert.pem"), []byte(CACert), 0600)
90
err = ioutil.WriteFile(config.JujuHomePath(name+"-private-key.pem"), []byte(CAKey), 0600)
94
err := os.Mkdir(HomePath(".ssh"), 0777)
96
err = ioutil.WriteFile(HomePath(".ssh", "id_rsa.pub"), []byte("auth key\n"), 0666)
102
// MakeFakeHome creates a new temporary directory through the test checker,
103
// and overrides the HOME environment variable to point to this new temporary
106
// A new ~/.juju/environments.yaml file is created with the content of the
107
// `envConfig` parameter, and CAKeys are written for each of the 'certNames'
109
func MakeFakeHome(c *C, envConfig string, certNames ...string) *FakeHome {
110
fake := MakeFakeHomeNoEnvironments(c, certNames...)
112
envs := config.JujuHomePath("environments.yaml")
113
err := ioutil.WriteFile(envs, []byte(envConfig), 0644)
119
func MakeEmptyFakeHome(c *C) *FakeHome {
120
fake := MakeEmptyFakeHomeWithoutJuju(c)
121
err := os.Mkdir(config.JujuHome(), 0700)
126
func MakeEmptyFakeHomeWithoutJuju(c *C) *FakeHome {
127
oldHomeEnv := os.Getenv("HOME")
128
oldJujuHomeEnv := os.Getenv("JUJU_HOME")
129
oldJujuEnv := os.Getenv("JUJU_ENV")
130
fakeHome := c.MkDir()
131
os.Setenv("HOME", fakeHome)
132
os.Setenv("JUJU_HOME", "")
133
os.Setenv("JUJU_ENV", "")
134
jujuHome := filepath.Join(fakeHome, ".juju")
135
oldJujuHome := config.SetJujuHome(jujuHome)
137
oldHomeEnv: oldHomeEnv,
138
oldJujuEnv: oldJujuEnv,
139
oldJujuHomeEnv: oldJujuHomeEnv,
140
oldJujuHome: oldJujuHome,
145
func HomePath(names ...string) string {
146
all := append([]string{os.Getenv("HOME")}, names...)
147
return filepath.Join(all...)
150
func (h *FakeHome) Restore() {
151
config.SetJujuHome(h.oldJujuHome)
152
os.Setenv("JUJU_ENV", h.oldJujuEnv)
153
os.Setenv("JUJU_HOME", h.oldJujuHomeEnv)
154
os.Setenv("HOME", h.oldHomeEnv)
157
func (h *FakeHome) AddFiles(c *C, files []TestFile) {
158
for _, f := range files {
159
path := HomePath(f.Name)
160
err := os.MkdirAll(filepath.Dir(path), 0700)
162
err = ioutil.WriteFile(path, []byte(f.Data), 0666)
164
h.files = append(h.files, f)
168
// FileContents returns the test file contents for the
169
// given specified path (which may be relative, so
170
// we compare with the base filename only).
171
func (h *FakeHome) FileContents(c *C, path string) string {
172
for _, f := range h.files {
173
if filepath.Base(f.Name) == filepath.Base(path) {
177
c.Fatalf("path attribute holds unknown test file: %q", path)
181
// FileExists returns if the given relative file path exists
183
func (h *FakeHome) FileExists(path string) bool {
184
for _, f := range h.files {
192
func MakeFakeHomeWithFiles(c *C, files []TestFile) *FakeHome {
193
fake := MakeEmptyFakeHome(c)
194
fake.AddFiles(c, files)
198
func MakeSampleHome(c *C) *FakeHome {
199
return MakeFakeHome(c, SingleEnvConfig, SampleCertName)
202
func MakeMultipleEnvHome(c *C) *FakeHome {
203
return MakeFakeHome(c, MultipleEnvConfig, SampleCertName, "erewhemos-2")