# Path: tests/utils.py
# CONTAINER_NAME = "uwsgi-nginx-test"
# 
# def generate_dockerfile_content_simple_app(name: str) -> str:
#     content = f"FROM tiangolo/uwsgi-nginx:{name}\n"
#     content += "COPY ./app/main.py /app/main.py\n"
#     return content
# 
# def get_logs(container: Container) -> str:
#     logs = container.logs()
#     return logs.decode("utf-8")
# 
# def remove_previous_container(client: DockerClient) -> None:
#     try:
#         previous = client.containers.get(CONTAINER_NAME)
#         previous.stop()
#         previous.remove()
#     except NotFound:
#         return None


import os
import time
import docker
from pathlib import Path
from docker.models.containers import Container
from ..utils import (
    CONTAINER_NAME,
    generate_dockerfile_content_simple_app,
    get_logs,
    remove_previous_container,
)



client = docker.from_env()


def verify_container(container: Container) -> None:
    logs = get_logs(container)
    assert 'unable to find "application" callable in file /app/main.py' in logs
    assert (
        "unable to load app 0 (mountpoint='') (callable not found or import error)"
        in logs
    )
    assert "*** no app loaded. GAME OVER ***" in logs
    assert "INFO exited: uwsgi (exit status 22; not expected)" in logs
    assert (
        "INFO success: quit_on_failure entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)"
        in logs
    )
    assert "WARN received SIGTERM indicating exit request" in logs
    assert "INFO stopped: nginx (exit status 0)" in logs
    assert "INFO stopped: quit_on_failure (terminated by SIGTERM)" in logs


def test_on_broken_quit_container() -> None:
    name = os.getenv("NAME", "")

# Path: tests/utils.py
# CONTAINER_NAME = "uwsgi-nginx-test"
# 
# def generate_dockerfile_content_simple_app(name: str) -> str:
#     content = f"FROM tiangolo/uwsgi-nginx:{name}\n"
#     content += "COPY ./app/main.py /app/main.py\n"
#     return content
# 
# def get_logs(container: Container) -> str:
#     logs = container.logs()
#     return logs.decode("utf-8")
# 
# def remove_previous_container(client: DockerClient) -> None:
#     try:
#         previous = client.containers.get(CONTAINER_NAME)
#         previous.stop()
#         previous.remove()
#     except NotFound:
#         return None


# Path: tests/test_02_app/test_broken_app.py
import os
import time
import docker
from pathlib import Path
from docker.models.containers import Container
from ..utils import (
    CONTAINER_NAME,
    generate_dockerfile_content_simple_app,
    get_logs,
    remove_previous_container,
)




client = docker.from_env()


def verify_container(container: Container) -> None:
    logs = get_logs(container)
    assert 'unable to find "application" callable in file /app/main.py' in logs
    assert (
        "unable to load app 0 (mountpoint='') (callable not found or import error)"
        in logs
    )
    assert "*** no app loaded. GAME OVER ***" in logs
    assert "INFO exited: uwsgi (exit status 22; not expected)" in logs
    assert (
        "INFO success: quit_on_failure entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)"
        in logs
    )
    assert "WARN received SIGTERM indicating exit request" in logs
    assert "INFO stopped: nginx (exit status 0)" in logs
    assert "INFO stopped: quit_on_failure (terminated by SIGTERM)" in logs


def test_on_broken_quit_container() -> None:
    name = os.getenv("NAME", "")

    dockerfile_content = generate_dockerfile_content_simple_app(name)