particle-os-tools/test-dkms-functionality.sh
2025-07-14 01:09:07 -07:00

399 lines
No EOL
9.6 KiB
Bash

#!/bin/bash
# Particle-OS DKMS Functionality Test Script
# Tests all DKMS features implemented in apt-layer
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Test configuration
TEST_MODULE="test-dkms-module"
TEST_VERSION="1.0.0"
TEST_KERNEL="$(uname -r)"
# Check if running as root
check_root() {
if [[ $EUID -ne 0 ]]; then
log_error "This script must be run as root for DKMS testing"
exit 1
fi
}
# Check if apt-layer is available
check_apt_layer() {
if ! command -v apt-layer &> /dev/null; then
log_error "apt-layer command not found. Please install Particle-OS tools first."
exit 1
fi
}
# Check if DKMS is available
check_dkms() {
if ! command -v dkms &> /dev/null; then
log_warning "DKMS not found. Installing DKMS..."
apt update
apt install -y dkms
fi
}
# Test 1: DKMS Status Command
test_dkms_status() {
log_info "Test 1: Testing DKMS status command"
if apt-layer --dkms-status; then
log_success "DKMS status command works"
return 0
else
log_error "DKMS status command failed"
return 1
fi
}
# Test 2: DKMS List Command
test_dkms_list() {
log_info "Test 2: Testing DKMS list command"
if apt-layer --dkms-list; then
log_success "DKMS list command works"
return 0
else
log_error "DKMS list command failed"
return 1
fi
}
# Test 3: Create Test DKMS Module
create_test_dkms_module() {
log_info "Test 3: Creating test DKMS module"
local test_dir="/tmp/test-dkms-module"
local dkms_dir="/usr/src/${TEST_MODULE}-${TEST_VERSION}"
# Create test module directory
mkdir -p "$test_dir"
cd "$test_dir"
# Create simple test module
cat > "test_module.c" << 'EOF'
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Particle-OS Test");
MODULE_DESCRIPTION("Test DKMS module for Particle-OS");
MODULE_VERSION("1.0.0");
static int __init test_init(void) {
printk(KERN_INFO "Test DKMS module loaded\n");
return 0;
}
static void __exit test_exit(void) {
printk(KERN_INFO "Test DKMS module unloaded\n");
}
module_init(test_init);
module_exit(test_exit);
EOF
# Create Makefile
cat > "Makefile" << EOF
obj-m += test_module.o
all:
make -C /lib/modules/\$(shell uname -r)/build M=\$(PWD) modules
clean:
make -C /lib/modules/\$(shell uname -r)/build M=\$(PWD) clean
EOF
# Create dkms.conf
cat > "dkms.conf" << EOF
PACKAGE_NAME="test-dkms-module"
PACKAGE_VERSION="1.0.0"
BUILT_MODULE_NAME[0]="test_module"
DEST_MODULE_LOCATION[0]="/kernel/drivers/misc"
AUTOINSTALL="yes"
EOF
# Copy to DKMS source directory
cp -r "$test_dir" "$dkms_dir"
log_success "Test DKMS module created at $dkms_dir"
return 0
}
# Test 4: Install DKMS Module
test_dkms_install() {
log_info "Test 4: Testing DKMS module installation"
if apt-layer --dkms-install "$TEST_MODULE" "$TEST_VERSION"; then
log_success "DKMS module installation works"
return 0
else
log_error "DKMS module installation failed"
return 1
fi
}
# Test 5: Verify DKMS Module Installation
test_dkms_verify_installation() {
log_info "Test 5: Verifying DKMS module installation"
# Check if module is listed in DKMS
if dkms status | grep -q "$TEST_MODULE/$TEST_VERSION"; then
log_success "DKMS module found in status"
else
log_error "DKMS module not found in status"
return 1
fi
# Check if module is loaded
if lsmod | grep -q "test_module"; then
log_success "DKMS module is loaded"
else
log_warning "DKMS module is not loaded (this is normal for test modules)"
fi
return 0
}
# Test 6: Rebuild DKMS Module
test_dkms_rebuild() {
log_info "Test 6: Testing DKMS module rebuild"
if apt-layer --dkms-rebuild "$TEST_MODULE" "$TEST_VERSION" "$TEST_KERNEL"; then
log_success "DKMS module rebuild works"
return 0
else
log_error "DKMS module rebuild failed"
return 1
fi
}
# Test 7: Rebuild All DKMS Modules
test_dkms_rebuild_all() {
log_info "Test 7: Testing rebuild all DKMS modules"
if apt-layer --dkms-rebuild-all "$TEST_KERNEL"; then
log_success "DKMS rebuild all works"
return 0
else
log_error "DKMS rebuild all failed"
return 1
fi
}
# Test 8: Remove DKMS Module
test_dkms_remove() {
log_info "Test 8: Testing DKMS module removal"
if apt-layer --dkms-remove "$TEST_MODULE" "$TEST_VERSION"; then
log_success "DKMS module removal works"
return 0
else
log_error "DKMS module removal failed"
return 1
fi
}
# Test 9: Verify DKMS Module Removal
test_dkms_verify_removal() {
log_info "Test 9: Verifying DKMS module removal"
# Check if module is no longer listed in DKMS
if ! dkms status | grep -q "$TEST_MODULE/$TEST_VERSION"; then
log_success "DKMS module successfully removed"
return 0
else
log_error "DKMS module still found in status"
return 1
fi
}
# Test 10: NVIDIA Status Command
test_nvidia_status() {
log_info "Test 10: Testing NVIDIA status command"
if apt-layer --nvidia-status; then
log_success "NVIDIA status command works"
return 0
else
log_warning "NVIDIA status command failed (may not have NVIDIA hardware)"
return 0 # Not a failure if no NVIDIA hardware
fi
}
# Test 11: GPU Switch Command
test_gpu_switch() {
log_info "Test 11: Testing GPU switch command"
# Test with integrated GPU
if apt-layer --gpu-switch integrated; then
log_success "GPU switch to integrated works"
else
log_warning "GPU switch to integrated failed (may not have dual GPU)"
fi
# Test with NVIDIA GPU
if apt-layer --gpu-switch nvidia; then
log_success "GPU switch to NVIDIA works"
else
log_warning "GPU switch to NVIDIA failed (may not have NVIDIA GPU)"
fi
return 0 # Not a failure if no dual GPU setup
}
# Test 12: NVIDIA Prime Configuration
test_nvidia_prime_configure() {
log_info "Test 12: Testing NVIDIA Prime configuration"
if apt-layer --nvidia-prime-configure; then
log_success "NVIDIA Prime configuration works"
return 0
else
log_warning "NVIDIA Prime configuration failed (may not have NVIDIA hardware)"
return 0 # Not a failure if no NVIDIA hardware
fi
}
# Cleanup function
cleanup() {
log_info "Cleaning up test environment..."
# Remove test module if it exists
if dkms status | grep -q "$TEST_MODULE/$TEST_VERSION"; then
apt-layer --dkms-remove "$TEST_MODULE" "$TEST_VERSION" || true
fi
# Remove test module directory
rm -rf "/usr/src/${TEST_MODULE}-${TEST_VERSION}" || true
rm -rf "/tmp/test-dkms-module" || true
log_success "Cleanup completed"
}
# Main test function
run_tests() {
local test_results=()
local test_count=0
local passed_count=0
local failed_count=0
log_info "Starting Particle-OS DKMS functionality tests..."
echo "=================================================="
# Pre-test checks
check_root
check_apt_layer
check_dkms
# Run tests
local tests=(
"test_dkms_status"
"test_dkms_list"
"create_test_dkms_module"
"test_dkms_install"
"test_dkms_verify_installation"
"test_dkms_rebuild"
"test_dkms_rebuild_all"
"test_dkms_remove"
"test_dkms_verify_removal"
"test_nvidia_status"
"test_gpu_switch"
"test_nvidia_prime_configure"
)
for test_func in "${tests[@]}"; do
((test_count++))
log_info "Running test $test_count: $test_func"
if $test_func; then
test_results+=("$test_func")
((passed_count++))
else
test_results+=("$test_func")
((failed_count++))
fi
echo ""
done
# Print results
echo "=================================================="
log_info "Test Results Summary:"
echo "Total tests: $test_count"
echo "Passed: $passed_count"
echo "Failed: $failed_count"
echo ""
log_info "Detailed Results:"
for result in "${test_results[@]}"; do
echo " $result"
done
echo ""
if [[ $failed_count -eq 0 ]]; then
log_success "All DKMS tests passed! 🎉"
return 0
else
log_error "Some DKMS tests failed. Please check the output above."
return 1
fi
}
# Handle script interruption
trap cleanup EXIT
# Parse command line arguments
case "${1:-}" in
--help|-h)
echo "Particle-OS DKMS Functionality Test Script"
echo ""
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --help, -h Show this help message"
echo " --cleanup Run cleanup only"
echo ""
echo "This script tests all DKMS functionality implemented in Particle-OS apt-layer."
echo "Must be run as root."
exit 0
;;
--cleanup)
cleanup
exit 0
;;
"")
run_tests
;;
*)
log_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac