Skip to content

Commit

Permalink
Merge pull request #14 from vearne/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
vearne authored Jun 5, 2024
2 parents 86be4f8 + 4383fd3 commit eb2212a
Show file tree
Hide file tree
Showing 39 changed files with 2,921 additions and 103 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ autotest extract -x "//title" -j '[
## TODO
* [x] 1) support utilizing the script language Lua to ascertain the conformity of HTTP responses with expectations.
* [x] 2) output report to file
* [ ] 3) support automating tests for gRPC protocol-based API services.
* [x] 3) support automating tests for gRPC protocol-based API services.
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ autotest extract -x "//title" -j '[
## TODO
* [x] 1) 支持使用脚本语言Lua判断HTTP response是否符合预期
* [x] 2) 输出report到文件中
* [ ] 3) 支持对gRPC协议的API服务进行自动化测试
* [x] 3) 支持对gRPC协议的API服务进行自动化测试



3 changes: 2 additions & 1 deletion config_files/.env.dev
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
HOST=localhost:8080
HOST=localhost:8080
GRPC_SERVER=localhost:50031
6 changes: 3 additions & 3 deletions config_files/autotest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ global:
worker_num: 5
# default: true
ignore_testcase_fail: true
debug: false
debug: true
# Timeout setting for each request
request_timeout: 5s

logger:
# 1. "info"
# 2. "debug"
# 3. "error"
level: "info"
level: "debug"
file_path: "/var/log/test/autotest.log"
report:
dir_path: "/var/log/test/report/"
Expand All @@ -21,7 +21,7 @@ http_rule_files:
- "./config_files/my_http_api.yml"

grpc_rule_files:
# - "my_grpc_api.yml"
- "./config_files/my_grpc_api.yml"



144 changes: 144 additions & 0 deletions config_files/my_grpc_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
- id: 1
desc: "get one book"
request:
address: "{{ GRPC_SERVER }}"
symbol: "Bookstore/GetBook"
body: |
{
"id": 1
}
rules:
- name: "GrpcCodeEqualRule"
expected: "OK"
- name: "GrpcBodyEqualRule"
xpath: "/code"
expected: "Success"
- name: "GrpcBodyEqualRule"
xpath: "/data/title"
expected: "The Go Programming Language"

- id: 2
desc: "list all books"
request:
address: "{{ GRPC_SERVER }}"
symbol: "Bookstore/ListBook"
body: "{}"
rules:
- name: "GrpcCodeEqualRule"
expected: "OK"
- name: "GrpcBodyEqualRule"
xpath: "/code"
expected: "Success"
- name: "GrpcBodyAtLeastOneRule"
xpath: "//title"
expected: "The Go Programming Language"

- id: 3
desc: "add new book"
request:
address: "{{ GRPC_SERVER }}"
symbol: "Bookstore/AddBook"
body: |
{
"title": "title3",
"author": "author3"
}
rules:
- name: "GrpcCodeEqualRule"
expected: "OK"
- name: "GrpcBodyEqualRule"
xpath: "/code"
expected: "Success"
- name: "GrpcBodyEqualRule"
xpath: "/data/title"
expected: "title3"
export:
xpath: "/data/id"
# Extract the id value and export it to the variable MY_BOOK_ID
exportTo: "MY_BOOK_ID"
# default is string, optional value: integer | string | float
type: integer

- id: 4
desc: "get book 3"
dependOnIDs: [3]
request:
address: "{{ GRPC_SERVER }}"
symbol: "Bookstore/GetBook"
body: |
{
"id": {{ MY_BOOK_ID }}
}
rules:
- name: "GrpcCodeEqualRule"
expected: "OK"
- name: "GrpcBodyEqualRule"
xpath: "/code"
expected: "Success"
- name: "GrpcBodyEqualRule"
xpath: "//title"
expected: "title3"

- id: 5
desc: "update book 2"
request:
address: "{{ GRPC_SERVER }}"
symbol: "Bookstore/UpdateBook"
body: |
{
"id": 2,
"title": "title2-2",
"author": "author2-2"
}
rules:
- name: "GrpcCodeEqualRule"
expected: "OK"
- name: "GrpcBodyEqualRule"
xpath: "/code"
expected: "Success"
- name: "GrpcBodyEqualRule"
xpath: "/data/title"
expected: "title2-2"

- id: 6
desc: "get book 2"
delay: 1s
dependOnIDs: [5]
request:
address: "{{ GRPC_SERVER }}"
symbol: "Bookstore/GetBook"
body: |
{
"id": 2
}
rules:
- name: "GrpcCodeEqualRule"
expected: "OK"
- name: "GrpcBodyEqualRule"
xpath: "/code"
expected: "Success"
- name: "GrpcBodyEqualRule"
xpath: "/data/title"
expected: "title2-2"

- id: 7
desc: "get book 2"
delay: 1s
dependOnIDs: [6]
request:
address: "{{ GRPC_SERVER }}"
symbol: "Bookstore/GetBook"
body: |
{
"id": 2
}
rules:
- name: "GrpcCodeEqualRule"
expected: "OK"
- name: "GrpcLuaRule"
lua: |
function verify(r)
local json = require "json";
local body = json.decode(r:body());
return body.data.title == "title2-2" and body.data.author == "author2-2";
end
24 changes: 6 additions & 18 deletions config_files/my_http_api.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
- id: 1
desc: "list all books"
request:
method: "get"
url: "http://{{ HOST }}/api/books"
rules:
- name: "HttpStatusEqualRule"
expectedStatus: 200
- name: "HttpBodyAtLeastOneRule"
xpath: "//title"
Expected: "Effective Go"

- id: 2
desc: "add a new book"
request:
Expand All @@ -25,10 +13,10 @@
}
rules:
- name: "HttpStatusEqualRule"
expectedStatus: 200
expected: 200
- name: "HttpBodyEqualRule"
xpath: "/title"
Expected: "book3_title"
expected: "book3_title"
export:
xpath: "/id"
# Extract the id value and export it to the variable MY_BOOK_ID
Expand Down Expand Up @@ -56,10 +44,10 @@
}
rules:
- name: "HttpStatusEqualRule"
expectedStatus: 200
expected: 200
- name: "HttpBodyEqualRule"
xpath: "/author"
Expected: "book3_author-2"
expected: "book3_author-2"
- name: "HttpLuaRule"
lua: |
function verify(r)
Expand All @@ -78,7 +66,7 @@
url: "http://{{ HOST }}/api/books/1"
rules:
- name: "HttpStatusEqualRule"
expectedStatus: 204
expected: 204

- id: 5
desc: "try to get book1"
Expand All @@ -91,4 +79,4 @@
url: "http://{{ HOST }}/api/books/1"
rules:
- name: "HttpStatusEqualRule"
expectedStatus: 404
expected: 404
File renamed without changes.
6 changes: 5 additions & 1 deletion docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ services:
httpapi:
image: woshiaotian/httpapi:latest
ports:
- '8080:8080'
- '8080:8080'
grpcapi:
image: woshiaotian/grpcapi:latest
ports:
- '50031:50031'
30 changes: 30 additions & 0 deletions example/grpc_api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 使用更新的 golang 基础镜像
FROM golang:1.21 AS builder

ARG PROJECT="github.com/vearne/autotest/example/grpc_api"

# Copy代码
ADD . $GOPATH/src/$PROJECT
# 设置工作目录
WORKDIR $GOPATH/src/$PROJECT

# 下载依赖
RUN go mod download

# 使用 CGO_DISABLED=0 来构建 Go 二进制文件
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /app/grpcapi .

# 使用轻量级的 Alpine Linux 基础镜像
FROM alpine:latest

# 设置工作目录
WORKDIR /app

# 从构建阶段将二进制文件复制到最终镜像中
COPY --from=builder /app/grpcapi .

# 暴露应用运行的端口
EXPOSE 50031

# 运行二进制文件
CMD ["./grpcapi"]
11 changes: 11 additions & 0 deletions example/grpc_api/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
LDFLAGS := -ldflags "-s -w"
IMAGE := woshiaotian/grpcapi:latest

.PHONY: build
build:
env go build ${LDFLAGS} -o grpcapi ./

.PHONY: image-multiple
image-multiple:
docker buildx build -f ./Dockerfile \
--platform linux/amd64,linux/arm64 --push -t ${IMAGE} .
4 changes: 4 additions & 0 deletions example/grpc_api/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## generate *.pb.go
```
protoc --go_out=plugins=grpc:. ./proto/*.proto
```
25 changes: 25 additions & 0 deletions example/grpc_api/client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# list
grpcurl --plaintext 127.0.0.1:50031 list

# describe
grpcurl --plaintext 127.0.0.1:50031 describe

# add book
grpcurl --plaintext -emit-defaults -d '{"title": "title3","author": "author3"}'\
127.0.0.1:50031 Bookstore/AddBook

# delete book
grpcurl --plaintext -emit-defaults -d '{"id": 1}'\
127.0.0.1:50031 Bookstore/DeleteBook

# update book
grpcurl --plaintext -emit-defaults -d '{"id":2, "title": "title2-2","author": "author2-2"}'\
127.0.0.1:50031 Bookstore/UpdateBook

# list book
grpcurl --plaintext -emit-defaults -d '{}'\
127.0.0.1:50031 Bookstore/ListBook

# get book
grpcurl --plaintext -emit-defaults -d '{"id":1}'\
127.0.0.1:50031 Bookstore/GetBook
12 changes: 12 additions & 0 deletions example/grpc_api/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/vearne/autotest/example/grpc_api

go 1.21.0

require (
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
12 changes: 12 additions & 0 deletions example/grpc_api/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
Loading

0 comments on commit eb2212a

Please sign in to comment.