Merge lp:~simpoir/landscape-client/fix_tempfile_cleaning into lp:~landscape/landscape-client/trunk

Proposed by Simon Poirier
Status: Merged
Approved by: Simon Poirier
Approved revision: 919
Merged at revision: 917
Proposed branch: lp:~simpoir/landscape-client/fix_tempfile_cleaning
Merge into: lp:~landscape/landscape-client/trunk
Diff against target: 320 lines (+44/-43)
10 files modified
landscape/broker/tests/test_store.py (+2/-10)
landscape/lib/tests/test_persist.py (+7/-7)
landscape/manager/aptsources.py (+6/-1)
landscape/manager/scriptexecution.py (+3/-6)
landscape/monitor/tests/test_cephusage.py (+2/-4)
landscape/monitor/tests/test_usermonitor.py (+2/-3)
landscape/package/tests/test_changer.py (+1/-1)
landscape/tests/helpers.py (+18/-8)
landscape/tests/test_service.py (+1/-1)
landscape/tests/test_watchdog.py (+2/-2)
To merge this branch: bzr merge lp:~simpoir/landscape-client/fix_tempfile_cleaning
Reviewer Review Type Date Requested Status
🤖 Landscape Builder test results Approve
Chris Glass (community) Approve
Benji York (community) Approve
Review via email: mp+303189@code.launchpad.net

Commit message

This branches does some cleanup in test tempfiles and fixes their broken
removal in test helpers.

Description of the change

This branches does some cleanup in test tempfiles and fixes their broken
removal in test helpers.

Testing instructions:

export TMP=$(mktemp -d)
trial -j8 landscape
ls $TMP

To post a comment you must log in.
Revision history for this message
🤖 Landscape Builder (landscape-builder) :
review: Abstain (executing tests)
Revision history for this message
🤖 Landscape Builder (landscape-builder) wrote :

Command: TRIAL_ARGS=-j4 make check
Result: Success
Revno: 918
Branch: lp:~simpoir/landscape-client/fix_tempfile_cleaning
Jenkins: https://ci.lscape.net/job/latch-test/7857/

review: Approve (test results)
Revision history for this message
Benji York (benji) wrote :

Other than the inline, preexisting trivial, this branch looks good.

review: Approve
Revision history for this message
Chris Glass (tribaal) wrote :

Nice! +1

(agree with Benji's point FWIW)

review: Approve
919. By Simon Poirier

address review nitpick

Revision history for this message
🤖 Landscape Builder (landscape-builder) :
review: Abstain (executing tests)
Revision history for this message
🤖 Landscape Builder (landscape-builder) wrote :

Command: TRIAL_ARGS=-j4 make check
Result: Success
Revno: 919
Branch: lp:~simpoir/landscape-client/fix_tempfile_cleaning
Jenkins: https://ci.lscape.net/job/latch-test/7889/

review: Approve (test results)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/broker/tests/test_store.py'
2--- landscape/broker/tests/test_store.py 2016-06-15 23:00:54 +0000
3+++ landscape/broker/tests/test_store.py 2016-08-19 18:33:45 +0000
4@@ -1,5 +1,3 @@
5-import tempfile
6-import shutil
7 import os
8 import mock
9
10@@ -14,8 +12,8 @@
11
12 def setUp(self):
13 super(MessageStoreTest, self).setUp()
14- self.temp_dir = tempfile.mkdtemp()
15- self.persist_filename = tempfile.mktemp()
16+ self.temp_dir = self.makeDir()
17+ self.persist_filename = self.makeFile()
18 self.store = self.create_store()
19
20 def create_store(self):
21@@ -29,12 +27,6 @@
22 store.add_schema(Message("resynchronize", {}))
23 return store
24
25- def tearDown(self):
26- super(MessageStoreTest, self).tearDown()
27- shutil.rmtree(self.temp_dir)
28- if os.path.isfile(self.persist_filename):
29- os.unlink(self.persist_filename)
30-
31 def test_get_set_sequence(self):
32 self.assertEqual(self.store.get_sequence(), 0)
33 self.store.set_sequence(3)
34
35=== modified file 'landscape/lib/tests/test_persist.py'
36--- landscape/lib/tests/test_persist.py 2013-04-01 17:53:37 +0000
37+++ landscape/lib/tests/test_persist.py 2016-08-19 18:33:45 +0000
38@@ -324,7 +324,7 @@
39 for path in self.set_result:
40 self.persist.set(path, self.set_result[path])
41
42- filename = self.makeFile()
43+ filename = self.makePersistFile()
44 self.persist.save(filename)
45
46 persist = self.build_persist()
47@@ -335,7 +335,7 @@
48 self.format(result, self.set_result))
49
50 def test_save_on_unexistent_dir(self):
51- dirname = self.makeFile()
52+ dirname = self.makePersistFile()
53 filename = os.path.join(dirname, "foobar")
54
55 self.assertFalse(os.path.exists(dirname))
56@@ -355,7 +355,7 @@
57 Persist can be constructed with a filename, and Persist.save with no
58 arguments will write to that filename.
59 """
60- filename = self.makeFile()
61+ filename = self.makePersistFile()
62 persist = self.build_persist(filename=filename)
63 self.assertFalse(os.path.exists(filename))
64 persist.save()
65@@ -373,7 +373,7 @@
66 If a Persist is created with a default filename, and the filename
67 exists, it will be loaded.
68 """
69- filename = self.makeFile()
70+ filename = self.makePersistFile()
71 persist = self.build_persist(filename=filename)
72 persist.set("foo", "bar")
73 persist.save()
74@@ -394,7 +394,7 @@
75 self.assertEqual(persist.get("a"), 1)
76
77 def test_load_empty_files_wont_break(self):
78- filename = self.makeFile("")
79+ filename = self.makePersistFile("")
80 self.persist.load(filename)
81
82 def test_load_empty_files_restore_backup(self):
83@@ -402,7 +402,7 @@
84 If the current file is empty, it tries to load the old one if it
85 exists.
86 """
87- filename = self.makeFile("")
88+ filename = self.makePersistFile("")
89 filename_old = filename + ".old"
90
91 self.persist.set("a", 1)
92@@ -425,7 +425,7 @@
93 If the file doesn't exist, it tries to load the old one if present and
94 valid.
95 """
96- filename = self.makeFile("")
97+ filename = self.makePersistFile("")
98 filename_old = filename + ".old"
99 os.unlink(filename)
100
101
102=== modified file 'landscape/manager/aptsources.py'
103--- landscape/manager/aptsources.py 2013-04-01 09:25:31 +0000
104+++ landscape/manager/aptsources.py 2016-08-19 18:33:45 +0000
105@@ -103,7 +103,12 @@
106 fd, path = tempfile.mkstemp()
107 os.close(fd)
108 new_sources = file(path, "w")
109- for line in file(self.SOURCES_LIST):
110+ try:
111+ source_file = open(self.SOURCES_LIST)
112+ except:
113+ os.unlink(path)
114+ raise
115+ for line in source_file:
116 stripped_line = line.strip()
117 if not stripped_line or stripped_line.startswith("#"):
118 new_sources.write(line)
119
120=== modified file 'landscape/manager/scriptexecution.py'
121--- landscape/manager/scriptexecution.py 2013-05-17 22:32:49 +0000
122+++ landscape/manager/scriptexecution.py 2016-08-19 18:33:45 +0000
123@@ -205,9 +205,9 @@
124 return self._respond(FAILED, str(failure), opid)
125
126 @inlineCallbacks
127- def _save_attachments(self, attachments, uid, gid, computer_id):
128+ def _save_attachments(self, attachments, uid, gid, computer_id, env):
129 root_path = self.registry.config.url.rsplit("/", 1)[0] + "/attachment/"
130- attachment_dir = tempfile.mkdtemp()
131+ env["LANDSCAPE_ATTACHMENTS"] = attachment_dir = tempfile.mkdtemp()
132 headers = {"User-Agent": "landscape-client/%s" % VERSION,
133 "Content-Type": "application/octet-stream",
134 "X-Computer-ID": computer_id}
135@@ -277,15 +277,12 @@
136 "broker.bpickle"))
137 persist = persist.root_at("registration")
138 computer_id = persist.get("secure-id")
139- d = self._save_attachments(attachments, uid, gid, computer_id)
140+ d = self._save_attachments(attachments, uid, gid, computer_id, env)
141 else:
142 d = succeed(None)
143
144 def prepare_script(attachment_dir):
145
146- if attachment_dir is not None:
147- env["LANDSCAPE_ATTACHMENTS"] = attachment_dir
148-
149 return self._run_script(
150 filename, uid, gid, path, env, time_limit)
151
152
153=== modified file 'landscape/monitor/tests/test_cephusage.py'
154--- landscape/monitor/tests/test_cephusage.py 2016-06-15 16:11:17 +0000
155+++ landscape/monitor/tests/test_cephusage.py 2016-08-19 18:33:45 +0000
156@@ -1,5 +1,4 @@
157 import mock
158-import tempfile
159 from landscape.lib.fs import touch_file
160 from landscape.tests.helpers import LandscapeTest, MonitorHelper
161 from landscape.monitor.cephusage import CephUsage
162@@ -110,8 +109,7 @@
163 plugin._ceph_config = None
164 self.assertFalse(plugin._should_run())
165
166-
167- @mock.patch("logging.info")
168+ @mock.patch("logging.info")
169 def test_wb_should_run_no_rados(self, logging):
170 """
171 If the Rados library cannot be imported (CephUsage._has_rados is False)
172@@ -131,7 +129,7 @@
173 """
174 plugin = CephUsage()
175 plugin._has_rados = True
176- plugin._ceph_config = tempfile.mktemp()
177+ plugin._ceph_config = self.makeFile()
178 touch_file(plugin._ceph_config)
179 self.assertTrue(plugin._should_run())
180
181
182=== modified file 'landscape/monitor/tests/test_usermonitor.py'
183--- landscape/monitor/tests/test_usermonitor.py 2016-06-15 20:34:35 +0000
184+++ landscape/monitor/tests/test_usermonitor.py 2016-08-19 18:33:45 +0000
185@@ -1,5 +1,4 @@
186 import os
187-import tempfile
188
189 from mock import Mock, ANY
190 from twisted.internet.defer import fail
191@@ -359,7 +358,7 @@
192
193 # Create the (temporary, test) user update flag file.
194 landscape.monitor.usermonitor.USER_UPDATE_FLAG_FILE = \
195- update_flag_file = tempfile.mkstemp()[1]
196+ update_flag_file = self.makeFile("")
197 self.addCleanup(lambda: os.remove(update_flag_file))
198
199 # Trigger a detect changes.
200@@ -381,7 +380,7 @@
201
202 # Create the (temporary, test) user update flag file.
203 landscape.monitor.usermonitor.USER_UPDATE_FLAG_FILE = \
204- update_flag_file = tempfile.mkstemp()[1]
205+ update_flag_file = self.makeFile("")
206
207 self.broker_service.message_store.set_accepted_types(["users"])
208 self.provider.users = [("jdoe", "x", 1000, 1000, "JD,,,,",
209
210=== modified file 'landscape/package/tests/test_changer.py'
211--- landscape/package/tests/test_changer.py 2016-06-16 21:13:41 +0000
212+++ landscape/package/tests/test_changer.py 2016-08-19 18:33:45 +0000
213@@ -881,7 +881,7 @@
214 from previous runs.
215 """
216 existing_deb_path = os.path.join(self.config.binaries_path, "123.deb")
217- self.makeFile(basename=existing_deb_path, content="foo")
218+ self.makeFile(path=existing_deb_path, content="foo")
219 self.changer.init_channels([])
220 self.assertFalse(os.path.exists(existing_deb_path))
221
222
223=== modified file 'landscape/tests/helpers.py'
224--- landscape/tests/helpers.py 2016-06-17 00:17:50 +0000
225+++ landscape/tests/helpers.py 2016-08-19 18:33:45 +0000
226@@ -94,7 +94,7 @@
227 "%s" % (diff, extra))
228
229
230-class LandscapeTest(MessageTestCase, HelperTestCase, TestCase):
231+class LandscapeTest(MessageTestCase, HelperTestCase, TestCase, ):
232
233 def setUp(self):
234 self._old_config_filenames = BaseConfiguration.default_config_filenames
235@@ -227,16 +227,12 @@
236
237 The file is removed after the test runs.
238 """
239- if path is not None:
240- self.addCleanup(shutil.rmtree, path, ignore_errors=True)
241- elif basename is not None:
242+ if basename is not None:
243 if dirname is None:
244 dirname = tempfile.mkdtemp()
245- self.addCleanup(shutil.rmtree, dirname, ignore_errors=True)
246 path = os.path.join(dirname, basename)
247- else:
248+ elif path is None:
249 fd, path = tempfile.mkstemp(suffix, prefix, dirname)
250- self.addCleanup(shutil.rmtree, path, ignore_errors=True)
251 os.close(fd)
252 if content is None:
253 os.unlink(path)
254@@ -244,8 +240,22 @@
255 file = open(path, "w")
256 file.write(content)
257 file.close()
258+ self.addCleanup(self._clean_file, path)
259 return path
260
261+ def _clean_file(self, path):
262+ """Try to remove a filesystem path, whether it's a directory or file.
263+
264+ @param path: the path to remove
265+ """
266+ try:
267+ if os.path.isdir(path):
268+ shutil.rmtree(path)
269+ else:
270+ os.unlink(path)
271+ except OSError:
272+ pass
273+
274 def makeDir(self, suffix="", prefix="tmp", dirname=None, path=None):
275 """Create a temporary directory and return the path to it.
276
277@@ -259,7 +269,7 @@
278 os.makedirs(path)
279 else:
280 path = tempfile.mkdtemp(suffix, prefix, dirname)
281- self.addCleanup(shutil.rmtree, path, ignore_errors=True)
282+ self.addCleanup(self._clean_file, path)
283 return path
284
285
286
287=== modified file 'landscape/tests/test_service.py'
288--- landscape/tests/test_service.py 2016-06-15 17:07:39 +0000
289+++ landscape/tests/test_service.py 2016-08-19 18:33:45 +0000
290@@ -40,7 +40,7 @@
291 """
292
293 class PersistService(TestService):
294- persist_filename = self.makeFile(content="")
295+ persist_filename = self.makePersistFile(content="")
296
297 service = PersistService(self.config)
298 self.assertEqual(service.persist.filename, service.persist_filename)
299
300=== modified file 'landscape/tests/test_watchdog.py'
301--- landscape/tests/test_watchdog.py 2016-06-16 23:27:52 +0000
302+++ landscape/tests/test_watchdog.py 2016-08-19 18:33:45 +0000
303@@ -1305,7 +1305,7 @@
304 "landscape", None, os.getuid(), None, None, None, None)
305 reactor = FakeReactor()
306 with mock.patch("landscape.watchdog.pwd", new=self.fake_pwd):
307- run(["--log-dir", self.makeFile()], reactor=reactor)
308+ run(["--log-dir", self.makeDir()], reactor=reactor)
309 self.assertTrue(reactor.running)
310
311 def test_no_landscape_user(self):
312@@ -1328,7 +1328,7 @@
313
314 reactor = FakeReactor()
315 with mock.patch("landscape.watchdog.pwd", new=self.fake_pwd):
316- run(["--log-dir", self.makeFile()], reactor=reactor)
317+ run(["--log-dir", self.makeDir()], reactor=reactor)
318 self.assertNotIn("DEBIAN_YO", os.environ)
319 self.assertNotIn("DEBCONF_YO", os.environ)
320 self.assertNotIn("LANDSCAPE_ATTACHMENTS", os.environ)

Subscribers

People subscribed via source and target branches

to all changes: