particle-os-tools/test_dbus_signals.sh
Joe Particle 4f00e140a3
Some checks failed
Compile apt-layer (v2) / compile (push) Failing after 3h13m50s
feat: add changelog, docs, and update various scripts for modular CLI milestone
2025-07-17 09:22:29 +00:00

182 lines
No EOL
5.3 KiB
Bash

#!/bin/bash
# D-Bus Signal Testing Script
# Tests D-Bus signals emitted by the apt-ostree daemon
set -e
echo "=== apt-ostree D-Bus Signal Testing ==="
echo "Monitoring D-Bus signals from apt-ostree daemon..."
echo
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Signal counter
SIGNALS_RECEIVED=0
# Function to monitor signals
monitor_signals() {
echo -e "${CYAN}Starting D-Bus signal monitoring...${NC}"
echo -e "${YELLOW}Press Ctrl+C to stop monitoring${NC}"
echo
# Monitor signals using dbus-monitor
dbus-monitor --system "interface='org.debian.aptostree1.Sysroot'" | while read -r line; do
if [[ $line =~ "signal" ]]; then
SIGNALS_RECEIVED=$((SIGNALS_RECEIVED + 1))
echo -e "${GREEN}📡 Signal #$SIGNALS_RECEIVED received:${NC}"
echo -e "${BLUE}$line${NC}"
elif [[ $line =~ "member=" ]]; then
echo -e "${PURPLE} Member: $line${NC}"
elif [[ $line =~ "string" ]] || [[ $line =~ "double" ]]; then
echo -e "${YELLOW} Data: $line${NC}"
fi
done
}
# Function to test signals by triggering operations
test_signals() {
echo -e "${CYAN}Testing D-Bus signals by triggering operations...${NC}"
echo
# Test 1: Deploy operation
echo -e "${BLUE}Test 1: Triggering Deploy operation${NC}"
dbus-send --system --dest=org.debian.aptostree1 --print-reply /org/debian/aptostree1/Sysroot org.debian.aptostree1.Sysroot.Deploy string:"test-signal-deploy" >/dev/null 2>&1 &
sleep 2
# Test 2: Upgrade operation
echo -e "${BLUE}Test 2: Triggering Upgrade operation${NC}"
dbus-send --system --dest=org.debian.aptostree1 --print-reply /org/debian/aptostree1/Sysroot org.debian.aptostree1.Sysroot.Upgrade >/dev/null 2>&1 &
sleep 2
# Test 3: Rollback operation
echo -e "${BLUE}Test 3: Triggering Rollback operation${NC}"
dbus-send --system --dest=org.debian.aptostree1 --print-reply /org/debian/aptostree1/Sysroot org.debian.aptostree1.Sysroot.Rollback >/dev/null 2>&1 &
sleep 2
echo -e "${GREEN}Signal tests completed!${NC}"
}
# Function to test signal subscription
test_signal_subscription() {
echo -e "${CYAN}Testing D-Bus signal subscription...${NC}"
echo
# Create a Python script to subscribe to signals
cat > /tmp/test_signal_subscription.py << 'EOF'
#!/usr/bin/env python3
import asyncio
import json
from dbus_next.aio import MessageBus
from dbus_next import BusType
async def main():
# Connect to system bus
bus = await MessageBus(bus_type=BusType.SYSTEM).connect()
# Subscribe to signals
def on_transaction_progress(transaction_id, operation, progress, message):
print(f"🔔 TransactionProgress: {transaction_id} - {operation} - {progress}% - {message}")
def on_property_changed(interface_name, property_name, value):
print(f"🔔 PropertyChanged: {interface_name}.{property_name} = {value}")
def on_status_changed(status):
print(f"🔔 StatusChanged: {status}")
# Add signal handlers
bus.add_message_handler(on_transaction_progress)
bus.add_message_handler(on_property_changed)
bus.add_message_handler(on_status_changed)
print("📡 Listening for D-Bus signals...")
print("Press Ctrl+C to stop")
try:
# Keep listening
await asyncio.Event().wait()
except KeyboardInterrupt:
print("\n🛑 Signal monitoring stopped")
if __name__ == "__main__":
asyncio.run(main())
EOF
# Run the signal subscription test
python3 /tmp/test_signal_subscription.py &
SUBSCRIPTION_PID=$!
# Wait a moment, then trigger operations
sleep 3
echo -e "${BLUE}Triggering operations to test signal subscription...${NC}"
dbus-send --system --dest=org.debian.aptostree1 --print-reply /org/debian/aptostree1/Sysroot org.debian.aptostree1.Sysroot.Deploy string:"subscription-test" >/dev/null 2>&1
sleep 2
dbus-send --system --dest=org.debian.aptostree1 --print-reply /org/debian/aptostree1/Sysroot org.debian.aptostree1.Sysroot.Upgrade >/dev/null 2>&1
sleep 2
# Stop the subscription test
kill $SUBSCRIPTION_PID 2>/dev/null || true
rm -f /tmp/test_signal_subscription.py
}
# Main menu
echo "Choose a test option:"
echo "1) Monitor signals in real-time"
echo "2) Test signals by triggering operations"
echo "3) Test signal subscription"
echo "4) Run all tests"
echo "5) Exit"
echo
read -p "Enter your choice (1-5): " choice
case $choice in
1)
monitor_signals
;;
2)
test_signals
;;
3)
test_signal_subscription
;;
4)
echo -e "${CYAN}Running all signal tests...${NC}"
echo
# Start monitoring in background
monitor_signals &
MONITOR_PID=$!
# Wait a moment, then run tests
sleep 2
test_signals
# Stop monitoring
kill $MONITOR_PID 2>/dev/null || true
echo -e "${GREEN}All signal tests completed!${NC}"
;;
5)
echo "Exiting..."
exit 0
;;
*)
echo -e "${RED}Invalid choice. Exiting.${NC}"
exit 1
;;
esac
echo
echo -e "${GREEN}Signal testing completed!${NC}"
echo -e "${YELLOW}Total signals received: $SIGNALS_RECEIVED${NC}"