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