Skip to content

Commit

Permalink
vdk-trino: change test container library
Browse files Browse the repository at this point in the history
This change is part of fixing vdk-trino which is failing regularly in
the nightly builds . See
#1559

Until now we used pytest-docker to start service container, this is
switching it to testcontainers-python.

There are multiple advanrages of using it.

* Supposedly Testcontainers is built with performance in mind and uses a
variety of techniques to minimize container startup time and resource
usage like image reuse, container reuse, asynchronous operations.

* It is far more popular lately -
https://hugovk.github.io/top-pypi-packages/  is a site I frequently used
to decide which library is likely to be more stable/used . It is showing
testcontainers is 2000 places ahead of pytest-docker

* Testcontainers provides a wider range of features for managing
containers, such as starting and stopping them, waiting for them to
become available, and exposing their ports to the host system

* Testcontainers-Python allows you to define and configure containers
using Python code instaed of yaml which pytest-docker required.

Testing Done: ran the test suite of vdk-trino a few times and it was
more stable and quick. I have not run actual benchmarks and recorded
times but it seems abotu twice faster possibly. We will verify this as
part of the nightly builds.

Signed-off-by: Antoni Ivanov <[email protected]>
  • Loading branch information
antoniivanov committed Feb 27, 2023
1 parent 7032d97 commit 2d46804
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 41 deletions.
2 changes: 1 addition & 1 deletion projects/vdk-plugins/vdk-trino/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# testing requirements
click
docker-compose
pytest-docker
testcontainers
trino
vdk-core
vdk-test-utils
57 changes: 25 additions & 32 deletions projects/vdk-plugins/vdk-trino/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
# Copyright 2021-2023 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
import os
import time
from unittest import mock

import pytest
from vdk.plugin.test_utils.util_funcs import CliEntryBasedTestRunner
from vdk.plugin.trino import trino_plugin
from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_for_logs

VDK_TRINO_HOST = "VDK_TRINO_HOST"
VDK_DB_DEFAULT_TYPE = "VDK_DB_DEFAULT_TYPE"
VDK_TRINO_PORT = "VDK_TRINO_PORT"
VDK_TRINO_USE_SSL = "VDK_TRINO_USE_SSL"


def is_responsive(runner):
try:
result = runner.invoke(["trino-query", "--query", "SELECT 1"])
if result.exit_code == 0:
return True
except:
return False


@pytest.fixture(scope="session")
def docker_compose_file(pytestconfig):
return os.path.join(
os.path.dirname(os.path.abspath(__file__)), "docker-compose.yml"
)


@pytest.fixture(scope="session")
@mock.patch.dict(
os.environ,
Expand All @@ -40,19 +23,29 @@ def docker_compose_file(pytestconfig):
VDK_TRINO_USE_SSL: "False",
},
)
def trino_service(docker_ip, docker_services):
def trino_service(request):
"""Ensure that Trino service is up and responsive."""
# os.system("echo Check open ports:")
# os.system("ss -lntu")
runner = CliEntryBasedTestRunner(trino_plugin)

# give the server some time to start before checking if it is ready
# before adding this sleep there were intermittent fails of the CI/CD with error:
# requests.exceptions.ConnectionError:
# ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
# More info: https://stackoverflow.com/questions/383738/104-connection-reset-by-peer-socket-error-or-when-does-closing-a-socket-resu
time.sleep(3)

docker_services.wait_until_responsive(
timeout=30.0, pause=0.3, check=lambda: is_responsive(runner)
)
port = int(os.environ[VDK_TRINO_PORT])
container = DockerContainer("trinodb/trino:latest").with_bind_ports(port, port)
try:
container.start()
wait_for_logs(container, "SERVER STARTED", timeout=120)
os.environ[VDK_TRINO_HOST] = container.get_container_host_ip()
os.environ[VDK_TRINO_PORT] = str(container.get_exposed_port(port))
print(
f"Trino service started on port {container.get_exposed_port(port)} and host {container.get_container_host_ip()}"
)
except Exception as e:
print(f"Failed to start Trino service: {e}")
print(f"Container logs: {container.get_logs()}")
raise e

def stop_container():
container.stop()
print("Trino service stopped")

request.addfinalizer(stop_container)

return container
8 changes: 0 additions & 8 deletions projects/vdk-plugins/vdk-trino/tests/docker-compose.yml

This file was deleted.

0 comments on commit 2d46804

Please sign in to comment.