Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vdk-greenplum: adopt testcontainers #2498

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions projects/vdk-plugins/vdk-greenplum/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
docker-compose

# testing requirements
pytest-docker<2

# install earlier version due to https://github.com/yaml/pyyaml/issues/601
PyYAML==5.3.1
testcontainers
vdk-test-utils
59 changes: 39 additions & 20 deletions projects/vdk-plugins/vdk-greenplum/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Copyright 2021-2023 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
import logging
import os
import time
from functools import partial
from unittest import mock

import pytest
from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_for
from vdk.plugin.greenplum import greenplum_plugin
from vdk.plugin.test_utils.util_funcs import CliEntryBasedTestRunner

Expand All @@ -15,21 +19,16 @@
VDK_GREENPLUM_HOST = "VDK_GREENPLUM_HOST"
VDK_GREENPLUM_PORT = "VDK_GREENPLUM_PORT"

log = logging.getLogger(__name__)


def _is_responsive(runner):
try:
result = runner.invoke(["greenplum-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"
)
except Exception as e:
raise ConnectionError(str(e)) from e


@pytest.fixture(scope="session")
Expand All @@ -44,17 +43,37 @@ def docker_compose_file(pytestconfig):
VDK_GREENPLUM_PORT: "5432",
},
)
def greenplum_service(docker_ip, docker_services):
def greenplum_service(request):
"""Ensure that Greenplum service is up and responsive."""
runner = CliEntryBasedTestRunner(greenplum_plugin)
# os.system("echo Check open ports:")
# os.system("ss -lntu")
port = int(os.environ[VDK_GREENPLUM_PORT])
container = (
DockerContainer("datagrip/greenplum:6.8")
.with_bind_ports(port, port)
.with_env("GREENPLUM_USER", os.environ[VDK_GREENPLUM_PORT])
.with_env("GREENPLUM_PASSWORD", os.environ[VDK_GREENPLUM_PASSWORD])
)

# 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)
try:
container.start()
runner = CliEntryBasedTestRunner(greenplum_plugin)
wait_for(partial(_is_responsive, runner))
# wait 2 seconds to make sure the service is up and responsive
# might be unnecessary but it's out of abundance of caution
time.sleep(2)
log.info(
f"Greenplum service started on port {container.get_exposed_port(port)} and host {container.get_container_host_ip()}"
)
except Exception as e:
log.info(f"Failed to start Greenplum service: {e}")
log.info(f"Container logs: {container.get_logs()}")
raise e

docker_services.wait_until_responsive(
timeout=30.0, pause=0.3, check=lambda: _is_responsive(runner)
)
def stop_container():
container.stop()
log.info("Greenplum service stopped")

request.addfinalizer(stop_container)

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

This file was deleted.