Skip to content

Commit

Permalink
tex2pdf pytest: allow running against pre-started docker container
Browse files Browse the repository at this point in the history
Use the options
	pytest --no-docker-setup --docker-port 6301
to disable docker setup and connect to an already running container
at port 6301.
  • Loading branch information
norbusan committed Sep 11, 2024
1 parent 8f6da14 commit c263a6e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
5 changes: 5 additions & 0 deletions tex2pdf_service/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def pytest_addoption(parser):
parser.addoption("--no-docker-setup", action="store_true", help="do not run docker setup, expect a running docker")
parser.addoption("--docker-port", action="store", default="33031", help="outside docker port to use")

62 changes: 32 additions & 30 deletions tex2pdf_service/tests/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,40 @@ def submit_tarball(service: str, tarball: str, outcome_file: str, tex2pdf_timeou
return meta

@pytest.fixture(scope="module")
def docker_container():
os.makedirs("tests/output", exist_ok=True)
def docker_container(request):
PORT = request.config.getoption('--docker-port')
url = f"http://localhost:{PORT}"

image_name = "public-tex2pdf-app-2024-2024-07-21"
container_name = "test-arxiv-tex2pdf"
dockerport = "8080"
os.makedirs("tests/output", exist_ok=True)

subprocess.call(["docker", "kill", container_name])
if not request.config.getoption('--no-docker-setup'):
image_name = "public-tex2pdf-app-2024-2024-07-21"
container_name = "test-arxiv-tex2pdf"
dockerport = "8080"

# Make sure the container is the latest
args = ["make", "app.docker"]
make = subprocess.run(args, encoding='utf-8', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if make.returncode != 0:
print(make.stdout)
print(make.stderr)
pass
subprocess.call(["docker", "kill", container_name])

# Start the container
args = ["docker", "run", '--security-opt', "no-new-privileges=true", "--cpus", "1", "--rm",
"-d",
"-p", f"{PORT}:{dockerport}",
"-e", f"PORT={dockerport}",
"--name", container_name,
image_name]
docker = subprocess.run(args, encoding='utf-8', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if docker.returncode != 0:
logging.error("tex2pdf container did not start")
pass
# Make sure the container is the latest
args = ["make", "app.docker"]
make = subprocess.run(args, encoding='utf-8', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if make.returncode != 0:
print(make.stdout)
print(make.stderr)
pass

# container_id = docker.stdout
# Start the container
args = ["docker", "run", '--security-opt', "no-new-privileges=true", "--cpus", "1", "--rm",
"-d",
"-p", f"{PORT}:{dockerport}",
"-e", f"PORT={dockerport}",
"--name", container_name,
image_name]
docker = subprocess.run(args, encoding='utf-8', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if docker.returncode != 0:
logging.error("tex2pdf container did not start")
pass

# Wait for the API to be ready
url = f"http://localhost:{PORT}"
for _ in range(60): # retries for 60 seconds
try:
response = requests.get(url)
Expand All @@ -89,10 +90,11 @@ def docker_container():

yield url

# Stop the container after tests
with open("tex2pdf.log", "w", encoding="utf-8") as log:
subprocess.call(["docker", "logs", container_name], stdout=log, stderr=log)
subprocess.call(["docker", "kill", container_name])
if not request.config.getoption('--no-docker-setup'):
# Stop the container after tests
with open("tex2pdf.log", "w", encoding="utf-8") as log:
subprocess.call(["docker", "logs", container_name], stdout=log, stderr=log)
subprocess.call(["docker", "kill", container_name])

@pytest.mark.integration
def test_api_hello(docker_container):
Expand Down

0 comments on commit c263a6e

Please sign in to comment.