Merge lp:~thumper/juju-core/patch-with-cleanup into lp:~go-bot/juju-core/trunk

Proposed by Tim Penhey
Status: Merged
Approved by: Tim Penhey
Approved revision: no longer in the source branch.
Merged at revision: 1837
Proposed branch: lp:~thumper/juju-core/patch-with-cleanup
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 573 lines (+165/-84)
15 files modified
agent/format-1.16_whitebox_test.go (+8/-8)
cmd/jujud/machine_test.go (+2/-3)
cmd/logging_test.go (+1/-2)
container/lxc/lxc_test.go (+3/-2)
environs/config/config_test.go (+4/-4)
instance/address_test.go (+6/-4)
provider/azure/environ_test.go (+1/-2)
testing/cleanup.go (+21/-0)
testing/cleanup_test.go (+57/-2)
testing/environ.go (+0/-9)
testing/patch.go (+17/-7)
testing/patch_test.go (+34/-16)
worker/provisioner/lxc-broker_test.go (+2/-1)
worker/uniter/charm/git_test.go (+1/-3)
worker/uniter/debug/server_test.go (+8/-21)
To merge this branch: bzr merge lp:~thumper/juju-core/patch-with-cleanup
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+186464@code.launchpad.net

Commit message

PatchEnvironment and PatchValue on cleanup suite

The Set function was moved from testing/checkers to
testing. It wasn't a checker and it has also been
renamed to PatchValue, and in testing/patch.go. The
PatchEnvironment function has been added to that
file too.

Tests were added for PatchEnvironment.

Methods were added to CleanupSuite for PatchValue
and PatchEnvironment that automatically register
the restore functions as cleanup methods.

Call sites of PatchEnvironment and jc.Set have been
updated (although not necessarily all of them).

https://codereview.appspot.com/13770045/

Description of the change

PatchEnvironment and PatchValue on cleanup suite

The Set function was moved from testing/checkers to
testing. It wasn't a checker and it has also been
renamed to PatchValue, and in testing/patch.go. The
PatchEnvironment function has been added to that
file too.

Tests were added for PatchEnvironment.

Methods were added to CleanupSuite for PatchValue
and PatchEnvironment that automatically register
the restore functions as cleanup methods.

Call sites of PatchEnvironment and jc.Set have been
updated (although not necessarily all of them).

https://codereview.appspot.com/13770045/

To post a comment you must log in.
Revision history for this message
Tim Penhey (thumper) wrote :

Reviewers: mp+186464_code.launchpad.net,

Message:
Please take a look.

Description:
PatchEnvironment and PatchValue on cleanup suite

The Set function was moved from testing/checkers to
testing. It wasn't a checker and it has also been
renamed to PatchValue, and in testing/patch.go. The
PatchEnvironment function has been added to that
file too.

Tests were added for PatchEnvironment.

Methods were added to CleanupSuite for PatchValue
and PatchEnvironment that automatically register
the restore functions as cleanup methods.

Call sites of PatchEnvironment and jc.Set have been
updated (although not necessarily all of them).

https://code.launchpad.net/~thumper/juju-core/patch-with-cleanup/+merge/186464

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/13770045/

Affected files (+158, -81 lines):
   A [revision details]
   M agent/format-1.16_whitebox_test.go
   M cmd/jujud/machine_test.go
   M cmd/logging_test.go
   M container/lxc/lxc_test.go
   M environs/config/config_test.go
   M instance/address_test.go
   M instance/testing/instance.go
   M provider/azure/environ_test.go
   M testing/cleanup.go
   M testing/cleanup_test.go
   M testing/environ.go
   M testing/patch.go
   M testing/patch_test.go
   M worker/provisioner/lxc-broker_test.go
   M worker/uniter/charm/git_test.go
   M worker/uniter/debug/server_test.go

Revision history for this message
Ian Booth (wallyworld) wrote :

LGTM. I didn't see the issue you said exists. I'll assume you'll fix it
before landing :-)

https://codereview.appspot.com/13770045/diff/1/testing/cleanup_test.go
File testing/cleanup_test.go (right):

https://codereview.appspot.com/13770045/diff/1/testing/cleanup_test.go#newcode74
testing/cleanup_test.go:74: // and to avoid calling them twice, clear
out the stack
this comment doesn't really make sense - can it be reworded?

https://codereview.appspot.com/13770045/

Revision history for this message
Roger Peppe (rogpeppe) wrote :

I just noticed this.

"The Set function was moved from testing/checkers to
testing. It wasn't a checker and it has also been
renamed to PatchValue, and in testing/patch.go. The
PatchEnvironment function has been added to that
file too."

There was a particular reason that I defined Set inside
testing/checkers rather than in testing - dependencies.
There are various places that can't import testing because
it has a particularly hefty set of dependencies.

It may be that "checkers" is not a great name for a package
that includes both checkers and the patch functionality,
but I think there should be a place for our minimal-dependency
testing-related code, and I think that the package we import
as "jc" (whatever its path is) is a nice place for that.

I quite liked the original "jc.Set" name too - easy to type
and nicely indicative of its purpose.

Would you object too horribly if I reverted that part of this
CL, and moved testing.PatchEnvironment to jc.Setenv ?

https://codereview.appspot.com/13770045/

Revision history for this message
Andrew Wilkins (axwalk) wrote :

On 2013/09/19 15:15:12, rog wrote:
> I just noticed this.

> "The Set function was moved from testing/checkers to
> testing. It wasn't a checker and it has also been
> renamed to PatchValue, and in testing/patch.go. The
> PatchEnvironment function has been added to that
> file too."

> There was a particular reason that I defined Set inside
> testing/checkers rather than in testing - dependencies.
> There are various places that can't import testing because
> it has a particularly hefty set of dependencies.

> It may be that "checkers" is not a great name for a package
> that includes both checkers and the patch functionality,
> but I think there should be a place for our minimal-dependency
> testing-related code, and I think that the package we import
> as "jc" (whatever its path is) is a nice place for that.

IMHO, if this were in "checkers", then "checkers" needs to change name.
Perhaps a new subpackage is in order for small test utilities. Like,
"testutils" or something. This new subpackage could be required to have
no dependencies other than third-party (gocheck/loggo),
testing/checkers, and stdlib.

> I quite liked the original "jc.Set" name too - easy to type
> and nicely indicative of its purpose.

> Would you object too horribly if I reverted that part of this
> CL, and moved testing.PatchEnvironment to jc.Setenv ?

https://codereview.appspot.com/13770045/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'agent/format-1.16_whitebox_test.go'
2--- agent/format-1.16_whitebox_test.go 2013-09-02 22:22:00 +0000
3+++ agent/format-1.16_whitebox_test.go 2013-09-19 11:38:26 +0000
4@@ -105,13 +105,13 @@
5 }
6
7 func (s *format_1_16Suite) TestMigrate(c *gc.C) {
8- defer testing.PatchEnvironment(JujuLxcBridge, "lxc bridge")()
9- defer testing.PatchEnvironment(JujuProviderType, "provider type")()
10- defer testing.PatchEnvironment(osenv.JujuContainerType, "container type")()
11- defer testing.PatchEnvironment(JujuStorageDir, "storage dir")()
12- defer testing.PatchEnvironment(JujuStorageAddr, "storage addr")()
13- defer testing.PatchEnvironment(JujuSharedStorageDir, "shared storage dir")()
14- defer testing.PatchEnvironment(JujuSharedStorageAddr, "shared storage addr")()
15+ s.PatchEnvironment(JujuLxcBridge, "lxc bridge")
16+ s.PatchEnvironment(JujuProviderType, "provider type")
17+ s.PatchEnvironment(osenv.JujuContainerType, "container type")
18+ s.PatchEnvironment(JujuStorageDir, "storage dir")
19+ s.PatchEnvironment(JujuStorageAddr, "storage addr")
20+ s.PatchEnvironment(JujuSharedStorageDir, "shared storage dir")
21+ s.PatchEnvironment(JujuSharedStorageAddr, "shared storage addr")
22
23 config := s.newConfig(c)
24 s.formatter.migrate(config)
25@@ -130,7 +130,7 @@
26 }
27
28 func (s *format_1_16Suite) TestMigrateOnlySetsExisting(c *gc.C) {
29- defer testing.PatchEnvironment(JujuProviderType, "provider type")()
30+ s.PatchEnvironment(JujuProviderType, "provider type")
31
32 config := s.newConfig(c)
33 s.formatter.migrate(config)
34
35=== modified file 'cmd/jujud/machine_test.go'
36--- cmd/jujud/machine_test.go 2013-09-12 14:49:01 +0000
37+++ cmd/jujud/machine_test.go 2013-09-19 11:38:26 +0000
38@@ -33,7 +33,6 @@
39 type MachineSuite struct {
40 agentSuite
41 lxc.TestSuite
42- restoreCacheDir jc.Restorer
43 }
44
45 var _ = gc.Suite(&MachineSuite{})
46@@ -41,11 +40,11 @@
47 func (s *MachineSuite) SetUpSuite(c *gc.C) {
48 s.agentSuite.SetUpSuite(c)
49 s.TestSuite.SetUpSuite(c)
50- s.restoreCacheDir = jc.Set(&charm.CacheDir, c.MkDir())
51+ restore := testing.PatchValue(&charm.CacheDir, c.MkDir())
52+ s.AddSuiteCleanup(func(*gc.C) { restore() })
53 }
54
55 func (s *MachineSuite) TearDownSuite(c *gc.C) {
56- s.restoreCacheDir()
57 s.TestSuite.TearDownSuite(c)
58 s.agentSuite.TearDownSuite(c)
59 }
60
61=== modified file 'cmd/logging_test.go'
62--- cmd/logging_test.go 2013-09-17 04:06:28 +0000
63+++ cmd/logging_test.go 2013-09-19 11:38:26 +0000
64@@ -24,9 +24,8 @@
65 var _ = gc.Suite(&LogSuite{})
66
67 func (s *LogSuite) SetUpTest(c *gc.C) {
68- restore := testing.PatchEnvironment(osenv.JujuLoggingConfig, "")
69+ s.PatchEnvironment(osenv.JujuLoggingConfig, "")
70 s.AddCleanup(func(_ *gc.C) {
71- restore()
72 loggo.ResetLoggers()
73 loggo.ResetWriters()
74 })
75
76=== modified file 'container/lxc/lxc_test.go'
77--- container/lxc/lxc_test.go 2013-08-22 07:16:20 +0000
78+++ container/lxc/lxc_test.go 2013-09-19 11:38:26 +0000
79@@ -17,6 +17,7 @@
80
81 "launchpad.net/juju-core/container/lxc"
82 "launchpad.net/juju-core/instance"
83+ instancetest "launchpad.net/juju-core/instance/testing"
84 jujutesting "launchpad.net/juju-core/juju/testing"
85 "launchpad.net/juju-core/testing"
86 jc "launchpad.net/juju-core/testing/checkers"
87@@ -208,11 +209,11 @@
88
89 result, err := foo.ListContainers()
90 c.Assert(err, gc.IsNil)
91- testing.MatchInstances(c, result, foo1, foo2, foo3)
92+ instancetest.MatchInstances(c, result, foo1, foo2, foo3)
93
94 result, err = bar.ListContainers()
95 c.Assert(err, gc.IsNil)
96- testing.MatchInstances(c, result, bar1, bar2)
97+ instancetest.MatchInstances(c, result, bar1, bar2)
98 }
99
100 type NetworkSuite struct {
101
102=== modified file 'environs/config/config_test.go'
103--- environs/config/config_test.go 2013-09-12 23:37:50 +0000
104+++ environs/config/config_test.go 2013-09-19 11:38:26 +0000
105@@ -736,9 +736,9 @@
106 }
107 }
108
109-func (*ConfigSuite) TestConfigAttrs(c *gc.C) {
110+func (s *ConfigSuite) TestConfigAttrs(c *gc.C) {
111 // Normally this is handled by testing.FakeHome
112- defer testing.PatchEnvironment(osenv.JujuLoggingConfig, "")()
113+ s.PatchEnvironment(osenv.JujuLoggingConfig, "")
114 attrs := map[string]interface{}{
115 "type": "my-type",
116 "name": "my-name",
117@@ -930,10 +930,10 @@
118 c.Assert(config.LoggingConfig(), gc.Equals, logConfig)
119 }
120
121-func (*ConfigSuite) TestLoggingConfigFromEnvironment(c *gc.C) {
122+func (s *ConfigSuite) TestLoggingConfigFromEnvironment(c *gc.C) {
123 defer makeFakeHome(c).Restore()
124 logConfig := "<root>=INFO"
125- defer testing.PatchEnvironment(osenv.JujuLoggingConfig, logConfig)()
126+ s.PatchEnvironment(osenv.JujuLoggingConfig, logConfig)
127
128 config := newTestConfig(c, nil)
129 c.Assert(config.LoggingConfig(), gc.Equals, logConfig)
130
131=== modified file 'instance/address_test.go'
132--- instance/address_test.go 2013-09-18 04:40:29 +0000
133+++ instance/address_test.go 2013-09-19 11:38:26 +0000
134@@ -9,10 +9,12 @@
135
136 gc "launchpad.net/gocheck"
137
138- jc "launchpad.net/juju-core/testing/checkers"
139+ "launchpad.net/juju-core/testing"
140 )
141
142-type AddressSuite struct{}
143+type AddressSuite struct {
144+ testing.LoggingSuite
145+}
146
147 var _ = gc.Suite(&AddressSuite{})
148
149@@ -151,14 +153,14 @@
150 }
151 }
152
153-func (*AddressSuite) TestHostAddresses(c *gc.C) {
154+func (s *AddressSuite) TestHostAddresses(c *gc.C) {
155 // Mock the call to net.LookupIP made from HostAddresses.
156 var lookupIPs []net.IP
157 var lookupErr error
158 lookupIP := func(addr string) ([]net.IP, error) {
159 return append([]net.IP{}, lookupIPs...), lookupErr
160 }
161- defer jc.Set(&netLookupIP, lookupIP).Restore()
162+ s.PatchValue(&netLookupIP, lookupIP)
163
164 // err is only non-nil if net.LookupIP fails.
165 addrs, err := HostAddresses("")
166
167=== added directory 'instance/testing'
168=== renamed file 'testing/instance.go' => 'instance/testing/instance.go'
169=== modified file 'provider/azure/environ_test.go'
170--- provider/azure/environ_test.go 2013-09-19 00:22:15 +0000
171+++ provider/azure/environ_test.go 2013-09-19 11:38:26 +0000
172@@ -703,8 +703,7 @@
173 }
174
175 func (s *environSuite) setServiceDeletionConcurrency(nbGoroutines int) {
176- restore := jc.Set(&maxConcurrentDeletes, nbGoroutines)
177- s.AddCleanup(func(*gc.C) { restore() })
178+ s.PatchValue(&maxConcurrentDeletes, nbGoroutines)
179 }
180
181 func (s *environSuite) TestStopInstancesDestroysMachines(c *gc.C) {
182
183=== modified file 'testing/cleanup.go'
184--- testing/cleanup.go 2013-09-17 02:46:26 +0000
185+++ testing/cleanup.go 2013-09-19 11:38:26 +0000
186@@ -40,10 +40,31 @@
187 }
188 }
189
190+// AddCleanup pushes the cleanup function onto the stack of functions to be
191+// called during TearDownTest.
192 func (s *CleanupSuite) AddCleanup(cleanup CleanupFunc) {
193 s.testStack = append(s.testStack, cleanup)
194 }
195
196+// AddSuiteCleanup pushes the cleanup function onto the stack of functions to
197+// be called during TearDownSuite.
198 func (s *CleanupSuite) AddSuiteCleanup(cleanup CleanupFunc) {
199 s.suiteStack = append(s.suiteStack, cleanup)
200 }
201+
202+// PatchEnvironment sets the environment variable 'name' the the value passed
203+// in. The old value is saved and returned to the original value at test tear
204+// down time using a cleanup function.
205+func (s *CleanupSuite) PatchEnvironment(name, value string) {
206+ restore := PatchEnvironment(name, value)
207+ s.AddCleanup(func(*gc.C) { restore() })
208+}
209+
210+// PatchValue sets the 'dest' variable the the value passed in. The old value
211+// is saved and returned to the original value at test tear down time using a
212+// cleanup function. The value must be assignable to the element type of the
213+// destination.
214+func (s *CleanupSuite) PatchValue(dest, value interface{}) {
215+ restore := PatchValue(dest, value)
216+ s.AddCleanup(func(*gc.C) { restore() })
217+}
218
219=== modified file 'testing/cleanup_test.go'
220--- testing/cleanup_test.go 2013-09-17 02:46:26 +0000
221+++ testing/cleanup_test.go 2013-09-19 11:38:26 +0000
222@@ -1,6 +1,8 @@
223 package testing_test
224
225 import (
226+ "os"
227+
228 gc "launchpad.net/gocheck"
229
230 "launchpad.net/juju-core/testing"
231@@ -36,7 +38,8 @@
232 s.TearDownSuite(c)
233 c.Assert(order, gc.DeepEquals, []string{"second", "first"})
234
235- // and to avoid calling them twice, clear out the stack.
236+ // SetUpSuite resets the cleanup stack, this stops the cleanup functions
237+ // being called again.
238 s.SetUpSuite(c)
239 }
240
241@@ -52,6 +55,58 @@
242 s.TearDownTest(c)
243 c.Assert(order, gc.DeepEquals, []string{"second", "first"})
244
245- // and to avoid calling them twice, clear out the stack.
246+ // SetUpTest resets the cleanup stack, this stops the cleanup functions
247+ // being called again.
248+ s.SetUpTest(c)
249+}
250+
251+func (s *cleanupSuite) TestPatchEnvironment(c *gc.C) {
252+ const envName = "TESTING_PATCH_ENVIRONMENT"
253+ // remember the old value, and set it to something we can check
254+ oldValue := os.Getenv(envName)
255+ os.Setenv(envName, "initial")
256+
257+ s.PatchEnvironment(envName, "new value")
258+ // Using check to make sure the environment gets set back properly in the test.
259+ c.Check(os.Getenv(envName), gc.Equals, "new value")
260+
261+ s.TearDownTest(c)
262+ c.Check(os.Getenv(envName), gc.Equals, "initial")
263+
264+ // SetUpTest resets the cleanup stack, this stops the cleanup functions
265+ // being called again.
266+ s.SetUpTest(c)
267+ // explicitly return the envName to the old value
268+ os.Setenv(envName, oldValue)
269+}
270+
271+func (s *cleanupSuite) TestPatchValueInt(c *gc.C) {
272+ i := 42
273+ s.PatchValue(&i, 0)
274+ c.Assert(i, gc.Equals, 0)
275+
276+ s.TearDownTest(c)
277+ c.Assert(i, gc.Equals, 42)
278+
279+ // SetUpTest resets the cleanup stack, this stops the cleanup functions
280+ // being called again.
281+ s.SetUpTest(c)
282+}
283+
284+func (s *cleanupSuite) TestPatchValueFunction(c *gc.C) {
285+ function := func() string {
286+ return "original"
287+ }
288+
289+ s.PatchValue(&function, func() string {
290+ return "patched"
291+ })
292+ c.Assert(function(), gc.Equals, "patched")
293+
294+ s.TearDownTest(c)
295+ c.Assert(function(), gc.Equals, "original")
296+
297+ // SetUpTest resets the cleanup stack, this stops the cleanup functions
298+ // being called again.
299 s.SetUpTest(c)
300 }
301
302=== modified file 'testing/environ.go'
303--- testing/environ.go 2013-09-15 22:35:59 +0000
304+++ testing/environ.go 2013-09-19 11:38:26 +0000
305@@ -225,12 +225,3 @@
306 func MakeMultipleEnvHome(c *gc.C) *FakeHome {
307 return MakeFakeHome(c, MultipleEnvConfig, SampleCertName, "erewhemos-2")
308 }
309-
310-// PatchEnvironment provides a test a simple way to override a single
311-// environment variable. A function is returned that will return the
312-// environment to what it was before.
313-func PatchEnvironment(name, value string) func() {
314- oldValue := os.Getenv(name)
315- os.Setenv(name, value)
316- return func() { os.Setenv(name, oldValue) }
317-}
318
319=== renamed file 'testing/checkers/set.go' => 'testing/patch.go'
320--- testing/checkers/set.go 2013-09-10 17:36:09 +0000
321+++ testing/patch.go 2013-09-19 11:38:26 +0000
322@@ -1,6 +1,7 @@
323-package checkers
324+package testing
325
326 import (
327+ "os"
328 "reflect"
329 )
330
331@@ -13,12 +14,10 @@
332 r()
333 }
334
335-// Set sets the value pointed to by the given
336-// destination to the given value, and returns
337-// a function to restore it to its original value.
338-// The value must be assignable to the element
339-// type of the destination.
340-func Set(dest, value interface{}) Restorer {
341+// PatchValue sets the value pointed to by the given destination to the given
342+// value, and returns a function to restore it to its original value. The
343+// value must be assignable to the element type of the destination.
344+func PatchValue(dest, value interface{}) Restorer {
345 destv := reflect.ValueOf(dest).Elem()
346 oldv := reflect.New(destv.Type()).Elem()
347 oldv.Set(destv)
348@@ -33,3 +32,14 @@
349 destv.Set(oldv)
350 }
351 }
352+
353+// PatchEnvironment provides a test a simple way to override a single
354+// environment variable. A function is returned that will return the
355+// environment to what it was before.
356+func PatchEnvironment(name, value string) Restorer {
357+ oldValue := os.Getenv(name)
358+ os.Setenv(name, value)
359+ return func() {
360+ os.Setenv(name, oldValue)
361+ }
362+}
363
364=== renamed file 'testing/checkers/set_test.go' => 'testing/patch_test.go'
365--- testing/checkers/set_test.go 2013-09-13 14:48:13 +0000
366+++ testing/patch_test.go 2013-09-19 11:38:26 +0000
367@@ -1,55 +1,73 @@
368-package checkers_test
369+package testing_test
370
371 import (
372 "errors"
373+ "os"
374
375 gc "launchpad.net/gocheck"
376
377- jc "launchpad.net/juju-core/testing/checkers"
378+ "launchpad.net/juju-core/testing"
379 )
380
381-type SetSuite struct{}
382-
383-var _ = gc.Suite(&SetSuite{})
384-
385-func (*SetSuite) TestSetInt(c *gc.C) {
386+type PatchValueSuite struct{}
387+
388+var _ = gc.Suite(&PatchValueSuite{})
389+
390+func (*PatchValueSuite) TestSetInt(c *gc.C) {
391 i := 99
392- restore := jc.Set(&i, 88)
393+ restore := testing.PatchValue(&i, 88)
394 c.Assert(i, gc.Equals, 88)
395 restore()
396 c.Assert(i, gc.Equals, 99)
397 }
398
399-func (*SetSuite) TestSetError(c *gc.C) {
400+func (*PatchValueSuite) TestSetError(c *gc.C) {
401 oldErr := errors.New("foo")
402 newErr := errors.New("bar")
403 err := oldErr
404- restore := jc.Set(&err, newErr)
405+ restore := testing.PatchValue(&err, newErr)
406 c.Assert(err, gc.Equals, newErr)
407 restore()
408 c.Assert(err, gc.Equals, oldErr)
409 }
410
411-func (*SetSuite) TestSetErrorToNil(c *gc.C) {
412+func (*PatchValueSuite) TestSetErrorToNil(c *gc.C) {
413 oldErr := errors.New("foo")
414 err := oldErr
415- restore := jc.Set(&err, nil)
416+ restore := testing.PatchValue(&err, nil)
417 c.Assert(err, gc.Equals, nil)
418 restore()
419 c.Assert(err, gc.Equals, oldErr)
420 }
421
422-func (*SetSuite) TestSetMapToNil(c *gc.C) {
423+func (*PatchValueSuite) TestSetMapToNil(c *gc.C) {
424 oldMap := map[string]int{"foo": 1234}
425 m := oldMap
426- restore := jc.Set(&m, nil)
427+ restore := testing.PatchValue(&m, nil)
428 c.Assert(m, gc.IsNil)
429 restore()
430 c.Assert(m, gc.DeepEquals, oldMap)
431 }
432
433-func (*SetSuite) TestSetPanicsWhenNotAssignable(c *gc.C) {
434+func (*PatchValueSuite) TestSetPanicsWhenNotAssignable(c *gc.C) {
435 i := 99
436 type otherInt int
437- c.Assert(func() { jc.Set(&i, otherInt(88)) }, gc.PanicMatches, `reflect\.Set: value of type checkers_test\.otherInt is not assignable to type int`)
438+ c.Assert(func() { testing.PatchValue(&i, otherInt(88)) }, gc.PanicMatches, `reflect\.Set: value of type testing_test\.otherInt is not assignable to type int`)
439+}
440+
441+type PatchEnvironmentSuite struct{}
442+
443+var _ = gc.Suite(&PatchEnvironmentSuite{})
444+
445+func (*PatchEnvironmentSuite) TestPatchEnvironment(c *gc.C) {
446+ const envName = "TESTING_PATCH_ENVIRONMENT"
447+ // remember the old value, and set it to something we can check
448+ oldValue := os.Getenv(envName)
449+ os.Setenv(envName, "initial")
450+ restore := testing.PatchEnvironment(envName, "new value")
451+ // Using check to make sure the environment gets set back properly in the test.
452+ c.Check(os.Getenv(envName), gc.Equals, "new value")
453+ restore()
454+ c.Check(os.Getenv(envName), gc.Equals, "initial")
455+ os.Setenv(envName, oldValue)
456 }
457
458=== modified file 'worker/provisioner/lxc-broker_test.go'
459--- worker/provisioner/lxc-broker_test.go 2013-08-30 05:21:55 +0000
460+++ worker/provisioner/lxc-broker_test.go 2013-09-19 11:38:26 +0000
461@@ -20,6 +20,7 @@
462 "launchpad.net/juju-core/environs"
463 "launchpad.net/juju-core/environs/config"
464 "launchpad.net/juju-core/instance"
465+ instancetest "launchpad.net/juju-core/instance/testing"
466 jujutesting "launchpad.net/juju-core/juju/testing"
467 "launchpad.net/juju-core/names"
468 "launchpad.net/juju-core/provider"
469@@ -163,7 +164,7 @@
470 func (s *lxcBrokerSuite) assertInstances(c *gc.C, inst ...instance.Instance) {
471 results, err := s.broker.AllInstances()
472 c.Assert(err, gc.IsNil)
473- coretesting.MatchInstances(c, results, inst...)
474+ instancetest.MatchInstances(c, results, inst...)
475 }
476
477 func (s *lxcBrokerSuite) lxcContainerDir(inst instance.Instance) string {
478
479=== modified file 'worker/uniter/charm/git_test.go'
480--- worker/uniter/charm/git_test.go 2013-08-16 02:59:08 +0000
481+++ worker/uniter/charm/git_test.go 2013-09-19 11:38:26 +0000
482@@ -22,7 +22,6 @@
483 type GitDirSuite struct {
484 testing.GitSuite
485 LoggingSuite testing.LoggingSuite
486- resetLcAll func()
487 }
488
489 var _ = gc.Suite(&GitDirSuite{})
490@@ -30,11 +29,10 @@
491 func (s *GitDirSuite) SetUpTest(c *gc.C) {
492 s.GitSuite.SetUpTest(c)
493 s.LoggingSuite.SetUpTest(c)
494- s.resetLcAll = testing.PatchEnvironment("LC_ALL", "en_US")
495+ s.LoggingSuite.PatchEnvironment("LC_ALL", "en_US")
496 }
497
498 func (s *GitDirSuite) TearDownTest(c *gc.C) {
499- s.resetLcAll()
500 s.LoggingSuite.TearDownTest(c)
501 s.GitSuite.TearDownTest(c)
502 }
503
504=== modified file 'worker/uniter/debug/server_test.go'
505--- worker/uniter/debug/server_test.go 2013-09-16 01:55:29 +0000
506+++ worker/uniter/debug/server_test.go 2013-09-19 11:38:26 +0000
507@@ -14,14 +14,15 @@
508
509 gc "launchpad.net/gocheck"
510
511+ "launchpad.net/juju-core/testing"
512 jc "launchpad.net/juju-core/testing/checkers"
513 )
514
515 type DebugHooksServerSuite struct {
516+ testing.LoggingSuite
517 ctx *HooksContext
518 fakebin string
519 tmpdir string
520- oldenv []string
521 }
522
523 var _ = gc.Suite(&DebugHooksServerSuite{})
524@@ -35,33 +36,19 @@
525
526 var fakecommands = []string{"tmux"}
527
528-func (s *DebugHooksServerSuite) setenv(key, value string) {
529- s.oldenv = append(s.oldenv, key, os.Getenv(key))
530- os.Setenv(key, value)
531-}
532-
533 func (s *DebugHooksServerSuite) SetUpTest(c *gc.C) {
534 s.fakebin = c.MkDir()
535 s.tmpdir = c.MkDir()
536- s.setenv("PATH", s.fakebin+":"+os.Getenv("PATH"))
537- s.setenv("TMPDIR", s.tmpdir)
538- s.setenv("TEST_RESULT", "")
539+ s.PatchEnvironment("PATH", s.fakebin+":"+os.Getenv("PATH"))
540+ s.PatchEnvironment("TMPDIR", s.tmpdir)
541+ s.PatchEnvironment("TEST_RESULT", "")
542 for _, name := range fakecommands {
543 err := ioutil.WriteFile(filepath.Join(s.fakebin, name), []byte(echocommand), 0777)
544 c.Assert(err, gc.IsNil)
545 }
546 s.ctx = NewHooksContext("foo/8")
547 s.ctx.FlockDir = s.tmpdir
548- s.setenv("JUJU_UNIT_NAME", s.ctx.Unit)
549-}
550-
551-func (s *DebugHooksServerSuite) TearDownTest(c *gc.C) {
552- if len(s.oldenv) > 0 {
553- for i := len(s.oldenv) - 2; i >= 0; i = i - 2 {
554- os.Setenv(s.oldenv[i], s.oldenv[i+1])
555- }
556- s.oldenv = nil
557- }
558+ s.PatchEnvironment("JUJU_UNIT_NAME", s.ctx.Unit)
559 }
560
561 func (s *DebugHooksServerSuite) TestFindSession(c *gc.C) {
562@@ -124,9 +111,9 @@
563 // process' death).
564 ch := make(chan bool)
565 var clientExited bool
566- defer jc.Set(&waitClientExit, func(*ServerSession) {
567+ s.PatchValue(&waitClientExit, func(*ServerSession) {
568 clientExited = <-ch
569- })()
570+ })
571 go func() { ch <- true }()
572 err = session.RunHook("myhook", s.tmpdir, os.Environ())
573 c.Assert(clientExited, jc.IsTrue)

Subscribers

People subscribed via source and target branches

to status/vote changes: