Merge lp:~salgado/launchpad/fix-readonly-test-helpers into lp:launchpad

Proposed by Guilherme Salgado
Status: Merged
Approved by: Brad Crittenden
Approved revision: not available
Merged at revision: not available
Proposed branch: lp:~salgado/launchpad/fix-readonly-test-helpers
Merge into: lp:launchpad
Diff against target: 76 lines (+29/-7)
2 files modified
lib/canonical/launchpad/tests/readonly.py (+19/-6)
lib/canonical/launchpad/webapp/tests/test_publication.py (+10/-1)
To merge this branch: bzr merge lp:~salgado/launchpad/fix-readonly-test-helpers
Reviewer Review Type Date Requested Status
Gary Poster (community) release-critical Approve
Brad Crittenden (community) code Approve
Review via email: mp+18073@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Guilherme Salgado (salgado) wrote :

Change the read-only test helpers to assert that the mode switch was
successful, thus forcing the mode change to be logged at the moment they
happen.

Revision history for this message
Brad Crittenden (bac) :
review: Approve (code)
Revision history for this message
Guilherme Salgado (salgado) wrote :
Download full text (3.3 KiB)

Thanks for the review, Brad, but my changes have broken an existing test
because it was calling remove_read_only_file() after calling
publication.beforeTraversal(request). That's problematic because the
mode switch can't actually happen at that point (it can only happen at
the beginning of a request) so the new assertion I added was failing.

The change below makes the new assertion conditional on a newly added
method argument, so that it can be skipped when needed.

=== modified file 'lib/canonical/launchpad/tests/readonly.py'
--- lib/canonical/launchpad/tests/readonly.py 2010-01-26 13:06:36 +0000
+++ lib/canonical/launchpad/tests/readonly.py 2010-01-26 17:41:29 +0000
@@ -31,9 +31,17 @@
     assert is_read_only(), "Switching to read-only failed."

-def remove_read_only_file():
- """Remove the file named read-only.txt from the root of the tree."""
+def remove_read_only_file(assert_mode_switch=True):
+ """Remove the file named read-only.txt from the root of the tree.
+
+ May also assert that the mode switch actually happened (i.e. not
+ is_read_only()). This assertion has to be conditional because some tests
+ will use this during the processing of a request, when a mode change can't
+ happen (i.e. is_read_only() will still return True during that request's
+ processing, even though the read-only.txt file has been removed).
+ """
     os.remove(read_only_file_path)
- # Assert that the switch succeeded and make sure the mode change is
- # logged.
- assert not is_read_only(), "Switching to read-write failed."
+ if assert_mode_switch:
+ # Assert that the switch succeeded and make sure the mode change is
+ # logged.
+ assert not is_read_only(), "Switching to read-write failed."

=== modified file 'lib/canonical/launchpad/webapp/tests/test_publication.py'
--- lib/canonical/launchpad/webapp/tests/test_publication.py 2010-01-19 04:22:17 +0000
+++ lib/canonical/launchpad/webapp/tests/test_publication.py 2010-01-26 17:53:28 +0000
@@ -23,6 +23,7 @@
 from canonical.launchpad.interfaces.lpstorm import IMasterStore
 from canonical.launchpad.interfaces.oauth import IOAuthConsumerSet
 from canonical.launchpad.ftests import ANONYMOUS, login
+from canonical.launchpad.readonly import is_read_only
 from canonical.launchpad.tests.readonly import (
     remove_read_only_file, touch_read_only_file)
 import canonical.launchpad.webapp.adapter as dbadapter
@@ -79,6 +80,9 @@
         # Cleanup needed so that further tests can start processing other
         # requests (e.g. calling beforeTraversal).
         self.publication.endRequest(self.request, None)
+ # Force pending mode switches to actually happen and get logged so
+ # that we don't interfere with other tests.
+ assert not is_read_only()

     def setUp(self):
         TestCase.setUp(self)
@@ -127,7 +131,10 @@
             touch_read_only_file()
             self.publication.beforeTraversal(self.request)
         finally:
- remove_read_only_file()
+ # Tell remove_read_only_file() to not assert that the mode switch
+ # actually happened, as we know it won't happen until this request
+ # ...

Read more...

Revision history for this message
Brad Crittenden (bac) wrote :

I've looked at your mods and it looks good.

self.request, None)
+ # Force pending mode switches to actually happen and get logged so
+ # that we don't interfere with other tests.
+ assert not is_read_only()

Please add a failure message to this assert.

review: Approve (code)
Revision history for this message
Gary Poster (gary) :
review: Approve (release-critical)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/canonical/launchpad/tests/readonly.py'
2--- lib/canonical/launchpad/tests/readonly.py 2010-01-13 20:06:09 +0000
3+++ lib/canonical/launchpad/tests/readonly.py 2010-01-27 11:28:15 +0000
4@@ -13,7 +13,7 @@
5 import os
6
7 from canonical.launchpad.readonly import (
8- read_only_file_exists, read_only_file_path)
9+ is_read_only, read_only_file_exists, read_only_file_path)
10
11
12 def touch_read_only_file():
13@@ -26,9 +26,22 @@
14 "already exists.")
15 f = open(read_only_file_path, 'w')
16 f.close()
17-
18-
19-def remove_read_only_file():
20- """Remove the file named read-only.txt from the root of the tree."""
21+ # Assert that the switch succeeded and make sure the mode change is
22+ # logged.
23+ assert is_read_only(), "Switching to read-only failed."
24+
25+
26+def remove_read_only_file(assert_mode_switch=True):
27+ """Remove the file named read-only.txt from the root of the tree.
28+
29+ May also assert that the mode switch actually happened (i.e. not
30+ is_read_only()). This assertion has to be conditional because some tests
31+ will use this during the processing of a request, when a mode change can't
32+ happen (i.e. is_read_only() will still return True during that request's
33+ processing, even though the read-only.txt file has been removed).
34+ """
35 os.remove(read_only_file_path)
36-
37+ if assert_mode_switch:
38+ # Assert that the switch succeeded and make sure the mode change is
39+ # logged.
40+ assert not is_read_only(), "Switching to read-write failed."
41
42=== modified file 'lib/canonical/launchpad/webapp/tests/test_publication.py'
43--- lib/canonical/launchpad/webapp/tests/test_publication.py 2010-01-19 04:22:17 +0000
44+++ lib/canonical/launchpad/webapp/tests/test_publication.py 2010-01-27 11:28:15 +0000
45@@ -23,6 +23,7 @@
46 from canonical.launchpad.interfaces.lpstorm import IMasterStore
47 from canonical.launchpad.interfaces.oauth import IOAuthConsumerSet
48 from canonical.launchpad.ftests import ANONYMOUS, login
49+from canonical.launchpad.readonly import is_read_only
50 from canonical.launchpad.tests.readonly import (
51 remove_read_only_file, touch_read_only_file)
52 import canonical.launchpad.webapp.adapter as dbadapter
53@@ -79,6 +80,11 @@
54 # Cleanup needed so that further tests can start processing other
55 # requests (e.g. calling beforeTraversal).
56 self.publication.endRequest(self.request, None)
57+ # Force pending mode switches to actually happen and get logged so
58+ # that we don't interfere with other tests.
59+ assert not is_read_only(), (
60+ "A test failed to clean things up properly, leaving the app "
61+ "in read-only mode.")
62
63 def setUp(self):
64 TestCase.setUp(self)
65@@ -127,7 +133,10 @@
66 touch_read_only_file()
67 self.publication.beforeTraversal(self.request)
68 finally:
69- remove_read_only_file()
70+ # Tell remove_read_only_file() to not assert that the mode switch
71+ # actually happened, as we know it won't happen until this request
72+ # is finished.
73+ remove_read_only_file(assert_mode_switch=False)
74
75 # Here the mode has changed to read-only, so the stores were removed
76 # from zstorm.