Skip to content

Add unit test support and add unit test for new chroot_correct_qemu logic that is out of execute_chroot_script #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)"
68 changes: 68 additions & 0 deletions src/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,71 @@ function load_module_config() {
echo "================================================================"
done
}

function chroot_correct_qemu() {
local host_arch="$1"
local target_arch="$2"
local chroot_script="$3"
local custom_pi_os_path="$4"

# 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
}
37 changes: 5 additions & 32 deletions src/custompios
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set -e

export LC_ALL=C

source ${CUSTOM_PI_OS_PATH}/common.sh
source "${CUSTOM_PI_OS_PATH}"/common.sh

echo_green -e "\nBUILD STARTED @ $(date)!\n"

Expand Down Expand Up @@ -51,36 +51,7 @@ function execute_chroot_script() {
chmod 755 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 Expand Up @@ -128,7 +99,7 @@ install_cleanup_trap
install_fail_on_error_trap
unmount_image $BASE_MOUNT_PATH force || true

pushd $BASE_WORKSPACE
pushd "${BASE_WORKSPACE}"
if [ -e *.img ]; then
rm *.img
fi
Expand Down Expand Up @@ -207,6 +178,8 @@ pushd $BASE_WORKSPACE
echo "No remote and submodules config detected"
fi
echo $ARMBIAN_CONFIG_TXT_FILE
# if you need anything from common running in execute_chroot_script, export it here
export -f chroot_correct_qemu
export -f execute_chroot_script
bash -x "${CHROOT_SCRIPT}"

Expand Down
Loading