From 64808fbc26b41c26f00f6bcc5a56b065443b407c Mon Sep 17 00:00:00 2001 From: Assem Hasna Date: Thu, 12 Oct 2023 11:36:53 +0200 Subject: [PATCH 1/5] =?UTF-8?q?refactor:=20engine=20preinst=20and=20add=20?= =?UTF-8?q?postinst=20=F0=9F=98=B5=E2=80=8D=F0=9F=92=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/_10_test.yml | 9 +- engine/Cargo.toml | 4 +- ....service => chainflip-engine-perv.service} | 2 +- engine/package/perseverance/postinst | 54 ++++++++++ engine/package/perseverance/preinst | 99 +++++++++-------- .../preinst-tests/mocks/mock_commands.sh | 4 +- ...st.bats => test_preinst_perseverance.bats} | 26 ++--- .../tests/test_preinst_sisyphos.bats | 70 ++++++++++++ ....service => chainflip-engine-prev.service} | 2 +- engine/package/sisyphos/postinst | 54 ++++++++++ engine/package/sisyphos/preinst | 101 ++++++++++++++++++ state-chain/node/package/perseverance/preinst | 94 ---------------- ...st.bats => test_preinst_perseverance.bats} | 0 .../tests/test_preinst_sisyphos.bats | 61 +++++++++++ 14 files changed, 423 insertions(+), 157 deletions(-) rename engine/package/perseverance/{chainflip-engine-lsr.service => chainflip-engine-perv.service} (81%) create mode 100755 engine/package/perseverance/postinst rename engine/package/preinst-tests/tests/{test_preinst.bats => test_preinst_perseverance.bats} (62%) create mode 100644 engine/package/preinst-tests/tests/test_preinst_sisyphos.bats rename engine/package/sisyphos/{chainflip-engine-lsr.service => chainflip-engine-prev.service} (75%) create mode 100755 engine/package/sisyphos/postinst create mode 100755 engine/package/sisyphos/preinst delete mode 100755 state-chain/node/package/perseverance/preinst rename state-chain/node/package/preinst-tests/tests/{test_preinst.bats => test_preinst_perseverance.bats} (100%) create mode 100644 state-chain/node/package/preinst-tests/tests/test_preinst_sisyphos.bats diff --git a/.github/workflows/_10_test.yml b/.github/workflows/_10_test.yml index 0f9c9721ad..94af445224 100644 --- a/.github/workflows/_10_test.yml +++ b/.github/workflows/_10_test.yml @@ -67,6 +67,9 @@ jobs: tests_path: - state-chain/node/package/preinst-tests - engine/package/preinst-tests + variant: + - perseverance + - sisyphos steps: - name: Checkout ๐Ÿ uses: actions/checkout@v3 @@ -79,5 +82,9 @@ jobs: - name: Test ๐Ÿงช shell: bash run: | - bats tests/test_preinst.bats + if [[ -f "../${{ matrix.variant }}/preinst" ]]; then + bats tests/test_preinst.bats + else + echo "No preinst script found. Skipping preinst tests." + fi working-directory: ${{ matrix.tests_path }} diff --git a/engine/Cargo.toml b/engine/Cargo.toml index 2d151af5ea..3de548baa9 100644 --- a/engine/Cargo.toml +++ b/engine/Cargo.toml @@ -16,9 +16,9 @@ priority = "required" section = "rust" systemd-units = [ { unit-name = "chainflip-engine", enable = false }, - { unit-name = "chainflip-engine-lsr", enable = false } + { unit-name = "chainflip-engine-prev", enable = false } ] -assets = [["target/release/chainflip-engine", "usr/bin/chainflip-engine-lsr", "755"]] +assets = [["target/release/chainflip-engine", "usr/bin/", "755"]] [package.metadata.deb.variants.development] maintainer-scripts = "package/development" diff --git a/engine/package/perseverance/chainflip-engine-lsr.service b/engine/package/perseverance/chainflip-engine-perv.service similarity index 81% rename from engine/package/perseverance/chainflip-engine-lsr.service rename to engine/package/perseverance/chainflip-engine-perv.service index fb21504fad..7f8c1c2fbf 100644 --- a/engine/package/perseverance/chainflip-engine-lsr.service +++ b/engine/package/perseverance/chainflip-engine-perv.service @@ -7,7 +7,7 @@ Restart=always RestartSec=30 Type=simple -ExecStart=/usr/bin/chainflip-engine-lsr --config-root /etc/chainflip/ +ExecStart=/usr/bin/chainflip-engine-prev --config-root /etc/chainflip/ Environment=RUST_LOG="chainflip_engine=debug,multisig=debug,warn,chainflip_engine::p2p=error" LimitNOFILE=16000 LimitNOFILESoft=16000 diff --git a/engine/package/perseverance/postinst b/engine/package/perseverance/postinst new file mode 100755 index 0000000000..f0088f7ee0 --- /dev/null +++ b/engine/package/perseverance/postinst @@ -0,0 +1,54 @@ +#!/bin/bash +set -e + +check_upgrade() { + if [ "$1" != "upgrade" ]; then + echo "chainflip-engine: Fresh install detected, skipping migration" + exit 0 + else + echo "chainflip-engine: Upgrade detected, migrating" + fi +} + +enable_service() { + if systemctl is-enabled --quiet "$1"; then + echo "$1 is already enabled" + else + if systemctl enable "$1"; then + echo "$1 enabled" + else + echo "Error enabling $1" + exit 1 + fi + fi +} + +start_service() { + if systemctl is-active --quiet $1; then + echo "$1 is already running" + else + if systemctl start $1; then + echo "$1 started" + else + echo "Error starting $1" + exit 1 + fi + fi +} + +# Main entry point +main() { + check_upgrade "$1" + enable_service "chainflip-engine" + enable_service "chainflip-engine-prev" + start_service "chainflip-engine" + start_service "chainflip-engine-prev" +} + +# Only run the main function if the script is not being sourced +# This allows you to source the script in your tests without running main +if [ "${BASH_SOURCE[0]}" = "${0}" ]; then + main "$@" +fi + +# #DEBHELPER# diff --git a/engine/package/perseverance/preinst b/engine/package/perseverance/preinst index fff2c8898d..6f5fb90fdc 100755 --- a/engine/package/perseverance/preinst +++ b/engine/package/perseverance/preinst @@ -2,9 +2,7 @@ set -e CF_ROOT=/etc/chainflip -CF_ENGINE_TARGET_VERSION="0.9" -SYSTEMD_PATH=/etc/systemd/system -CF_ENGINE_CONFIG_FILE="$CF_ROOT/config/Settings.toml" +CF_ENGINE_TARGET_VERSION="0.10.0" check_upgrade() { if [ "$1" != "upgrade" ]; then @@ -16,67 +14,82 @@ check_upgrade() { } stop_service() { - if systemctl is-active --quiet chainflip-engine; then - if systemctl stop chainflip-engine; then - echo "chainflip-engine stopped" + if systemctl is-active --quiet "$1"; then + if systemctl stop "$1"; then + echo "$1 stopped" else - echo "Error stopping chainflip-engine" + echo "Error stopping $1" exit 1 fi else - echo "chainflip-engine is already stopped" + echo "$1 is already stopped" fi } -check_version() { - CF_NODE_CURRENT_VERSION=$(chainflip-engine -V) - if echo "$CF_NODE_CURRENT_VERSION" | grep $CF_ENGINE_TARGET_VERSION > /dev/null; then - echo "chainflip-engine: skipping migration" - exit 0 - else - echo "chainflip-engine: Detected older version, migrating" - fi -} +compare_semver() { + # Extract SemVer using Regular Expressions + extract_semver() { + if [[ $1 =~ ([0-9]+\.[0-9]+\.[0-9]+) ]]; then + echo "${BASH_REMATCH[1]}" + else + echo "" + fi + } + ver1_raw="$1" + ver2_raw="$2" -backup_and_remove_systemd_overrides() { - if [ -f $SYSTEMD_PATH/chainflip-engine.service.d/override.conf ]; then - echo "chainflip-engine: systemd overrides found - backing up and removing" - mv $SYSTEMD_PATH/chainflip-engine.service.d/override.conf $SYSTEMD_PATH/chainflip-engine.service.d.bak/override.conf - fi -} + ver1=$(extract_semver "$ver1_raw") + ver2=$(extract_semver "$ver2_raw") -remove_old_keyshares() { - if [ -d "$CF_ROOT/data.db" ]; then - echo "chainflip-engine: Removing old data.db directory" - rm -rf "$CF_ROOT/data.db" - fi + # Split versions into major, minor, and patch numbers + IFS='.' read -ra ver1_parts <<< "$ver1" + IFS='.' read -ra ver2_parts <<< "$ver2" + + # Compare major, minor, and patch numbers + for i in 0 1 2; do + if [[ -z "${ver1_parts[$i]}" ]]; then ver1_parts[$i]=0; fi + if [[ -z "${ver2_parts[$i]}" ]]; then ver2_parts[$i]=0; fi + + if (( ${ver1_parts[$i]} > ${ver2_parts[$i]} )); then + echo 1 + return + elif (( ${ver1_parts[$i]} < ${ver2_parts[$i]} )); then + echo -1 + return + fi + done + # If we reach here, the versions are equal so far. You can extend this + # to handle pre-release labels and build metadata if needed. + echo 0 } -remove_old_logs() { - if [ -f "/var/log/chainflip-engine.log" ]; then - echo "chainflip-engine: Removing old log file" - rm -rf "/var/log/chainflip-engine*" +check_version() { + CF_ENGINE_CURRENT_VERSION=$(chainflip-engine -V) + result=$(compare_semver "$CF_ENGINE_CURRENT_VERSION" "$CF_ENGINE_TARGET_VERSION") + if (( result == 1 )); then + echo "Current version is greater than target version, skipping migration" + exit 0 + elif (( result == -1 )); then + echo "Current version is less than target version, migrating" + else + echo "Both versions are equal. Skipping migration" + exit 0 fi } -update_engine_config() { - echo "chainflip-engine: Updating engine config" +move_binary() { + echo "chainflip-engine: Moving binary" + mv /usr/bin/chainflip-engine /usr/bin/chainflip-engine-prev } -reload_systemctl() { - systemctl daemon-reload -} # Main entry point main() { check_upgrade "$1" check_version - stop_service - remove_old_keyshares - backup_and_remove_systemd_overrides - remove_old_logs - update_engine_config - reload_systemctl + # stop_service "chainflip-engine-prev" #TODO: for next release, stop chainflip-engine-prev too + stop_service "chainflip-engine" + move_binary } # Only run the main function if the script is not being sourced diff --git a/engine/package/preinst-tests/mocks/mock_commands.sh b/engine/package/preinst-tests/mocks/mock_commands.sh index 77af2687b4..5c71203d49 100644 --- a/engine/package/preinst-tests/mocks/mock_commands.sh +++ b/engine/package/preinst-tests/mocks/mock_commands.sh @@ -2,7 +2,7 @@ SERVICE_STATUS="active" systemctl() { - if [ "$1" = "is-active" ] && [ "$3" = "chainflip-engine" ]; then + if [ "$1" = "is-active" ]; then if [ "$SERVICE_STATUS" = "active" ]; then return 0 else @@ -10,7 +10,7 @@ systemctl() { fi fi - if [ "$1" = "stop" ] && [ "$2" = "chainflip-engine" ]; then + if [ "$1" = "stop" ]; then return 0 # assume it's successful fi diff --git a/engine/package/preinst-tests/tests/test_preinst.bats b/engine/package/preinst-tests/tests/test_preinst_perseverance.bats similarity index 62% rename from engine/package/preinst-tests/tests/test_preinst.bats rename to engine/package/preinst-tests/tests/test_preinst_perseverance.bats index dac104e2bb..73152c938a 100644 --- a/engine/package/preinst-tests/tests/test_preinst.bats +++ b/engine/package/preinst-tests/tests/test_preinst_perseverance.bats @@ -9,7 +9,7 @@ source 'tests/setup.bats' source ../perseverance/preinst # Run the function - result=$(stop_service) + result=$(stop_service "chainflip-engine") # Assert on the result [ "$result" = "chainflip-engine stopped" ] } @@ -21,7 +21,7 @@ source 'tests/setup.bats' source ../perseverance/preinst # Run the function - result=$(stop_service 2>&1) + result=$(stop_service "chainflip-engine" 2>&1) # Assert on the result [ "$result" = "chainflip-engine is already stopped" ] } @@ -42,29 +42,29 @@ source 'tests/setup.bats' [ "$result" = "chainflip-engine: Upgrade detected, migrating" ] } -@test "Migration runs if local minor version doesn't match target" { - VERSION_OUTPUT="chainflip-engine 0.8.1-7989b2cb5e4" +@test "Migration runs if current version is less than target" { + VERSION_OUTPUT="chainflip-engine 0.9.3-7989b2cb5e4" source ../perseverance/preinst result=$(check_version) - [ "$result" = "chainflip-engine: Detected older version, migrating" ] + [ "$result" = "Current version is less than target version, migrating" ] } -@test "Migration doesn't run if local minor version matchs target" { - VERSION_OUTPUT="chainflip-engine 0.9.1-7989b2cb5e4" +@test "Migration doesn't run if current version is greater than target" { + VERSION_OUTPUT="chainflip-engine 0.11.0-7989b2cb5e4" source ../perseverance/preinst result=$(check_version) echo $result - [ "$result" = "chainflip-engine: skipping migration" ] + [ "$result" = "Current version is greater than target version, skipping migration" ] } -@test "Update Engine Config does what it is supposed to do" { - # TODO: Add real test once we add functionality to update engine config. - source ../perseverance/preinst +@test "Migration doesn't run if current version is equal to target" { + VERSION_OUTPUT="chainflip-engine 0.10.0-7989b2cb5e4" - result=$(update_engine_config) + source ../perseverance/preinst + result=$(check_version) - [ "$result" = "chainflip-engine: Updating engine config" ] + [ "$result" = "Both versions are equal. Skipping migration" ] } diff --git a/engine/package/preinst-tests/tests/test_preinst_sisyphos.bats b/engine/package/preinst-tests/tests/test_preinst_sisyphos.bats new file mode 100644 index 0000000000..22de1338b7 --- /dev/null +++ b/engine/package/preinst-tests/tests/test_preinst_sisyphos.bats @@ -0,0 +1,70 @@ +#!/usr/bin/env bats + +source 'tests/setup.bats' + +@test "Check when chainflip-engine service is active" { + # Set the mock to behave as if the service is active + SERVICE_STATUS="active" + + source ../sisyphos/preinst + + # Run the function + result=$(stop_service "chainflip-engine") + # Assert on the result + [ "$result" = "chainflip-engine stopped" ] +} + +@test "Check when chainflip-engine service is inactive" { + # Set the mock to behave as if the service is inactive + SERVICE_STATUS="inactive" + + source ../sisyphos/preinst + + # Run the function + result=$(stop_service "chainflip-engine" 2>&1) + # Assert on the result + [ "$result" = "chainflip-engine is already stopped" ] +} + +@test "Preinstall script doesn't run on fresh install" { + source ../sisyphos/preinst + + result=$(check_upgrade) + + [ "$result" = "chainflip-engine: Fresh install detected, skipping migration" ] +} + +@test "Preinstall script runs on upgrade" { + source ../sisyphos/preinst + + result=$(check_upgrade "upgrade") + + [ "$result" = "chainflip-engine: Upgrade detected, migrating" ] +} + +@test "Migration runs if current version is less than target" { + VERSION_OUTPUT="chainflip-engine 0.9.3-7989b2cb5e4" + + source ../sisyphos/preinst + result=$(check_version) + + [ "$result" = "Current version is less than target version, migrating" ] +} + +@test "Migration doesn't run if current version is greater than target" { + VERSION_OUTPUT="chainflip-engine 0.11.0-7989b2cb5e4" + + source ../sisyphos/preinst + result=$(check_version) + echo $result + [ "$result" = "Current version is greater than target version, skipping migration" ] +} + +@test "Migration doesn't run if current version is equal to target" { + VERSION_OUTPUT="chainflip-engine 0.10.0-7989b2cb5e4" + + source ../sisyphos/preinst + result=$(check_version) + + [ "$result" = "Both versions are equal. Skipping migration" ] +} diff --git a/engine/package/sisyphos/chainflip-engine-lsr.service b/engine/package/sisyphos/chainflip-engine-prev.service similarity index 75% rename from engine/package/sisyphos/chainflip-engine-lsr.service rename to engine/package/sisyphos/chainflip-engine-prev.service index b090e754ea..cb86f67235 100644 --- a/engine/package/sisyphos/chainflip-engine-lsr.service +++ b/engine/package/sisyphos/chainflip-engine-prev.service @@ -7,7 +7,7 @@ Restart=always RestartSec=30 Type=simple -ExecStart=/usr/bin/chainflip-engine-lsr --config-root /etc/chainflip/ +ExecStart=/usr/bin/chainflip-engine-prev --config-root /etc/chainflip/ LimitNOFILE=16000 LimitNOFILESoft=16000 diff --git a/engine/package/sisyphos/postinst b/engine/package/sisyphos/postinst new file mode 100755 index 0000000000..f0088f7ee0 --- /dev/null +++ b/engine/package/sisyphos/postinst @@ -0,0 +1,54 @@ +#!/bin/bash +set -e + +check_upgrade() { + if [ "$1" != "upgrade" ]; then + echo "chainflip-engine: Fresh install detected, skipping migration" + exit 0 + else + echo "chainflip-engine: Upgrade detected, migrating" + fi +} + +enable_service() { + if systemctl is-enabled --quiet "$1"; then + echo "$1 is already enabled" + else + if systemctl enable "$1"; then + echo "$1 enabled" + else + echo "Error enabling $1" + exit 1 + fi + fi +} + +start_service() { + if systemctl is-active --quiet $1; then + echo "$1 is already running" + else + if systemctl start $1; then + echo "$1 started" + else + echo "Error starting $1" + exit 1 + fi + fi +} + +# Main entry point +main() { + check_upgrade "$1" + enable_service "chainflip-engine" + enable_service "chainflip-engine-prev" + start_service "chainflip-engine" + start_service "chainflip-engine-prev" +} + +# Only run the main function if the script is not being sourced +# This allows you to source the script in your tests without running main +if [ "${BASH_SOURCE[0]}" = "${0}" ]; then + main "$@" +fi + +# #DEBHELPER# diff --git a/engine/package/sisyphos/preinst b/engine/package/sisyphos/preinst new file mode 100755 index 0000000000..6f5fb90fdc --- /dev/null +++ b/engine/package/sisyphos/preinst @@ -0,0 +1,101 @@ +#!/bin/bash +set -e + +CF_ROOT=/etc/chainflip +CF_ENGINE_TARGET_VERSION="0.10.0" + +check_upgrade() { + if [ "$1" != "upgrade" ]; then + echo "chainflip-engine: Fresh install detected, skipping migration" + exit 0 + else + echo "chainflip-engine: Upgrade detected, migrating" + fi +} + +stop_service() { + if systemctl is-active --quiet "$1"; then + if systemctl stop "$1"; then + echo "$1 stopped" + else + echo "Error stopping $1" + exit 1 + fi + else + echo "$1 is already stopped" + fi +} + +compare_semver() { + # Extract SemVer using Regular Expressions + extract_semver() { + if [[ $1 =~ ([0-9]+\.[0-9]+\.[0-9]+) ]]; then + echo "${BASH_REMATCH[1]}" + else + echo "" + fi + } + ver1_raw="$1" + ver2_raw="$2" + + ver1=$(extract_semver "$ver1_raw") + ver2=$(extract_semver "$ver2_raw") + + # Split versions into major, minor, and patch numbers + IFS='.' read -ra ver1_parts <<< "$ver1" + IFS='.' read -ra ver2_parts <<< "$ver2" + + # Compare major, minor, and patch numbers + for i in 0 1 2; do + if [[ -z "${ver1_parts[$i]}" ]]; then ver1_parts[$i]=0; fi + if [[ -z "${ver2_parts[$i]}" ]]; then ver2_parts[$i]=0; fi + + if (( ${ver1_parts[$i]} > ${ver2_parts[$i]} )); then + echo 1 + return + elif (( ${ver1_parts[$i]} < ${ver2_parts[$i]} )); then + echo -1 + return + fi + done + # If we reach here, the versions are equal so far. You can extend this + # to handle pre-release labels and build metadata if needed. + echo 0 +} + +check_version() { + CF_ENGINE_CURRENT_VERSION=$(chainflip-engine -V) + result=$(compare_semver "$CF_ENGINE_CURRENT_VERSION" "$CF_ENGINE_TARGET_VERSION") + if (( result == 1 )); then + echo "Current version is greater than target version, skipping migration" + exit 0 + elif (( result == -1 )); then + echo "Current version is less than target version, migrating" + else + echo "Both versions are equal. Skipping migration" + exit 0 + fi +} + +move_binary() { + echo "chainflip-engine: Moving binary" + mv /usr/bin/chainflip-engine /usr/bin/chainflip-engine-prev +} + + +# Main entry point +main() { + check_upgrade "$1" + check_version + # stop_service "chainflip-engine-prev" #TODO: for next release, stop chainflip-engine-prev too + stop_service "chainflip-engine" + move_binary +} + +# Only run the main function if the script is not being sourced +# This allows you to source the script in your tests without running main +if [ "${BASH_SOURCE[0]}" = "${0}" ]; then + main "$@" +fi + +# #DEBHELPER# diff --git a/state-chain/node/package/perseverance/preinst b/state-chain/node/package/perseverance/preinst deleted file mode 100755 index ee892f8048..0000000000 --- a/state-chain/node/package/perseverance/preinst +++ /dev/null @@ -1,94 +0,0 @@ -#!/bin/bash -set -e - -CF_ROOT=/etc/chainflip -CF_NODE_TARGET_VERSION="0.9" -SYSTEMD_PATH=/etc/systemd/system - -check_upgrade() { - if [ "$1" != "upgrade" ]; then - echo "chainflip-node: Fresh install detected, skipping migration" - exit 0 - else - echo "chainflip-node: Upgrade detected, migrating" - fi -} - -stop_service() { - if systemctl is-active --quiet chainflip-node; then - if systemctl stop chainflip-node; then - echo "chainflip-node stopped" - else - echo "Error stopping chainflip-node" - exit 1 - fi - else - echo "chainflip-node is already stopped" - fi -} - - -check_version() { - CF_NODE_CURRENT_VERSION=$(chainflip-node -V) - if echo "$CF_NODE_CURRENT_VERSION" | grep $CF_NODE_TARGET_VERSION > /dev/null; then - echo "chainflip-node: skipping migration" - exit 0 - else - echo "chainflip-node: Detected older version, migrating" - fi -} - -backup_and_remove_systemd_overrides() { - if [ -f $SYSTEMD_PATH/chainflip-node.service.d/override.conf ]; then - echo "chainflip-node: systemd overrides found - backing up and removing" - mv $SYSTEMD_PATH/chainflip-node.service.d/override.conf $SYSTEMD_PATH/chainflip-node.service.d.bak/override.conf - fi -} - -remove_old_logs() { - if [ -f "/var/log/chainflip-node.log" ]; then - echo "chainflip-node: Removing old log file" - rm -rf "/var/log/chainflip-node*" - fi -} - -purge_chaindata() { - if [ -d $CF_ROOT/chaindata ]; then - echo "chainflip-node: chaindata found in $CF_ROOT/chaindata - removing" - rm -rf $CF_ROOT/chaindata - fi - - if [ -d $CF_ROOT/data ]; then - echo "chainflip-node: chaindata found in $CF_ROOT/data - removing" - rm -rf $CF_ROOT/data - fi - - if [ -d $CF_ROOT/chainspecs ]; then - echo "chainflip-node: chainspecs found in $CF_ROOT/chainspecs - removing" - rm -rf $CF_ROOT/chainspecs - fi -} - -reload_systemctl() { - systemctl daemon-reload -} - -# Main entry point -main() { - check_upgrade "$1" - check_version - stop_service - backup_and_remove_systemd_overrides - remove_old_logs - purge_chaindata - reload_systemctl - exit 0 -} - -# Only run the main function if the script is not being sourced -# This allows you to source the script in your tests without running main -if [ "${BASH_SOURCE[0]}" = "${0}" ]; then - main "$@" -fi - -# #DEBHELPER# diff --git a/state-chain/node/package/preinst-tests/tests/test_preinst.bats b/state-chain/node/package/preinst-tests/tests/test_preinst_perseverance.bats similarity index 100% rename from state-chain/node/package/preinst-tests/tests/test_preinst.bats rename to state-chain/node/package/preinst-tests/tests/test_preinst_perseverance.bats diff --git a/state-chain/node/package/preinst-tests/tests/test_preinst_sisyphos.bats b/state-chain/node/package/preinst-tests/tests/test_preinst_sisyphos.bats new file mode 100644 index 0000000000..c2940ffa6e --- /dev/null +++ b/state-chain/node/package/preinst-tests/tests/test_preinst_sisyphos.bats @@ -0,0 +1,61 @@ +#!/usr/bin/env bats + +source 'tests/setup.bats' + +@test "Check when chainflip-node service is active" { + # Set the mock to behave as if the service is active + SERVICE_STATUS="active" + + source ../sisyphos/preinst + + # Run the function + result=$(stop_service) + # Assert on the result + [ "$result" = "chainflip-node stopped" ] +} + +@test "Check when chainflip-node service is inactive" { + # Set the mock to behave as if the service is inactive + SERVICE_STATUS="inactive" + + source ../sisyphos/preinst + + # Run the function + result=$(stop_service 2>&1) + # Assert on the result + [ "$result" = "chainflip-node is already stopped" ] +} + +@test "Preinstall script doesn't run on fresh install" { + source ../sisyphos/preinst + + result=$(check_upgrade) + + [ "$result" = "chainflip-node: Fresh install detected, skipping migration" ] +} + +@test "Preinstall script runs on upgrade" { + source ../sisyphos/preinst + + result=$(check_upgrade "upgrade") + + [ "$result" = "chainflip-node: Upgrade detected, migrating" ] +} + +@test "Migration runs if local minor version doesn't match target" { + VERSION_OUTPUT="chainflip-node 0.8.1-7989b2cb5e4" + + source ../sisyphos/preinst + result=$(check_version) + + [ "$result" = "chainflip-node: Detected older version, migrating" ] +} + +@test "Migration doesn't run if local minor version matchs target" { + VERSION_OUTPUT="chainflip-node 0.9.1-7989b2cb5e4" + + source ../sisyphos/preinst + result=$(check_version) + echo $result + [ "$result" = "chainflip-node: skipping migration" ] +} From ea1fb87ada9514752cdd792289f42c89c469b709 Mon Sep 17 00:00:00 2001 From: Assem Hasna Date: Thu, 12 Oct 2023 11:39:10 +0200 Subject: [PATCH 2/5] fix: preinst tests --- .github/workflows/_10_test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/_10_test.yml b/.github/workflows/_10_test.yml index 94af445224..323f01d3df 100644 --- a/.github/workflows/_10_test.yml +++ b/.github/workflows/_10_test.yml @@ -83,7 +83,7 @@ jobs: shell: bash run: | if [[ -f "../${{ matrix.variant }}/preinst" ]]; then - bats tests/test_preinst.bats + bats tests/test_preinst_${{ matrix.variant }}.bats else echo "No preinst script found. Skipping preinst tests." fi From 2fb204345cfbd6af581b73d6c178ddbe509952ec Mon Sep 17 00:00:00 2001 From: Assem Hasna Date: Thu, 12 Oct 2023 12:04:35 +0200 Subject: [PATCH 3/5] chore: remove unused var --- engine/package/perseverance/preinst | 1 - engine/package/sisyphos/preinst | 1 - 2 files changed, 2 deletions(-) diff --git a/engine/package/perseverance/preinst b/engine/package/perseverance/preinst index 6f5fb90fdc..3700e50ed2 100755 --- a/engine/package/perseverance/preinst +++ b/engine/package/perseverance/preinst @@ -1,7 +1,6 @@ #!/bin/bash set -e -CF_ROOT=/etc/chainflip CF_ENGINE_TARGET_VERSION="0.10.0" check_upgrade() { diff --git a/engine/package/sisyphos/preinst b/engine/package/sisyphos/preinst index 6f5fb90fdc..3700e50ed2 100755 --- a/engine/package/sisyphos/preinst +++ b/engine/package/sisyphos/preinst @@ -1,7 +1,6 @@ #!/bin/bash set -e -CF_ROOT=/etc/chainflip CF_ENGINE_TARGET_VERSION="0.10.0" check_upgrade() { From 5171964876728dab8cab4b2198b301c271547b49 Mon Sep 17 00:00:00 2001 From: Assem Hasna Date: Thu, 12 Oct 2023 15:11:17 +0200 Subject: [PATCH 4/5] fix: don't stop service --- engine/package/perseverance/postinst | 2 -- engine/package/perseverance/preinst | 5 ++--- engine/package/sisyphos/postinst | 2 -- engine/package/sisyphos/preinst | 5 ++--- 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/engine/package/perseverance/postinst b/engine/package/perseverance/postinst index f0088f7ee0..df196b1ac2 100755 --- a/engine/package/perseverance/postinst +++ b/engine/package/perseverance/postinst @@ -39,9 +39,7 @@ start_service() { # Main entry point main() { check_upgrade "$1" - enable_service "chainflip-engine" enable_service "chainflip-engine-prev" - start_service "chainflip-engine" start_service "chainflip-engine-prev" } diff --git a/engine/package/perseverance/preinst b/engine/package/perseverance/preinst index 3700e50ed2..7ede70f252 100755 --- a/engine/package/perseverance/preinst +++ b/engine/package/perseverance/preinst @@ -76,7 +76,7 @@ check_version() { fi } -move_binary() { +copy_binary() { echo "chainflip-engine: Moving binary" mv /usr/bin/chainflip-engine /usr/bin/chainflip-engine-prev } @@ -87,8 +87,7 @@ main() { check_upgrade "$1" check_version # stop_service "chainflip-engine-prev" #TODO: for next release, stop chainflip-engine-prev too - stop_service "chainflip-engine" - move_binary + copy_binary } # Only run the main function if the script is not being sourced diff --git a/engine/package/sisyphos/postinst b/engine/package/sisyphos/postinst index f0088f7ee0..df196b1ac2 100755 --- a/engine/package/sisyphos/postinst +++ b/engine/package/sisyphos/postinst @@ -39,9 +39,7 @@ start_service() { # Main entry point main() { check_upgrade "$1" - enable_service "chainflip-engine" enable_service "chainflip-engine-prev" - start_service "chainflip-engine" start_service "chainflip-engine-prev" } diff --git a/engine/package/sisyphos/preinst b/engine/package/sisyphos/preinst index 3700e50ed2..7ede70f252 100755 --- a/engine/package/sisyphos/preinst +++ b/engine/package/sisyphos/preinst @@ -76,7 +76,7 @@ check_version() { fi } -move_binary() { +copy_binary() { echo "chainflip-engine: Moving binary" mv /usr/bin/chainflip-engine /usr/bin/chainflip-engine-prev } @@ -87,8 +87,7 @@ main() { check_upgrade "$1" check_version # stop_service "chainflip-engine-prev" #TODO: for next release, stop chainflip-engine-prev too - stop_service "chainflip-engine" - move_binary + copy_binary } # Only run the main function if the script is not being sourced From b33059da6c92710d2b82fd9406f8712cbbffc149 Mon Sep 17 00:00:00 2001 From: Assem Hasna Date: Thu, 12 Oct 2023 15:12:53 +0200 Subject: [PATCH 5/5] fix: cp instead of mv --- engine/package/perseverance/preinst | 2 +- engine/package/sisyphos/preinst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/engine/package/perseverance/preinst b/engine/package/perseverance/preinst index 7ede70f252..3cebc699e7 100755 --- a/engine/package/perseverance/preinst +++ b/engine/package/perseverance/preinst @@ -78,7 +78,7 @@ check_version() { copy_binary() { echo "chainflip-engine: Moving binary" - mv /usr/bin/chainflip-engine /usr/bin/chainflip-engine-prev + cp /usr/bin/chainflip-engine /usr/bin/chainflip-engine-prev } diff --git a/engine/package/sisyphos/preinst b/engine/package/sisyphos/preinst index 7ede70f252..3cebc699e7 100755 --- a/engine/package/sisyphos/preinst +++ b/engine/package/sisyphos/preinst @@ -78,7 +78,7 @@ check_version() { copy_binary() { echo "chainflip-engine: Moving binary" - mv /usr/bin/chainflip-engine /usr/bin/chainflip-engine-prev + cp /usr/bin/chainflip-engine /usr/bin/chainflip-engine-prev }