| 1 |
#!/usr/bin/env python |
| 2 |
import json |
| 3 |
import os |
| 4 |
import re |
| 5 |
import signal |
| 6 |
import subprocess |
| 7 |
import sys |
| 8 |
import threading |
| 9 |
import time |
| 10 |
|
| 11 |
import mozunit |
| 12 |
from buildconfig import topobjdir, topsrcdir |
| 13 |
|
| 14 |
here = os.path.dirname(__file__) |
| 15 |
|
| 16 |
if os.name == "nt": |
| 17 |
PROCESS_CREATION_FLAGS = subprocess.CREATE_NEW_PROCESS_GROUP |
| 18 |
else: |
| 19 |
PROCESS_CREATION_FLAGS = 0 |
| 20 |
|
| 21 |
|
| 22 |
# This is copied from <python/mozperftest/mozperftest/utils.py>. It's copied |
| 23 |
# instead of imported since mozperfest is Python 3, and this file is |
| 24 |
# (currently) Python 2. |
| 25 |
def _install_package(virtualenv_manager, package): |
| 26 |
from pip._internal.req.constructors import install_req_from_line |
| 27 |
|
| 28 |
req = install_req_from_line(package) |
| 29 |
req.check_if_exists(use_user_site=False) |
| 30 |
# already installed, check if it's in our venv |
| 31 |
if req.satisfied_by is not None: |
| 32 |
venv_site_lib = os.path.abspath( |
| 33 |
os.path.join(virtualenv_manager.bin_path, "..", "lib") |
| 34 |
) |
| 35 |
site_packages = os.path.abspath(req.satisfied_by.location) |
| 36 |
if site_packages.startswith(venv_site_lib): |
| 37 |
# already installed in this venv, we can skip |
| 38 |
return |
| 39 |
|
| 40 |
subprocess.check_call( |
| 41 |
[ |
| 42 |
virtualenv_manager.python_path, |
| 43 |
"-m", |
| 44 |
"pip", |
| 45 |
"install", |
| 46 |
package, |
| 47 |
] |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
def _kill_mozproxy(pid): |
| 52 |
kill_signal = getattr(signal, "CTRL_BREAK_EVENT", signal.SIGINT) |
| 53 |
os.kill(pid, kill_signal) |
| 54 |
|
| 55 |
|
| 56 |
class OutputHandler: |
| 57 |
def __init__(self): |
| 58 |
self.port = None |
| 59 |
self.port_event = threading.Event() |
| 60 |
|
| 61 |
def __call__(self, line): |
| 62 |
line = line.rstrip(b"\r\n") |
| 63 |
if not line.strip(): |
| 64 |
return |
| 65 |
line = line.decode("utf-8", errors="replace") |
| 66 |
# Print the output we received so we have useful logs if a test fails. |
| 67 |
print(line) |
| 68 |
|
| 69 |
try: |
| 70 |
data = json.loads(line) |
| 71 |
except ValueError: |
| 72 |
return |
| 73 |
|
| 74 |
if isinstance(data, dict) and "action" in data: |
| 75 |
# Retrieve the port number for the proxy server from the logs of |
| 76 |
# our subprocess. |
| 77 |
m = re.match(r"Proxy running on port (\d+)", data.get("message", "")) |
| 78 |
if m: |
| 79 |
self.port = m.group(1) |
| 80 |
self.port_event.set() |
| 81 |
|
| 82 |
def finished(self): |
| 83 |
self.port_event.set() |
| 84 |
|
| 85 |
|
| 86 |
def test_help(): |
| 87 |
p = subprocess.run([sys.executable, "-m", "mozproxy", "--help"]) |
| 88 |
assert p.returncode == 0 |
| 89 |
|
| 90 |
|
| 91 |
def test_run_record_no_files(): |
| 92 |
output_handler = OutputHandler() |
| 93 |
p = subprocess.Popen( |
| 94 |
[ |
| 95 |
sys.executable, |
| 96 |
"-m", |
| 97 |
"mozproxy", |
| 98 |
"--local", |
| 99 |
"--mode=record", |
| 100 |
"--binary=firefox", |
| 101 |
"--topsrcdir=" + topsrcdir, |
| 102 |
"--objdir=" + topobjdir, |
| 103 |
], |
| 104 |
creationflags=PROCESS_CREATION_FLAGS, |
| 105 |
stdout=subprocess.PIPE, |
| 106 |
stderr=subprocess.STDOUT, |
| 107 |
text=False, |
| 108 |
) |
| 109 |
|
| 110 |
for line in p.stdout: |
| 111 |
output_handler(line) |
| 112 |
if output_handler.port_event.is_set(): |
| 113 |
break |
| 114 |
output_handler.finished() |
| 115 |
# The first time we run mozproxy, we need to fetch mitmproxy, which can |
| 116 |
# take a while... |
| 117 |
assert output_handler.port_event.wait(120) is True |
| 118 |
# Give mitmproxy a bit of time to start up so we can verify that it's |
| 119 |
# actually running before we kill mozproxy. |
| 120 |
time.sleep(5) |
| 121 |
_kill_mozproxy(p.pid) |
| 122 |
|
| 123 |
# Assert process raises error |
| 124 |
assert p.wait(10) == 2 |
| 125 |
assert output_handler.port is None |
| 126 |
|
| 127 |
|
| 128 |
def test_run_record_multiple_files(): |
| 129 |
output_handler = OutputHandler() |
| 130 |
p = subprocess.Popen( |
| 131 |
[ |
| 132 |
sys.executable, |
| 133 |
"-m", |
| 134 |
"mozproxy", |
| 135 |
"--local", |
| 136 |
"--mode=record", |
| 137 |
"--binary=firefox", |
| 138 |
"--topsrcdir=" + topsrcdir, |
| 139 |
"--objdir=" + topobjdir, |
| 140 |
os.path.join(here, "files", "new_record.zip"), |
| 141 |
os.path.join(here, "files", "new_record2.zip"), |
| 142 |
], |
| 143 |
creationflags=PROCESS_CREATION_FLAGS, |
| 144 |
stdout=subprocess.PIPE, |
| 145 |
stderr=subprocess.STDOUT, |
| 146 |
text=False, |
| 147 |
) |
| 148 |
|
| 149 |
for line in p.stdout: |
| 150 |
output_handler(line) |
| 151 |
if output_handler.port_event.is_set(): |
| 152 |
break |
| 153 |
output_handler.finished() |
| 154 |
# The first time we run mozproxy, we need to fetch mitmproxy, which can |
| 155 |
# take a while... |
| 156 |
assert output_handler.port_event.wait(120) is True |
| 157 |
# Give mitmproxy a bit of time to start up so we can verify that it's |
| 158 |
# actually running before we kill mozproxy. |
| 159 |
time.sleep(5) |
| 160 |
_kill_mozproxy(p.pid) |
| 161 |
|
| 162 |
assert p.wait(10) == 4 |
| 163 |
assert output_handler.port is None |
| 164 |
|
| 165 |
|
| 166 |
def test_run_record(): |
| 167 |
output_handler = OutputHandler() |
| 168 |
p = subprocess.Popen( |
| 169 |
[ |
| 170 |
sys.executable, |
| 171 |
"-m", |
| 172 |
"mozproxy", |
| 173 |
"--local", |
| 174 |
"--mode=record", |
| 175 |
"--binary=firefox", |
| 176 |
"--topsrcdir=" + topsrcdir, |
| 177 |
"--objdir=" + topobjdir, |
| 178 |
os.path.join(here, "files", "record.zip"), |
| 179 |
], |
| 180 |
creationflags=PROCESS_CREATION_FLAGS, |
| 181 |
stdout=subprocess.PIPE, |
| 182 |
stderr=subprocess.STDOUT, |
| 183 |
text=False, |
| 184 |
) |
| 185 |
|
| 186 |
for line in p.stdout: |
| 187 |
output_handler(line) |
| 188 |
if output_handler.port_event.is_set(): |
| 189 |
break |
| 190 |
output_handler.finished() |
| 191 |
try: |
| 192 |
# The first time we run mozproxy, we need to fetch mitmproxy, which can |
| 193 |
# take a while... |
| 194 |
assert output_handler.port_event.wait(120) is True |
| 195 |
# Give mitmproxy a bit of time to start up so we can verify that it's |
| 196 |
# actually running before we kill mozproxy. |
| 197 |
time.sleep(5) |
| 198 |
_kill_mozproxy(p.pid) |
| 199 |
|
| 200 |
assert p.wait(10) == 0 |
| 201 |
assert output_handler.port is not None |
| 202 |
finally: |
| 203 |
os.remove(os.path.join(here, "files", "record.zip")) |
| 204 |
|
| 205 |
|
| 206 |
def test_run_playback(): |
| 207 |
output_handler = OutputHandler() |
| 208 |
p = subprocess.Popen( |
| 209 |
[ |
| 210 |
sys.executable, |
| 211 |
"-m", |
| 212 |
"mozproxy", |
| 213 |
"--local", |
| 214 |
"--binary=firefox", |
| 215 |
"--topsrcdir=" + topsrcdir, |
| 216 |
"--objdir=" + topobjdir, |
| 217 |
os.path.join(here, "files", "mitm5-linux-firefox-amazon.zip"), |
| 218 |
], |
| 219 |
creationflags=PROCESS_CREATION_FLAGS, |
| 220 |
stdout=subprocess.PIPE, |
| 221 |
stderr=subprocess.STDOUT, |
| 222 |
text=False, |
| 223 |
) |
| 224 |
|
| 225 |
for line in p.stdout: |
| 226 |
output_handler(line) |
| 227 |
if output_handler.port_event.is_set(): |
| 228 |
break |
| 229 |
output_handler.finished() |
| 230 |
# The first time we run mozproxy, we need to fetch mitmproxy, which can |
| 231 |
# take a while... |
| 232 |
assert output_handler.port_event.wait(120) is True |
| 233 |
# Give mitmproxy a bit of time to start up so we can verify that it's |
| 234 |
# actually running before we kill mozproxy. |
| 235 |
time.sleep(5) |
| 236 |
_kill_mozproxy(p.pid) |
| 237 |
|
| 238 |
assert p.wait(10) == 0 |
| 239 |
assert output_handler.port is not None |
| 240 |
|
| 241 |
|
| 242 |
def test_failure(): |
| 243 |
output_handler = OutputHandler() |
| 244 |
p = subprocess.Popen( |
| 245 |
[ |
| 246 |
sys.executable, |
| 247 |
"-m", |
| 248 |
"mozproxy", |
| 249 |
"--local", |
| 250 |
# Exclude some options here to trigger a command-line error. |
| 251 |
os.path.join(here, "files", "mitm5-linux-firefox-amazon.zip"), |
| 252 |
], |
| 253 |
creationflags=PROCESS_CREATION_FLAGS, |
| 254 |
stdout=subprocess.PIPE, |
| 255 |
stderr=subprocess.STDOUT, |
| 256 |
text=False, |
| 257 |
) |
| 258 |
for line in p.stdout: |
| 259 |
output_handler(line) |
| 260 |
if output_handler.port_event.is_set(): |
| 261 |
break |
| 262 |
output_handler.finished() |
| 263 |
assert output_handler.port_event.wait(10) is True |
| 264 |
assert p.wait(10) == 2 |
| 265 |
assert output_handler.port is None |
| 266 |
|
| 267 |
|
| 268 |
if __name__ == "__main__": |
| 269 |
mozunit.main(runwith="pytest") |