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

Proposed by Tim Penhey
Status: Merged
Approved by: John A Meinel
Approved revision: no longer in the source branch.
Merged at revision: 1791
Proposed branch: lp:~thumper/juju-core/cleanup-suite
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 153 lines (+110/-9)
3 files modified
testing/cleanup.go (+46/-0)
testing/cleanup_test.go (+57/-0)
testing/log.go (+7/-9)
To merge this branch: bzr merge lp:~thumper/juju-core/cleanup-suite
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+185199@code.launchpad.net

Commit message

Add a base cleaner suite to add cleanup methods

Also made the LoggerSuite embed it, so most existing
tests get the magic.

https://codereview.appspot.com/13633045/

Description of the change

Add a base cleaner suite to add cleanup methods

Also made the LoggerSuite embed it, so most existing
tests get the magic.

https://codereview.appspot.com/13633045/

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

Reviewers: mp+185199_code.launchpad.net,

Message:
Please take a look.

Description:
Add a base cleaner suite to add cleanup methods

Also made the LoggerSuite embed it, so most existing
tests get the magic.

https://code.launchpad.net/~thumper/juju-core/cleanup-suite/+merge/185199

(do not edit description out of merge proposal)

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

Affected files (+112, -9 lines):
   A [revision details]
   A testing/cleanup.go
   A testing/cleanup_test.go
   M testing/log.go

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

On 2013/09/12 05:05:17, thumper wrote:
> Please take a look.

LGTM.

https://codereview.appspot.com/13633045/

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

LGTM but I'd like some tests to ensure there's no cross contamination
between suite and test cleanup

https://codereview.appspot.com/13633045/

Revision history for this message
Go Bot (go-bot) wrote :
Download full text (165.0 KiB)

The attempt to merge lp:~thumper/juju-core/cleanup-suite into lp:juju-core failed. Below is the output from the failed tests.

ok launchpad.net/juju-core/agent 0.714s
ok launchpad.net/juju-core/agent/tools 0.238s
ok launchpad.net/juju-core/bzr 6.872s
ok launchpad.net/juju-core/cert 2.158s
ok launchpad.net/juju-core/charm 0.571s
? launchpad.net/juju-core/charm/hooks [no test files]
ok launchpad.net/juju-core/cloudinit 0.020s
ok launchpad.net/juju-core/cmd 0.249s
? launchpad.net/juju-core/cmd/builddb [no test files]
? launchpad.net/juju-core/cmd/charmd [no test files]
? launchpad.net/juju-core/cmd/charmload [no test files]

----------------------------------------------------------------------
PANIC: addmachine.go:0: AddMachineSuite.SetUpTest

[LOG] 98.04033 INFO juju mongod: error command line: unknown option sslOnNormalPorts
[LOG] 98.04037 INFO juju mongod: use --help for help
... Panic: no reachable servers (PC=0x414321)

/usr/lib/go/src/pkg/runtime/panic.c:229
  in panic
/home/tarmac/trees/src/launchpad.net/juju-core/testing/mgo.go:198
  in MgoDial
/home/tarmac/trees/src/launchpad.net/juju-core/testing/mgo.go:205
  in MgoSuite.SetUpTest
/home/tarmac/trees/src/launchpad.net/juju-core/juju/testing/conn.go:138
  in JujuConnSuite.SetUpTest
/home/tarmac/trees/src/launchpad.net/juju-core/juju/testing/repo.go:25
  in RepoSuite.SetUpTest

----------------------------------------------------------------------
PANIC: addmachine.go:0: AddMachineSuite.TearDownTest

... Panic: runtime error: invalid memory address or nil pointer dereference (PC=0x414321)

/usr/lib/go/src/pkg/runtime/panic.c:229
  in panic
/usr/lib/go/src/pkg/runtime/panic.c:487
  in panicstring
/usr/lib/go/src/pkg/runtime/os_linux.c:236
  in sigpanic
/home/tarmac/trees/src/launchpad.net/juju-core/state/state.go:1044
  in State.SetAdminMongoPassword
/home/tarmac/trees/src/launchpad.net/juju-core/juju/testing/conn.go:264
  in JujuConnSuite.tearDownConn
/home/tarmac/trees/src/launchpad.net/juju-core/juju/testing/conn.go:144
  in JujuConnSuite.TearDownTest
/home/tarmac/trees/src/launchpad.net/juju-core/juju/testing/repo.go:44
  in RepoSuite.TearDownTest

----------------------------------------------------------------------
PANIC: addmachine_test.go:82: AddMachineSuite.TestAddContainerToExistingMachine

... Panic: Fixture has panicked (see related PANIC)

----------------------------------------------------------------------
PANIC: addmachine.go:0: AddRelationSuite.SetUpTest

... Panic: no reachable servers (PC=0x414321)

/usr/lib/go/src/pkg/runtime/panic.c:229
  in panic
/home/tarmac/trees/src/launchpad.net/juju-core/testing/mgo.go:198
  in MgoDial
/home/tarmac/trees/src/launchpad.net/juju-core/testing/mgo.go:205
  in MgoSuite.SetUpTest
/home/tarmac/trees/src/launchpad.net/juju-core/juju/testing/conn.go:138
  in JujuConnSuite.SetUpTest
/home/tarmac/trees/src/launchpad.net/juju-core/juju/testing/repo.go:25
  in RepoSuite.SetUpTest

----------------------------------------------------------------------
PANIC: addmachine.go:0: AddRelationSuite.TearDownTest

... Panic: runtime error: invalid memory address or nil pointer dereference (PC=0x414321)

/usr/lib/go/src/pkg/...

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

LGTM with a couple of minor suggestions.

Embedding this into LoggingSuite makes me
feel twitchy though. This a marriage entirely
of convenience - LoggingSuite previously
was about a single thing only, and now has
this functionality shoehorned in just because
lots of things use LoggingSuite.

I would be more inclined to embed it in some
of the "kitchen-sink" suites: JujuConnSuite;
or rename LoggingSuite to LoggingAndCleanupSuite
perhaps throughout.

I like the functionality in general though.

https://codereview.appspot.com/13633045/diff/1/testing/cleanup.go
File testing/cleanup.go (right):

https://codereview.appspot.com/13633045/diff/1/testing/cleanup.go#newcode14
testing/cleanup.go:14: testStack []func()
One possibility is to use jc.Restorer instead of func() here, which
perhaps makes the purpose of the functions more clear.

It doesn't really make any difference, just a thought.

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

https://codereview.appspot.com/13633045/diff/1/testing/cleanup_test.go#newcode10
testing/cleanup_test.go:10: testing.CleanupSuite
I'm not sure we need to embed this - you can just
declare it in each test, which I think will make the
tests a little clearer as it avoids shared state.

https://codereview.appspot.com/13633045/

Revision history for this message
John A Meinel (jameinel) wrote :

The problem with embedding it in each test, is then you have to update your SetUp and TearDown to call yet-another method, etc. So it is "just one more embed" but that is one more of a bunch of other bits.

This is the sort of thing you would put on a "base" suite if we had such a thing. LoggingSuite comes pretty close. We could rename it for clarity if we like that more.

The failure in the test suite was because the bot rebooted and I missed one line. It should land now. I'm going to mark it Approved because I don't want Tim to have to wait on Bot failure. However, please respond to Roger's comments.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'testing/cleanup.go'
2--- testing/cleanup.go 1970-01-01 00:00:00 +0000
3+++ testing/cleanup.go 2013-09-12 05:03:44 +0000
4@@ -0,0 +1,46 @@
5+// Copyright 2013 Canonical Ltd.
6+// Licensed under the AGPLv3, see LICENCE file for details.
7+
8+package testing
9+
10+import (
11+ gc "launchpad.net/gocheck"
12+)
13+
14+// CleanupSuite adds the ability to add cleanup functions that are called
15+// during either test tear down or suite tear down depending on the method
16+// called.
17+type CleanupSuite struct {
18+ testStack []func()
19+ suiteStack []func()
20+}
21+
22+func (s *CleanupSuite) SetUpSuite(c *gc.C) {
23+ s.suiteStack = nil
24+}
25+
26+func (s *CleanupSuite) TearDownSuite(c *gc.C) {
27+ s.callStack(s.suiteStack)
28+}
29+
30+func (s *CleanupSuite) SetUpTest(c *gc.C) {
31+ s.testStack = nil
32+}
33+
34+func (s *CleanupSuite) TearDownTest(c *gc.C) {
35+ s.callStack(s.testStack)
36+}
37+
38+func (s *CleanupSuite) callStack(stack []func()) {
39+ for i := len(stack) - 1; i >= 0; i-- {
40+ stack[i]()
41+ }
42+}
43+
44+func (s *CleanupSuite) AddCleanup(cleanup func()) {
45+ s.testStack = append(s.testStack, cleanup)
46+}
47+
48+func (s *CleanupSuite) AddSuiteCleanup(cleanup func()) {
49+ s.suiteStack = append(s.suiteStack, cleanup)
50+}
51
52=== added file 'testing/cleanup_test.go'
53--- testing/cleanup_test.go 1970-01-01 00:00:00 +0000
54+++ testing/cleanup_test.go 2013-09-12 05:03:44 +0000
55@@ -0,0 +1,57 @@
56+package testing_test
57+
58+import (
59+ gc "launchpad.net/gocheck"
60+
61+ "launchpad.net/juju-core/testing"
62+)
63+
64+type cleanupSuite struct {
65+ testing.CleanupSuite
66+}
67+
68+var _ = gc.Suite(&cleanupSuite{})
69+
70+func (s *cleanupSuite) TestTearDownSuiteEmpty(c *gc.C) {
71+ // The suite stack is empty initially, check we can tear that down.
72+ s.TearDownSuite(c)
73+ s.SetUpSuite(c)
74+}
75+
76+func (s *cleanupSuite) TestTearDownTestEmpty(c *gc.C) {
77+ // The test stack is empty initially, check we can tear that down.
78+ s.TearDownTest(c)
79+ s.SetUpTest(c)
80+}
81+
82+func (s *cleanupSuite) TestAddSuiteCleanup(c *gc.C) {
83+ order := []string{}
84+ s.AddSuiteCleanup(func() {
85+ order = append(order, "first")
86+ })
87+ s.AddSuiteCleanup(func() {
88+ order = append(order, "second")
89+ })
90+
91+ s.TearDownSuite(c)
92+ c.Assert(order, gc.DeepEquals, []string{"second", "first"})
93+
94+ // and to avoid calling them twice, clear out the stack.
95+ s.SetUpSuite(c)
96+}
97+
98+func (s *cleanupSuite) TestAddCleanup(c *gc.C) {
99+ order := []string{}
100+ s.AddCleanup(func() {
101+ order = append(order, "first")
102+ })
103+ s.AddCleanup(func() {
104+ order = append(order, "second")
105+ })
106+
107+ s.TearDownTest(c)
108+ c.Assert(order, gc.DeepEquals, []string{"second", "first"})
109+
110+ // and to avoid calling them twice, clear out the stack.
111+ s.SetUpTest(c)
112+}
113
114=== modified file 'testing/log.go'
115--- testing/log.go 2013-08-28 17:20:01 +0000
116+++ testing/log.go 2013-09-12 05:03:44 +0000
117@@ -14,7 +14,7 @@
118 // LoggingSuite redirects the juju logger to the test logger
119 // when embedded in a gocheck suite type.
120 type LoggingSuite struct {
121- restoreLog func()
122+ CleanupSuite
123 }
124
125 type gocheckWriter struct {
126@@ -27,21 +27,19 @@
127 }
128
129 func (t *LoggingSuite) SetUpSuite(c *gc.C) {
130+ t.CleanupSuite.SetUpSuite(c)
131 t.setUp(c)
132-}
133-
134-func (t *LoggingSuite) TearDownSuite(c *gc.C) {
135- loggo.ResetLoggers()
136- loggo.ResetWriters()
137+ t.AddSuiteCleanup(func() {
138+ loggo.ResetLoggers()
139+ loggo.ResetWriters()
140+ })
141 }
142
143 func (t *LoggingSuite) SetUpTest(c *gc.C) {
144+ t.CleanupSuite.SetUpTest(c)
145 t.setUp(c)
146 }
147
148-func (t *LoggingSuite) TearDownTest(c *gc.C) {
149-}
150-
151 func (t *LoggingSuite) setUp(c *gc.C) {
152 loggo.ResetWriters()
153 loggo.ReplaceDefaultWriter(&gocheckWriter{c})

Subscribers

People subscribed via source and target branches

to status/vote changes: