Skip to content

Commit

Permalink
das-toolbox-125: Add test to dbms peer
Browse files Browse the repository at this point in the history
  • Loading branch information
levisingularity committed Nov 6, 2024
1 parent ddf6914 commit 0747e06
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 157 deletions.
3 changes: 2 additions & 1 deletion src/commands/dbms_peer/dbms_peer_container_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def start_container(

container = self._start_container(
command=command_params,
network_mode="host",
volumes={
file.name: {
"bind": secrets_target_path,
Expand All @@ -105,4 +106,4 @@ def start_container(
self.stop()

if exit_code != 0:
raise DockerError()
raise DockerError("DBMS peer could not be started")
12 changes: 6 additions & 6 deletions src/common/docker/container_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/fixtures/dbms_context.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
table_name columnB IN ('ABC', 'FGH'); is_analysis=false
public.atoms handle IN ('d0e88a51-b5dd-4000-a1c7-e55a7e12423a', '4ed34a99-120b-4c38-a1ab-348894e15d3f')
9 changes: 9 additions & 0 deletions tests/integration/fixtures/sql/db-adapter.sql
Original file line number Diff line number Diff line change
@@ -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');
18 changes: 13 additions & 5 deletions tests/integration/libs/docker.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
186 changes: 42 additions & 144 deletions tests/integration/test_dbms_peer.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}

0 comments on commit 0747e06

Please sign in to comment.