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: Created Oct. 16, 2018, 11:42 a.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)
33 with open(filepath) as f: 34 with open(filepath) as f:
34 locale = os.path.split(os.path.split(filepath)[0])[1] 35 locale = os.path.split(os.path.split(filepath)[0])[1]
35 dirdata[os.path.join(locale, output_file)] = f.read().strip() 36 dirdata[os.path.join(locale, output_file)] = f.read().strip()
36 return dirdata 37 return dirdata
37 38
38 39
39 @contextlib.contextmanager 40 @contextlib.contextmanager
40 def run_test_server(site_path): 41 def run_test_server(site_path, new_env=None):
41 """Run test server, yield its URL. Terminate server on next iteration. 42 """Run test server, yield its URL. Terminate server on next iteration.
42 43
43 This function is intended be used in a pytest fixture. 44 This function is intended be used in a pytest fixture.
45
46 Parameters
47 ----------
48 site_path: str
49 The path to the website's source code.
50 new_env: dict
51 The environment under which the server will be run. If `None`, this
52 will be inherited from the main process.
53
54 Returns
55 -------
56 str
57 The url where the server runs.
58
44 """ 59 """
45 args = ['python', 'runserver.py', site_path] 60 args = ['python', 'runserver.py', site_path]
46 # 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
47 # 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
48 # handling the SIGTERM sent by p.kill() or terminate() 63 # handling the SIGTERM sent by p.kill() or terminate()
49 # Issue: https://github.com/pallets/werkzeug/issues/58 64 # Issue: https://github.com/pallets/werkzeug/issues/58
65 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid,
66 env=new_env)
50 67
Vasily Kuznetsov 2018/10/16 13:18:23 This looks kind of unrelated. Probably better remo
Tudor Avram 2018/10/18 13:44:05 Done.
51 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid) 68 try:
52 time.sleep(0.5) 69 _wait_for_server()
53 yield 'http://localhost:5000/' 70 yield 'http://localhost:5000/'
54 os.killpg(os.getpgid(p.pid), signal.SIGTERM) 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
55 97
56 98
57 def create_in_memory_zip(file_names, file_data): 99 def create_in_memory_zip(file_names, file_data):
58 """Create a BytesIO object with the contents of a zip file. 100 """Create a BytesIO object with the contents of a zip file.
59 101
60 Parameters 102 Parameters
61 ---------- 103 ----------
62 file_names: iterable 104 file_names: iterable
63 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.
64 file_data: iterable 106 file_data: iterable
(...skipping 13 matching lines...) Expand all
78 120
79 memory_zip.seek(0) 121 memory_zip.seek(0)
80 return memory_zip 122 return memory_zip
81 123
82 124
83 def exception_test(func, exception, exp_msg, *args, **kw): 125 def exception_test(func, exception, exp_msg, *args, **kw):
84 with pytest.raises(exception) as err: 126 with pytest.raises(exception) as err:
85 func(*args, **kw) 127 func(*args, **kw)
86 128
87 assert exp_msg in str(err.value) 129 assert exp_msg in str(err.value)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld