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

Split tests into files. #66

Merged
merged 1 commit into from
Dec 2, 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
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ In some cases, shell may report platform incorrectly. Run with forced platform:

### Running tests

tests/bats/node_modules/.bin/bats tests/bats/data.bats --tap
# or
DOCKER_DEFAULT_PLATFORM=linux/amd64 tests/bats/node_modules/.bin/bats --tap tests/bats/data.bats
tests/bats/node_modules/.bin/bats tests/bats/image.bats --tap
tests/bats/node_modules/.bin/bats tests/bats/seed.bats --tap

### Publishing

Expand Down
60 changes: 2 additions & 58 deletions tests/bats/data.bats → tests/bats/image.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#
# Test functionality.
#
# tests/bats/node_modules/.bin/bats --tap tests/bats/data.bats
# tests/bats/node_modules/.bin/bats --tap tests/bats/image.bats
#
# Note that these tests will always run only for the linux/amd64 platform by
# default. To run the tests for other platforms, set the BUILDX_PLATFORMS and
# DOCKER_DEFAULT_PLATFORM environment variables to the desired platform(s). But
# make sure that the platform is supported by the Docker buildx driver.
#
# BUILDX_PLATFORMS=linux/arm64 DOCKER_DEFAULT_PLATFORM=linux/arm64 tests/bats/node_modules/.bin/bats --tap tests/bats/data.bats
# BUILDX_PLATFORMS=linux/arm64 DOCKER_DEFAULT_PLATFORM=linux/arm64 tests/bats/node_modules/.bin/bats --tap tests/bats/image.bats
#
# Make sure to commit the source code change before running the tests as it
# copies the source code at the last commit to the test directory.
Expand Down Expand Up @@ -92,59 +92,3 @@ load _helper
assert_success
assert_contains "mytesttable" "${output}"
}

@test "Seeding of the data works" {
tag="${TEST_DOCKER_TAG_PREFIX}$(random_string_lower)"
export BASE_IMAGE="drevops/mariadb-drupal-data-test:${tag}-base"
dst_image="drevops/mariadb-drupal-data-test:${tag}-dst"

step "Prepare base image."

substep "Copying fixture DB dump."
file="${BUILD_DIR}/db.sql"
cp "${BATS_TEST_DIRNAME}/fixtures/db.sql" "${file}"

substep "Build and push a fresh base image tagged with ${BASE_IMAGE}."
docker buildx build --platform "${BUILDX_PLATFORMS}" --load --push --no-cache -t "${BASE_IMAGE}" .

step "Assert seeding without mysql upgrade works."

# Pass the destination platform to the seeding script.
# Note that the name for the variable `BUILDX_PLATFORMS` in the test was
# chosen to be different from the `DESTINATION_PLATFORMS` in the seeding
# script to separate building images when preparing the test environment
# from the seeding process.
export DESTINATION_PLATFORMS="${BUILDX_PLATFORMS}"
substep "Run DB seeding script for ${dst_image} from the base image ${BASE_IMAGE} for destination platform(s) ${DESTINATION_PLATFORMS}."
./seed-db.sh "${file}" "${dst_image}" >&3

substep "Start container from the seeded image ${dst_image}."
# Start container with a non-root user to imitate limited host permissions.
cid=$(docker run --user 1000 -d "${dst_image}" 2>&3)

wait_mysql "${cid}"

substep "Assert that data was captured into the new image."
run docker exec --user 1000 "${cid}" /usr/bin/mysql -e "use drupal;show tables;" drupal
assert_output_contains "users"

substep "Assert that the mysql upgrade was skipped by default."
run docker logs "${cid}"
assert_output_not_contains "starting mysql upgrade"

step "Assert mysql upgrade works in container started from already seeded image."

substep "Start container from the seeded image ${dst_image} and request an upgrade."
# Start container with a non-root user to imitate limited host permissions.
cid=$(docker run --user 1000 -d -e FORCE_MYSQL_UPGRADE=1 "${dst_image}")

wait_mysql "${cid}"

substep "Assert that the mysql upgrade was performed."
run docker logs "${cid}"
assert_output_contains "starting mysql upgrade"

substep "Assert that data is present in the new image after the upgrade."
run docker exec --user 1000 "${cid}" /usr/bin/mysql -e "use drupal;show tables;" drupal
assert_output_contains "users"
}
73 changes: 73 additions & 0 deletions tests/bats/seed.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bats
#
# Test functionality.
#
# tests/bats/node_modules/.bin/bats --tap tests/bats/seed.bats
#
# Note that these tests will always run only for the linux/amd64 platform by
# default. To run the tests for other platforms, set the BUILDX_PLATFORMS and
# DOCKER_DEFAULT_PLATFORM environment variables to the desired platform(s). But
# make sure that the platform is supported by the Docker buildx driver.
#
# BUILDX_PLATFORMS=linux/arm64 DOCKER_DEFAULT_PLATFORM=linux/arm64 tests/bats/node_modules/.bin/bats --tap tests/bats/seed.bats
#
# Make sure to commit the source code change before running the tests as it
# copies the source code at the last commit to the test directory.

load _helper

@test "Seeding of the data works" {
tag="${TEST_DOCKER_TAG_PREFIX}$(random_string_lower)"
export BASE_IMAGE="drevops/mariadb-drupal-data-test:${tag}-base"
dst_image="drevops/mariadb-drupal-data-test:${tag}-dst"

step "Prepare base image."

substep "Copying fixture DB dump."
file="${BUILD_DIR}/db.sql"
cp "${BATS_TEST_DIRNAME}/fixtures/db.sql" "${file}"

substep "Build and push a fresh base image tagged with ${BASE_IMAGE}."
docker buildx build --platform "${BUILDX_PLATFORMS}" --load --push --no-cache -t "${BASE_IMAGE}" .

step "Assert seeding without mysql upgrade works."

# Pass the destination platform to the seeding script.
# Note that the name for the variable `BUILDX_PLATFORMS` in the test was
# chosen to be different from the `DESTINATION_PLATFORMS` in the seeding
# script to separate building images when preparing the test environment
# from the seeding process.
export DESTINATION_PLATFORMS="${BUILDX_PLATFORMS}"
substep "Run DB seeding script for ${dst_image} from the base image ${BASE_IMAGE} for destination platform(s) ${DESTINATION_PLATFORMS}."
./seed-db.sh "${file}" "${dst_image}" >&3

substep "Start container from the seeded image ${dst_image}."
# Start container with a non-root user to imitate limited host permissions.
cid=$(docker run --user 1000 -d "${dst_image}" 2>&3)

wait_mysql "${cid}"

substep "Assert that data was captured into the new image."
run docker exec --user 1000 "${cid}" /usr/bin/mysql -e "use drupal;show tables;" drupal
assert_output_contains "users"

substep "Assert that the mysql upgrade was skipped by default."
run docker logs "${cid}"
assert_output_not_contains "starting mysql upgrade"

step "Assert mysql upgrade works in container started from already seeded image."

substep "Start container from the seeded image ${dst_image} and request an upgrade."
# Start container with a non-root user to imitate limited host permissions.
cid=$(docker run --user 1000 -d -e FORCE_MYSQL_UPGRADE=1 "${dst_image}")

wait_mysql "${cid}"

substep "Assert that the mysql upgrade was performed."
run docker logs "${cid}"
assert_output_contains "starting mysql upgrade"

substep "Assert that data is present in the new image after the upgrade."
run docker exec --user 1000 "${cid}" /usr/bin/mysql -e "use drupal;show tables;" drupal
assert_output_contains "users"
}