diff --git a/src/commands/dbms_peer/dbms_peer_container_manager.py b/src/commands/dbms_peer/dbms_peer_container_manager.py index db4c50a..15fdc34 100644 --- a/src/commands/dbms_peer/dbms_peer_container_manager.py +++ b/src/commands/dbms_peer/dbms_peer_container_manager.py @@ -86,6 +86,7 @@ def start_container( container = self._start_container( command=command_params, + network_mode="host", volumes={ file.name: { "bind": secrets_target_path, @@ -105,4 +106,4 @@ def start_container( self.stop() if exit_code != 0: - raise DockerError() + raise DockerError("DBMS peer could not be started") diff --git a/src/common/docker/container_manager.py b/src/common/docker/container_manager.py index e20d304..6ed493d 100644 --- a/src/common/docker/container_manager.py +++ b/src/common/docker/container_manager.py @@ -118,8 +118,8 @@ def is_running(self) -> bool | None: ) return len(result) > 0 - except docker.errors.APIError: - raise DockerError() + except docker.errors.APIError as e: + raise DockerError(e.explanation) def get_label(self, label: str) -> Union[dict, None]: container_name = self.get_container().name @@ -134,8 +134,8 @@ def get_label(self, label: str) -> Union[dict, None]: label, None, ) - except docker.errors.APIError: - raise DockerContainerNotFoundError() + except docker.errors.APIError as e: + raise DockerContainerNotFoundError(e.explanation) def logs(self) -> None: container_name = self.get_container().name @@ -194,8 +194,8 @@ def stop(self) -> None: try: container = self.get_docker_client().containers.get(container_name) - except docker.errors.APIError: - raise DockerContainerNotFoundError() + except docker.errors.APIError as e: + raise DockerContainerNotFoundError(e.explanation) try: container.kill() diff --git a/tests/integration/fixtures/dbms_context.txt b/tests/integration/fixtures/dbms_context.txt index 671fbd3..19e5cc1 100644 --- a/tests/integration/fixtures/dbms_context.txt +++ b/tests/integration/fixtures/dbms_context.txt @@ -1 +1 @@ -table_name columnB IN ('ABC', 'FGH'); is_analysis=false \ No newline at end of file +public.atoms handle IN ('d0e88a51-b5dd-4000-a1c7-e55a7e12423a', '4ed34a99-120b-4c38-a1ab-348894e15d3f') diff --git a/tests/integration/fixtures/sql/db-adapter.sql b/tests/integration/fixtures/sql/db-adapter.sql new file mode 100644 index 0000000..939bf92 --- /dev/null +++ b/tests/integration/fixtures/sql/db-adapter.sql @@ -0,0 +1,9 @@ +CREATE TABLE atoms ( + id INT PRIMARY KEY, + handle VARCHAR(100) +); + +INSERT INTO atoms (id, handle) VALUES +(1, 'd0e88a51-b5dd-4000-a1c7-e55a7e12423a'), +(2, '2e2d9792-8784-4173-925c-ee7feb27713d'), +(3, '6fff1c0f-50c4-4826-b7bc-9c8366f4699e'); diff --git a/tests/integration/libs/docker.bash b/tests/integration/libs/docker.bash index 66af342..d2a3374 100644 --- a/tests/integration/libs/docker.bash +++ b/tests/integration/libs/docker.bash @@ -2,17 +2,25 @@ openfaas_repository="trueagi/openfaas" +function is_container_running() { + local container_name="$1" + if ! docker ps --format '{{.Names}}' | grep -q "^${container_name}\$"; then + return 1 + fi + + return 0 +} + + function is_service_up() { local container_name local service_name="$1" container_name=$(get_config ".${service_name}.container_name") - if ! docker ps --format '{{.Names}}' | grep -q "^${container_name}\$"; then - return 1 - fi - - return 0 + is_container_running "$container_name" + + return $? } function service_stop() { diff --git a/tests/integration/test_dbms_peer.bats b/tests/integration/test_dbms_peer.bats index 7447fd6..ee8fb72 100644 --- a/tests/integration/test_dbms_peer.bats +++ b/tests/integration/test_dbms_peer.bats @@ -50,153 +50,51 @@ setup() { @test "Invalid path for --context parameter" { local invalid_path="/path/invalid" run das-cli dbms-peer run --client-hostname localhost --client-port 5432 --client-username postgres --client-password pass --client-database db --context "$invalid_path" - assert_output "Error: Invalid value for '--context': File '$invalid_path' does not exist." + assert_line --partial "Error: Invalid value for '--context': File '$invalid_path' does not exist." } @test "Directory provided instead of file for --context" { run das-cli dbms-peer run --client-hostname localhost --client-port 5432 --client-username postgres --client-password pass --client-database db --context "$test_fixtures_dir" - assert_output "Error: Invalid value for '--context': File '$test_fixtures_dir' is a directory." + assert_line --partial "Error: Invalid value for '--context': File '$test_fixtures_dir' is a directory." } -# @test "Starting DAS Peer when db is not up" { -# local mongodb_container_name="$(get_config ".mongodb.container_name")" -# local redis_container_name="$(get_config ".redis.container_name")" - -# run das-cli das-peer start - -# assert_output "$mongodb_container_name is not running -# $redis_container_name is not running -# [DockerContainerNotFoundError] -# Please use 'db start' to start required services before running 'das-peer start'." - -# run is_service_up redis -# assert_failure - -# run is_service_up mongodb -# assert_failure - -# run is_service_up das-peer -# assert_failure -# } - -# @test "Starting DAS Peer command" { -# local mongodb_container_name="$(get_config ".mongodb.container_name")" -# local mongodb_port="$(get_config ".mongodb.port")" -# local redis_container_name="$(get_config ".redis.container_name")" -# local redis_port="$(get_config .redis.port)" -# local das_peer_port=30100 - -# das-cli db start - -# run das-cli das-peer start - -# assert_output "$mongodb_container_name is running on port $mongodb_port -# $redis_container_name is running on port $redis_port -# Starting DAS Peer server... -# DAS Peer is runnig on port $das_peer_port" - -# sleep 15s - -# run is_service_up redis -# assert_success - -# run is_service_up mongodb -# assert_success - -# run is_service_up das_peer -# assert_success -# } - -# @test "Should display an error message if the database is unavailable when attempting to start das-peer" { -# local mongodb_container_name="$(get_config ".mongodb.container_name")" -# local mongodb_port="$(get_config ".mongodb.port")" -# local redis_container_name="$(get_config ".redis.container_name")" -# local redis_port="$(get_config .redis.port)" -# local das_peer_port="$(get_config .das_peer.port)" - -# run is_service_up redis -# assert_failure - -# run is_service_up mongodb -# assert_failure - -# run das-cli das-peer start - -# assert_output "$mongodb_container_name is not running -# $redis_container_name is not running -# [DockerContainerNotFoundError] -# Please use 'db start' to start required services before running 'das-peer start'." - -# run is_service_up das_peer -# assert_failure -# } - -# @test "Should display an error message if containers are already running when attempting to start das-peer" { -# local mongodb_container_name="$(get_config ".mongodb.container_name")" -# local mongodb_port="$(get_config ".mongodb.port")" -# local redis_container_name="$(get_config ".redis.container_name")" -# local redis_port="$(get_config .redis.port)" - -# das-cli db start - -# run is_service_up redis -# assert_success - -# run is_service_up mongodb -# assert_success - -# das-cli das-peer start - -# run is_service_up das_peer -# assert_success - -# run das-cli das-peer start - -# assert_output "$mongodb_container_name is running on port $mongodb_port -# $redis_container_name is running on port $redis_port -# Starting DAS Peer server... -# [DockerContainerDuplicateError] The Docker container is already running. Cannot start another container with the same name." - -# } - -# @test "Should stop das-peer successfully" { -# das-cli db start - -# run is_service_up redis -# assert_success - -# run is_service_up mongodb -# assert_success - -# das-cli das-peer start - -# sleep 15s - -# run is_service_up das_peer -# assert_success - -# run das-cli das-peer stop - -# assert_output "Stopping DAS Peer service... -# The DAS Peer service has been stopped." - -# run is_service_up das_peer -# assert_failure -# } - -# @test "Should display a warning when das-peer is already stopped" { -# local das_peer_container_name="$(get_config ".das_peer.container_name")" - -# das-cli db start - -# run is_service_up redis -# assert_success - -# run is_service_up mongodb -# assert_success - -# run das-cli das-peer stop - -# assert_output "Stopping DAS Peer service... -# The DAS Peer service named $das_peer_container_name is already stopped." -# } +@test "Should run DBMS Peer successfuly" { + local postgres_container_name="dbms_peer_postgres" + local postgres_password="postgres" + local postgres_username="postgres" + local postgres_database="integration_db" + local postgres_port="5432" + local postgres_initdb="$test_fixtures_dir/sql/db-adapter.sql" + + "$(dirname "${BATS_TEST_DIRNAME}")/../scripts/start_postgres.sh" \ + -n $postgres_container_name \ + -p $postgres_password \ + -d $postgres_database \ + -u $postgres_username \ + -P $postgres_port \ + -i $postgres_initdb + + sleep 15s + + run is_container_running $postgres_container_name + assert_success + + local client_hostname="localhost" + local client_port="$postgres_port" + local client_username="$postgres_username" + local client_password="$postgres_password" + local client_database="$postgres_database" + local context="$test_fixtures_dir/dbms_context.txt" + local context_db="$(cat $context | cut -d " " -f 1)" + + run das-cli dbms-peer run \ + --client-hostname $client_hostname \ + --client-port $client_port \ + --client-username $client_username \ + --client-password $client_password \ + --client-database $client_database \ + --context "$context" + + assert_line --partial "The '$(cat $context_db)' has been mapped" + assert_line --partial "Done." +}