fix: improve mock Forgejo server port handling
This commit is contained in:
parent
7190abb4e1
commit
dfedd80d0d
2 changed files with 24 additions and 4 deletions
|
|
@ -33,6 +33,6 @@ steps:
|
||||||
commands:
|
commands:
|
||||||
- apk add --no-cache bash curl jq git coreutils
|
- apk add --no-cache bash curl jq git coreutils
|
||||||
- MOCK_FORGE_PORT=3001 python3 tests/mock-forgejo.py > /tmp/mock.log 2>&1 &
|
- MOCK_FORGE_PORT=3001 python3 tests/mock-forgejo.py > /tmp/mock.log 2>&1 &
|
||||||
# Wait for mock to be ready
|
# Wait for mock to be ready (might be on a different port if 3001 is in use)
|
||||||
- for i in $(seq 1 30); do curl -sf http://localhost:3001/api/v1/version >/dev/null 2>&1 && break || sleep 1; done
|
- for i in $(seq 1 30); do curl -sf http://localhost:$MOCK_FORGE_PORT/api/v1/version >/dev/null 2>&1 && break || sleep 1; done
|
||||||
- SMOKE_FORGE_URL=http://localhost:3001 FORGE_URL=http://localhost:3001 bash tests/smoke-init.sh
|
- SMOKE_FORGE_URL=http://localhost:$MOCK_FORGE_PORT FORGE_URL=http://localhost:$MOCK_FORGE_PORT bash tests/smoke-init.sh
|
||||||
|
|
|
||||||
|
|
@ -698,11 +698,31 @@ class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
|
||||||
allow_reuse_address = True
|
allow_reuse_address = True
|
||||||
|
|
||||||
|
|
||||||
|
def find_free_port(start_port):
|
||||||
|
"""Find a free port starting from start_port."""
|
||||||
|
for port in range(start_port, start_port + 100):
|
||||||
|
try:
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
s.bind(("0.0.0.0", port))
|
||||||
|
s.close()
|
||||||
|
return port
|
||||||
|
except OSError:
|
||||||
|
continue
|
||||||
|
return start_port
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Start the mock server."""
|
"""Start the mock server."""
|
||||||
global SHUTDOWN_REQUESTED
|
global SHUTDOWN_REQUESTED
|
||||||
|
|
||||||
port = int(os.environ.get("MOCK_FORGE_PORT", 3000))
|
base_port = int(os.environ.get("MOCK_FORGE_PORT", 3000))
|
||||||
|
port = find_free_port(base_port)
|
||||||
|
|
||||||
|
# If we found a different port, update the environment variable
|
||||||
|
if port != base_port:
|
||||||
|
os.environ["MOCK_FORGE_PORT"] = str(port)
|
||||||
|
|
||||||
server = ThreadingHTTPServer(("0.0.0.0", port), ForgejoHandler)
|
server = ThreadingHTTPServer(("0.0.0.0", port), ForgejoHandler)
|
||||||
|
|
||||||
print(f"Mock Forgejo server starting on port {port}", file=sys.stderr)
|
print(f"Mock Forgejo server starting on port {port}", file=sys.stderr)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue