Skip to content

Commit b2e114b

Browse files
author
Thomas Preud'homme
committed
[LNT] Python 3 support: Parse HTML as text
Summary: Several tests and the testsuite run import code parse text read from the data attribute of a Flask response object (werkzeug.wrappers.Response). However, in Python3 reading from that attribute yields binary data. The attribute is in fact a property whose getter get_data() as a as_text parameter that allows to request the result to be a string instead. This commit adapts all reads from that property to request strings instead of binary data (the default), except for a few cases in tests/server/ui/V4Pages.py where the data is fed to ElementTree's fromstring method in get_xml_tree() which tries to reencode it in ascii in Python 2 mode if it gets a unicode string. Reviewers: cmatthews, hubert.reinterpretcast, kristof.beyls, leandron, PrzemekWirkus Reviewed By: PrzemekWirkus Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D68829
1 parent 8a8832c commit b2e114b

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

lnt/server/ui/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def post():
306306
"""Add a new run into the lnt database"""
307307
session = request.session
308308
db = request.get_db()
309-
data = request.data
309+
data = request.get_data(as_text=True)
310310
select_machine = request.values.get('select_machine', 'match')
311311
merge = request.values.get('merge', None)
312312
result = lnt.util.ImportData.import_from_string(

tests/server/db/Migrations.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ def sanity_check_instance(instance_path):
2828

2929
# Visit all the test suites.
3030
test_suite_link_rex = re.compile(""" <a href="(.*)">(.*)</a><br>""")
31-
test_suite_list_start = index.data.index("<h3>Test Suites</h3>")
32-
test_suite_list_end = index.data.index("</div>", test_suite_list_start)
33-
for ln in index.data[test_suite_list_start:test_suite_list_end].split("\n"):
31+
data = index.get_data(as_text=True)
32+
test_suite_list_start = data.index("<h3>Test Suites</h3>")
33+
test_suite_list_end = data.index("</div>", test_suite_list_start)
34+
for ln in data[test_suite_list_start:test_suite_list_end].split("\n"):
3435
# Ignore non-matching lines.
3536
print(ln, file=sys.stderr)
3637
m = test_suite_link_rex.match(ln)

tests/server/ui/V4Pages.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def check_code(client, url, expected_code=HTTP_OK, data_to_send=None):
7878

7979
def check_html(client, url, expected_code=HTTP_OK, data_to_send=None):
8080
resp = check_code(client, url, expected_code, data_to_send)
81-
validate_html(resp.data)
81+
validate_html(resp.get_data(as_text=True))
8282
return resp
8383

8484

@@ -145,7 +145,7 @@ def find_table_with_heading(tree, table_heading):
145145

146146
def check_nr_machines_reported(client, url, expected_nr_machines):
147147
resp = check_code(client, url)
148-
html = resp.data
148+
html = resp.get_data(as_text=sys.version_info[0] >= 3)
149149
tree = get_xml_tree(html)
150150
# look for the table containing the machines on the page.
151151
# do this by looking for the title containing "Reported Machine Order"
@@ -166,7 +166,7 @@ def convert_html_to_text(element):
166166

167167
def get_table_by_header(client, url, table_header):
168168
resp = check_code(client, url)
169-
html = resp.data
169+
html = resp.get_data(as_text=sys.version_info[0] >= 3)
170170
tree = get_xml_tree(html)
171171
table = find_table_with_heading(tree, table_header)
172172
assert table is not None, \
@@ -227,7 +227,7 @@ def check_body_nr_tests_table(client, url, expected_content):
227227
def check_producer_label(client, url, label):
228228
table_header = "Produced by"
229229
resp = check_code(client, url)
230-
tree = get_xml_tree(resp.data)
230+
tree = get_xml_tree(resp.get_data(as_text=sys.version_info[0] >= 3))
231231
table = find_table_by_thead_content(tree, table_header)
232232
check_row_is_in_table(table, label)
233233

@@ -333,7 +333,7 @@ def main():
333333
follow_redirects=True)
334334
assert r.status_code == HTTP_OK
335335
# Should see baseline displayed in page body.
336-
assert "Baseline - foo_baseline" in r.data
336+
assert "Baseline - foo_baseline" in r.get_data(as_text=True)
337337

338338
# Now demote it.
339339
data2 = dict(name="foo_baseline",
@@ -344,7 +344,7 @@ def main():
344344
r = client.post('/v4/nts/order/3', data=data2, follow_redirects=True)
345345
assert r.status_code == HTTP_OK
346346
# Baseline should no longer be shown in page baseline.
347-
assert "Baseline - foo_baseline" not in r.data
347+
assert "Baseline - foo_baseline" not in r.get_data(as_text=True)
348348

349349
# Leave a baseline in place for the rest of the tests.
350350
client.post('/v4/nts/order/3', data=form_data)
@@ -638,10 +638,11 @@ def main():
638638
check_json(client, '/db_default/v4/nts/graph?switch_min_mean=yes&plot.0=1.3.2&json=true')
639639
app.testing = False
640640
error_page = check_html(client, '/explode', expected_code=500)
641-
assert re.search("division (or modulo )?by zero", error_page.data)
641+
assert re.search("division (or modulo )?by zero",
642+
error_page.get_data(as_text=True))
642643

643644
error_page = check_html(client, '/gone', expected_code=404)
644-
assert "test" in error_page.data
645+
assert "test" in error_page.get_data(as_text=True)
645646

646647
check_html(client, '/sleep?timeout=0', expected_code=200)
647648

@@ -650,9 +651,9 @@ def main():
650651
check_html(client, '/rules')
651652
check_html(client, '/log')
652653
resp = check_code(client, '/__health')
653-
assert resp.data == "Ok"
654+
assert resp.get_data(as_text=True) == "Ok"
654655
resp = check_code(client, '/ping')
655-
assert resp.data == "pong"
656+
assert resp.get_data(as_text=True) == "pong"
656657

657658
# Check we can convert a sample into a graph page.
658659
graph_to_sample = check_code(client, '/db_default/v4/nts/graph_for_sample/10/compile_time?foo=bar',

tests/server/ui/test_api_modify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def test_02_delete_machine(self):
148148
resp = client.delete('api/db_default/v4/nts/machines/2',
149149
headers={'AuthToken': 'test_token'})
150150
self.assertEqual(resp.status_code, 200)
151-
self.assertEqual(resp.get_data(),
151+
self.assertEqual(resp.get_data(as_text=True),
152152
'''Deleting runs 3 5 6 7 8 9 (6/6)
153153
Deleted machine machine2:2
154154
''')

tests/server/ui/test_matrix_page.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,26 @@ def test_config_errors(self):
3636
client = self.client
3737
reply = check_code(client, '/v4/nts/matrix',
3838
expected_code=HTTP_NOT_FOUND)
39-
self.assertIn("Request requires some data arguments.", reply.data)
39+
self.assertIn("Request requires some data arguments.",
40+
reply.get_data(as_text=True))
4041

4142
reply = check_code(client, '/v4/nts/matrix?plot.0=1.1.1',
4243
expected_code=HTTP_NOT_FOUND)
43-
self.assertIn("No data found.", reply.data)
44+
self.assertIn("No data found.", reply.get_data(as_text=True))
4445

4546
reply = check_code(client, '/v4/nts/matrix?plot.0=a.2.0',
4647
expected_code=HTTP_BAD_REQUEST)
47-
self.assertIn("malformed", reply.data)
48+
self.assertIn("malformed", reply.get_data(as_text=True))
4849

4950
reply = check_code(client, '/v4/nts/matrix?plot.0=999.0.0',
5051
expected_code=HTTP_NOT_FOUND)
51-
self.assertIn("Invalid machine", reply.data)
52+
self.assertIn("Invalid machine", reply.get_data(as_text=True))
5253
reply = check_code(client, '/v4/nts/matrix?plot.0=1.999.0',
5354
expected_code=HTTP_NOT_FOUND)
54-
self.assertIn("Invalid test", reply.data)
55+
self.assertIn("Invalid test", reply.get_data(as_text=True))
5556
reply = check_code(client, '/v4/nts/matrix?plot.0=1.1.999',
5657
expected_code=HTTP_NOT_FOUND)
57-
self.assertIn("Invalid field", reply.data)
58+
self.assertIn("Invalid field", reply.get_data(as_text=True))
5859

5960
def test_matrix_view(self):
6061
"""Does the page load with the data as expected.
@@ -66,15 +67,16 @@ def test_matrix_view(self):
6667
description="foo_description",
6768
prmote=True)
6869
rc = client.post('/v4/nts/order/6', data=form_data)
69-
self.assertEquals(rc.status_code, HTTP_REDIRECT)
70+
self.assertEqual(rc.status_code, HTTP_REDIRECT)
7071
check_code(client, '/v4/nts/set_baseline/1',
7172
expected_code=HTTP_REDIRECT)
7273

7374
reply = check_html(client, '/v4/nts/matrix?plot.0=2.6.2')
7475
# Make sure the data is in the page.
75-
self.assertIn("test6", reply.data)
76-
self.assertIn("1.0000", reply.data)
77-
self.assertIn("1.2000", reply.data)
76+
data = reply.get_data(as_text=True)
77+
self.assertIn("test6", data)
78+
self.assertIn("1.0000", data)
79+
self.assertIn("1.2000", data)
7880

7981
reply = check_html(client, '/v4/nts/matrix?plot.0=2.6.2&limit=1')
8082

0 commit comments

Comments
 (0)