-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add
preinst
script unit tests 🧪 (#3942)
* 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
Showing
25 changed files
with
422 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
on: | ||
workflow_call: | ||
|
||
env: | ||
FORCE_COLOR: 1 | ||
|
||
jobs: | ||
fetch: | ||
strategy: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,9 @@ on: | |
required: false | ||
default: 120 | ||
|
||
env: | ||
FORCE_COLOR: 1 | ||
|
||
permissions: | ||
packages: read | ||
contents: read | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,4 +44,4 @@ jobs: | |
with: | ||
version: ci/${{ github.sha }}/ | ||
environment: dev | ||
secrets: inherit | ||
secrets: inherit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.