Skip to content

Commit ae291c9

Browse files
committed
add auto test
1 parent ff1a0e6 commit ae291c9

18 files changed

+868
-0
lines changed

test/auto-test/1_web_gin.sh

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
3+
webServiceName="user"
4+
webDir="1_web_gin_${webServiceName}"
5+
6+
mysqlDSN="root:123456@(192.168.3.37:3306)/school"
7+
mysqlTable="teacher"
8+
9+
function checkResult() {
10+
result=$1
11+
if [ ${result} -ne 0 ]; then
12+
exit ${result}
13+
fi
14+
}
15+
16+
function stopService() {
17+
pid=$(ps -ef | grep "./cmd/${webServiceName}" | grep -v grep | awk '{print $2}')
18+
if [ "${pid}" != "" ]; then
19+
kill -9 ${pid}
20+
fi
21+
}
22+
23+
function testRequest() {
24+
echo "--------------------- 20s 后测试开始 ---------------------"
25+
sleep 20
26+
for i in {1..3}; do
27+
echo -e "\n\n"
28+
echo "${i} 获取详情 [GET] curl http://localhost:8080/api/v1/teacher/1"
29+
curl http://localhost:8080/api/v1/teacher/1
30+
echo -e "\n\n"
31+
sleep 3
32+
33+
echo -e "\n\n"
34+
echo "${i} 获取列表 [GET] curl http://localhost:8080/api/v1/teacher/list"
35+
curl http://localhost:8080/api/v1/teacher/list
36+
echo -e "\n\n"
37+
sleep 3
38+
39+
echo -e "\n\n"
40+
echo ${i}' 获取列表 [POST] curl -X POST http://localhost:8080/api/v1/teacher/list -H "X-Request-Id: qaz12wx3ed4" -H "Content-Type: application/json" -d "{\"columns\":[{\"exp\":\">\",\"name\":\"id\",\"value\":1}],\"page\":0,\"size\":10}"'
41+
curl -X POST http://localhost:8080/api/v1/teacher/list -H "X-Request-Id: qaz12wx3ed4" -H "Content-Type: application/json" -d "{\"columns\":[{\"exp\":\">\",\"name\":\"id\",\"value\":1}],\"page\":0,\"size\":10}"
42+
echo -e "\n\n"
43+
sleep 3
44+
done
45+
echo ""
46+
echo "--------------------- 测试结束!---------------------"
47+
stopService
48+
}
49+
50+
if [ -d "${webDir}" ]; then
51+
echo "web服务 ${webDir} 已存在"
52+
else
53+
echo "创建web服务 ${webDir}"
54+
sponge web http \
55+
--module-name=${webServiceName} \
56+
--server-name=${webServiceName} \
57+
--project-name=webdemo \
58+
--db-dsn=${mysqlDSN} \
59+
--db-table=${mysqlTable} \
60+
--out=./${webDir}
61+
checkResult $?
62+
fi
63+
64+
cd ${webDir}
65+
66+
echo "make docs"
67+
make docs
68+
checkResult $?
69+
70+
echo "test request"
71+
testRequest &
72+
73+
echo "make run"
74+
make run
75+
checkResult $?

test/auto-test/2_micro_grpc.sh

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
3+
grpcServiceName="user"
4+
grpcDir="2_micro_grpc_${grpcServiceName}"
5+
6+
mysqlDSN="root:123456@(192.168.3.37:3306)/school"
7+
mysqlTable="teacher"
8+
9+
function checkResult() {
10+
result=$1
11+
if [ ${result} -ne 0 ]; then
12+
exit ${result}
13+
fi
14+
}
15+
16+
function stopService() {
17+
pid=$(ps -ef | grep "./cmd/${grpcServiceName}" | grep -v grep | awk '{print $2}')
18+
if [ "${pid}" != "" ]; then
19+
kill -9 ${pid}
20+
fi
21+
}
22+
23+
function testRequest() {
24+
echo "--------------------- 20s 后测试开始 ---------------------"
25+
sleep 20
26+
cd internal/service
27+
for i in {1..3}; do
28+
echo -e "\n\n"
29+
echo "${i} 获取详情 go test -run Test_service_teacher_methods/GetByID id=1"
30+
sed -i "s/Id: 0,/Id: 1,/g" teacher_client_test.go
31+
go test -run Test_service_teacher_methods/GetByID
32+
sed -i "s/Id: 1,/Id: 0,/g" teacher_client_test.go
33+
echo -e "\n\n"
34+
sleep 3
35+
36+
echo -e "\n\n"
37+
echo "${i} 获取列表 go test -run Test_service_teacher_methods/ListByLastID"
38+
sed -i "s/Limit: 0,/Limit: 3,/g" teacher_client_test.go
39+
go test -run Test_service_teacher_methods/ListByLastID
40+
sed -i "s/Limit: 3,/Limit: 0,/g" teacher_client_test.go
41+
echo -e "\n\n"
42+
sleep 3
43+
done
44+
cd -
45+
echo "--------------------- 测试结束!---------------------"
46+
stopService
47+
}
48+
49+
if [ -d "${grpcDir}" ]; then
50+
echo "微服务 ${grpcDir} 已存在"
51+
else
52+
echo "创建微服务 ${grpcDir}"
53+
sponge micro rpc \
54+
--module-name=${grpcServiceName} \
55+
--server-name=${grpcServiceName} \
56+
--project-name=grpcdemo \
57+
--db-dsn=${mysqlDSN} \
58+
--db-table=${mysqlTable} \
59+
--out=./${grpcDir}
60+
checkResult $?
61+
fi
62+
63+
cd ${grpcDir}
64+
checkResult $?
65+
66+
echo "make proto"
67+
make proto
68+
checkResult $?
69+
70+
echo "test request"
71+
testRequest &
72+
73+
echo "make run"
74+
make run
75+
checkResult $?

test/auto-test/3_web_gin_pb.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
webServiceName="user"
4+
webDir="3_web_gin_pb_${webServiceName}"
5+
6+
function checkResult() {
7+
result=$1
8+
if [ ${result} -ne 0 ]; then
9+
exit ${result}
10+
fi
11+
}
12+
13+
function stopService() {
14+
pid=$(ps -ef | grep "./cmd/${webServiceName}" | grep -v grep | awk '{print $2}')
15+
if [ "${pid}" != "" ]; then
16+
kill -9 ${pid}
17+
fi
18+
}
19+
20+
function testRequest() {
21+
echo "--------------------- 20s 后测试开始 ---------------------"
22+
sleep 20
23+
for i in {1..3}; do
24+
echo -e "\n\n"
25+
echo ${i} 'curl -X POST http://localhost:8080/api/v1/auth/register -H "Content-Type: application/json" -d "{\"email\":\"foo@bar.com\",\"password\":\"123456\"}"'
26+
curl -X POST http://localhost:8080/api/v1/auth/register -H "Content-Type: application/json" -d "{\"email\":\"foo@bar.com\",\"password\":\"123456\"}"
27+
echo -e "\n\n"
28+
sleep 3
29+
30+
echo -e "\n\n"
31+
echo 'curl -X POST http://localhost:8080/api/v1/auth/register -H "Content-Type: application/json" -H "X-Request-Id: qaz12wx3ed4" -d "{\"email\":\"foo@bar.com\",\"password\":\"123456\"}"'
32+
curl -X POST http://localhost:8080/api/v1/auth/register -H "Content-Type: application/json" -H "X-Request-Id: qaz12wx3ed4" -d "{\"email\":\"foo@bar.com\",\"password\":\"123456\"}"
33+
echo -e "\n\n"
34+
sleep 3
35+
done
36+
echo "--------------------- 测试结束!---------------------"
37+
stopService
38+
}
39+
40+
if [ -d "${webDir}" ]; then
41+
echo "web服务 ${webDir} 已存在"
42+
else
43+
echo "创建web服务 ${webDir}"
44+
sponge web http-pb \
45+
--module-name=${webServiceName} \
46+
--server-name=${webServiceName} \
47+
--project-name=ginpbdemo \
48+
--protobuf-file=./files/user.proto \
49+
--out=./${webDir}
50+
checkResult $?
51+
fi
52+
53+
cd ${webDir}
54+
checkResult $?
55+
56+
echo "make proto"
57+
make proto
58+
checkResult $?
59+
60+
echo "替换示例模板代码"
61+
replaceCode ../files/web_gin_pb_content ./internal/handler/user.go
62+
checkResult $?
63+
64+
echo "test request"
65+
testRequest &
66+
67+
echo "make run"
68+
make run
69+
checkResult $?

test/auto-test/4_micro_grpc_pb.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
3+
grpcServiceName="user"
4+
grpcDir="4_micro_grpc_pb_${grpcServiceName}"
5+
6+
function checkResult() {
7+
result=$1
8+
if [ ${result} -ne 0 ]; then
9+
exit ${result}
10+
fi
11+
}
12+
13+
function stopService() {
14+
pid=$(ps -ef | grep "./cmd/${grpcServiceName}" | grep -v grep | awk '{print $2}')
15+
if [ "${pid}" != "" ]; then
16+
kill -9 ${pid}
17+
fi
18+
}
19+
20+
function testRequest() {
21+
echo "--------------------- 20s 后测试开始 ---------------------"
22+
sleep 20
23+
cd internal/service
24+
for i in {1..3}; do
25+
echo -e "\n\n"
26+
echo "${i} go test -run Test_service_user_methods/Register"
27+
go test -run Test_service_user_methods/Register
28+
echo -e "\n\n"
29+
done
30+
cd -
31+
echo "--------------------- 测试结束!---------------------"
32+
stopService
33+
}
34+
35+
if [ -d "${grpcDir}" ]; then
36+
echo "微服务 ${grpcDir} 已存在"
37+
else
38+
echo "创建微服务 ${grpcDir}"
39+
sponge micro rpc-pb \
40+
--module-name=${grpcServiceName} \
41+
--server-name=${grpcServiceName} \
42+
--project-name=grpcpbdemo \
43+
--protobuf-file=./files/user2.proto \
44+
--out=./${grpcDir}
45+
checkResult $?
46+
fi
47+
48+
cd ${grpcDir}
49+
checkResult $?
50+
51+
echo "make proto"
52+
make proto
53+
checkResult $?
54+
55+
echo "替换示例模板代码"
56+
replaceCode ../files/micro_grpc_pb_content ./internal/service/user2.go
57+
checkResult $?
58+
replaceCode ../files/micro_grpc_pb_content2 ./internal/service/user2_client_test.go
59+
checkResult $?
60+
61+
echo "test request"
62+
testRequest &
63+
64+
echo "make run"
65+
make run
66+
checkResult $?

0 commit comments

Comments
 (0)