Skip to content

Commit

Permalink
chore: add preinst script unit tests 🧪 (#3942)
Browse files Browse the repository at this point in the history
* chore: add `preinst` script unit tests 🧪

* chore: test refactored preinst script in development

* fix: `#DEBHELPER#`tag

* debug: use bash as the preinst interpreter

* chore: add CI for bats tests 🦇

* fix: set shell to bash

* chore: remove `exit 0`

* chore: colorful logs 🌈

* chore: add engine preinst tests 🤝

* chore: add placeholder for updating engine config tests
  • Loading branch information
ahasna authored and dandanlen committed Sep 6, 2023
1 parent 85b2a9c commit 4baedab
Show file tree
Hide file tree
Showing 25 changed files with 422 additions and 105 deletions.
1 change: 1 addition & 0 deletions .github/workflows/_01_pre_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ on:
workflow_call:

env:
FORCE_COLOR: 1
SCCACHE_CACHE_SIZE: 32G
SCCACHE_VERSION: v0.4.1
SCCACHE_REDIS: ${{ secrets.SCCACHE_REDIS }}
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/_02_retrieve-bins.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
on:
workflow_call:

env:
FORCE_COLOR: 1

jobs:
fetch:
strategy:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/_03_check_versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
required: false
default: ${{ github.ref_name }}

env:
FORCE_COLOR: 1

jobs:
check-version:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/_04_merge_commit_summary.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
on:
workflow_call:

env:
FORCE_COLOR: 1

jobs:
build-deb:
runs-on: ubuntu-20.04
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/_100_benchmarks_runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ permissions:
contents: read

env:
FORCE_COLOR: 1
CF_GITHUB_RUNNERS_MANAGEMENT_TOKEN: ${{ secrets.CF_GITHUB_RUNNERS_MANAGEMENT_TOKEN }}

jobs:
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/_100_run_benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ on:
- 4vCPU-8GB
- 4vCPU-16GB

env:
FORCE_COLOR: 1

jobs:
run_benchmarks:
runs-on:
Expand Down Expand Up @@ -65,4 +68,4 @@ jobs:
body: "This pull request was opened automatically. Close it if you think it was opened in error."
branch: auto/benchmark-results
delete-branch: true
token: ${{ secrets.CF_GITHUB_BOT_TOKEN }}
token: ${{ secrets.CF_GITHUB_BOT_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/_10_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
default: true

env:
FORCE_COLOR: 1
RUSTC_WRAPPER: sccache
SCCACHE_CACHE_SIZE: 32G
SCCACHE_VERSION: v0.4.1
Expand Down Expand Up @@ -58,3 +59,25 @@ jobs:
token: ${{ secrets.CF_BACKEND_CODECOV_TOKEN }} # not required for public repos
files: lcov.info
fail_ci_if_error: false

test-preinst-scripts:
runs-on: ubuntu-latest
strategy:
matrix:
tests_path:
- state-chain/node/package/preinst-tests
- engine/package/preinst-tests
steps:
- name: Checkout 🏁
uses: actions/checkout@v3

- name: Install bats 🦇
run: |
sudo apt-get update
sudo apt-get install -y bats
- name: Test 🧪
shell: bash
run: |
bats tests/test_preinst.bats
working-directory: ${{ matrix.tests_path }}
1 change: 1 addition & 0 deletions .github/workflows/_20_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
type: string

env:
FORCE_COLOR: 1
SCCACHE_CACHE_SIZE: 32G
SCCACHE_VERSION: v0.4.1
SCCACHE_REDIS: ${{ secrets.SCCACHE_REDIS }}
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/_24_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ permissions:
contents: read
packages: write

env:
FORCE_COLOR: 1

jobs:
get-date-time:
runs-on: ubuntu-20.04
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/_25_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
description: Environment
required: true
type: string

env:
FORCE_COLOR: 1

jobs:
packages:
runs-on: [hetzner]
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/_30_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:
type: string

env:
FORCE_COLOR: 1
APTLY_VERSION: "1.5.0"
REGISTRY: "ghcr.io"
REPO_REGION: "eu-central-1"
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/_40_post_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ on:
required: false
default: 120

env:
FORCE_COLOR: 1

permissions:
packages: read
contents: read
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/_50_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
type: string
required: true

env:
FORCE_COLOR: 1

jobs:
release:
runs-on: ubuntu-${{ matrix.ubuntu_version }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/_90_helmcharts_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
- name: Linting ${{ matrix.chart }} 📝
run: >
helm lint
charts/${{ matrix.chart }}
charts/${{ matrix.chart }}
2 changes: 1 addition & 1 deletion .github/workflows/ci-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ jobs:
with:
version: ci/${{ github.sha }}/
environment: dev
secrets: inherit
secrets: inherit
3 changes: 2 additions & 1 deletion .github/workflows/ci-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
- cron: '0 0 * * *'

env:
FORCE_COLOR: 1
RUSTC_WRAPPER: sccache
SCCACHE_CACHE_SIZE: 32G
SCCACHE_VERSION: v0.4.1
Expand Down Expand Up @@ -42,4 +43,4 @@ jobs:
with:
version: nightly/
environment: dev
secrets: inherit
secrets: inherit
1 change: 1 addition & 0 deletions .github/workflows/docker-build-rust-base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
type: boolean
default: false
env:
FORCE_COLOR: 1
REGISTRY: ghcr.io
IMAGE_NAME: "${{ github.repository }}/rust-base"

Expand Down
130 changes: 74 additions & 56 deletions engine/package/perseverance/preinst
Original file line number Diff line number Diff line change
@@ -1,70 +1,88 @@
#!/bin/sh
#!/bin/bash
set -e

if [ "$1" != "upgrade" ]; then
echo "chainflip-engine: Fresh install detected, skipping migration"
exit 0
fi

ENGINE_VERSION=$(chainflip-engine -V)

if echo "$ENGINE_VERSION" | grep 0.9; then
echo "chainflip-engine: Detected version 0.9, skipping migration"
exit 0
fi

CF_ROOT=/etc/chainflip
CF_ENGINE_TARGET_VERSION="0.9"
SYSTEMD_PATH=/etc/systemd/system
CF_ENGINE_CONFIG_FILE="$CF_ROOT/config/Settings.toml"

# Stop the service
if systemctl is-active --quiet chainflip-engine; then
echo "Stopping chainflip-engine..."
sudo systemctl stop chainflip-engine
echo "chainflip-engine stopped."
else
echo "chainflip-engine is already stopped."
fi

# Delete the old engine data.db directory
if [ -d "$CF_ROOT/data.db" ]; then
echo "chainflip-engine: Removing old data.db directory"
rm -rf "$CF_ROOT/data.db"
fi
check_upgrade() {
if [ "$1" != "upgrade" ]; then
echo "chainflip-engine: Fresh install detected, skipping migration"
exit 0
else
echo "chainflip-engine: Upgrade detected, migrating"
fi
}

# Backup and delete systemd overrides if they exist
if [ -f /etc/systemd/system/chainflip-engine.service.d/override.conf ]; then
echo "chainflip-engine: systemd overrides found - backing up and removing"
rm -rf /etc/systemd/system/chainflip-engine.service.d
fi
stop_service() {
if systemctl is-active --quiet chainflip-engine; then
if systemctl stop chainflip-engine; then
echo "chainflip-engine stopped"
else
echo "Error stopping chainflip-engine"
exit 1
fi
else
echo "chainflip-engine is already stopped"
fi
}

# Remove old log files to free space
if [ -f "/var/log/chainflip-engine.log" ]; then
echo "chainflip-engine: Removing old log file"
rm -rf "/var/log/chainflip-engine*"
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
}

echo "Updating pDOT endpoint ..."
sed -i 's|wss://pdot.chainflip.xyz:443|wss://rpc-pdot.chainflip.io:443|g' /etc/chainflip/config/Settings.toml
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
}

FILE=/etc/chainflip/config/Settings.toml
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
}

# Check if [dot] section exists
if grep -q "\[dot\]" "$FILE"; then
# [dot] section exists, use awk to check for http_node_endpoint after [dot]
if ! awk '/\[dot\]/ {flag=1; next} flag && /http_node_endpoint/ {print; exit} flag && /^\[/ {exit}' "$FILE" | grep -q "http_node_endpoint"; then
# http_node_endpoint does not exist under [dot], add it
sed -i '/\[dot\]/a http_node_endpoint = "https://rpc-pdot.chainflip.io:443"' "$FILE"
echo "http_node_endpoint key added under [dot]"
else
echo "http_node_endpoint key already exists under [dot]"
remove_old_logs() {
if [ -f "/var/log/chainflip-engine.log" ]; then
echo "chainflip-engine: Removing old log file"
rm -rf "/var/log/chainflip-engine*"
fi
else
echo "[dot] section does not exist in the file"
fi
}

echo "Migration complete. Restarting chainflip-engine..."
update_engine_config() {
echo "chainflip-engine: Updating engine config"
}

systemctl daemon-reload
reload_systemctl() {
systemctl daemon-reload
}

#DEBHELPER#
# 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
}

# 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

exit 0
# #DEBHELPER#
25 changes: 25 additions & 0 deletions engine/package/preinst-tests/mocks/mock_commands.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add a global variable to control the behavior of the mock
SERVICE_STATUS="active"

systemctl() {
if [ "$1" = "is-active" ] && [ "$3" = "chainflip-engine" ]; then
if [ "$SERVICE_STATUS" = "active" ]; then
return 0
else
return 1
fi
fi

if [ "$1" = "stop" ] && [ "$2" = "chainflip-engine" ]; then
return 0 # assume it's successful
fi

echo "Unhandled systemctl command: $@"
}

# Mock for chainflip-engine
chainflip-engine() {
if [ "$1" = "-V" ]; then
echo "$VERSION_OUTPUT"
fi
}
14 changes: 14 additions & 0 deletions engine/package/preinst-tests/tests/setup.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bats
CF_ROOT=/tmp/chainflip
setup() {
# Load our mocks
source mocks/mock_commands.sh
mkdir -p $CF_ROOT/data.db
}

teardown() {
# Cleanup any artifacts or reset environment variables
unset VERSION_OUTPUT
unset SERVICE_STATUS
rm -rf $CF_ROOT
}
Loading

0 comments on commit 4baedab

Please sign in to comment.