|
| 1 | +# Copyright 2023 Ericsson AB |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Codechecker server functionality, and related functions |
| 17 | +""" |
| 18 | + |
| 19 | +import os |
| 20 | +import shutil |
| 21 | +import signal |
| 22 | +import socket |
| 23 | +import subprocess |
| 24 | +import tempfile |
| 25 | +import time |
| 26 | + |
| 27 | + |
| 28 | +# Based on: |
| 29 | +# https://dev.to/farcellier/wait-for-a-server-to-respond-in-python-488e |
| 30 | +def wait_port( |
| 31 | + port: int, |
| 32 | + host: str = "localhost", |
| 33 | + timeout: int = 3000, |
| 34 | + attempt_every: int = 100, |
| 35 | +) -> bool: |
| 36 | + """ |
| 37 | + Wait until a port would be open, |
| 38 | + for example the port 8001 for CodeChecker server |
| 39 | + """ |
| 40 | + start = time.monotonic() |
| 41 | + while True: |
| 42 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 43 | + try: |
| 44 | + s.connect((host, port)) |
| 45 | + s.close() |
| 46 | + return True |
| 47 | + except ConnectionRefusedError: |
| 48 | + if timeout is not None and time.monotonic() - start > ( |
| 49 | + timeout / 1000 |
| 50 | + ): |
| 51 | + return False |
| 52 | + |
| 53 | + time.sleep(attempt_every / 1000) |
| 54 | + |
| 55 | + |
| 56 | +class CodeCheckerServer: |
| 57 | + """ |
| 58 | + CodeCheckerServer object for testing. |
| 59 | + Cleans up after itself. |
| 60 | + """ |
| 61 | + def __init__(self, port="8001"): |
| 62 | + self.running = False |
| 63 | + self.port = port |
| 64 | + self.temp_workspace = tempfile.mkdtemp() |
| 65 | + self.start_codechecker_server() |
| 66 | + |
| 67 | + def __del__(self): |
| 68 | + self.stop_codechecker_server() |
| 69 | + |
| 70 | + def start_codechecker_server(self): |
| 71 | + """ |
| 72 | + Starts a CodeChecker server instance on port 8001 |
| 73 | + This server must be shutdown with stop_codechecker_sever |
| 74 | + """ |
| 75 | + if self.running: |
| 76 | + return |
| 77 | + server_command = [ |
| 78 | + "CodeChecker", |
| 79 | + "server", |
| 80 | + "--workspace", |
| 81 | + self.temp_workspace, |
| 82 | + "--port", |
| 83 | + self.port, |
| 84 | + ] |
| 85 | + # These file/popen processes are closed when the object dies |
| 86 | + # pylint: disable=consider-using-with |
| 87 | + self.devnull = open(os.devnull, "w", encoding="utf-8") |
| 88 | + # pylint: disable=consider-using-with |
| 89 | + self.server_process: subprocess.Popen = subprocess.Popen( |
| 90 | + server_command, stdout=self.devnull |
| 91 | + ) |
| 92 | + assert wait_port( |
| 93 | + port=8001, timeout=10000 |
| 94 | + ), "Failed to start CodeChecker server" |
| 95 | + self.running = True |
| 96 | + |
| 97 | + def stop_codechecker_server(self): |
| 98 | + """ |
| 99 | + Stops the CodeChecker server started by start_codechecker_server |
| 100 | + """ |
| 101 | + os.kill(self.server_process.pid, signal.SIGTERM) |
| 102 | + self.server_process.wait() |
| 103 | + self.running = False |
| 104 | + self.devnull.close() |
| 105 | + shutil.rmtree(self.temp_workspace) |
0 commit comments