Skip to content

Commit

Permalink
Add unit tests and run them on github actions see also #240
Browse files Browse the repository at this point in the history
  • Loading branch information
guysoft committed Jan 6, 2025
1 parent 7d4e97c commit 541d20d
Show file tree
Hide file tree
Showing 5 changed files with 409 additions and 29 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Run Tests via Makefile

on:
push:
branches:
- "master"
- "devel"
- "docker-github-actions"
- "release/v1"
- "feature/unit-testing"
- "beta"
tags:
- "*"

jobs:
test:
name: Run Makefile Tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

# - name: Install dependencies
# run: |
# sudo apt-get update
# sudo apt-get install -y qemu-user-static binfmt-support

- name: Run Makefile
run: make test

- name: Archive test results
if: always()
uses: actions/upload-artifact@v4
with:
name: test-results
path: tests/
retention-days: 60
57 changes: 57 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Makefile for running shell script tests

# Shell to use
SHELL := /bin/bash

# Find all test files
TEST_DIR := tests
TEST_FILES := $(wildcard $(TEST_DIR)/test_*.sh)

# Colors for output
BLUE := \033[1;34m
GREEN := \033[1;32m
RED := \033[1;31m
NC := \033[0m # No Color

.PHONY: all test clean help

# Default target
all: test

# Help message
help:
@echo "Available targets:"
@echo " make test - Run all tests"
@echo " make clean - Clean up temporary files"
@echo " make help - Show this help message"

# Run all tests
test:
@echo -e "$(BLUE)Running tests...$(NC)"
@echo "═══════════════════════════════════════"
@success=true; \
for test in $(TEST_FILES); do \
echo -e "$(BLUE)Running $$test...$(NC)"; \
if chmod +x $$test && ./$$test; then \
echo -e "$(GREEN)$$test passed$(NC)"; \
else \
echo -e "$(RED)$$test failed$(NC)"; \
success=false; \
fi; \
echo "───────────────────────────────────────"; \
done; \
echo ""; \
if $$success; then \
echo -e "$(GREEN)All tests passed successfully!$(NC)"; \
exit 0; \
else \
echo -e "$(RED)Some tests failed!$(NC)"; \
exit 1; \
fi

# Clean up any temporary files (if needed)
clean:
@echo -e "$(BLUE)Cleaning up...$(NC)"
@find $(TEST_DIR) -type f -name "*.tmp" -delete
@find $(TEST_DIR) -type f -name "*.log" -delete
@echo -e "$(GREEN)Cleanup complete$(NC)"
66 changes: 66 additions & 0 deletions src/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,69 @@ function load_module_config() {
echo "================================================================"
done
}

function chroot_correct_qemu() {
local host_arch="$1"
local target_arch="$2"

# Validate inputs
if [[ -z "$host_arch" ]] || [[ -z "$target_arch" ]]; then
echo "Error: Missing required arguments"
echo "Usage: setup_qemu_chroot host_arch target_arch chroot_script custom_pi_os_path"
return 1
fi

# Copy required scripts
cp "$chroot_script" chroot_script
chmod 755 chroot_script
cp "${custom_pi_os_path}/common.sh" common.sh
chmod 755 common.sh

# Set up QEMU if needed
if [[ "$host_arch" != "armv7l" ]] || [[ "$host_arch" != "aarch64" ]]; then
if [[ "$target_arch" == "armv7l" ]] || [[ "$target_arch" == "armhf" ]]; then
if grep -q gentoo /etc/os-release; then
ROOT="$(realpath .)" emerge --usepkgonly --oneshot --nodeps qemu
else
cp "$(which qemu-arm-static)" usr/bin/qemu-arm-static
fi
elif [[ "$target_arch" == "aarch64" ]] || [[ "$target_arch" == "arm64" ]]; then
if grep -q gentoo /etc/os-release; then
ROOT="$(realpath .)" emerge --usepkgonly --oneshot --nodeps qemu
else
cp "$(which qemu-aarch64-static)" usr/bin/qemu-aarch64-static
fi
fi
fi

# Execute chroot with appropriate QEMU setup
if [[ "$host_arch" != "armv7l" ]] && [[ "$host_arch" != "aarch64" ]] && [[ "$host_arch" != "arm64" ]]; then
echo "Detected we are on a non-arm device"
if [[ "$target_arch" == "armv7l" ]] || [[ "$target_arch" == "armhf" ]]; then
echo "Building on non-ARM device a armv7l system, using qemu-arm-static"
if grep -q gentoo /etc/os-release; then
echo "Building on gentoo non-ARM device a armv7l system, using qemu-arm"
chroot . usr/bin/qemu-arm /bin/bash /chroot_script
else
echo "Using normal non-arm qemu for armv7l"
chroot . usr/bin/qemu-arm-static /bin/bash /chroot_script
fi
elif [[ "$target_arch" == "aarch64" ]] || [[ "$target_arch" == "arm64" ]]; then
echo "Building on non-ARM device a aarch64/arm64 system, using qemu-aarch64-static"
if grep -q gentoo /etc/os-release; then
chroot . usr/bin/qemu-aarch64 /bin/bash /chroot_script
else
chroot . usr/bin/qemu-aarch64-static /bin/bash /chroot_script
fi
else
echo "Unknown arch, building on: $host_arch image: $target_arch"
return 1
fi
elif { [[ "$target_arch" == "armv7l" ]] || [[ "$target_arch" == "armhf" ]]; } && [[ "$host_arch" != "armv7l" ]]; then
echo "Building on aarch64/arm64 device a armv7l system, using qemu-arm-static"
chroot . usr/bin/qemu-arm-static /bin/bash /chroot_script
else
echo "Building on ARM device a armv7l/aarch64/arm64 system, not using qemu"
chroot . /bin/bash /chroot_script
fi
}
30 changes: 1 addition & 29 deletions src/custompios
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,7 @@ function execute_chroot_script() {
cp "${CUSTOM_PI_OS_PATH}"/common.sh common.sh
chmod 755 common.sh

if [ "$(uname -m)" != "armv7l" ] && [ "$(uname -m)" != "aarch64" ] && [ "$(uname -m)" != "arm64" ] ; then
echo "Detected we are on a non-arm device"
if [ "$BASE_ARCH" == "armv7l" ] || [ "$BASE_ARCH" == "armhf" ]; then
echo "Building on non-ARM device a armv7l system, using qemu-arm-static"
if (grep -q gentoo /etc/os-release);then
echo "Building on gentoo non-ARM device a aarch64/arm64 system, using qemu-aarch64-static"
chroot . usr/bin/qemu-arm /bin/bash /chroot_script
else
echo "Using normal non-arm qemu for armv7l"
chroot . usr/bin/qemu-arm-static /bin/bash /chroot_script
fi
elif [ "$BASE_ARCH" == "aarch64" ] || [ "$BASE_ARCH" == "arm64" ]; then
echo "Building on non-ARM device a aarch64/arm64 system, using qemu-aarch64-static"
if (grep -q gentoo /etc/os-release);then
chroot . usr/bin/qemu-aarch64 /bin/bash /chroot_script
else
chroot . usr/bin/qemu-aarch64-static /bin/bash /chroot_script
fi
else
echo "Unknown arch, building on: $(uname -m) image: $BASE_ARCH"
exit 1
fi
elif { [ "$BASE_ARCH" == "armv7l" ] || [ "$BASE_ARCH" == "armhf" ]; } && [ "$(uname -m)" != "armv7l" ]; then
echo "Building on aarch64/arm64 device a armv7l system, using qemu-arm-static"
chroot . usr/bin/qemu-arm-static /bin/bash /chroot_script
else
echo "Building on ARM device a armv7l/aarch64/arm64 system, not using qemu"
chroot . /bin/bash /chroot_script
fi
chroot_correct_qemu "$(uname -m)" "$BASE_ARCH" "$2" "${CUSTOM_PI_OS_PATH}"

# Handle exported items
if [ -d "custompios_export" ]; then
Expand Down
Loading

0 comments on commit 541d20d

Please sign in to comment.