Skip to content

Stack running after stopping test in vscode #123

@diegoquintanav

Description

@diegoquintanav

Hi! this is a follow-up from #117. I'm trying to get the containers created by pytest destroyed at teardown as by the examples shown:

Here's my setup

version: "3.7"

services:
  minio-src:
    image: minio/minio:RELEASE.2023-09-04T19-57-37Z
    container_name: minio-src
    ports:
      - "9010:9000"
      - "9011:9001"
    command: server /data --console-address ':9001' --address ':9000'
    environment:
      - MINIO_ROOT_USER=${MINIO_SRC_ACCESS_KEY_TEST-minio_src_access_key_test}
      - MINIO_ROOT_PASSWORD=${MINIO_SRC_SECRET_KEY_TEST-minio_src_secret_key_test}

  minio-dest:
    image: minio/minio:RELEASE.2023-09-04T19-57-37Z
    container_name: minio-dest
    ports:
      - "9012:9000"
      - "9013:9001"
    command: server /data --console-address ':9001' --address ':9000'
    environment:
      - MINIO_ROOT_USER=${MINIO_DEST_ACCESS_KEY_TEST-minio_dest_access_key_test}
      - MINIO_ROOT_PASSWORD=${MINIO_DEST_SECRET_KEY_TEST-minio_dest_secret_key_test}

and my test

from time import sleep
from typing import Generator

import boto3
import pytest
import requests

MINIO_SRC_ACCESS_KEY = "minio_src_access_key_test"
MINIO_SRC_SECRET_KEY = "minio_src_secret_key_test"
MINIO_SRC_BUCKET_NAME = "src"


# Pin the project name to avoid creating multiple stacks
@pytest.fixture(scope="session")
def docker_compose_project_name() -> str:
    return "pytest"


# Stop the stack before starting a new one
@pytest.fixture(scope="session")
def docker_setup(docker_compose_project_name):
    # https://github.com/avast/pytest-docker/pull/117
    return ["down -v", "up --build -d"]


def is_minio_responsive(endpoint: str) -> bool:
    try:
        sleep(1)  # Allow some time for the service to respond
        print(f"Checking MinIO health at {endpoint}/minio/health/live")
        headers = {"User-Agent": "Pytest"}
        response = requests.get(
            f"{endpoint}/minio/health/live",
            headers=headers,
        )

        print(f"MinIO health check response: {response.status_code} - {response.text}")
        condition_met = response.status_code == 200
        return condition_met
    except ConnectionError:
        return False


@pytest.fixture(scope="session")
def http_service_src(
    docker_ip,
    docker_services,
):
    """Ensure that HTTP service is up and responsive."""

    port = docker_services.port_for("minio-src", 9000)
    endpoint = f"http://{docker_ip}:{port}"

    docker_services.wait_until_responsive(
        timeout=30.0,
        pause=1,
        check=lambda: is_minio_responsive(endpoint),
    )

    yield endpoint



@pytest.fixture(scope="session")
def source_client(http_service_src) -> boto3.client:
    """Create a MinIO client for the source bucket."""
    s3_client_src = boto3.client(
        "s3",
        aws_access_key_id=MINIO_SRC_ACCESS_KEY,
        aws_secret_access_key=MINIO_SRC_SECRET_KEY,
        endpoint_url=http_service_src,
    )
    yield s3_client_src


@pytest.fixture(scope="session")
def source_bucket(source_client) -> Generator[str, None, None]:
    """Create a source bucket and yield its name."""
    bucket_name = MINIO_SRC_BUCKET_NAME
    source_client.create_bucket(Bucket=bucket_name)

    yield bucket_name
    source_client.delete_bucket(Bucket=bucket_name)


@pytest.mark.minio
def test_minio_src_bucket_exists(
    source_client: boto3.client,
    source_bucket: str,
):
    """Test if the source bucket exists."""
    response = source_client.list_buckets()
    bucket_names = [bucket["Name"] for bucket in response["Buckets"]]
    assert MINIO_SRC_BUCKET_NAME in bucket_names, "Source bucket does not exist." # debug until here and stop before finishing

pytest -m minio passes, but if I debug it with vscode and stop (Shift+F5) before letting the test finish, I'm getting stray containers that I need to remove manually with -p pytest down -v

$ docker compose -f tests/docker-compose.yml -p pytest ps 
NAME        IMAGE                                      COMMAND                                                                                SERVICE     CREATED              STATUS              PORTS
minio-src   minio/minio:RELEASE.2023-09-04T19-57-37Z   "/usr/bin/docker-entrypoint.sh server /data --console-address :9001 --address :9000"   minio-src   About a minute ago   Up About a minute   0.0.0.0:9010->9000/tcp, :::9010->9000/tcp, 0.0.0.0:9011->9001/tcp, :::9011->9001/tcp

what am I doing wrong? thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions