2022-09-19 13:21:31 +00:00
|
|
|
import pytest
|
|
|
|
import requests
|
2022-10-15 02:30:08 +00:00
|
|
|
import socket
|
|
|
|
import os
|
2022-09-19 13:21:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
2022-12-04 14:31:57 +00:00
|
|
|
default_base_url = "http://web" if os.getenv("DOCKER") else "http://localhost:8080"
|
2022-10-15 02:30:08 +00:00
|
|
|
|
|
|
|
parser.addoption("--home_dir", action="store", default="/home/pi")
|
|
|
|
parser.addoption("--base_url", action="store", default=default_base_url)
|
|
|
|
parser.addoption("--httpserver_host", action="store", default=socket.gethostname())
|
|
|
|
parser.addoption("--httpserver_listen_address", action="store", default="0.0.0.0")
|
2022-12-05 17:58:23 +00:00
|
|
|
parser.addoption("--piscsi_username", action="store", default="pi")
|
|
|
|
parser.addoption("--piscsi_password", action="store", default="piscsi")
|
2022-09-19 13:21:31 +00:00
|
|
|
|
|
|
|
|
2022-10-15 02:30:08 +00:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def env(pytestconfig):
|
|
|
|
home_dir = pytestconfig.getoption("home_dir")
|
|
|
|
return {
|
|
|
|
"is_docker": bool(os.getenv("DOCKER")),
|
|
|
|
"home_dir": home_dir,
|
2022-12-05 17:58:23 +00:00
|
|
|
"cfg_dir": f"{home_dir}/.config/piscsi",
|
2022-10-15 02:30:08 +00:00
|
|
|
"images_dir": f"{home_dir}/images",
|
2022-11-01 23:43:24 +00:00
|
|
|
"file_server_dir": f"{home_dir}/shared_files",
|
2022-10-15 02:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-19 23:33:47 +00:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def httpserver_listen_address(pytestconfig):
|
|
|
|
return (pytestconfig.getoption("httpserver_listen_address"), 0)
|
|
|
|
|
|
|
|
|
2022-09-19 13:21:31 +00:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2022-09-19 23:33:47 +00:00
|
|
|
def set_httpserver_hostname(pytestconfig, httpserver):
|
2022-10-15 02:30:08 +00:00
|
|
|
# We need httpserver.url_for() to generate URLs pointing to the correct host
|
2022-09-19 23:33:47 +00:00
|
|
|
httpserver.host = pytestconfig.getoption("httpserver_host")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def ensure_all_devices_detached(create_http_client):
|
|
|
|
http_client = create_http_client()
|
|
|
|
http_client.post("/scsi/detach_all")
|
2022-09-19 13:21:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def create_http_client(pytestconfig):
|
|
|
|
def create(authenticate=True):
|
|
|
|
session = requests.Session()
|
|
|
|
session.headers.update({"Accept": "application/json"})
|
|
|
|
session.original_request = session.request
|
|
|
|
|
|
|
|
def relative_request(method, url, *args, **kwargs):
|
|
|
|
if url[:4] != "http":
|
|
|
|
url = pytestconfig.getoption("base_url") + url
|
|
|
|
|
|
|
|
return session.original_request(method, url, *args, **kwargs)
|
|
|
|
|
|
|
|
session.request = relative_request
|
|
|
|
|
|
|
|
if authenticate:
|
|
|
|
session.post(
|
|
|
|
"/login",
|
|
|
|
data={
|
2022-12-05 17:58:23 +00:00
|
|
|
"username": pytestconfig.getoption("piscsi_username"),
|
|
|
|
"password": pytestconfig.getoption("piscsi_password"),
|
2022-09-19 13:21:31 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
return session
|
|
|
|
|
|
|
|
return create
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def http_client(create_http_client):
|
|
|
|
return create_http_client(authenticate=True)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def http_client_unauthenticated(create_http_client):
|
|
|
|
return create_http_client(authenticate=False)
|