-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.sh
executable file
·171 lines (143 loc) · 4.62 KB
/
test.sh
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
#!/bin/bash
# by https://github.com/oneclickvirt/incus_images
# 2025.01.31
set -e
trap 'echo "发生错误,行号: $LINENO, 命令: $BASH_COMMAND"' ERR
if [[ $# -ne 1 ]]; then
echo "用法: $0 <完整镜像名称>"
exit 1
fi
image_name="$1"
if [[ "$image_name" == *"x86_64"* ]]; then
fixed_images_file="x86_64_fixed_images.txt"
elif [[ "$image_name" == *"arm64"* ]]; then
fixed_images_file="arm64_fixed_images.txt"
else
echo "错误:无法识别镜像架构"
exit 1
fi
date=$(date)
{
echo "$date"
echo "------------------------------------------"
echo "测试镜像: $image_name"
} >> log
test_status_file=$(mktemp)
echo "success" > "$test_status_file"
cleanup() {
local exit_code=$?
echo "清理资源..."
incus stop test 2>/dev/null || true
incus delete -f test 2>/dev/null || true
incus image delete myc 2>/dev/null || true
if [[ -f "$test_status_file" ]]; then
if [[ "$(cat "$test_status_file")" != "success" ]]; then
echo "测试失败,从列表中移除镜像"
sed -i "/$image_name/d" "$fixed_images_file"
fi
else
echo "警告:test_status_file 丢失,跳过失败处理" | tee -a log
fi
rm -f "$test_status_file"
rm -f incus.tar.xz rootfs.squashfs "$image_name"
echo "------------------------------------------" >> log
# 检查 systemd-resolved 是否被影响,并尝试恢复
if systemctl is-active --quiet systemd-resolved; then
echo "重启 systemd-resolved 以恢复 DNS"
systemctl restart systemd-resolved || true
fi
exit $exit_code
}
trap cleanup EXIT
echo "开始下载镜像..."
if ! curl -m 1800 -LO "https://github.com/oneclickvirt/incus_images/releases/download/${image_name%%_*}/$image_name"; then
echo "主下载失败,尝试备用下载源..."
if ! curl -m 1800 -LO "https://cdn.spiritlhl.net/https://github.com/oneclickvirt/incus_images/releases/download/${image_name%%_*}/$image_name"; then
echo "错误:所有下载源均失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
fi
if [ ! -f "$image_name" ] || [ ! -s "$image_name" ]; then
echo "错误:镜像文件不存在或为空" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
chmod 777 "$image_name"
if ! unzip -q "$image_name"; then
echo "错误:解压失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
echo "$image_name" >> "$fixed_images_file"
if ! incus image import incus.tar.xz rootfs.squashfs --alias myc; then
echo "错误:镜像导入失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
if ! incus init myc test; then
echo "错误:容器初始化失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
if ! incus start test; then
echo "错误:容器启动失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
sleep 10
ssh_test() {
local retries=0
local max_retries=5
while [ $retries -lt $max_retries ]; do
if incus exec test -- lsof -i:22 2>/dev/null | grep -q ssh; then
echo "SSH服务正常" | tee -a log
return 0
fi
echo "等待SSH服务启动,尝试 $((retries + 1))/$max_retries..." | tee -a log
sleep $((2**retries))
((retries++))
done
echo "SSH服务异常" | tee -a log
return 1
}
network_test() {
incus exec test -- cp /etc/resolv.conf /etc/resolv.conf.bak || true
incus exec test -- sh -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
if incus exec test -- curl -m 10 -skL https://cdn.spiritlhl.net/https://raw.githubusercontent.com/spiritLHLS/ecs/main/back/test | grep -q "success"; then
echo "网络连通正常" | tee -a log
incus exec test -- mv /etc/resolv.conf.bak /etc/resolv.conf 2>/dev/null || true
return 0
else
echo "网络连接失败" | tee -a log
incus exec test -- mv /etc/resolv.conf.bak /etc/resolv.conf 2>/dev/null || true
return 1
fi
}
if ! ssh_test; then
echo "fail" > "$test_status_file"
exit 1
fi
if ! network_test; then
echo "fail" > "$test_status_file"
exit 1
fi
echo "执行重启测试..." | tee -a log
if ! incus stop test; then
echo "错误:容器停止失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
if ! incus start test; then
echo "错误:容器重启失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
sleep 15
if ! network_test; then
echo "重启后网络测试失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
echo "所有测试通过" | tee -a log
exit 0