Skip to content

Commit

Permalink
* Prune docker build cache in integration tests. (#976)
Browse files Browse the repository at this point in the history
* Show requirement file content before pip install.
* For all tests running docker containers, show container logs if an exception was raised.
* Update control requirements to truss 0.9.14 (required also incrementing httpx version).
  • Loading branch information
marius-baseten authored Jun 14, 2024
1 parent a765131 commit e8d7c98
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ jobs:
matrix:
split_group: ["1", "2", "3", "4", "5"]
steps:
- name: Purge Docker cache
run: docker builder prune -af
- uses: actions/checkout@v3
- uses: ./.github/actions/setup-python/
- run: poetry install
Expand All @@ -77,6 +79,8 @@ jobs:
strategy:
fail-fast: false
steps:
- name: Purge Docker cache
run: docker builder prune -af
- uses: actions/checkout@v3
- uses: ./.github/actions/setup-python/
- run: poetry install
Expand Down
2 changes: 2 additions & 0 deletions truss/templates/base.Dockerfile.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ RUN apt-get update && apt-get install --yes --no-install-recommends $(cat {{syst
{% block install_requirements %}
{%- if should_install_user_requirements_file %}
COPY ./{{user_supplied_requirements_filename}} {{user_supplied_requirements_filename}}
RUN cat {{user_supplied_requirements_filename}}
RUN pip install -r {{user_supplied_requirements_filename}} --no-cache-dir && rm -rf /root/.cache/pip
{%- endif %}
{%- if should_install_requirements %}
COPY ./{{config_requirements_filename}} {{config_requirements_filename}}
RUN cat {{config_requirements_filename}}
RUN pip install -r {{config_requirements_filename}} --no-cache-dir && rm -rf /root/.cache/pip
{%- endif %}
{% endblock %}
Expand Down
4 changes: 2 additions & 2 deletions truss/templates/control/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
dataclasses-json==0.5.7
truss==0.9.10 # This needs to support py3.8. It can be incremented to releases after #955 (i.e. >0.9.12).
truss==0.9.14
fastapi==0.109.1
uvicorn==0.24.0
uvloop==0.19.0
tenacity==8.1.0
httpx==0.24.1
httpx==0.27.0
python-json-logger==2.0.2
loguru==0.7.2
38 changes: 36 additions & 2 deletions truss/tests/test_testing_utilities_for_other_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file contains shared code to be used in other tests
# TODO(pankaj): Using a tests file for shared code is not ideal, we should
# move it to a regular file. This is a short term hack.

import json
import shutil
import subprocess
import time
Expand All @@ -17,12 +17,46 @@
@contextmanager
def ensure_kill_all():
try:
yield
with _show_container_logs_if_raised():
yield
finally:
kill_all_with_retries()
ensure_free_disk_space()


def _human_readable_json_logs(raw_logs: str) -> str:
output = []
for line in raw_logs.splitlines():
try:
log_entry = json.loads(line)
human_readable_log = " ".join(
f"{key}: {value}" for key, value in log_entry.items()
)
output.append(f"\t{human_readable_log}")
except Exception:
output.append(line)
return "\n".join(output)


@contextmanager
def _show_container_logs_if_raised():
initial_ids = {c.id for c in get_containers({TRUSS: True})}
exception_raised = False
try:
yield
except Exception:
exception_raised = True
raise
finally:
if exception_raised:
print("An exception was raised, showing logs of all containers.")
containers = get_containers({TRUSS: True})
new_containers = [c for c in containers if c.id not in initial_ids]
for container in new_containers:
print(f"Logs for container {container.name} ({container.id}):")
print(_human_readable_json_logs(container.logs()))


def kill_all_with_retries(num_retries: int = 10):
kill_all()
attempts = 0
Expand Down

0 comments on commit e8d7c98

Please sign in to comment.