diff --git a/README.md b/README.md index 91f27d4..755d4b5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/tests/bats/data.bats b/tests/bats/image.bats similarity index 60% rename from tests/bats/data.bats rename to tests/bats/image.bats index 6bb5864..eac8c31 100644 --- a/tests/bats/data.bats +++ b/tests/bats/image.bats @@ -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. @@ -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" -} diff --git a/tests/bats/seed.bats b/tests/bats/seed.bats new file mode 100644 index 0000000..b18b51a --- /dev/null +++ b/tests/bats/seed.bats @@ -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" +}