Skip to content

Commit 63aaabf

Browse files
committed
Add unit tests and run them on github actions see also #240
1 parent 29355dc commit 63aaabf

File tree

5 files changed

+409
-29
lines changed

5 files changed

+409
-29
lines changed

.github/workflows/tests.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Run Tests via Makefile
2+
3+
on:
4+
push:
5+
branches:
6+
- "master"
7+
- "devel"
8+
- "docker-github-actions"
9+
- "release/v1"
10+
- "feature/unit-testing"
11+
- "beta"
12+
tags:
13+
- "*"
14+
15+
jobs:
16+
test:
17+
name: Run Makefile Tests
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
# - name: Install dependencies
24+
# run: |
25+
# sudo apt-get update
26+
# sudo apt-get install -y qemu-user-static binfmt-support
27+
28+
- name: Run Makefile
29+
run: make test
30+
31+
- name: Archive test results
32+
if: always()
33+
uses: actions/upload-artifact@v4
34+
with:
35+
name: test-results
36+
path: tests/
37+
retention-days: 60

Makefile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Makefile for running shell script tests
2+
3+
# Shell to use
4+
SHELL := /bin/bash
5+
6+
# Find all test files
7+
TEST_DIR := tests
8+
TEST_FILES := $(wildcard $(TEST_DIR)/test_*.sh)
9+
10+
# Colors for output
11+
BLUE := \033[1;34m
12+
GREEN := \033[1;32m
13+
RED := \033[1;31m
14+
NC := \033[0m # No Color
15+
16+
.PHONY: all test clean help
17+
18+
# Default target
19+
all: test
20+
21+
# Help message
22+
help:
23+
@echo "Available targets:"
24+
@echo " make test - Run all tests"
25+
@echo " make clean - Clean up temporary files"
26+
@echo " make help - Show this help message"
27+
28+
# Run all tests
29+
test:
30+
@echo -e "$(BLUE)Running tests...$(NC)"
31+
@echo "═══════════════════════════════════════"
32+
@success=true; \
33+
for test in $(TEST_FILES); do \
34+
echo -e "$(BLUE)Running $$test...$(NC)"; \
35+
if chmod +x $$test && ./$$test; then \
36+
echo -e "$(GREEN)$$test passed$(NC)"; \
37+
else \
38+
echo -e "$(RED)$$test failed$(NC)"; \
39+
success=false; \
40+
fi; \
41+
echo "───────────────────────────────────────"; \
42+
done; \
43+
echo ""; \
44+
if $$success; then \
45+
echo -e "$(GREEN)All tests passed successfully!$(NC)"; \
46+
exit 0; \
47+
else \
48+
echo -e "$(RED)Some tests failed!$(NC)"; \
49+
exit 1; \
50+
fi
51+
52+
# Clean up any temporary files (if needed)
53+
clean:
54+
@echo -e "$(BLUE)Cleaning up...$(NC)"
55+
@find $(TEST_DIR) -type f -name "*.tmp" -delete
56+
@find $(TEST_DIR) -type f -name "*.log" -delete
57+
@echo -e "$(GREEN)Cleanup complete$(NC)"

src/common.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,69 @@ function load_module_config() {
595595
echo "================================================================"
596596
done
597597
}
598+
599+
function chroot_correct_qemu() {
600+
local host_arch="$1"
601+
local target_arch="$2"
602+
603+
# Validate inputs
604+
if [[ -z "$host_arch" ]] || [[ -z "$target_arch" ]]; then
605+
echo "Error: Missing required arguments"
606+
echo "Usage: setup_qemu_chroot host_arch target_arch chroot_script custom_pi_os_path"
607+
return 1
608+
fi
609+
610+
# Copy required scripts
611+
cp "$chroot_script" chroot_script
612+
chmod 755 chroot_script
613+
cp "${custom_pi_os_path}/common.sh" common.sh
614+
chmod 755 common.sh
615+
616+
# Set up QEMU if needed
617+
if [[ "$host_arch" != "armv7l" ]] || [[ "$host_arch" != "aarch64" ]]; then
618+
if [[ "$target_arch" == "armv7l" ]] || [[ "$target_arch" == "armhf" ]]; then
619+
if grep -q gentoo /etc/os-release; then
620+
ROOT="$(realpath .)" emerge --usepkgonly --oneshot --nodeps qemu
621+
else
622+
cp "$(which qemu-arm-static)" usr/bin/qemu-arm-static
623+
fi
624+
elif [[ "$target_arch" == "aarch64" ]] || [[ "$target_arch" == "arm64" ]]; then
625+
if grep -q gentoo /etc/os-release; then
626+
ROOT="$(realpath .)" emerge --usepkgonly --oneshot --nodeps qemu
627+
else
628+
cp "$(which qemu-aarch64-static)" usr/bin/qemu-aarch64-static
629+
fi
630+
fi
631+
fi
632+
633+
# Execute chroot with appropriate QEMU setup
634+
if [[ "$host_arch" != "armv7l" ]] && [[ "$host_arch" != "aarch64" ]] && [[ "$host_arch" != "arm64" ]]; then
635+
echo "Detected we are on a non-arm device"
636+
if [[ "$target_arch" == "armv7l" ]] || [[ "$target_arch" == "armhf" ]]; then
637+
echo "Building on non-ARM device a armv7l system, using qemu-arm-static"
638+
if grep -q gentoo /etc/os-release; then
639+
echo "Building on gentoo non-ARM device a armv7l system, using qemu-arm"
640+
chroot . usr/bin/qemu-arm /bin/bash /chroot_script
641+
else
642+
echo "Using normal non-arm qemu for armv7l"
643+
chroot . usr/bin/qemu-arm-static /bin/bash /chroot_script
644+
fi
645+
elif [[ "$target_arch" == "aarch64" ]] || [[ "$target_arch" == "arm64" ]]; then
646+
echo "Building on non-ARM device a aarch64/arm64 system, using qemu-aarch64-static"
647+
if grep -q gentoo /etc/os-release; then
648+
chroot . usr/bin/qemu-aarch64 /bin/bash /chroot_script
649+
else
650+
chroot . usr/bin/qemu-aarch64-static /bin/bash /chroot_script
651+
fi
652+
else
653+
echo "Unknown arch, building on: $host_arch image: $target_arch"
654+
return 1
655+
fi
656+
elif { [[ "$target_arch" == "armv7l" ]] || [[ "$target_arch" == "armhf" ]]; } && [[ "$host_arch" != "armv7l" ]]; then
657+
echo "Building on aarch64/arm64 device a armv7l system, using qemu-arm-static"
658+
chroot . usr/bin/qemu-arm-static /bin/bash /chroot_script
659+
else
660+
echo "Building on ARM device a armv7l/aarch64/arm64 system, not using qemu"
661+
chroot . /bin/bash /chroot_script
662+
fi
663+
}

src/custompios

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,7 @@ function execute_chroot_script() {
5252
cp "${CUSTOM_PI_OS_PATH}"/common.sh common.sh
5353
chmod 755 common.sh
5454

55-
if [ "$(uname -m)" != "armv7l" ] && [ "$(uname -m)" != "aarch64" ] && [ "$(uname -m)" != "arm64" ] ; then
56-
echo "Detected we are on a non-arm device"
57-
if [ "$BASE_ARCH" == "armv7l" ] || [ "$BASE_ARCH" == "armhf" ]; then
58-
echo "Building on non-ARM device a armv7l system, using qemu-arm-static"
59-
if (grep -q gentoo /etc/os-release);then
60-
echo "Building on gentoo non-ARM device a aarch64/arm64 system, using qemu-aarch64-static"
61-
chroot . usr/bin/qemu-arm /bin/bash /chroot_script
62-
else
63-
echo "Using normal non-arm qemu for armv7l"
64-
chroot . usr/bin/qemu-arm-static /bin/bash /chroot_script
65-
fi
66-
elif [ "$BASE_ARCH" == "aarch64" ] || [ "$BASE_ARCH" == "arm64" ]; then
67-
echo "Building on non-ARM device a aarch64/arm64 system, using qemu-aarch64-static"
68-
if (grep -q gentoo /etc/os-release);then
69-
chroot . usr/bin/qemu-aarch64 /bin/bash /chroot_script
70-
else
71-
chroot . usr/bin/qemu-aarch64-static /bin/bash /chroot_script
72-
fi
73-
else
74-
echo "Unknown arch, building on: $(uname -m) image: $BASE_ARCH"
75-
exit 1
76-
fi
77-
elif { [ "$BASE_ARCH" == "armv7l" ] || [ "$BASE_ARCH" == "armhf" ]; } && [ "$(uname -m)" != "armv7l" ]; then
78-
echo "Building on aarch64/arm64 device a armv7l system, using qemu-arm-static"
79-
chroot . usr/bin/qemu-arm-static /bin/bash /chroot_script
80-
else
81-
echo "Building on ARM device a armv7l/aarch64/arm64 system, not using qemu"
82-
chroot . /bin/bash /chroot_script
83-
fi
55+
chroot_correct_qemu "$(uname -m)" "$BASE_ARCH" "$2" "${CUSTOM_PI_OS_PATH}"
8456

8557
# Handle exported items
8658
if [ -d "custompios_export" ]; then

0 commit comments

Comments
 (0)