Skip to content

Commit

Permalink
🧪 test(cd): Add smoke test for Backup Retention Policy image.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Feb 5, 2025
1 parent b4e2bba commit ad9eb1a
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 1 deletion.
1 change: 1 addition & 0 deletions .deploy/backup-retention-policy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ COPY .backup/keep.py /keep.py
COPY scripts/cronjob /cronjob

ENV \

Check warning on line 13 in .deploy/backup-retention-policy/Dockerfile

View workflow job for this annotation

GitHub Actions / Continuous integration (build image backup-retention-policy) (ubuntu-latest)

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$BACKUP_RETENTION_POLICY_SCHEDULE' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/
NOW="" \
BACKUP_RETENTION_POLICY_SCHEDULE="" \
CRONJOB_SCHEDULE="${BACKUP_RETENTION_POLICY_SCHEDULE}"

Expand Down
4 changes: 3 additions & 1 deletion .deploy/backup-retention-policy/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

set -o xtrace

NOW="${NOW:-$(date +'%Y-%m-%d_%H:%M:%S')}"

KEEP_PY="${1}"
BACKUP_DIR="${2}"

ALL="$(ls "${BACKUP_DIR}" | sort)"

KEEP="$(python3 "${KEEP_PY}" <<< "${ALL}")"}
KEEP="$(python3 "${KEEP_PY}" "${NOW}" <<< "${ALL}")"

DELETE="$(comm -23 <(echo "${ALL}") <(echo "${KEEP}"))"

Expand Down
179 changes: 179 additions & 0 deletions .github/workflows/ci:build:image:backup-retention-policy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:

timeout-minutes: 15

outputs:
image: ${{ steps.build-push.outputs.image }}

Check failure on line 34 in .github/workflows/ci:build:image:backup-retention-policy.yml

View workflow job for this annotation

GitHub Actions / Continuous integration (lint workflows)

property "build-push" is not defined in object type {}

steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
Expand All @@ -46,3 +49,179 @@ jobs:
with:
file: .deploy/backup-retention-policy/Dockerfile
package: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

test:
name: Continuous integration (build image backup-retention-policy test)

needs:
- build

strategy:
matrix:
platform:
- ubuntu-latest

runs-on: ${{ matrix.platform }}

timeout-minutes: 3

steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
.github/actions/docker/container/is-running/action.yml
sparse-checkout-cone-mode: false

- name: Get image URL
id: image-url
env:
IMAGE_METADATA: ${{ needs.build.outputs.image }}
run: |
IMAGE_URL="$(echo "${IMAGE_METADATA}" | jq -r 'first(.tags[] | select(test(":sha-[0-9a-f]{40}$")))')"
echo "url=${IMAGE_URL}" >> "${GITHUB_OUTPUT}"
test -n "${IMAGE_URL}"
- name: Configure
id: configure
run: |
echo "schedule=$(date -d "+1 minutes 10 seconds" '+%M %H * * *')" >> "${GITHUB_OUTPUT}"
- name: Create test backups directory
run: |
mkdir backups
touch backups/2021-01-01_12:34:56
touch backups/2022-01-01_12:34:56
touch backups/2022-01-31_12:34:56
touch backups/2023-01-11_12:34:56
touch backups/2023-01-22_12:34:56
touch backups/2023-02-01_12:34:56
touch backups/2023-02-02_12:34:56
touch backups/2023-02-03_12:34:56
touch backups/2023-02-04_12:34:56
touch backups/2023-02-05_12:34:56
touch backups/2023-02-06_12:34:56
touch backups/2023-02-07_12:34:56
touch backups/2023-02-08_12:34:56
touch backups/2023-02-09_12:34:56
touch backups/2023-02-10_12:34:56
touch backups/2026-02-04_12:34:56
touch backups/2026-02-04_12:34:57
touch backups/2026-03-04_12:34:56
touch backups/2027-06-05_12:34:56
- name: Run server
env:
NOW: 2023-02-04_12:34:56
BACKUP_RETENTION_POLICY_SCHEDULE: ${{ steps.configure.outputs.schedule }}
run: |

Check failure on line 116 in .github/workflows/ci:build:image:backup-retention-policy.yml

View workflow job for this annotation

GitHub Actions / Continuous integration (lint workflows)

shellcheck reported issue in this script: SC2215:warning:7:3: This flag is used as a command name. Bad line break or missing

Check failure on line 116 in .github/workflows/ci:build:image:backup-retention-policy.yml

View workflow job for this annotation

GitHub Actions / Continuous integration (lint workflows)

property "server-image-url" is not defined in object type {configure: {conclusion: string; outcome: string; outputs: {string => string}}; image-url: {conclusion: string; outcome: string; outputs: {string => string}}}
docker container run \
--init \
--detach \
--env "NOW=${NOW}" \
--env "BACKUP_RETENTION_POLICY_SCHEDULE=${BACKUP_RETENTION_POLICY_SCHEDULE}" \
--mount type=bind,src=backups/,dst=/backups
--name container \
${{ steps.server-image-url.outputs.url }}
- name: Wait for container to be running
uses: ./.github/actions/docker/container/is-running
with:
container: container
timeout: 60

- name: Show docker containers
if: always()
run: |
docker ps -a
- name: Show container logs
if: always()
run: |
docker container logs --timestamps container
- name: Verify backup files
env:
DELAY: 60
INTERVAL: 5
TIMEOUT: 120
run: |
echo "Waiting ${DELAY} seconds before starting checks..."
sleep "${DELAY}"
cat << EOF > expected.txt
backups/2021-01-01_12:34:56
backups/2022-01-31_12:34:56
backups/2023-01-22_12:34:56
backups/2023-02-05_12:34:56
backups/2023-02-06_12:34:56
backups/2023-02-07_12:34:56
backups/2023-02-08_12:34:56
backups/2023-02-09_12:34:56
backups/2023-02-10_12:34:56
backups/2026-02-04_12:34:56
backups/2026-02-04_12:34:57
backups/2026-03-04_12:34:56
backups/2027-06-05_12:34:56
EOF
sort expected.txt > expected_sorted.txt
start_time=$(date +%s)
while true; do
echo "Checking backup files (attempt duration: $(($(date +%s) - start_time))s)"
if [ ! -d "backups" ]; then
echo "Backups directory not found - waiting..."
else
find backups -type f | sort > actual_sorted.txt
if diff -q expected_sorted.txt actual_sorted.txt > /dev/null; then
echo "✅ All backup files verified!"
exit 0
else
echo "⚠️ File mismatch detected - retrying..."
diff -y expected_sorted.txt actual_sorted.txt || true
fi
fi
if [ $(($(date +%s) - start_time)) -ge ${TIMEOUT} ]; then
echo "❌ Timeout reached after ${TIMEOUT} seconds"
exit 1
fi
sleep "${INTERVAL}"
done
- name: Show docker containers
if: always()
run: |
docker ps -a
- name: Show container logs
if: always()
run: |
docker container logs --timestamps container
- name: Stop container
run: |
docker container stop container
- name: Show docker containers
if: always()
run: |
docker ps -a
- name: Show container logs
if: always()
run: |
docker container logs --timestamps container
- name: Remove container
run: |
docker container rm container
- name: Show docker containers
if: always()
run: |
docker ps -a

0 comments on commit ad9eb1a

Please sign in to comment.