Skip to content

Commit 2a5c056

Browse files
committed
Add cleanup, extraction, and testing scripts
1 parent f891b97 commit 2a5c056

3 files changed

Lines changed: 319 additions & 0 deletions

File tree

scripts/clean.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
3+
# =============================================================================
4+
# Cleanup Script
5+
# =============================================================================
6+
7+
set -euo pipefail
8+
9+
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10+
readonly PROJECT_DIR="$(dirname "${SCRIPT_DIR}")"
11+
12+
readonly GREEN='\033[0;32m'
13+
readonly BLUE='\033[0;34m'
14+
readonly YELLOW='\033[1;33m'
15+
readonly NC='\033[0m'
16+
17+
CLEAN_DOCKER=false
18+
CLEAN_ALL=false
19+
20+
# Parse arguments
21+
while [[ $# -gt 0 ]]; do
22+
case $1 in
23+
--docker)
24+
CLEAN_DOCKER=true
25+
shift
26+
;;
27+
--all)
28+
CLEAN_ALL=true
29+
CLEAN_DOCKER=true
30+
shift
31+
;;
32+
*)
33+
echo "Unknown option: $1"
34+
echo "Usage: $0 [--docker] [--all]"
35+
exit 1
36+
;;
37+
esac
38+
done
39+
40+
echo -e "${BLUE}Cleaning build artifacts...${NC}"
41+
42+
cd "${PROJECT_DIR}"
43+
44+
# Remove build artifacts
45+
echo "Removing build artifacts..."
46+
rm -f output.iso boot.img bzImage initramfs initramfs.cpio.gz
47+
rm -rf mnt/ myiso/ initramfs/ build/ output/
48+
echo -e "${GREEN}✓ Build artifacts removed${NC}"
49+
50+
# Clean Docker
51+
if [[ "${CLEAN_DOCKER}" == "true" ]]; then
52+
echo -e "${BLUE}Cleaning Docker images...${NC}"
53+
docker rmi handbuilt-linux:latest 2>/dev/null || echo -e "${YELLOW}Image not found${NC}"
54+
docker rmi handbuilt-linux:kernel 2>/dev/null || true
55+
docker rmi handbuilt-linux:busybox 2>/dev/null || true
56+
docker rmi handbuilt-linux:iso 2>/dev/null || true
57+
echo -e "${GREEN}✓ Docker images removed${NC}"
58+
fi
59+
60+
# Full cleanup
61+
if [[ "${CLEAN_ALL}" == "true" ]]; then
62+
echo -e "${BLUE}Performing full cleanup...${NC}"
63+
docker system prune -f 2>/dev/null || true
64+
echo -e "${GREEN}✓ Full cleanup complete${NC}"
65+
fi
66+
67+
echo -e "${GREEN}Cleanup complete!${NC}"

scripts/extract.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
3+
# =============================================================================
4+
# Extract Build Artifacts from Docker Image
5+
# =============================================================================
6+
7+
set -euo pipefail
8+
9+
readonly IMAGE_NAME="${1:-handbuilt-linux:latest}"
10+
readonly OUTPUT_DIR="${2:-./output}"
11+
12+
readonly GREEN='\033[0;32m'
13+
readonly BLUE='\033[0;34m'
14+
readonly NC='\033[0m'
15+
16+
echo -e "${BLUE}Extracting artifacts from ${IMAGE_NAME}...${NC}"
17+
18+
# Create output directory
19+
mkdir -p "${OUTPUT_DIR}"
20+
21+
# Extract ISO
22+
echo "Extracting ISO..."
23+
docker run --rm "${IMAGE_NAME}" cat /distro/output.iso > "${OUTPUT_DIR}/output.iso"
24+
echo -e "${GREEN}✓ ISO extracted: ${OUTPUT_DIR}/output.iso${NC}"
25+
26+
# Extract kernel
27+
echo "Extracting kernel..."
28+
docker run --rm "${IMAGE_NAME}" cat /distro/bzImage > "${OUTPUT_DIR}/bzImage"
29+
echo -e "${GREEN}✓ Kernel extracted: ${OUTPUT_DIR}/bzImage${NC}"
30+
31+
# Extract initramfs
32+
echo "Extracting initramfs..."
33+
docker run --rm "${IMAGE_NAME}" cat /distro/initramfs > "${OUTPUT_DIR}/initramfs"
34+
echo -e "${GREEN}✓ Initramfs extracted: ${OUTPUT_DIR}/initramfs${NC}"
35+
36+
echo -e "${GREEN}All artifacts extracted to ${OUTPUT_DIR}/${NC}"

scripts/test.sh

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/usr/bin/env bash
2+
3+
# =============================================================================
4+
# Test Script for Linux Distribution
5+
# =============================================================================
6+
7+
set -euo pipefail
8+
9+
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10+
readonly PROJECT_DIR="$(dirname "${SCRIPT_DIR}")"
11+
12+
readonly GREEN='\033[0;32m'
13+
readonly RED='\033[0;31m'
14+
readonly BLUE='\033[0;34m'
15+
readonly YELLOW='\033[1;33m'
16+
readonly NC='\033[0m'
17+
18+
TESTS_PASSED=0
19+
TESTS_FAILED=0
20+
21+
# -----------------------------------------------------------------------------
22+
# Helper Functions
23+
# -----------------------------------------------------------------------------
24+
25+
log_info() {
26+
echo -e "${BLUE}[INFO]${NC} $*"
27+
}
28+
29+
log_success() {
30+
echo -e "${GREEN}[PASS]${NC} $*"
31+
((TESTS_PASSED++))
32+
}
33+
34+
log_error() {
35+
echo -e "${RED}[FAIL]${NC} $*" >&2
36+
((TESTS_FAILED++))
37+
}
38+
39+
log_warning() {
40+
echo -e "${YELLOW}[WARN]${NC} $*"
41+
}
42+
43+
# -----------------------------------------------------------------------------
44+
# Tests
45+
# -----------------------------------------------------------------------------
46+
47+
test_docker_image_exists() {
48+
log_info "Testing if Docker image exists..."
49+
if docker image inspect handbuilt-linux:latest &> /dev/null; then
50+
log_success "Docker image exists"
51+
return 0
52+
else
53+
log_error "Docker image not found"
54+
return 1
55+
fi
56+
}
57+
58+
test_iso_exists() {
59+
log_info "Testing if ISO can be extracted..."
60+
if docker run --rm handbuilt-linux:latest test -f /distro/output.iso; then
61+
log_success "ISO exists in Docker image"
62+
return 0
63+
else
64+
log_error "ISO not found in Docker image"
65+
return 1
66+
fi
67+
}
68+
69+
test_kernel_exists() {
70+
log_info "Testing if kernel can be extracted..."
71+
if docker run --rm handbuilt-linux:latest test -f /distro/bzImage; then
72+
log_success "Kernel exists in Docker image"
73+
return 0
74+
else
75+
log_error "Kernel not found in Docker image"
76+
return 1
77+
fi
78+
}
79+
80+
test_initramfs_exists() {
81+
log_info "Testing if initramfs can be extracted..."
82+
if docker run --rm handbuilt-linux:latest test -f /distro/initramfs; then
83+
log_success "Initramfs exists in Docker image"
84+
return 0
85+
else
86+
log_error "Initramfs not found in Docker image"
87+
return 1
88+
fi
89+
}
90+
91+
test_iso_is_valid() {
92+
log_info "Testing if ISO is valid..."
93+
local iso_file="${PROJECT_DIR}/output.iso"
94+
95+
if [[ ! -f "${iso_file}" ]]; then
96+
log_warning "ISO not extracted yet, extracting..."
97+
docker run --rm handbuilt-linux:latest cat /distro/output.iso > "${iso_file}"
98+
fi
99+
100+
if file "${iso_file}" | grep -q "ISO 9660"; then
101+
log_success "ISO is valid"
102+
return 0
103+
else
104+
log_error "ISO is not valid"
105+
return 1
106+
fi
107+
}
108+
109+
test_initramfs_is_valid() {
110+
log_info "Testing if initramfs is valid..."
111+
local initramfs_file="${PROJECT_DIR}/initramfs"
112+
113+
if [[ ! -f "${initramfs_file}" ]]; then
114+
log_warning "Initramfs not extracted yet, extracting..."
115+
docker run --rm handbuilt-linux:latest cat /distro/initramfs > "${initramfs_file}"
116+
fi
117+
118+
if file "${initramfs_file}" | grep -q "gzip compressed"; then
119+
log_success "Initramfs is valid"
120+
return 0
121+
else
122+
log_error "Initramfs is not valid"
123+
return 1
124+
fi
125+
}
126+
127+
test_build_sh_syntax() {
128+
log_info "Testing build.sh syntax..."
129+
if bash -n "${PROJECT_DIR}/build.sh"; then
130+
log_success "build.sh syntax is valid"
131+
return 0
132+
else
133+
log_error "build.sh has syntax errors"
134+
return 1
135+
fi
136+
}
137+
138+
test_init_sh_syntax() {
139+
log_info "Testing init.sh syntax..."
140+
if sh -n "${PROJECT_DIR}/init.sh"; then
141+
log_success "init.sh syntax is valid"
142+
return 0
143+
else
144+
log_error "init.sh has syntax errors"
145+
return 1
146+
fi
147+
}
148+
149+
test_dockerfile_lint() {
150+
log_info "Testing Dockerfile with hadolint..."
151+
if command -v hadolint &> /dev/null; then
152+
if hadolint "${PROJECT_DIR}/Dockerfile" 2>&1 | grep -v "DL3008"; then
153+
log_success "Dockerfile lint passed"
154+
return 0
155+
else
156+
log_warning "Dockerfile has lint warnings (non-critical)"
157+
return 0
158+
fi
159+
else
160+
log_warning "hadolint not found, skipping Dockerfile lint"
161+
return 0
162+
fi
163+
}
164+
165+
test_qemu_available() {
166+
log_info "Testing if QEMU is available..."
167+
if command -v qemu-system-x86_64 &> /dev/null; then
168+
log_success "QEMU is available"
169+
return 0
170+
else
171+
log_warning "QEMU not found (optional for testing)"
172+
return 0
173+
fi
174+
}
175+
176+
# -----------------------------------------------------------------------------
177+
# Main
178+
# -----------------------------------------------------------------------------
179+
180+
main() {
181+
echo "========================================"
182+
echo " handbuilt-linux Test Suite"
183+
echo "========================================"
184+
echo ""
185+
186+
# Run tests
187+
test_docker_image_exists || true
188+
test_iso_exists || true
189+
test_kernel_exists || true
190+
test_initramfs_exists || true
191+
test_iso_is_valid || true
192+
test_initramfs_is_valid || true
193+
test_build_sh_syntax || true
194+
test_init_sh_syntax || true
195+
test_dockerfile_lint || true
196+
test_qemu_available || true
197+
198+
# Summary
199+
echo ""
200+
echo "========================================"
201+
echo " Test Summary"
202+
echo "========================================"
203+
echo -e "${GREEN}Passed: ${TESTS_PASSED}${NC}"
204+
echo -e "${RED}Failed: ${TESTS_FAILED}${NC}"
205+
echo ""
206+
207+
if [[ ${TESTS_FAILED} -eq 0 ]]; then
208+
echo -e "${GREEN}All tests passed!${NC}"
209+
return 0
210+
else
211+
echo -e "${RED}Some tests failed!${NC}"
212+
return 1
213+
fi
214+
}
215+
216+
main "$@"

0 commit comments

Comments
 (0)