From 16357073324233999a36e5897c0b76f5cb3027e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20V=C3=A9rit=C3=A9?= Date: Tue, 15 Jul 2025 09:36:16 +0200 Subject: [PATCH 1/3] fix(bats): replace random generator for tables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes an issue with: cat /dev/urandom | head -n 1 | base64 | ... When a linefeed character appears in the first few random bytes, the string is too short, and may conflict with other generated short names (actually seen on some invocations of make bats-test...) Use `head -c 12 /dev/urandom` instead, and move it to a dedicated function. Signed-off-by: Daniel Vérité --- scripts/bats/test/libs/partitions.bash | 2 +- scripts/bats/test/libs/seeds.bash | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/bats/test/libs/partitions.bash b/scripts/bats/test/libs/partitions.bash index 2ce1e80..d55c8e1 100644 --- a/scripts/bats/test/libs/partitions.bash +++ b/scripts/bats/test/libs/partitions.bash @@ -52,7 +52,7 @@ generate_daily_partition_name() { } generate_table_name() { - cat /dev/urandom | head -n 1 | base64 | tr -dc '[:alnum:]' | tr '[:upper:]' '[:lower:]' | cut -c -13 | sed -e 's/^[0-9]/a/g' + echo "tbl_"$(random_suffix) } # Generic method to create child partitions with specified ranges diff --git a/scripts/bats/test/libs/seeds.bash b/scripts/bats/test/libs/seeds.bash index 69e84a0..45b4944 100644 --- a/scripts/bats/test/libs/seeds.bash +++ b/scripts/bats/test/libs/seeds.bash @@ -1,3 +1,9 @@ +# Emit a random string suitable for unquoted database identifiers (lower case, ASCII) +# The result may start with a number. +random_suffix() { + head -c 12 /dev/urandom | base64 | tr -dc '[:alnum:]' | tr '[:upper:]' '[:lower:]' +} + init_database() { QUERY="CREATE DATABASE unittest;" execute_sql "${QUERY}" postgres From 8fc1619d723a10c82ddf447badd0c2226256467d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20V=C3=A9rit=C3=A9?= Date: Tue, 15 Jul 2025 09:36:36 +0200 Subject: [PATCH 2/3] test(bats): remove temporary yaml configuration files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Vérité --- scripts/bats/20_check.bats | 3 +++ scripts/bats/30_provisioning.bats | 18 +++++++++++++++++- scripts/bats/40_cleanup.bats | 2 ++ scripts/bats/test/libs/seeds.bash | 4 ++-- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/scripts/bats/20_check.bats b/scripts/bats/20_check.bats index 72c7761..721c081 100644 --- a/scripts/bats/20_check.bats +++ b/scripts/bats/20_check.bats @@ -49,6 +49,7 @@ EOF assert_success assert_output --partial "All partitions are correctly configured" + rm "$CONFIGURATION_FILE" } @test "Test check return an error when retention partitions are missing" { @@ -79,6 +80,7 @@ EOF assert_failure assert_output --partial "Found missing tables" + rm "$CONFIGURATION_FILE" } @test "Test check return an error when preProvisioned partitions are missing" { @@ -109,6 +111,7 @@ EOF assert_failure assert_output --partial "Found missing tables" + rm "$CONFIGURATION_FILE" } @test "Test check succeeding with an UUID partition key" { diff --git a/scripts/bats/30_provisioning.bats b/scripts/bats/30_provisioning.bats index c36fae5..32aba39 100644 --- a/scripts/bats/30_provisioning.bats +++ b/scripts/bats/30_provisioning.bats @@ -45,6 +45,8 @@ EOF assert_table_exists public $(generate_daily_partition_name ${TABLE} -1) # retention partition assert_table_exists public $(generate_daily_partition_name ${TABLE} 0) # current partition assert_table_exists public $(generate_daily_partition_name ${TABLE} 1) # preProvisioned partition + + rm "$CONFIGURATION_FILE" } @test "Test that preProvisioned and retention partitions can be increased" { @@ -90,6 +92,8 @@ EOF assert_output --partial "All partitions are correctly provisioned" assert_table_exists public $(generate_daily_partition_name ${TABLE} -${NEW_RETENTION}) # New retention partition assert_table_exists public $(generate_daily_partition_name ${TABLE} ${NEW_PREPROVISIONED}) # New preProvisioned partition + + rm "$CONFIGURATION_FILE" } @test "Test monthly partitions" { @@ -127,6 +131,8 @@ EOF assert_table_exists public ${EXPECTED_LAST_TABLE} assert_table_exists public ${EXPECTED_CURRENT_TABLE} assert_table_exists public ${EXPECTED_NEXT_TABLE} + + rm "$CONFIGURATION_FILE" } @test "Test quarterly partitions" { @@ -157,12 +163,13 @@ EOF run "$PPM_PROG" run provisioning -c ${CONFIGURATION_FILE} - assert_success assert_output --partial "All partitions are correctly provisioned" assert_table_exists public ${EXPECTED_LAST_TABLE} assert_table_exists public ${EXPECTED_CURRENT_TABLE} assert_table_exists public ${EXPECTED_NEXT_TABLE} + + rm "$CONFIGURATION_FILE" } @test "Test yearly partitions" { @@ -198,6 +205,8 @@ EOF assert_table_exists public ${EXPECTED_LAST_TABLE} assert_table_exists public ${EXPECTED_CURRENT_TABLE} assert_table_exists public ${EXPECTED_NEXT_TABLE} + + rm "$CONFIGURATION_FILE" } @test "Test interval change" { @@ -264,6 +273,7 @@ EOF run list_existing_partitions "unittest" "public" ${TABLE} assert_output "$expected_mix" + rm "$CONFIGURATION_FILE" } @test "Test provisioning with multiple partition sets in the configuration" { @@ -315,6 +325,8 @@ EOF run list_existing_partitions "unittest" "public" "table_unittest2" assert_output "$expected2" + + rm "$CONFIGURATION_FILE" } @test "Test that provisioning continues after an error on a partition set" { @@ -363,6 +375,8 @@ EOF run list_existing_partitions "unittest" "public" "${TABLE}" assert_output "$expected2" + + rm "$CONFIGURATION_FILE" } @test "Test a timestamptz key with provisioning crossing a DST transition " { @@ -415,4 +429,6 @@ EOF run list_existing_partitions "unittest" "public" ${TABLE} assert_output "$expected_2" + rm "$CONFIGURATION_FILE" + } diff --git a/scripts/bats/40_cleanup.bats b/scripts/bats/40_cleanup.bats index 13405b5..0e258de 100644 --- a/scripts/bats/40_cleanup.bats +++ b/scripts/bats/40_cleanup.bats @@ -63,6 +63,8 @@ EOF for ((i=NEW_RETENTION +1; i<= INITIAL_PREPROVISIONED; i++));do assert_table_not_exists public $(generate_daily_partition_name ${TABLE} ${i}) done + + rm "$CONFIGURATION_FILE" } @test "Test that gaps in the partition set prevent any partition removal" { diff --git a/scripts/bats/test/libs/seeds.bash b/scripts/bats/test/libs/seeds.bash index 45b4944..e6621b3 100644 --- a/scripts/bats/test/libs/seeds.bash +++ b/scripts/bats/test/libs/seeds.bash @@ -67,8 +67,8 @@ generate_configuration_file() { local FILENAME=$(mktemp).yaml yq '. as $item ireduce ({}; . * $item )' "${CONFIGURATION_TEMPLATE_FILE}" "${TEMPORARY_FILE}" > "${FILENAME}" - - echo $FILENAME + rm "${TEMPORARY_FILE}" + echo "$FILENAME" } # Return a common configuration From 08dbffdec2ce304d683bf966ea65b5adf3b3e8a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20V=C3=A9rit=C3=A9?= Date: Tue, 15 Jul 2025 09:37:06 +0200 Subject: [PATCH 3/3] test(bats): use random database names instead of the hardcoded "unittest" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously if a database named "unittest" pre-existed, it was dropped and recreated unconditionally. Now the tests generate a random database name, and use the environment variable PPM_DATABASE to refer to it. Additionally, PGDATABASE is used for the partition manager instead of a hardcoded URL in the template file. Signed-off-by: Daniel Vérité --- scripts/bats/20_check.bats | 6 ++++- scripts/bats/30_provisioning.bats | 20 ++++++++------ scripts/bats/40_cleanup.bats | 8 ++++-- scripts/bats/configuration/template.yaml | 2 +- scripts/bats/test/libs/partitions.bash | 4 +-- scripts/bats/test/libs/seeds.bash | 10 ++++--- scripts/bats/test/libs/sql.bash | 33 ++++++++++++++---------- 7 files changed, 53 insertions(+), 30 deletions(-) diff --git a/scripts/bats/20_check.bats b/scripts/bats/20_check.bats index 721c081..df25535 100644 --- a/scripts/bats/20_check.bats +++ b/scripts/bats/20_check.bats @@ -5,7 +5,11 @@ load 'test/libs/seeds' load 'test/libs/sql' setup_file() { - reset_database + init_database +} + +teardown_file() { + drop_database } setup() { diff --git a/scripts/bats/30_provisioning.bats b/scripts/bats/30_provisioning.bats index 32aba39..eb2f00e 100644 --- a/scripts/bats/30_provisioning.bats +++ b/scripts/bats/30_provisioning.bats @@ -6,7 +6,11 @@ load 'test/libs/sql' load 'test/libs/time' setup_file() { - reset_database + init_database +} + +teardown_file() { + drop_database } setup() { @@ -237,7 +241,7 @@ EOF assert_success assert_output --partial "All partitions are correctly provisioned" - run list_existing_partitions "unittest" "public" ${TABLE} + run list_existing_partitions "public" ${TABLE} local expected_monthly=$(cat <