Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: tests/utils.py

Issue 29912588: Issue 7019 - [CMS] Refactor `test_server.py` (Closed)
Left Patch Set: Addressed comments from Patch Set #3 Created Oct. 23, 2018, 4:42 p.m.
Right Patch Set: Addressed docstring nit Created Oct. 29, 2018, 11 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « tests/test_page_outputs.py ('k') | tox.ini » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH 2 # Copyright (C) 2006-present eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 import contextlib 16 import contextlib
17 import os 17 import os
18 import signal 18 import signal
19 import subprocess 19 import subprocess
20 import time 20 import time
21 import zipfile 21 import zipfile
22 from io import BytesIO 22 from io import BytesIO
23 import urllib2
23 24
24 import pytest 25 import pytest
25 26
26 27
27 def get_dir_contents(path): 28 def get_dir_contents(path):
28 # TODO: This function is duplicated in test_page_outputs.py. 29 # TODO: This function is duplicated in test_page_outputs.py.
29 dirdata = {} 30 dirdata = {}
30 for dirpath, dirnames, filenames in os.walk(path): 31 for dirpath, dirnames, filenames in os.walk(path):
31 for output_file in filenames: 32 for output_file in filenames:
32 filepath = os.path.join(dirpath, output_file) 33 filepath = os.path.join(dirpath, output_file)
(...skipping 23 matching lines...) Expand all
56 The url where the server runs. 57 The url where the server runs.
57 58
58 """ 59 """
59 args = ['python', 'runserver.py', site_path] 60 args = ['python', 'runserver.py', site_path]
60 # Werkzeug is a dependency of flask which we are using for the mock api 61 # Werkzeug is a dependency of flask which we are using for the mock api
61 # however there is an issue with Werkzeug that prevents it from properly 62 # however there is an issue with Werkzeug that prevents it from properly
62 # handling the SIGTERM sent by p.kill() or terminate() 63 # handling the SIGTERM sent by p.kill() or terminate()
63 # Issue: https://github.com/pallets/werkzeug/issues/58 64 # Issue: https://github.com/pallets/werkzeug/issues/58
64 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid, 65 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid,
65 env=new_env) 66 env=new_env)
66 time.sleep(0.5) 67
67 yield 'http://localhost:5000/' 68 try:
68 os.killpg(os.getpgid(p.pid), signal.SIGINT) 69 _wait_for_server()
70 yield 'http://localhost:5000/'
71 finally:
72 os.killpg(os.getpgid(p.pid), signal.SIGINT)
73
74
75 def _wait_for_server():
76 """Test the server started by `run_test_server` and fail after 2s.
77
78 Expects the server to be active at `http://localhost:5000/`.
79
80 Raises
81 ------
82 Exception
83 If the server is not running after retry-ing for 2 seconds.
84 This will be the exception raised by `urllib2.urlopen`.
85
86 """
87 start_time = time.time()
88
89 while True:
90 try:
91 urllib2.urlopen('http://localhost:5000/')
92 break
93 except Exception:
94 time.sleep(.1)
95 if time.time() - start_time > 2:
96 raise
69 97
70 98
71 def create_in_memory_zip(file_names, file_data): 99 def create_in_memory_zip(file_names, file_data):
72 """Create a BytesIO object with the contents of a zip file. 100 """Create a BytesIO object with the contents of a zip file.
73 101
74 Parameters 102 Parameters
75 ---------- 103 ----------
76 file_names: iterable 104 file_names: iterable
77 Of file names. Should be the full paths of the file inside the zip. 105 Of file names. Should be the full paths of the file inside the zip.
78 file_data: iterable 106 file_data: iterable
(...skipping 13 matching lines...) Expand all
92 120
93 memory_zip.seek(0) 121 memory_zip.seek(0)
94 return memory_zip 122 return memory_zip
95 123
96 124
97 def exception_test(func, exception, exp_msg, *args, **kw): 125 def exception_test(func, exception, exp_msg, *args, **kw):
98 with pytest.raises(exception) as err: 126 with pytest.raises(exception) as err:
99 func(*args, **kw) 127 func(*args, **kw)
100 128
101 assert exp_msg in str(err.value) 129 assert exp_msg in str(err.value)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld