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

[#736] Added tests for webhook and docker deployment. #1257

Merged
merged 3 commits into from
Apr 5, 2024
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: 7 additions & 0 deletions .scaffold/tests/bats/_helper.deployment.bash
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,10 @@
local suffix="${1:-TEST}"
ssh-keygen -t rsa -b 4096 -N "" -f "${SSH_KEY_FIXTURE_DIR}/id_rsa_${suffix}"
}

provision_docker_config_file() {
export HOME="${BUILD_DIR}"
fixture_prepare_dir "${HOME}/.docker"
touch "${HOME}/.docker/config.json"
echo "{$1:-docker.io}" > "${HOME}/.docker/config.json"

Check warning on line 180 in .scaffold/tests/bats/_helper.deployment.bash

View check run for this annotation

Codecov / codecov/patch

.scaffold/tests/bats/_helper.deployment.bash#L177-L180

Added lines #L177 - L180 were not covered by tests
}
117 changes: 117 additions & 0 deletions .scaffold/tests/bats/deploy-docker.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env bats
#
# Tests for scripts/drevops/deploy-docker.sh script.
#
# shellcheck disable=SC2030,SC2031,SC2129,SC2155

load _helper.bash
load _helper.deployment.bash

@test "Missing DREVOPS_DEPLOY_DOCKER_MAP - deployment should not proceed" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
unset DREVOPS_DEPLOY_DOCKER_MAP
run scripts/drevops/deploy-docker.sh
assert_success
assert_output_contains "Services map is not specified in DREVOPS_DEPLOY_DOCKER_MAP variable. Docker deployment will not continue."
popd >/dev/null
}

@test "Docker deployment with valid DREVOPS_DEPLOY_DOCKER_MAP" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
export DREVOPS_DOCKER_IMAGE_TAG="test_latest"
export DOCKER_REGISTRY="registry.example.com"
provision_docker_config_file DOCKER_REGISTRY
export DREVOPS_DEPLOY_DOCKER_MAP="service1=image1,service2=image2,service3=image3"

declare -a STEPS=(
"Started DOCKER deployment."
"Processing service service1"
"@docker compose ps -q service1 # service1_service_id"
"Found \"service1\" service container with id \"service1_service_id\"."
"Committing Docker image with name \"${DOCKER_REGISTRY}/image1:${DREVOPS_DOCKER_IMAGE_TAG}\"."
"@docker commit service1_service_id ${DOCKER_REGISTRY}/image1:${DREVOPS_DOCKER_IMAGE_TAG} # sha256:service1_image_id"
"Committed Docker image with id \"service1_image_id\"."
"Pushing Docker image to the registry."
"@docker push ${DOCKER_REGISTRY}/image1:${DREVOPS_DOCKER_IMAGE_TAG}"
"Processing service service2"
"@docker compose ps -q service2 # service2_service_id"
"Found \"service2\" service container with id \"service2_service_id\"."
"Committing Docker image with name \"${DOCKER_REGISTRY}/image2:${DREVOPS_DOCKER_IMAGE_TAG}\"."
"@docker commit service2_service_id ${DOCKER_REGISTRY}/image2:${DREVOPS_DOCKER_IMAGE_TAG} # sha256:service2_image_id"
"Committed Docker image with id \"service2_image_id\"."
"Pushing Docker image to the registry."
"@docker push ${DOCKER_REGISTRY}/image2:${DREVOPS_DOCKER_IMAGE_TAG}"
"Processing service service3"
"@docker compose ps -q service3 # service3_service_id"
"Found \"service3\" service container with id \"service3_service_id\"."
"Committing Docker image with name \"${DOCKER_REGISTRY}/image3:${DREVOPS_DOCKER_IMAGE_TAG}\"."
"@docker commit service3_service_id ${DOCKER_REGISTRY}/image3:${DREVOPS_DOCKER_IMAGE_TAG} # sha256:service3_image_id"
"Committed Docker image with id \"service3_image_id\"."
"Pushing Docker image to the registry."
"@docker push ${DOCKER_REGISTRY}/image3:${DREVOPS_DOCKER_IMAGE_TAG}"
"Finished DOCKER deployment."
)

mocks="$(run_steps "setup")"

run ./scripts/drevops/deploy-docker.sh
assert_success

run_steps "assert" "${mocks[@]}"

run scripts/drevops/deploy-docker.sh

popd >/dev/null
}

@test "Docker deployment with services not running" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
export DREVOPS_DOCKER_IMAGE_TAG="test_latest"
export DOCKER_REGISTRY="registry.example.com"
provision_docker_config_file DOCKER_REGISTRY
export DREVOPS_DEPLOY_DOCKER_MAP="service1=image1"

declare -a STEPS=(
"Started DOCKER deployment."
"Processing service service1"
"@docker compose ps -q service1"
"Service \"service1\" is not running."
)

mocks="$(run_steps "setup")"

run ./scripts/drevops/deploy-docker.sh
assert_failure

run_steps "assert" "${mocks[@]}"

run scripts/drevops/deploy-docker.sh

popd >/dev/null
}

@test "Invalid DREVOPS_DEPLOY_DOCKER_MAP provided" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
export DREVOPS_DOCKER_IMAGE_TAG="test_latest"
export DOCKER_REGISTRY="registry.example.com"

# No key/value pair
export DREVOPS_DEPLOY_DOCKER_MAP="service1"
run ./scripts/drevops/deploy-docker.sh
assert_failure
assert_output_contains "invalid key/value pair \"service1\" provided."

# using a space delimiter
export DREVOPS_DEPLOY_DOCKER_MAP="service1=image1 service2=image2"
run scripts/drevops/deploy-docker.sh
assert_failure
assert_output_contains "invalid key/value pair \"service1=image1 service2=image2\" provided."

# No comma delimiter
export DREVOPS_DEPLOY_DOCKER_MAP="service1=image1=service2=image2"
run scripts/drevops/deploy-docker.sh
assert_failure
assert_output_contains "invalid key/value pair \"service1=image1=service2=image2\" provided."

popd >/dev/null
}
57 changes: 57 additions & 0 deletions .scaffold/tests/bats/deployment-webhook.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bats
#
# Test for webhook deployments.
#
# shellcheck disable=SC2030,SC2031,SC2129,SC2155

load _helper.bash
load _helper.deployment.bash

@test "Missing variable checks" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
unset DREVOPS_DEPLOY_WEBHOOK_URL
unset DREVOPS_DEPLOY_WEBHOOK_METHOD
unset DREVOPS_DEPLOY_WEBHOOK_RESPONSE_STATUS
run scripts/drevops/deploy-webhook.sh
assert_failure
assert_output_contains "Missing required value for DREVOPS_DEPLOY_WEBHOOK_URL."
mock_curl=$(mock_command "curl")
mock_set_output "${mock_curl}" "200" 1
export DREVOPS_DEPLOY_WEBHOOK_URL="https://example.com"
unset DREVOPS_DEPLOY_WEBHOOK_METHOD
unset DREVOPS_DEPLOY_WEBHOOK_RESPONSE_STATUS
run scripts/drevops/deploy-webhook.sh
assert_success
assert_output_not_contains "Missing required value for DREVOPS_DEPLOY_WEBHOOK_METHOD."
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently scripts have default values for these two environment variables and so check will currently not fail.

assert_output_not_contains "Missing required value for DREVOPS_DEPLOY_WEBHOOK_RESPONSE_STATUS."
popd >/dev/null
}

@test "Successful webhook deployment" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
export DREVOPS_DEPLOY_WEBHOOK_URL="https://example.com"
export DREVOPS_DEPLOY_WEBHOOK_METHOD="GET"
export DREVOPS_DEPLOY_WEBHOOK_RESPONSE_STATUS="200"
mock_curl=$(mock_command "curl")
mock_set_output "${mock_curl}" "${DREVOPS_DEPLOY_WEBHOOK_RESPONSE_STATUS}" 1

run scripts/drevops/deploy-webhook.sh
assert_success
assert_output_contains "Webhook call completed."
assert_output_contains "Finished WEBHOOK deployment."
popd >/dev/null
}

@test "Failed webhook deployment" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
export DREVOPS_DEPLOY_WEBHOOK_URL="https://example.com"
export DREVOPS_DEPLOY_WEBHOOK_METHOD="GET"
export DREVOPS_DEPLOY_WEBHOOK_RESPONSE_STATUS="200"
mock_curl=$(mock_command "curl")
mock_set_output "${mock_curl}" "400" 1

run scripts/drevops/deploy-webhook.sh
assert_failure
assert_output_contains "Unable to complete webhook deployment."
popd >/dev/null
}
43 changes: 43 additions & 0 deletions .scaffold/tests/bats/login-docker.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bats
#
# Test for login-docker script.
#
# shellcheck disable=SC2030,SC2031,SC2129,SC2155

load _helper.bash
load _helper.deployment.bash

@test "Docker configuration present" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
export DOCKER_REGISTRY="https://www.example.com"
provision_docker_config_file $DOCKER_REGISTRY
export HOME=${BUILD_DIR}
run scripts/drevops/login-docker.sh
assert_success
assert_output_contains Already logged in to registry \"https://www.example.com\"
popd >/dev/null
}

@test "DOCKER_REGISTRY, DOCKER_USER and DOCKER_PASS must be set" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
export HOME=${BUILD_DIR}
unset DOCKER_USER
unset DOCKER_PASS
run scripts/drevops/login-docker.sh
assert_success
assert_output_not_contains "Missing required value for DOCKER_REGISTRY."
assert_output_contains "Skipping login into Docker registry as either DOCKER_USER or DOCKER_PASS was not provided."
export DOCKER_USER="test_user"
run scripts/drevops/login-docker.sh
assert_success
assert_output_contains "Skipping login into Docker registry as either DOCKER_USER or DOCKER_PASS was not provided."
mock_docker=$(mock_command "docker")
mock_set_output "${mock_docker}" "Login Succeeded" 1
export DOCKER_PASS="test_pass"
export DOCKER_REGISTRY="https://www.example.com"
run scripts/drevops/login-docker.sh
assert_success
assert_equal "1" "$(mock_get_call_num "${mock_docker}" 1)"
assert_output_contains "Logging in to registry \"https://www.example.com\"."
popd >/dev/null
}
Loading