~ubuntu-branches/ubuntu/saucy/juju-core/saucy-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/testing/environ.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-07-11 17:18:27 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130711171827-vjqkg40r0dlf7ys2
Tags: 1.11.2-0ubuntu1
* New upstream release.
* Make juju-core the default juju (LP: #1190634):
  - d/control: Add virtual package juju -> juju-core.
  - d/juju-core.postinst.in: Bump priority of alternatives over that of
    python juju packages.
* Enable for all architectures (LP: #1172505):
  - d/control: Version BD on golang-go to >= 2:1.1.1 to ensure CGO
    support for non-x86 archs, make juju-core Arch: any.
  - d/README.source: Dropped - no longer required.
* d/watch: Updated for new upstream tarball naming.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
1
4
package testing
2
5
 
3
6
import (
8
11
        "path/filepath"
9
12
)
10
13
 
 
14
// EnvironConfig returns a default environment configuration suitable for
 
15
// testing.
 
16
func EnvironConfig(c *C) *config.Config {
 
17
        cfg, err := config.New(map[string]interface{}{
 
18
                "type":            "test",
 
19
                "name":            "test-name",
 
20
                "default-series":  "test-series",
 
21
                "authorized-keys": "test-keys",
 
22
                "agent-version":   "9.9.9.9",
 
23
                "ca-cert":         CACert,
 
24
                "ca-private-key":  "",
 
25
        })
 
26
        c.Assert(err, IsNil)
 
27
        return cfg
 
28
}
 
29
 
11
30
const SampleEnvName = "erewhemos"
12
31
const EnvDefault = "default:\n  " + SampleEnvName + "\n"
13
32
 
41
60
 
42
61
const SampleCertName = "erewhemos"
43
62
 
 
63
type TestFile struct {
 
64
        Name, Data string
 
65
}
 
66
 
44
67
type FakeHome struct {
45
 
        oldHome     string
46
 
        oldJujuHome string
 
68
        oldHomeEnv     string
 
69
        oldJujuEnv     string
 
70
        oldJujuHomeEnv string
 
71
        oldJujuHome    string
 
72
        files          []TestFile
47
73
}
48
74
 
49
75
// MakeFakeHomeNoEnvironments creates a new temporary directory through the
55
81
// dir.
56
82
func MakeFakeHomeNoEnvironments(c *C, certNames ...string) *FakeHome {
57
83
        fake := MakeEmptyFakeHome(c)
58
 
        err := os.Mkdir(config.JujuHome(), 0755)
59
 
        c.Assert(err, IsNil)
60
84
 
61
85
        for _, name := range certNames {
62
86
                err := ioutil.WriteFile(config.JujuHomePath(name+"-cert.pem"), []byte(CACert), 0600)
65
89
                c.Assert(err, IsNil)
66
90
        }
67
91
 
68
 
        err = os.Mkdir(HomePath(".ssh"), 0777)
 
92
        err := os.Mkdir(HomePath(".ssh"), 0777)
69
93
        c.Assert(err, IsNil)
70
94
        err = ioutil.WriteFile(HomePath(".ssh", "id_rsa.pub"), []byte("auth key\n"), 0666)
71
95
        c.Assert(err, IsNil)
91
115
}
92
116
 
93
117
func MakeEmptyFakeHome(c *C) *FakeHome {
94
 
        oldHome := os.Getenv("HOME")
 
118
        fake := MakeEmptyFakeHomeWithoutJuju(c)
 
119
        err := os.Mkdir(config.JujuHome(), 0700)
 
120
        c.Assert(err, IsNil)
 
121
        return fake
 
122
}
 
123
 
 
124
func MakeEmptyFakeHomeWithoutJuju(c *C) *FakeHome {
 
125
        oldHomeEnv := os.Getenv("HOME")
 
126
        oldJujuHomeEnv := os.Getenv("JUJU_HOME")
 
127
        oldJujuEnv := os.Getenv("JUJU_ENV")
95
128
        fakeHome := c.MkDir()
96
129
        os.Setenv("HOME", fakeHome)
97
 
        oldJujuHome := config.SetJujuHome(filepath.Join(fakeHome, ".juju"))
98
 
        return &FakeHome{oldHome, oldJujuHome}
 
130
        os.Setenv("JUJU_HOME", "")
 
131
        os.Setenv("JUJU_ENV", "")
 
132
        jujuHome := filepath.Join(fakeHome, ".juju")
 
133
        oldJujuHome := config.SetJujuHome(jujuHome)
 
134
        return &FakeHome{
 
135
                oldHomeEnv:     oldHomeEnv,
 
136
                oldJujuEnv:     oldJujuEnv,
 
137
                oldJujuHomeEnv: oldJujuHomeEnv,
 
138
                oldJujuHome:    oldJujuHome,
 
139
                files:          []TestFile{},
 
140
        }
99
141
}
100
142
 
101
143
func HomePath(names ...string) string {
105
147
 
106
148
func (h *FakeHome) Restore() {
107
149
        config.SetJujuHome(h.oldJujuHome)
108
 
        os.Setenv("HOME", h.oldHome)
 
150
        os.Setenv("JUJU_ENV", h.oldJujuEnv)
 
151
        os.Setenv("JUJU_HOME", h.oldJujuHomeEnv)
 
152
        os.Setenv("HOME", h.oldHomeEnv)
 
153
}
 
154
 
 
155
func (h *FakeHome) AddFiles(c *C, files []TestFile) {
 
156
        for _, f := range files {
 
157
                path := HomePath(f.Name)
 
158
                err := os.MkdirAll(filepath.Dir(path), 0700)
 
159
                c.Assert(err, IsNil)
 
160
                err = ioutil.WriteFile(path, []byte(f.Data), 0666)
 
161
                c.Assert(err, IsNil)
 
162
                h.files = append(h.files, f)
 
163
        }
 
164
}
 
165
 
 
166
// FileContents returns the test file contents for the
 
167
// given specified path (which may be relative, so
 
168
// we compare with the base filename only).
 
169
func (h *FakeHome) FileContents(c *C, path string) string {
 
170
        for _, f := range h.files {
 
171
                if filepath.Base(f.Name) == filepath.Base(path) {
 
172
                        return f.Data
 
173
                }
 
174
        }
 
175
        c.Fatalf("path attribute holds unknown test file: %q", path)
 
176
        panic("unreachable")
 
177
}
 
178
 
 
179
// FileExists returns if the given relative file path exists
 
180
// in the fake home.
 
181
func (h *FakeHome) FileExists(path string) bool {
 
182
        for _, f := range h.files {
 
183
                if f.Name == path {
 
184
                        return true
 
185
                }
 
186
        }
 
187
        return false
 
188
}
 
189
 
 
190
func MakeFakeHomeWithFiles(c *C, files []TestFile) *FakeHome {
 
191
        fake := MakeEmptyFakeHome(c)
 
192
        fake.AddFiles(c, files)
 
193
        return fake
109
194
}
110
195
 
111
196
func MakeSampleHome(c *C) *FakeHome {