Merge lp:~gocept/landscape-client/fetch-returns-bytes-in-tests into lp:~landscape/landscape-client/trunk

Proposed by Steffen Allner
Status: Merged
Approved by: Eric Snow
Approved revision: 1000
Merged at revision: 1004
Proposed branch: lp:~gocept/landscape-client/fetch-returns-bytes-in-tests
Merge into: lp:~landscape/landscape-client/trunk
Diff against target: 303 lines (+49/-40)
7 files modified
landscape/configuration.py (+4/-2)
landscape/lib/tests/test_cloud.py (+9/-8)
landscape/manager/tests/test_scriptexecution.py (+15/-8)
landscape/monitor/tests/test_computerinfo.py (+6/-5)
landscape/package/reporter.py (+2/-4)
landscape/package/tests/test_reporter.py (+4/-4)
landscape/tests/test_configuration.py (+9/-9)
To merge this branch: bzr merge lp:~gocept/landscape-client/fetch-returns-bytes-in-tests
Reviewer Review Type Date Requested Status
Simon Poirier (community) Approve
Eric Snow (community) Approve
🤖 Landscape Builder test results Approve
Gocept Pending
Review via email: mp+321977@code.launchpad.net

Commit message

Return bytes from mocks of landscape.lib.fetch.fetch().

In this MP an inconsistency between tests and actual implementation was fixed. The landscape.lib.fetch.fetch() returns bytes now explicitly, but this was not reflected in the mocks used throughout the landscape-client project.

I tried to convert all return values of the mocks to bytes and adjusted the tests accordingly.

Description of the change

In this MP an inconsistency between tests and actual implementation was fixed. The landscape.lib.fetch.fetch() returns bytes now explicitly, but this was not reflected in the mocks used throughout the landscape-client project.

I tried to convert all return values of the mocks to bytes and adjusted the tests accordingly.

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
Steffen Allner (sallner) wrote :

Inline clarification.

Revision history for this message
🤖 Landscape Builder (landscape-builder) wrote :
review: Approve (test results)
Revision history for this message
Eric Snow (ericsnowcurrently) wrote :

LGTM

review: Approve
Revision history for this message
Simon Poirier (simpoir) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/configuration.py'
2--- landscape/configuration.py 2017-03-28 06:09:31 +0000
3+++ landscape/configuration.py 2017-04-05 11:38:17 +0000
4@@ -9,11 +9,12 @@
5 from functools import partial
6 import base64
7 import getpass
8+import io
9 import os
10 import pwd
11 import sys
12
13-from landscape.compat import StringIO, input
14+from landscape.compat import input
15
16 from landscape.lib.tag import is_valid_tag
17
18@@ -117,7 +118,8 @@
19 os.environ["https_proxy"] = self.https_proxy
20 content = self.fetch_import_url(self.import_from)
21 parser = self._get_config_object(
22- alternative_config=StringIO(content))
23+ alternative_config=io.StringIO(
24+ content.decode("utf-8")))
25 elif not os.path.isfile(self.import_from):
26 raise ImportOptionError("File %s doesn't exist." %
27 self.import_from)
28
29=== modified file 'landscape/lib/tests/test_cloud.py'
30--- landscape/lib/tests/test_cloud.py 2017-03-09 14:14:57 +0000
31+++ landscape/lib/tests/test_cloud.py 2017-04-05 11:38:17 +0000
32@@ -21,14 +21,15 @@
33 return succeed(value)
34
35 self.fetch_func = fetch_stub
36- self.add_query_result("instance-id", "i00001")
37- self.add_query_result("ami-id", "ami-00002")
38- self.add_query_result("instance-type", "hs1.8xlarge")
39+ self.add_query_result("instance-id", b"i00001")
40+ self.add_query_result("ami-id", b"ami-00002")
41+ self.add_query_result("instance-type", b"hs1.8xlarge")
42
43 def add_query_result(self, name, value):
44 """
45 Add a url to self.query_results that is then available through
46- self.fetch_func.
47+ self.fetch_func. C{value} must be bytes or an Error as the original
48+ fetch returns bytes.
49 """
50 url = "%s/meta-data/%s" % (EC2_API, name)
51 self.query_results[url] = value
52@@ -84,9 +85,9 @@
53
54 def test_fetch_ec2_meta_data_truncates(self):
55 """L{_fetch_ec2_meta_data} truncates values that are too long."""
56- self.add_query_result("ami-id", "a" * MAX_LENGTH * 5)
57- self.add_query_result("instance-id", "b" * MAX_LENGTH * 5)
58- self.add_query_result("instance-type", "c" * MAX_LENGTH * 5)
59+ self.add_query_result("ami-id", b"a" * MAX_LENGTH * 5)
60+ self.add_query_result("instance-id", b"b" * MAX_LENGTH * 5)
61+ self.add_query_result("instance-type", b"c" * MAX_LENGTH * 5)
62 deferred = fetch_ec2_meta_data(fetch=self.fetch_func)
63 result = self.successResultOf(deferred)
64 self.assertEqual(
65@@ -107,7 +108,7 @@
66 self.successResultOf(
67 _fetch_ec2_item(
68 "instance-type", accumulate, fetch=self.fetch_func))
69- self.assertEqual(["i00001", "hs1.8xlarge"], accumulate)
70+ self.assertEqual([b"i00001", b"hs1.8xlarge"], accumulate)
71
72 def test_wb_fetch_ec2_item_error_returns_failure(self):
73 """
74
75=== modified file 'landscape/manager/tests/test_scriptexecution.py'
76--- landscape/manager/tests/test_scriptexecution.py 2017-03-27 11:52:09 +0000
77+++ landscape/manager/tests/test_scriptexecution.py 2017-04-05 11:38:17 +0000
78@@ -209,9 +209,10 @@
79 mock_umask.assert_has_calls([mock.call(0o022)])
80 mock_mkdtemp.assert_called_with()
81
82- def cleanup(_):
83+ def cleanup(result):
84 patch_umask.stop()
85 patch_mkdtemp.stop()
86+ return result
87
88 return result.addErrback(check).addBoth(cleanup)
89
90@@ -243,7 +244,7 @@
91 patch_fetch = mock.patch(
92 "landscape.manager.scriptexecution.fetch_async")
93 mock_fetch = patch_fetch.start()
94- mock_fetch.return_value = succeed("some other data")
95+ mock_fetch.return_value = succeed(b"some other data")
96
97 headers = {"User-Agent": "landscape-client/%s" % VERSION,
98 "Content-Type": "application/octet-stream",
99@@ -260,8 +261,10 @@
100 "https://localhost/attachment/14", headers=headers,
101 cainfo=None)
102
103- def cleanup(_):
104+ def cleanup(result):
105 patch_fetch.stop()
106+ # We have to return the Failure or result to get a working test.
107+ return result
108
109 return result.addCallback(check).addBoth(cleanup)
110
111@@ -281,7 +284,7 @@
112 patch_fetch = mock.patch(
113 "landscape.manager.scriptexecution.fetch_async")
114 mock_fetch = patch_fetch.start()
115- mock_fetch.return_value = succeed("some other data")
116+ mock_fetch.return_value = succeed(b"some other data")
117
118 headers = {"User-Agent": "landscape-client/%s" % VERSION,
119 "Content-Type": "application/octet-stream",
120@@ -298,8 +301,9 @@
121 "https://localhost/attachment/14", headers=headers,
122 cainfo="/some/key")
123
124- def cleanup(_):
125+ def cleanup(result):
126 patch_fetch.stop()
127+ return result
128
129 return result.addCallback(check).addBoth(cleanup)
130
131@@ -357,8 +361,9 @@
132 mock_chown.assert_called_with()
133 self.assertEqual(result, "foobar")
134
135- def cleanup(_):
136+ def cleanup(result):
137 patch_chown.stop()
138+ return result
139
140 return result.addErrback(check).addBoth(cleanup)
141
142@@ -397,8 +402,9 @@
143 def check(result):
144 mock_getpwnam.assert_called_with("user")
145
146- def cleanup(_):
147+ def cleanup(result):
148 patch_getpwnam.stop()
149+ return result
150
151 return result.addCallback(check).addBoth(cleanup)
152
153@@ -437,8 +443,9 @@
154 mock_chown.assert_has_calls(
155 [mock.call(mock.ANY, uid, gid) for x in range(3)])
156
157- def cleanup(_):
158+ def cleanup(result):
159 patch_chown.stop()
160+ return result
161
162 return result.addCallback(check).addBoth(cleanup)
163
164
165=== modified file 'landscape/monitor/tests/test_computerinfo.py'
166--- landscape/monitor/tests/test_computerinfo.py 2017-03-29 14:14:35 +0000
167+++ landscape/monitor/tests/test_computerinfo.py 2017-04-05 11:38:17 +0000
168@@ -62,14 +62,15 @@
169 return succeed(value)
170
171 self.fetch_func = fetch_stub
172- self.add_query_result("instance-id", "i00001")
173- self.add_query_result("ami-id", "ami-00002")
174- self.add_query_result("instance-type", "hs1.8xlarge")
175+ self.add_query_result("instance-id", b"i00001")
176+ self.add_query_result("ami-id", b"ami-00002")
177+ self.add_query_result("instance-type", b"hs1.8xlarge")
178
179 def add_query_result(self, name, value):
180 """
181 Add a url to self.query_results that is then available through
182- self.fetch_func.
183+ self.fetch_func. C{value} must be bytes or an Error as the original
184+ fetch returns bytes.
185 """
186 url = "http://169.254.169.254/latest/meta-data/" + name
187 self.query_results[url] = value
188@@ -494,7 +495,7 @@
189 self.assertEqual(1, plugin._cloud_retries)
190 self.assertEqual(None, result)
191 # Fix the error condition for the retry.
192- self.add_query_result("ami-id", "ami-00002")
193+ self.add_query_result("ami-id", b"ami-00002")
194 result = yield plugin._fetch_ec2_meta_data()
195 self.assertEqual({"instance-id": u"i00001", "ami-id": u"ami-00002",
196 "instance-type": u"hs1.8xlarge"},
197
198=== modified file 'landscape/package/reporter.py'
199--- landscape/package/reporter.py 2017-03-21 17:00:34 +0000
200+++ landscape/package/reporter.py 2017-04-05 11:38:17 +0000
201@@ -17,7 +17,7 @@
202 from landscape.lib.sequenceranges import sequence_to_ranges
203 from landscape.lib.twisted_util import gather_results, spawn_process
204 from landscape.lib.fetch import fetch_async
205-from landscape.lib.fs import touch_file
206+from landscape.lib.fs import touch_file, create_binary_file
207 from landscape.lib.lsb_release import parse_lsb_release, LSB_RELEASE_FILENAME
208
209 from landscape.package.taskhandler import (
210@@ -131,9 +131,7 @@
211 url = str(base_url + os.path.basename(hash_id_db_filename))
212
213 def fetch_ok(data):
214- hash_id_db_fd = open(hash_id_db_filename, "w")
215- hash_id_db_fd.write(data)
216- hash_id_db_fd.close()
217+ create_binary_file(hash_id_db_filename, data)
218 logging.info("Downloaded hash=>id database from %s" % url)
219
220 def fetch_error(failure):
221
222=== modified file 'landscape/package/tests/test_reporter.py'
223--- landscape/package/tests/test_reporter.py 2017-03-21 17:00:34 +0000
224+++ landscape/package/tests/test_reporter.py 2017-04-05 11:38:17 +0000
225@@ -270,7 +270,7 @@
226 return deferred.addCallback(got_result)
227
228 @mock.patch("landscape.package.reporter.fetch_async",
229- return_value=succeed("hash-ids"))
230+ return_value=succeed(b"hash-ids"))
231 @mock.patch("logging.info", return_value=None)
232 def test_fetch_hash_id_db(self, logging_mock, mock_fetch_async):
233
234@@ -308,7 +308,7 @@
235 return result
236
237 @mock.patch("landscape.package.reporter.fetch_async",
238- return_value=succeed("hash-ids"))
239+ return_value=succeed(b"hash-ids"))
240 @mock.patch("logging.info", return_value=None)
241 def test_fetch_hash_id_db_with_proxy(self, logging_mock, mock_fetch_async):
242 """fetching hash-id-db uses proxy settings"""
243@@ -414,7 +414,7 @@
244 return result
245
246 @mock.patch("landscape.package.reporter.fetch_async",
247- return_value=succeed("hash-ids"))
248+ return_value=succeed(b"hash-ids"))
249 def test_fetch_hash_id_db_with_default_url(self, mock_fetch_async):
250 # Let's say package_hash_id_url is not set but url is
251 self.config.data_path = self.makeDir()
252@@ -507,7 +507,7 @@
253 return result
254
255 @mock.patch("landscape.package.reporter.fetch_async",
256- return_value=succeed("hash-ids"))
257+ return_value=succeed(b"hash-ids"))
258 def test_fetch_hash_id_db_with_custom_certificate(self, mock_fetch_async):
259 """
260 The L{PackageReporter.fetch_hash_id_db} method takes into account the
261
262=== modified file 'landscape/tests/test_configuration.py'
263--- landscape/tests/test_configuration.py 2017-03-28 06:09:31 +0000
264+++ landscape/tests/test_configuration.py 2017-04-05 11:38:17 +0000
265@@ -1595,13 +1595,13 @@
266 self, mock_sysvconfig, mock_fetch, mock_print_text):
267 mock_sysvconfig().restart_landscape.return_value = True
268 configuration = (
269- "[client]\n"
270- "computer_title = New Title\n"
271- "account_name = New Name\n"
272- "registration_key = New Password\n"
273- "http_proxy = http://new.proxy\n"
274- "https_proxy = https://new.proxy\n"
275- "url = http://new.url\n")
276+ b"[client]\n"
277+ b"computer_title = New Title\n"
278+ b"account_name = New Name\n"
279+ b"registration_key = New Password\n"
280+ b"http_proxy = http://new.proxy\n"
281+ b"https_proxy = https://new.proxy\n"
282+ b"url = http://new.url\n")
283
284 mock_fetch.return_value = configuration
285
286@@ -1670,7 +1670,7 @@
287 "Fetching configuration from https://config.url...")
288
289 @mock.patch("landscape.configuration.print_text")
290- @mock.patch("landscape.configuration.fetch", return_value="")
291+ @mock.patch("landscape.configuration.fetch", return_value=b"")
292 def test_import_from_url_with_empty_content(
293 self, mock_fetch, mock_print_text):
294 # Use a command line option as well to test the precedence.
295@@ -1687,7 +1687,7 @@
296
297 @mock.patch("landscape.configuration.print_text")
298 @mock.patch("landscape.configuration.fetch",
299- return_value="<strong>BOGUS!</strong>")
300+ return_value=b"<strong>BOGUS!</strong>")
301 def test_import_from_url_with_bogus_content(
302 self, mock_fetch, mock_print_text):
303 # Use a command line option as well to test the precedence.

Subscribers

People subscribed via source and target branches

to all changes: