Skip to content

Commit

Permalink
[#736] Added tests for webhook and docker deployment. (#1257)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardgaunt authored Apr 5, 2024
1 parent 4c7fbbd commit fbd8070
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
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 @@ provision_ssh_key_with_suffix() {
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"
}
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."
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
}

1 comment on commit fbd8070

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.