|
2 | 2 | #
|
3 | 3 | # Copyright 2023 Endless OS Foundation LLC
|
4 | 4 | # SPDX-License-Identifier: GPL-2.0-or-later
|
| 5 | +import functools |
5 | 6 | import json
|
6 | 7 | import logging
|
| 8 | +import multiprocessing |
7 | 9 | import os
|
| 10 | +import queue |
| 11 | +import threading |
8 | 12 | from base64 import b64decode
|
9 | 13 | from glob import iglob
|
10 | 14 | from hashlib import md5
|
| 15 | +from http.server import SimpleHTTPRequestHandler |
| 16 | +from http.server import ThreadingHTTPServer |
11 | 17 | from pathlib import Path
|
12 | 18 |
|
13 | 19 | logger = logging.getLogger(__name__)
|
|
16 | 22 | CHANNELSDIR = TESTDIR / "channels"
|
17 | 23 |
|
18 | 24 |
|
| 25 | +class ExploreTestError(Exception): |
| 26 | + """Exceptions from kolibri-explore-plugin tests""" |
| 27 | + |
| 28 | + pass |
| 29 | + |
| 30 | + |
19 | 31 | def create_contentdir(content_path, channels_path=CHANNELSDIR):
|
20 | 32 | """Create content directory from channel JSON files"""
|
21 | 33 | from kolibri.core.content.constants.schema_versions import (
|
@@ -101,3 +113,77 @@ def create_contentdir(content_path, channels_path=CHANNELSDIR):
|
101 | 113 | bridge.connection.execute(table.insert(), data[table.name])
|
102 | 114 |
|
103 | 115 | bridge.end()
|
| 116 | + |
| 117 | + |
| 118 | +class LoggingHTTPRequestHandler(SimpleHTTPRequestHandler): |
| 119 | + """SimpleHTTPRequestHandler with logging""" |
| 120 | + |
| 121 | + def log_message(self, format, *args): |
| 122 | + logger.debug( |
| 123 | + "%s: %s - - [%s] %s", |
| 124 | + threading.current_thread().name, |
| 125 | + self.address_string(), |
| 126 | + self.log_date_time_string(), |
| 127 | + format % args, |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +class ContentServer: |
| 132 | + """Content HTTP server""" |
| 133 | + |
| 134 | + def __init__(self, path): |
| 135 | + self.path = Path(path) |
| 136 | + self.proc = None |
| 137 | + self.address = None |
| 138 | + self.url = None |
| 139 | + |
| 140 | + if not self.path.is_dir(): |
| 141 | + raise ValueError(f"{path} is not a directory") |
| 142 | + |
| 143 | + def __enter__(self): |
| 144 | + self.start() |
| 145 | + return self |
| 146 | + |
| 147 | + def __exit__(self, exc_type, exc_value, traceback): |
| 148 | + self.stop() |
| 149 | + |
| 150 | + def _run_server(self, path, queue): |
| 151 | + handler_class = functools.partial( |
| 152 | + LoggingHTTPRequestHandler, |
| 153 | + directory=path, |
| 154 | + ) |
| 155 | + server = ThreadingHTTPServer(("127.0.0.1", 0), handler_class) |
| 156 | + queue.put(server.server_address) |
| 157 | + server.serve_forever() |
| 158 | + |
| 159 | + def start(self): |
| 160 | + """Start the HTTP server |
| 161 | +
|
| 162 | + A separate process is used so that the HTTP server can block. |
| 163 | + """ |
| 164 | + addr_queue = multiprocessing.Queue() |
| 165 | + self.proc = multiprocessing.Process( |
| 166 | + target=self._run_server, args=(self.path, addr_queue) |
| 167 | + ) |
| 168 | + self.proc.start() |
| 169 | + if not self.proc.is_alive(): |
| 170 | + raise ExploreTestError(f"HTTP process {self.proc.pid} exited") |
| 171 | + try: |
| 172 | + self.address = addr_queue.get(True, 5) |
| 173 | + except queue.Empty: |
| 174 | + raise ExploreTestError( |
| 175 | + "HTTP process did not write address to queue" |
| 176 | + ) from None |
| 177 | + |
| 178 | + self.url = f"http://{self.address[0]}:{self.address[1]}" |
| 179 | + logger.debug( |
| 180 | + f"Serving {self.path} on {self.url} from process {self.proc.pid}" |
| 181 | + ) |
| 182 | + |
| 183 | + def stop(self): |
| 184 | + """Stop the HTTP server""" |
| 185 | + if self.proc is not None: |
| 186 | + if self.proc.is_alive(): |
| 187 | + logger.debug(f"Stopping HTTP server process {self.proc.pid}") |
| 188 | + self.proc.terminate() |
| 189 | + self.proc = None |
0 commit comments