Skip to content

Commit adac37e

Browse files
fanzu8hao022
authored andcommitted
ci: add end to end test and refactor test
Signed-off-by: fanzu8 <tuzengbing@gmail.com>
1 parent 36fa82a commit adac37e

11 files changed

Lines changed: 675 additions & 160 deletions

File tree

.github/workflows/scripts/qemu-2-run-in-vm.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ check_command_deps
9898
cd /mnt/host && pwd
9999
ls -alh /mnt/host
100100

101-
echo -e "\n\n⬅️ integration test..."
101+
echo -e "\n\n⬅️ test..."
102102

103-
make integration
103+
make test
104104

105-
echo -e "integration test ok."
105+
echo -e "✅ test ok."

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,15 @@ clean:
8282
mock-build:
8383
@go generate -run "mockery.*" -x ./...
8484

85+
test: all mock-build
86+
@bash integration/run.sh
87+
@bash e2e/run.sh
88+
8589
integration: all mock-build
86-
@bash integration/integration.sh
90+
@bash integration/run.sh
91+
92+
e2e: all
93+
@bash e2e/run.sh
8794

8895
force:;
8996

e2e/lib.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2026 The HuaTuo Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -euo pipefail
18+
19+
export TEST_LOG_TAG="E2E TEST"
20+
21+
source ${ROOT_DIR}/integration/lib.sh
22+
23+
# k8s
24+
k8s_create_pod() {
25+
local ns=$1
26+
local name=$2
27+
local image=$3
28+
local label=$4
29+
local num=$5
30+
31+
for i in $(seq 1 ${num}); do
32+
kubectl run "${name}-${i}" \
33+
-n ${ns} \
34+
--image=${image} \
35+
--restart=Never \
36+
-l ${label} \
37+
-- sleep infinity
38+
done
39+
}
40+
41+
k8s_delete_pod() {
42+
local ns=$1
43+
local label=$2
44+
kubectl delete pod --namespace "$ns" -l "$label"
45+
}
46+
47+
# kubelet
48+
kubelet_pods_json() {
49+
curl -sk "${CURL_TIMEOUT[@]}" \
50+
--cert ${KUBELET_CERT} \
51+
--key ${KUBELET_KEY} \
52+
--header "Content-Type: application/json" \
53+
${KUBELET_PODS_API}
54+
}
55+
56+
kubelet_pod_count() {
57+
local ns=$1
58+
local regex=$2
59+
kubelet_pods_json |
60+
jq --arg ns "$ns" --arg re "$regex" '
61+
[ .items[]
62+
| select(.metadata.namespace == $ns)
63+
| select(.metadata.name | test($re))
64+
| select(.status.phase == "Running")
65+
] | length
66+
' 2>/dev/null || echo 0
67+
}
68+
69+
assert_kubelet_pod_count() {
70+
local ns=$1 regex=$2 expect=$3 desc=${4:-"kubelet pod count"}
71+
72+
_assert() {
73+
local actual
74+
actual="$(kubelet_pod_count "$ns" "$regex")"
75+
assert_eq "$actual" "$expect" "$desc"
76+
}
77+
78+
wait_until \
79+
"$((WAIT_HUATUO_BAMAI_TIMEOUT / 2))" \
80+
"${WAIT_HUATUO_BAMAI_INTERVAL}" \
81+
"$desc" \
82+
_assert
83+
}
84+
85+
assert_huatuo_bamai_pod_count() {
86+
local regex=$1 expect=$2 desc=${3:-"huatuo-bamai pod count"}
87+
_assert() {
88+
local actual
89+
actual="$(huatuo_bamai_pod_count "$regex")"
90+
assert_eq "$actual" "$expect" "$desc"
91+
}
92+
93+
wait_until \
94+
"$((WAIT_HUATUO_BAMAI_TIMEOUT / 2))" \
95+
"${WAIT_HUATUO_BAMAI_INTERVAL}" \
96+
"$desc" \
97+
_assert
98+
}
99+
100+
e2e_test_teardown() {
101+
local code=$1
102+
103+
huatuo_bamai_stop || true
104+
huatuo_bamai_log_check || true
105+
106+
if [[ $code -ne 0 ]]; then
107+
fatal "❌ e2e test failed with exit code: $code"
108+
fi
109+
}

e2e/run.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2026 The HuaTuo Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -euo pipefail
18+
19+
source "./integration/env.sh"
20+
source "${ROOT_DIR}/integration/lib.sh"
21+
source "${ROOT_DIR}/e2e/lib.sh"
22+
23+
_e2e_cleanup() {
24+
local code=$?
25+
[[ $code -eq 0 ]] && sleep 10 # wait more logs to be collected
26+
e2e_test_teardown "$code" || true
27+
exit $code
28+
}
29+
trap "_e2e_cleanup" EXIT
30+
31+
huatuo_bamai_start "${HUATUO_BAMAI_ARGS_E2E[@]}"
32+
33+
# auto run all test_*.sh scripts in the e2e
34+
for case in "${ROOT_DIR}"/e2e/test_*.sh; do
35+
[[ -f "$case" ]] || continue
36+
log_info "⬅️⬅️ start: $(basename "$case")"
37+
38+
if ! bash "$case"; then
39+
fatal "❌ failed: $(basename "$case")"
40+
fi
41+
42+
log_info "✅✅ passed: $(basename "$case")"
43+
done
44+
45+
log_info "🎉🎉 all e2e tests passed."

e2e/test_container.sh

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2026 The HuaTuo Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -euo pipefail
18+
19+
source ${ROOT_DIR}/integration/lib.sh
20+
source ${ROOT_DIR}/e2e/lib.sh
21+
22+
test_huatuo_bamai_default_container_exists() {
23+
log_info "⬅️ test huatuo-bamai default container exists"
24+
25+
assert_kubelet_pod_count \
26+
"${BUSINESS_POD_NS}" \
27+
"${BUSINESS_DEFAULT_POD_NAME_REGEX}" \
28+
"${BUSINESS_DEFAULT_POD_COUNT}" \
29+
"default pod exists in kubelet"
30+
31+
assert_huatuo_bamai_pod_count \
32+
"${BUSINESS_DEFAULT_POD_NAME_REGEX}" \
33+
"${BUSINESS_DEFAULT_POD_COUNT}" \
34+
"default pod exists in huatuo-bamai"
35+
36+
log_info "✅ test huatuo-bamai default container exists ok"
37+
}
38+
39+
test_huatuo_bamai_e2e_container_create() {
40+
log_info "⬅️ creating e2e test pods"
41+
42+
# ensure clean
43+
k8s_delete_pod "${BUSINESS_POD_NS}" "${BUSINESS_E2E_TEST_POD_LABEL}" || true
44+
45+
assert_kubelet_pod_count \
46+
"${BUSINESS_POD_NS}" \
47+
"${BUSINESS_E2E_TEST_POD_NAME_REGEX}" \
48+
"0" \
49+
"kubelet e2e pods cleaned"
50+
51+
assert_huatuo_bamai_pod_count \
52+
"${BUSINESS_E2E_TEST_POD_NAME_REGEX}" \
53+
"0" \
54+
"huatuo-bamai e2e pods cleaned"
55+
56+
# create
57+
k8s_create_pod \
58+
"${BUSINESS_POD_NS}" \
59+
"${BUSINESS_E2E_TEST_POD_NAME}" \
60+
"${BUSINESS_POD_IMAGE}" \
61+
"${BUSINESS_E2E_TEST_POD_LABEL}" \
62+
"${BUSINESS_E2E_TEST_POD_COUNT}"
63+
64+
assert_kubelet_pod_count \
65+
"${BUSINESS_POD_NS}" \
66+
"${BUSINESS_E2E_TEST_POD_NAME_REGEX}" \
67+
"${BUSINESS_E2E_TEST_POD_COUNT}" \
68+
"kubelet e2e pods created"
69+
70+
assert_huatuo_bamai_pod_count \
71+
"${BUSINESS_E2E_TEST_POD_NAME_REGEX}" \
72+
"${BUSINESS_E2E_TEST_POD_COUNT}" \
73+
"huatuo-bamai e2e pods created"
74+
75+
log_info "✅ test huatuo-bamai e2e container create ok"
76+
}
77+
78+
test_huatuo_bamai_e2e_container_delete() {
79+
log_info "⬅️ deleting e2e test pods"
80+
81+
assert_kubelet_pod_count \
82+
"${BUSINESS_POD_NS}" \
83+
"${BUSINESS_E2E_TEST_POD_NAME_REGEX}" \
84+
"${BUSINESS_E2E_TEST_POD_COUNT}" \
85+
"kubelet e2e pods exist before delete"
86+
87+
assert_huatuo_bamai_pod_count \
88+
"${BUSINESS_E2E_TEST_POD_NAME_REGEX}" \
89+
"${BUSINESS_E2E_TEST_POD_COUNT}" \
90+
"huatuo-bamai e2e pods exist before delete"
91+
92+
k8s_delete_pod "${BUSINESS_POD_NS}" "${BUSINESS_E2E_TEST_POD_LABEL}"
93+
94+
assert_kubelet_pod_count \
95+
"${BUSINESS_POD_NS}" \
96+
"${BUSINESS_E2E_TEST_POD_NAME_REGEX}" \
97+
"0" \
98+
"kubelet e2e pods deleted"
99+
100+
assert_huatuo_bamai_pod_count \
101+
"${BUSINESS_E2E_TEST_POD_NAME_REGEX}" \
102+
"0" \
103+
"huatuo-bamai e2e pods deleted"
104+
105+
log_info "✅ test huatuo-bamai e2e container delete ok"
106+
}
107+
108+
test_huatuo_bamai_default_container_exists
109+
test_huatuo_bamai_e2e_container_create
110+
test_huatuo_bamai_e2e_container_delete
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
export BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17+
set -euo pipefail
1818

19-
# Run the core integration tests.
20-
unshare --uts --mount bash -c '
21-
mount --make-rprivate /
22-
echo "huatuo-dev" > /proc/sys/kernel/hostname
23-
hostname huatuo-dev 2>/dev/null || true
19+
source ${ROOT_DIR}/integration/lib.sh
2420

25-
set -euo pipefail
26-
source "${BASEDIR}/utils.sh"
21+
test_huatuo_bamai_metrics() {
22+
log_info "⬅️ test huatuo-bamai metrics"
23+
for i in {1..10}; do
24+
huatuo_bamai_metrics >/dev/null
25+
sleep 0.2
26+
done
27+
log_info "✅ test huatuo-bamai metrics ok"
28+
}
2729

28-
# Always cleanup the tests.
29-
trap "test_teardown \$?" EXIT
30-
31-
test_setup
32-
test_metrics
33-
# more tests ...
34-
'
30+
test_huatuo_bamai_metrics

0 commit comments

Comments
 (0)