From 02a18bdd78d91c45f92ab3095e6c6cdffe54a0fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Wed, 31 May 2023 19:58:56 -0300 Subject: [PATCH] Added tests for post-checkout with compression --- tests/post-checkout-tgz.bats | 157 ++++++++++++++++++++++++++++++++++ tests/post-checkout-zip.bats | 158 +++++++++++++++++++++++++++++++++++ 2 files changed, 315 insertions(+) create mode 100644 tests/post-checkout-tgz.bats create mode 100644 tests/post-checkout-zip.bats diff --git a/tests/post-checkout-tgz.bats b/tests/post-checkout-tgz.bats new file mode 100644 index 0000000..9e07e57 --- /dev/null +++ b/tests/post-checkout-tgz.bats @@ -0,0 +1,157 @@ +#!/usr/bin/env bats + +# To debug stubs, uncomment these lines: +# export CACHE_DUMMY_STUB_DEBUG=/dev/tty +# export TAR_STUB_DEBUG=/dev/tty + +setup() { + load "${BATS_PLUGIN_PATH}/load.bash" + + mkdir -p tests/data/my_files + echo "all the llamas" > "tests/data/my_files/llamas.txt" + echo "no alpacas" > "tests/data/my_files/alpacas.txt" + + export BUILDKITE_PLUGIN_CACHE_BACKEND=dummy + export BUILDKITE_PLUGIN_CACHE_COMPRESSION=tgz + export BUILDKITE_PLUGIN_CACHE_PATH=tests/data/my_files + export BUILDKITE_PLUGIN_CACHE_MANIFEST=tests/data/my_files/llamas.txt + + # necessary for key-calculations + export BUILDKITE_LABEL="step-label" + export BUILDKITE_BRANCH="tests" + export BUILDKITE_ORGANIZATION_SLUG="bk-cache-test" + export BUILDKITE_PIPELINE_SLUG="cache-pipeline" + + # stub is the same for all tests + stub tar \ + "xzf \* \* : echo uncompressed \$2 into \$3" +} + +teardown() { + rm -rf tests/data + + unstub tar +} + +@test 'Existing file-based restore' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=file + + stub cache_dummy \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at file level' + assert_output --partial 'Cache is compressed, uncompressing...' + + unstub cache_dummy +} + +@test 'Existing file-based restore even when max-level is higher' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=all + + stub cache_dummy \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at file level' + assert_output --partial 'Cache is compressed, uncompressing...' + + unstub cache_dummy +} + +@test 'Existing step-based restore' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=step + + stub cache_dummy \ + 'exists \* : exit 1' \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at step level' + assert_output --partial 'Cache is compressed, uncompressing...' + + unstub cache_dummy +} + +@test 'Existing branch-based restore' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=branch + + stub cache_dummy \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at branch level' + assert_output --partial 'Cache is compressed, uncompressing...' + + unstub cache_dummy +} +@test 'Existing pipeline-based restore' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=pipeline + + stub cache_dummy \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at pipeline level' + assert_output --partial 'Cache is compressed, uncompressing...' + + unstub cache_dummy +} + +@test 'Existing all-based restore' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=all + + stub cache_dummy \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at all level' + assert_output --partial 'Cache is compressed, uncompressing...' + + unstub cache_dummy +} + +@test 'Existing lower level restore works' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=all + + stub cache_dummy \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at branch level' + assert_output --partial 'Cache is compressed, uncompressing...' + + unstub cache_dummy +} \ No newline at end of file diff --git a/tests/post-checkout-zip.bats b/tests/post-checkout-zip.bats new file mode 100644 index 0000000..c5cbb3d --- /dev/null +++ b/tests/post-checkout-zip.bats @@ -0,0 +1,158 @@ +#!/usr/bin/env bats + +# To debug stubs, uncomment these lines: +# export CACHE_DUMMY_STUB_DEBUG=/dev/tty +# export UNZIP_STUB_DEBUG=/dev/tty + +setup() { + load "${BATS_PLUGIN_PATH}/load.bash" + + mkdir -p tests/data/my_files + echo "all the llamas" > "tests/data/my_files/llamas.txt" + echo "no alpacas" > "tests/data/my_files/alpacas.txt" + + export BUILDKITE_PLUGIN_CACHE_BACKEND=dummy + export BUILDKITE_PLUGIN_CACHE_COMPRESSION=zip + export BUILDKITE_PLUGIN_CACHE_PATH=tests/data/my_files + export BUILDKITE_PLUGIN_CACHE_MANIFEST=tests/data/my_files/llamas.txt + + # necessary for key-calculations + export BUILDKITE_LABEL="step-label" + export BUILDKITE_BRANCH="tests" + export BUILDKITE_ORGANIZATION_SLUG="bk-cache-test" + export BUILDKITE_PIPELINE_SLUG="cache-pipeline" + + # stub is the same for all tests + stub unzip \ + "\* \* : echo uncompressed \$2 into \$3" +} + +teardown() { + rm -rf tests/data + + unstub unzip +} + +@test 'Existing file-based restore' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=file + + stub cache_dummy \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at file level' + assert_output --partial "Cache is compressed, uncompressing..." + + unstub cache_dummy +} + +@test 'Existing file-based restore even when max-level is higher' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=all + + stub cache_dummy \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at file level' + assert_output --partial "Cache is compressed, uncompressing..." + + unstub cache_dummy +} + +@test 'Existing step-based restore' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=step + + stub cache_dummy \ + 'exists \* : exit 1' \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at step level' + assert_output --partial "Cache is compressed, uncompressing..." + + unstub cache_dummy +} + +@test 'Existing branch-based restore' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=branch + + stub cache_dummy \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at branch level' + assert_output --partial "Cache is compressed, uncompressing..." + + unstub cache_dummy +} +@test 'Existing pipeline-based restore' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=pipeline + + stub cache_dummy \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at pipeline level' + assert_output --partial "Cache is compressed, uncompressing..." + + unstub cache_dummy +} + +@test 'Existing all-based restore' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=all + + stub cache_dummy \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at all level' + assert_output --partial "Cache is compressed, uncompressing..." + + unstub cache_dummy +} + + +@test 'Existing lower level restore works' { + export BUILDKITE_PLUGIN_CACHE_RESTORE=all + + stub cache_dummy \ + 'exists \* : exit 1' \ + 'exists \* : exit 1' \ + 'exists \* : exit 0' \ + "get \* \* : echo restoring \$2 to \$3" + + run "$PWD/hooks/post-checkout" + + assert_success + assert_output --partial 'Cache hit at branch level' + assert_output --partial "Cache is compressed, uncompressing..." + + unstub cache_dummy +} \ No newline at end of file