|
12 | 12 | # |
13 | 13 | # Standard library |
14 | 14 | # ---------------- |
| 15 | +import logging |
| 16 | +import os |
| 17 | +import platform |
| 18 | +import signal |
| 19 | +import time |
15 | 20 | import subprocess |
16 | | - |
| 21 | +import sys |
| 22 | +import unittest |
| 23 | +from urllib.request import urlopen |
| 24 | +from urllib.error import URLError |
17 | 25 |
|
18 | 26 | # Third-party imports |
19 | 27 | # ------------------- |
20 | 28 | import pytest |
21 | | - |
| 29 | +from pyvirtualdisplay import Display |
| 30 | +from selenium import webdriver |
| 31 | +from selenium.webdriver.chrome.options import Options |
22 | 32 |
|
23 | 33 | # Local imports |
24 | 34 | # ------------- |
25 | 35 | # This is necessary to bring in the shared pytest fixture. |
26 | 36 | from runestone.shared_conftest import _SeleniumUtils, selenium_driver # noqa: F401 |
27 | | -from runestone.unittest_base import ModuleFixture, HOST_URL, IS_WINDOWS |
| 37 | +logging.basicConfig(level=logging.WARN) |
| 38 | + |
| 39 | + |
| 40 | +# Globals |
| 41 | +# ======= |
| 42 | +# Select an unused port for serving web pages to the test suite. |
| 43 | +PORT = "8081" |
| 44 | +# Use the localhost for testing. |
| 45 | +HOST_ADDRESS = "127.0.0.1:" + PORT |
| 46 | +HOST_URL = "http://" + HOST_ADDRESS |
| 47 | + |
| 48 | +# Define the platform. |
| 49 | +IS_WINDOWS = platform.system() == "Windows" |
| 50 | +IS_LINUX = sys.platform.startswith("linux") |
| 51 | + |
| 52 | +mylogger = logging.getLogger() |
28 | 53 |
|
29 | 54 |
|
30 | 55 | # Fixtures |
@@ -68,3 +93,132 @@ def selenium_utils(selenium_driver): # noqa: F811 |
68 | 93 | def selenium_utils_get(selenium_utils): |
69 | 94 | selenium_utils.get("index.html") |
70 | 95 | return selenium_utils |
| 96 | + |
| 97 | + |
| 98 | +# Utility class |
| 99 | +# ============= |
| 100 | +# Define a class to build the test Runestone project, run the server, then shut it down when the tests complete. |
| 101 | +class ModuleFixture(unittest.TestCase): |
| 102 | + def __init__( |
| 103 | + self, |
| 104 | + # The path to the Python module in which the test resides. This provides a simple way to determine the path in which to run runestone build/serve. |
| 105 | + module_path, |
| 106 | + # True if the sphinx-build process must exit with status of 0 (success) |
| 107 | + exit_status_success=True, |
| 108 | + ): |
| 109 | + |
| 110 | + super(ModuleFixture, self).__init__() |
| 111 | + self.base_path = os.path.dirname(module_path) |
| 112 | + self.exit_status_success = exit_status_success |
| 113 | + # Windows Compatability |
| 114 | + if IS_WINDOWS and self.base_path == "": |
| 115 | + self.base_path = "." |
| 116 | + |
| 117 | + def setUpModule(self): |
| 118 | + # Change to this directory for running Runestone. |
| 119 | + self.old_cwd = os.getcwd() |
| 120 | + os.chdir(self.base_path) |
| 121 | + # Compile the docs. Save the stdout and stderr for examination. |
| 122 | + p = subprocess.run( |
| 123 | + ["runestone", "build", "--all"], capture_output=True, text=True, |
| 124 | + ) |
| 125 | + self.build_stdout_data = p.stdout |
| 126 | + self.build_stderr_data = p.stderr |
| 127 | + print(self.build_stdout_data + self.build_stderr_data) |
| 128 | + if self.exit_status_success: |
| 129 | + self.assertFalse(p.returncode) |
| 130 | + # Make sure any older servers on port 8081 are killed. |
| 131 | + if IS_WINDOWS: |
| 132 | + netstat_output = subprocess.run( |
| 133 | + # Flags are: |
| 134 | + # |
| 135 | + # -n: Display addresses numerically. Looking up names is slow. |
| 136 | + # -o: Include the PID for each connection. |
| 137 | + ["netstat", "-no"], |
| 138 | + capture_output=True, |
| 139 | + text=True, |
| 140 | + ).stdout |
| 141 | + # Skip the first four lines, which are headings. |
| 142 | + for connection in netstat_output.splitlines()[4:]: |
| 143 | + # Typical output is: |
| 144 | + ## Proto Local Address Foreign Address State PID |
| 145 | + ## TCP 127.0.0.1:1277 127.0.0.1:49971 ESTABLISHED 4624 |
| 146 | + proto, local_address, foreign_address, state, pid = connection.split() |
| 147 | + pid = int(pid) |
| 148 | + if local_address == HOST_ADDRESS and pid != 0: |
| 149 | + os.kill(pid, 0) |
| 150 | + else: |
| 151 | + lsof_output = subprocess.run( |
| 152 | + ["lsof", "-i", ":{0}".format(PORT)], capture_output=True, text=True, |
| 153 | + ).stdout |
| 154 | + for process in lsof_output.split("\n")[1:]: |
| 155 | + data = [x for x in process.split(" ") if x != ""] |
| 156 | + if len(data) <= 1: |
| 157 | + continue |
| 158 | + ptokill = int(data[1]) |
| 159 | + mylogger.warn( |
| 160 | + "Attempting to kill a stale runestone serve process: {}".format( |
| 161 | + ptokill |
| 162 | + ) |
| 163 | + ) |
| 164 | + os.kill(ptokill, signal.SIGKILL) |
| 165 | + time.sleep(2) # give the old process a couple seconds to clear out |
| 166 | + try: |
| 167 | + os.kill(ptokill, 0) # will throw an Error if process gone |
| 168 | + pytest.exit( |
| 169 | + "Stale runestone server can't kill process: {}".format(ptokill) |
| 170 | + ) |
| 171 | + except ProcessLookupError: |
| 172 | + # The process was killed |
| 173 | + pass |
| 174 | + except PermissionError: |
| 175 | + pytest.exit( |
| 176 | + "Another server is using port {} process: {}".format( |
| 177 | + PORT, ptokill |
| 178 | + ) |
| 179 | + ) |
| 180 | + except Exception: |
| 181 | + pytest.exit( |
| 182 | + "Unknown error while trying to kill stale runestone server" |
| 183 | + ) |
| 184 | + |
| 185 | + # Run the server. Simply calling ``runestone serve`` fails, since the process killed isn't the actual server, but probably a setuptools-created launcher. |
| 186 | + self.runestone_server = subprocess.Popen( |
| 187 | + [sys.executable, "-m", "runestone", "serve", "--port", PORT] |
| 188 | + ) |
| 189 | + |
| 190 | + # Testing time in dominated by browser startup/shutdown. So, simply run all tests in a module in a single browser instance to speed things up. See ``RunestoneTestCase.setUp`` for additional code to (mostly) clear the browser between tests. |
| 191 | + # |
| 192 | + # `PyVirtualDisplay <http://pyvirtualdisplay.readthedocs.io/en/latest/>`_ only runs on X-windows, meaning Linux. Mac seems to have `some support <https://support.apple.com/en-us/HT201341>`_. Windows is out of the question. |
| 193 | + if IS_LINUX and not os.environ.get("DISPLAY"): |
| 194 | + self.display = Display(visible=0, size=(1280, 1024)) |
| 195 | + self.display.start() |
| 196 | + else: |
| 197 | + self.display = None |
| 198 | + # self.driver = webdriver.PhantomJS() # use this for Jenkins auto testing |
| 199 | + options = Options() |
| 200 | + options.add_argument("--window-size=1200,800") |
| 201 | + options.add_argument("--no-sandbox") |
| 202 | + self.driver = webdriver.Chrome(options=options) # good for development. |
| 203 | + |
| 204 | + # Wait for the webserver to come up. |
| 205 | + for tries in range(50): |
| 206 | + try: |
| 207 | + urlopen(HOST_URL, timeout=5) |
| 208 | + except URLError: |
| 209 | + # Wait for the server to come up. |
| 210 | + time.sleep(0.1) |
| 211 | + else: |
| 212 | + # The server is up. We're done. |
| 213 | + break |
| 214 | + |
| 215 | + def tearDownModule(self): |
| 216 | + # Shut down Selenium. |
| 217 | + self.driver.close() |
| 218 | + self.driver.quit() |
| 219 | + if self.display: |
| 220 | + self.display.stop() |
| 221 | + # Shut down the server. |
| 222 | + self.runestone_server.kill() |
| 223 | + # Restore the directory. |
| 224 | + os.chdir(self.old_cwd) |
0 commit comments