Skip to content

Commit

Permalink
Added support for existing .dockerignore file.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Dec 3, 2024
1 parent 428315b commit 99cd0e4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
30 changes: 30 additions & 0 deletions seed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ cleanup() {
else
note "No logs available to display."
fi

if [ -f ".dockerignore.bak" ]; then
note "Restoring .dockerignore from .dockerignore.bak"
mv .dockerignore.bak .dockerignore
if [ ! -f ".dockerignore" ]; then
fail "Unable to restore .dockerignore from .dockerignore.bak"
exit 1
fi
pass "Restored .dockerignore from .dockerignore.bak"
fi
fi
}

Expand Down Expand Up @@ -185,6 +195,16 @@ fi
note "Destination image: ${DST_IMAGE}"
note "Destination platform(s): ${DESTINATION_PLATFORMS}"

if [ -f ".dockerignore" ]; then
note "Moving .dockerignore to .dockerignore.bak"
mv .dockerignore .dockerignore.bak
if [ ! -f ".dockerignore.bak" ]; then
fail "Unable to move .dockerignore to .dockerignore.bak"
exit 1
fi
pass "Moved .dockerignore to .dockerignore.bak"
fi

info "Stage 1: Produce database structure files from dump file"

task "Pulling the base image ${BASE_IMAGE}."
Expand Down Expand Up @@ -238,5 +258,15 @@ cid="$(get_started_container_id "${DST_IMAGE}")"
assert_db_was_imported "${cid}" 1000
stop_container "${cid}"

if [ -f ".dockerignore.bak" ]; then
note "Restoring .dockerignore from .dockerignore.bak"
mv .dockerignore.bak .dockerignore
if [ ! -f ".dockerignore" ]; then
fail "Unable to restore .dockerignore from .dockerignore.bak"
exit 1
fi
pass "Restored .dockerignore from .dockerignore.bak"
fi

info "Finished database seeding."
note "https://hub.docker.com/r/${DST_IMAGE%:*}/tags"
63 changes: 63 additions & 0 deletions tests/bats/seed.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#
# 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.
#
# shellcheck disable=SC2030,SC2031

load _helper

Expand Down Expand Up @@ -71,3 +73,64 @@ load _helper
run docker exec --user 1000 "${cid}" /usr/bin/mysql -e "use drupal;show tables;" drupal
assert_output_contains "users"
}

@test "Seeding of the data works with .dockerignore" {
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 .dockerignore file."
echo ".db-structure" >.dockerignore
assert_file_not_exists .dockerignore.bak

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.sh "${file}" "${dst_image}" >&3
assert_file_not_exists .dockerignore.bak

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"
}

0 comments on commit 99cd0e4

Please sign in to comment.