|
| 1 | +# -------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the ""Software""), to |
| 9 | +# deal in the Software without restriction, including without limitation the |
| 10 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | +# sell copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in |
| 15 | +# all copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 | +# IN THE SOFTWARE. |
| 24 | +# |
| 25 | +# -------------------------------------------------------------------------- |
| 26 | +import time |
| 27 | +import pytest |
| 28 | +import signal |
| 29 | +import os |
| 30 | +import subprocess |
| 31 | +import sys |
| 32 | +import random |
| 33 | +from six.moves import urllib |
| 34 | + |
| 35 | +def is_port_available(port_num): |
| 36 | + req = urllib.request.Request("http://localhost:{}/health".format(port_num)) |
| 37 | + try: |
| 38 | + return urllib.request.urlopen(req).code != 200 |
| 39 | + except Exception as e: |
| 40 | + return True |
| 41 | + |
| 42 | +def get_port(): |
| 43 | + count = 3 |
| 44 | + for _ in range(count): |
| 45 | + port_num = random.randrange(3000, 5000) |
| 46 | + if is_port_available(port_num): |
| 47 | + return port_num |
| 48 | + raise TypeError("Tried {} times, can't find an open port".format(count)) |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def port(): |
| 52 | + return os.environ["FLASK_PORT"] |
| 53 | + |
| 54 | +def start_testserver(): |
| 55 | + port = get_port() |
| 56 | + os.environ["FLASK_APP"] = "coretestserver" |
| 57 | + os.environ["FLASK_PORT"] = str(port) |
| 58 | + cmd = "flask run -p {}".format(port) |
| 59 | + if os.name == 'nt': #On windows, subprocess creation works without being in the shell |
| 60 | + child_process = subprocess.Popen(cmd, env=dict(os.environ)) |
| 61 | + else: |
| 62 | + #On linux, have to set shell=True |
| 63 | + child_process = subprocess.Popen(cmd, shell=True, preexec_fn=os.setsid, env=dict(os.environ)) |
| 64 | + count = 5 |
| 65 | + for _ in range(count): |
| 66 | + if not is_port_available(port): |
| 67 | + return child_process |
| 68 | + time.sleep(1) |
| 69 | + raise ValueError("Didn't start!") |
| 70 | + |
| 71 | +def terminate_testserver(process): |
| 72 | + if os.name == 'nt': |
| 73 | + process.kill() |
| 74 | + else: |
| 75 | + os.killpg(os.getpgid(process.pid), signal.SIGTERM) # Send the signal to all the process groups |
| 76 | + |
| 77 | +@pytest.fixture(autouse=True, scope="package") |
| 78 | +def testserver(): |
| 79 | + """Start the Autorest testserver.""" |
| 80 | + server = start_testserver() |
| 81 | + yield |
| 82 | + terminate_testserver(server) |
| 83 | + |
| 84 | + |
| 85 | +# Ignore collection of async tests for Python 2 |
| 86 | +collect_ignore_glob = [] |
| 87 | +if sys.version_info < (3, 5): |
| 88 | + collect_ignore_glob.append("*_async.py") |
0 commit comments