Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(98)

Delta Between Two Patch Sets: testing/environ.go

Issue 13355044: environs/config: more restrictive New
Left Patch Set: Created 11 years, 7 months ago
Right Patch Set: environs/config: more restrictive New Created 11 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « testing/attrs.go ('k') | worker/environ_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2013 Canonical Ltd. 1 // Copyright 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details. 2 // Licensed under the AGPLv3, see LICENCE file for details.
3 3
4 package testing 4 package testing
5 5
6 import ( 6 import (
7 "io/ioutil" 7 "io/ioutil"
8 "os" 8 "os"
9 "path/filepath" 9 "path/filepath"
10 10
11 . "launchpad.net/gocheck" 11 . "launchpad.net/gocheck"
12 12
13 "launchpad.net/juju-core/environs/config" 13 "launchpad.net/juju-core/environs/config"
14 "launchpad.net/juju-core/juju/osenv"
14 ) 15 )
15 16
16 var FakeConfig = Attrs{ 17 // FakeConfig() returns an environment configuration for a
17 » "type": "someprovider", 18 // fake provider with all required attributes set.
18 » "name": "testenv", 19 func FakeConfig() Attrs {
19 » "authorized-keys": "my-keys", 20 » return Attrs{
20 » "firewall-mode": config.FwInstance, 21 » » "type": "someprovider",
21 » "admin-secret": "fish", 22 » » "name": "testenv",
22 » "ca-cert": CACert, 23 » » "authorized-keys": "my-keys",
23 » "ca-private-key": CAKey, 24 » » "firewall-mode": config.FwInstance,
24 » "ssl-hostname-verification": true, 25 » » "admin-secret": "fish",
25 » "development": false, 26 » » "ca-cert": CACert,
26 » "state-port": 1234, 27 » » "ca-private-key": CAKey,
27 » "api-port": 4321, 28 » » "ssl-hostname-verification": true,
28 » "default-series": "precise", 29 » » "development": false,
30 » » "state-port": 19034,
31 » » "api-port": 17777,
32 » » "default-series": config.DefaultSeries,
33 » }
29 } 34 }
30 35
31 // EnvironConfig returns a default environment configuration suitable for 36 // EnvironConfig returns a default environment configuration suitable for
32 // setting in the state. 37 // setting in the state.
33 func EnvironConfig(c *C) *config.Config { 38 func EnvironConfig(c *C) *config.Config {
34 » attrs := FakeConfig.Merge(Attrs{ 39 » attrs := FakeConfig().Merge(Attrs{
35 "agent-version": "1.2.3", 40 "agent-version": "1.2.3",
36 }).Delete("admin-secret", "ca-private-key") 41 }).Delete("admin-secret", "ca-private-key")
37 cfg, err := config.New(config.NoDefaults, attrs) 42 cfg, err := config.New(config.NoDefaults, attrs)
38 c.Assert(err, IsNil) 43 c.Assert(err, IsNil)
39 return cfg 44 return cfg
40 } 45 }
41 46
42 const ( 47 const (
43 SampleEnvName = "erewhemos" 48 SampleEnvName = "erewhemos"
44 EnvDefault = "default:\n " + SampleEnvName + "\n" 49 EnvDefault = "default:\n " + SampleEnvName + "\n"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 } 134 }
130 135
131 func MakeEmptyFakeHome(c *C) *FakeHome { 136 func MakeEmptyFakeHome(c *C) *FakeHome {
132 fake := MakeEmptyFakeHomeWithoutJuju(c) 137 fake := MakeEmptyFakeHomeWithoutJuju(c)
133 err := os.Mkdir(config.JujuHome(), 0700) 138 err := os.Mkdir(config.JujuHome(), 0700)
134 c.Assert(err, IsNil) 139 c.Assert(err, IsNil)
135 return fake 140 return fake
136 } 141 }
137 142
138 func MakeEmptyFakeHomeWithoutJuju(c *C) *FakeHome { 143 func MakeEmptyFakeHomeWithoutJuju(c *C) *FakeHome {
139 » oldHomeEnv := os.Getenv("HOME") 144 » oldHomeEnv := osenv.Home()
140 oldJujuHomeEnv := os.Getenv("JUJU_HOME") 145 oldJujuHomeEnv := os.Getenv("JUJU_HOME")
141 oldJujuEnv := os.Getenv("JUJU_ENV") 146 oldJujuEnv := os.Getenv("JUJU_ENV")
142 fakeHome := c.MkDir() 147 fakeHome := c.MkDir()
143 » os.Setenv("HOME", fakeHome) 148 » osenv.SetHome(fakeHome)
144 os.Setenv("JUJU_HOME", "") 149 os.Setenv("JUJU_HOME", "")
145 os.Setenv("JUJU_ENV", "") 150 os.Setenv("JUJU_ENV", "")
146 jujuHome := filepath.Join(fakeHome, ".juju") 151 jujuHome := filepath.Join(fakeHome, ".juju")
147 oldJujuHome := config.SetJujuHome(jujuHome) 152 oldJujuHome := config.SetJujuHome(jujuHome)
148 return &FakeHome{ 153 return &FakeHome{
149 oldHomeEnv: oldHomeEnv, 154 oldHomeEnv: oldHomeEnv,
150 oldJujuEnv: oldJujuEnv, 155 oldJujuEnv: oldJujuEnv,
151 oldJujuHomeEnv: oldJujuHomeEnv, 156 oldJujuHomeEnv: oldJujuHomeEnv,
152 oldJujuHome: oldJujuHome, 157 oldJujuHome: oldJujuHome,
153 files: []TestFile{}, 158 files: []TestFile{},
154 } 159 }
155 } 160 }
156 161
157 func HomePath(names ...string) string { 162 func HomePath(names ...string) string {
158 » all := append([]string{os.Getenv("HOME")}, names...) 163 » all := append([]string{osenv.Home()}, names...)
159 return filepath.Join(all...) 164 return filepath.Join(all...)
160 } 165 }
161 166
162 func (h *FakeHome) Restore() { 167 func (h *FakeHome) Restore() {
163 config.SetJujuHome(h.oldJujuHome) 168 config.SetJujuHome(h.oldJujuHome)
164 os.Setenv("JUJU_ENV", h.oldJujuEnv) 169 os.Setenv("JUJU_ENV", h.oldJujuEnv)
165 os.Setenv("JUJU_HOME", h.oldJujuHomeEnv) 170 os.Setenv("JUJU_HOME", h.oldJujuHomeEnv)
166 » os.Setenv("HOME", h.oldHomeEnv) 171 » osenv.SetHome(h.oldHomeEnv)
167 } 172 }
168 173
169 func (h *FakeHome) AddFiles(c *C, files []TestFile) { 174 func (h *FakeHome) AddFiles(c *C, files []TestFile) {
170 for _, f := range files { 175 for _, f := range files {
171 path := HomePath(f.Name) 176 path := HomePath(f.Name)
172 err := os.MkdirAll(filepath.Dir(path), 0700) 177 err := os.MkdirAll(filepath.Dir(path), 0700)
173 c.Assert(err, IsNil) 178 c.Assert(err, IsNil)
174 err = ioutil.WriteFile(path, []byte(f.Data), 0666) 179 err = ioutil.WriteFile(path, []byte(f.Data), 0666)
175 c.Assert(err, IsNil) 180 c.Assert(err, IsNil)
176 h.files = append(h.files, f) 181 h.files = append(h.files, f)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 } 221 }
217 222
218 // PatchEnvironment provides a test a simple way to override a single 223 // PatchEnvironment provides a test a simple way to override a single
219 // environment variable. A function is returned that will return the 224 // environment variable. A function is returned that will return the
220 // environment to what it was before. 225 // environment to what it was before.
221 func PatchEnvironment(name, value string) func() { 226 func PatchEnvironment(name, value string) func() {
222 oldValue := os.Getenv(name) 227 oldValue := os.Getenv(name)
223 os.Setenv(name, value) 228 os.Setenv(name, value)
224 return func() { os.Setenv(name, oldValue) } 229 return func() { os.Setenv(name, oldValue) }
225 } 230 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b