2022-08-14 18:36:49 +08:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
import socket
|
|
|
|
import subprocess
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
last_port = 1234
|
|
|
|
|
2022-11-12 00:00:12 +08:00
|
|
|
mysql_client_ssl_opt = "--ssl"
|
2022-08-22 05:30:06 +08:00
|
|
|
mysql_client_opts = []
|
2022-11-12 00:00:12 +08:00
|
|
|
if "GITHUB_ACTION" in os.environ:
|
2022-08-14 18:36:49 +08:00
|
|
|
# Github uses MySQL instead of MariaDB
|
2022-11-12 00:00:12 +08:00
|
|
|
mysql_client_ssl_opt = "--ssl-mode=REQUIRED"
|
|
|
|
mysql_client_opts = ["--enable-cleartext-plugin"]
|
2022-08-14 18:36:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
def alloc_port():
|
|
|
|
global last_port
|
|
|
|
last_port += 1
|
|
|
|
return last_port
|
|
|
|
|
|
|
|
|
2022-11-12 00:00:12 +08:00
|
|
|
def wait_port(port, recv=True, timeout=60, for_process: subprocess.Popen = None):
|
|
|
|
logging.debug(f"Waiting for port {port}")
|
2022-08-14 18:36:49 +08:00
|
|
|
|
2022-11-12 00:00:12 +08:00
|
|
|
data = b""
|
2022-08-14 18:36:49 +08:00
|
|
|
|
|
|
|
def wait():
|
|
|
|
nonlocal data
|
|
|
|
while True:
|
|
|
|
try:
|
2022-11-12 00:00:12 +08:00
|
|
|
s = socket.create_connection(("localhost", port), timeout=5)
|
2022-08-14 18:36:49 +08:00
|
|
|
if recv:
|
2022-09-02 20:00:08 +08:00
|
|
|
while True:
|
|
|
|
data = s.recv(100)
|
|
|
|
if data:
|
|
|
|
break
|
2022-08-14 18:36:49 +08:00
|
|
|
else:
|
2022-11-12 00:00:12 +08:00
|
|
|
data = b""
|
2022-08-14 18:36:49 +08:00
|
|
|
s.close()
|
2022-11-12 00:00:12 +08:00
|
|
|
logging.debug(f"Port {port} is up")
|
2022-08-14 18:36:49 +08:00
|
|
|
return data
|
|
|
|
except socket.error:
|
2022-11-12 00:00:12 +08:00
|
|
|
if for_process:
|
|
|
|
try:
|
|
|
|
for_process.wait(timeout=0.1)
|
|
|
|
raise Exception("Process exited while waiting for port")
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
time.sleep(0.1)
|
2022-08-14 18:36:49 +08:00
|
|
|
|
|
|
|
t = threading.Thread(target=wait, daemon=True)
|
|
|
|
t.start()
|
2022-09-04 18:06:09 +08:00
|
|
|
t.join(timeout=timeout)
|
2022-08-14 18:36:49 +08:00
|
|
|
if t.is_alive():
|
2022-11-12 00:00:12 +08:00
|
|
|
raise Exception(f"Port {port} is not up")
|
2022-08-14 18:36:49 +08:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def wait_mysql_port(port):
|
2022-11-12 00:00:12 +08:00
|
|
|
logging.debug(f"Waiting for MySQL port {port}")
|
2022-08-14 18:36:49 +08:00
|
|
|
|
|
|
|
def wait():
|
|
|
|
while True:
|
|
|
|
try:
|
2022-11-12 00:00:12 +08:00
|
|
|
subprocess.check_call(
|
|
|
|
f'mysql --user=root --password=123 --host=127.0.0.1 --port={port} --execute="show schemas;"',
|
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
logging.debug(f"Port {port} is up")
|
2022-08-14 18:36:49 +08:00
|
|
|
break
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
time.sleep(1)
|
|
|
|
continue
|
|
|
|
|
|
|
|
t = threading.Thread(target=wait, daemon=True)
|
|
|
|
t.start()
|
|
|
|
t.join(timeout=60)
|
|
|
|
if t.is_alive():
|
2022-11-12 00:00:12 +08:00
|
|
|
raise Exception(f"Port {port} is not up")
|
2022-08-14 18:36:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
def create_ticket(url, username, target_name):
|
|
|
|
session = requests.Session()
|
|
|
|
session.verify = False
|
|
|
|
response = session.post(
|
2022-11-12 00:00:12 +08:00
|
|
|
f"{url}/@warpgate/api/auth/login",
|
2022-08-14 18:36:49 +08:00
|
|
|
json={
|
2022-11-12 00:00:12 +08:00
|
|
|
"username": "admin",
|
|
|
|
"password": "123",
|
2022-08-14 18:36:49 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code // 100 == 2
|
|
|
|
response = session.post(
|
2022-11-12 00:00:12 +08:00
|
|
|
f"{url}/@warpgate/admin/api/tickets",
|
2022-08-14 18:36:49 +08:00
|
|
|
json={
|
2022-11-12 00:00:12 +08:00
|
|
|
"username": username,
|
|
|
|
"target_name": target_name,
|
2022-08-14 18:36:49 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 201
|
2022-11-12 00:00:12 +08:00
|
|
|
return response.json()["secret"]
|