Skip to content

Commit

Permalink
ci: display more meaningful information when running E2E test cases f…
Browse files Browse the repository at this point in the history
…ailed (#1012)

* ci:  display more meaningful information when running E2E test cases fail
  • Loading branch information
nic-chen authored Dec 24, 2020
1 parent 46bf1ef commit c3e1d16
Show file tree
Hide file tree
Showing 30 changed files with 992 additions and 989 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend-e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
- name: run test
working-directory: ./api/test/e2e
run: go test
run: go test -v
56 changes: 28 additions & 28 deletions api/test/e2e/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (
func TestBalancer_roundrobin_with_weight(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create upstream (roundrobin with same weight)",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Desc: "create upstream (roundrobin with same weight)",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Body: `{
"nodes": [{
"host": "172.16.238.20",
Expand All @@ -53,10 +53,10 @@ func TestBalancer_roundrobin_with_weight(t *testing.T) {
ExpectStatus: http.StatusOK,
},
{
caseDesc: "create route using the upstream just created",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/1",
Desc: "create route using the upstream just created",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/1",
Body: `{
"uri": "/server_port",
"upstream_id": "1"
Expand All @@ -68,7 +68,7 @@ func TestBalancer_roundrobin_with_weight(t *testing.T) {
}

for _, tc := range tests {
testCaseCheck(tc)
testCaseCheck(tc, t)
}

// hit routes
Expand All @@ -82,10 +82,10 @@ func TestBalancer_roundrobin_with_weight(t *testing.T) {

tests = []HttpTestCase{
{
caseDesc: "create upstream (roundrobin with different weight)",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Desc: "create upstream (roundrobin with different weight)",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Body: `{
"nodes": [{
"host": "172.16.238.20",
Expand All @@ -109,7 +109,7 @@ func TestBalancer_roundrobin_with_weight(t *testing.T) {
},
}
for _, tc := range tests {
testCaseCheck(tc)
testCaseCheck(tc, t)
}

// hit routes
Expand All @@ -123,10 +123,10 @@ func TestBalancer_roundrobin_with_weight(t *testing.T) {

tests = []HttpTestCase{
{
caseDesc: "create upstream (roundrobin with weight 1 and 0) ",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Desc: "create upstream (roundrobin with weight 1 and 0) ",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Body: `{
"nodes": [{
"host": "172.16.238.20",
Expand All @@ -145,7 +145,7 @@ func TestBalancer_roundrobin_with_weight(t *testing.T) {
},
}
for _, tc := range tests {
testCaseCheck(tc)
testCaseCheck(tc, t)
}

// hit routes
Expand All @@ -157,10 +157,10 @@ func TestBalancer_roundrobin_with_weight(t *testing.T) {

tests = []HttpTestCase{
{
caseDesc: "create upstream (roundrobin with weight only 1 ) ",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Desc: "create upstream (roundrobin with weight only 1 ) ",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Body: `{
"nodes": [{
"host": "172.16.238.20",
Expand All @@ -174,7 +174,7 @@ func TestBalancer_roundrobin_with_weight(t *testing.T) {
},
}
for _, tc := range tests {
testCaseCheck(tc)
testCaseCheck(tc, t)
}

// hit routes
Expand All @@ -188,23 +188,23 @@ func TestBalancer_roundrobin_with_weight(t *testing.T) {
func TestBalancer_Delete(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "delete route",
Desc: "delete route",
Object: ManagerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/routes/1",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "delete upstream",
Desc: "delete upstream",
Object: ManagerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/upstreams/1",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "hit the route just deleted",
Desc: "hit the route just deleted",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/server_port",
Expand All @@ -215,6 +215,6 @@ func TestBalancer_Delete(t *testing.T) {
}

for _, tc := range tests {
testCaseCheck(tc)
testCaseCheck(tc, t)
}
}
106 changes: 54 additions & 52 deletions api/test/e2e/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func BatchTestServerPort(t *testing.T, times int) map[string]int {
var sleepTime = time.Duration(300) * time.Millisecond

type HttpTestCase struct {
caseDesc string
Desc string
Object *httpexpect.Expect
Method string
Path string
Expand All @@ -170,66 +170,68 @@ type HttpTestCase struct {
Sleep time.Duration //ms
}

func testCaseCheck(tc HttpTestCase) {
//init
expectObj := tc.Object
var req *httpexpect.Request
switch tc.Method {
case http.MethodGet:
req = expectObj.GET(tc.Path)
case http.MethodPut:
req = expectObj.PUT(tc.Path)
case http.MethodPost:
req = expectObj.POST(tc.Path)
case http.MethodDelete:
req = expectObj.DELETE(tc.Path)
case http.MethodPatch:
req = expectObj.PATCH(tc.Path)
case http.MethodOptions:
req = expectObj.OPTIONS(tc.Path)
default:
}
func testCaseCheck(tc HttpTestCase, t *testing.T) {
t.Run(tc.Desc, func(t *testing.T) {
//init
expectObj := tc.Object
var req *httpexpect.Request
switch tc.Method {
case http.MethodGet:
req = expectObj.GET(tc.Path)
case http.MethodPut:
req = expectObj.PUT(tc.Path)
case http.MethodPost:
req = expectObj.POST(tc.Path)
case http.MethodDelete:
req = expectObj.DELETE(tc.Path)
case http.MethodPatch:
req = expectObj.PATCH(tc.Path)
case http.MethodOptions:
req = expectObj.OPTIONS(tc.Path)
default:
}

if req == nil {
panic("fail to init request")
}
if req == nil {
panic("fail to init request")
}

if tc.Sleep != 0 {
time.Sleep(tc.Sleep)
}
if tc.Sleep != 0 {
time.Sleep(tc.Sleep)
}

if tc.Query != "" {
req.WithQueryString(tc.Query)
}
if tc.Query != "" {
req.WithQueryString(tc.Query)
}

//set header
for key, val := range tc.Headers {
req.WithHeader(key, val)
}
//set header
for key, val := range tc.Headers {
req.WithHeader(key, val)
}

//set body
if tc.Body != "" {
req.WithText(tc.Body)
}
//set body
if tc.Body != "" {
req.WithText(tc.Body)
}

//respond check
resp := req.Expect()
//respond check
resp := req.Expect()

//match http status
if tc.ExpectStatus != 0 {
resp.Status(tc.ExpectStatus)
}
//match http status
if tc.ExpectStatus != 0 {
resp.Status(tc.ExpectStatus)
}

//match headers
if tc.ExpectHeaders != nil {
for key, val := range tc.ExpectHeaders {
resp.Header(key).Equal(val)
//match headers
if tc.ExpectHeaders != nil {
for key, val := range tc.ExpectHeaders {
resp.Header(key).Equal(val)
}
}
}

//match body
if tc.ExpectBody != "" {
resp.Body().Contains(tc.ExpectBody)
}
//match body
if tc.ExpectBody != "" {
resp.Body().Contains(tc.ExpectBody)
}
})

}
Loading

0 comments on commit c3e1d16

Please sign in to comment.