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

Side by Side Diff: tests/utils.py

Issue 29912588: Issue 7019 - [CMS] Refactor `test_server.py` (Closed)
Patch Set: Simplified server waiting logic Created Oct. 29, 2018, 10:40 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/test_page_outputs.py ('k') | tox.ini » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
50 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid) 65 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid,
51 time.sleep(0.5) 66 env=new_env)
52 yield 'http://localhost:5000/' 67
53 os.killpg(os.getpgid(p.pid), signal.SIGTERM) 68 try:
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
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
54 97
55 98
56 def create_in_memory_zip(file_names, file_data): 99 def create_in_memory_zip(file_names, file_data):
57 """Create a BytesIO object with the contents of a zip file. 100 """Create a BytesIO object with the contents of a zip file.
58 101
59 Parameters 102 Parameters
60 ---------- 103 ----------
61 file_names: iterable 104 file_names: iterable
62 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.
63 file_data: iterable 106 file_data: iterable
(...skipping 13 matching lines...) Expand all
77 120
78 memory_zip.seek(0) 121 memory_zip.seek(0)
79 return memory_zip 122 return memory_zip
80 123
81 124
82 def exception_test(func, exception, exp_msg, *args, **kw): 125 def exception_test(func, exception, exp_msg, *args, **kw):
83 with pytest.raises(exception) as err: 126 with pytest.raises(exception) as err:
84 func(*args, **kw) 127 func(*args, **kw)
85 128
86 assert exp_msg in str(err.value) 129 assert exp_msg in str(err.value)
OLDNEW
« no previous file with comments | « tests/test_page_outputs.py ('k') | tox.ini » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld