Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add consumer e2e test #735

Merged
merged 5 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/test/e2e/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type HttpTestCase struct {
ExpectCode int
ExpectMessage string
ExpectBody string
PartialBody string
ExpectHeaders map[string]string
Sleep time.Duration //ms
}
Expand Down Expand Up @@ -167,4 +168,9 @@ func testCaseCheck(tc HttpTestCase) {
resp.Body().Contains(tc.ExpectBody)
}

//Partial body
if tc.PartialBody != "" {
resp.Body().Contains(tc.PartialBody)
}

}
241 changes: 241 additions & 0 deletions api/test/e2e/consumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package e2e

import (
"net/http"
"testing"
)

//CASE 1: add consumer with username
func TestConsumer_add_consumer_with_username(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumer",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "jack",
"plugins": {
"key-auth": {
"key": "auth-one"
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "create route",
Object: MangerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uri": "/hello",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"172.16.238.20:1980": 1
}
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "verify route",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
Headers: map[string]string{"apikey": "auth-one"},
ExpectStatus: http.StatusOK,
Sleep: sleepTime, //sleep x millisecond before verify route
},
{
caseDesc: "verify route without auth",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
ExpectStatus: http.StatusUnauthorized,
Sleep: sleepTime,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to delete the consumer jack at the end of the test case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may cause wrong for other test cases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I delete the created data uniformly in the last function TestConsumer_teardown

}
idbeta marked this conversation as resolved.
Show resolved Hide resolved

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

//CASE 2: add consumer without username
func TestConsumer_add_consumer_without_username(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumer",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"plugins": {
"key-auth": {
"key": "auth-new"
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
},
{
caseDesc: "verify route",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
Headers: map[string]string{"apikey": "auth-new"},
ExpectStatus: http.StatusUnauthorized,
Sleep: sleepTime,
},
}

idbeta marked this conversation as resolved.
Show resolved Hide resolved
for _, tc := range tests {
testCaseCheck(tc)
}
}

//CASE 3: add consumer with labels
func TestConsumer_add_consumer_with_labels(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumer",
Object: MangerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "jack2",
"labels": {
"build":"16",
"env":"production",
"version":"v2"
},
"plugins": {
"key-auth": {
"key": "auth-two"
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "verify consumer",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/apisix/admin/consumers/jack2",
Headers: map[string]string{"X-API-KEY": "edd1c9f034335f136f87ad84b625c8f1"},
ExpectStatus: http.StatusOK,
PartialBody: "\"env\":\"production\"",
Sleep: sleepTime,
},
{
caseDesc: "create route",
Object: MangerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uri": "/hello",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"172.16.238.20:1980": 1
}
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "verify route",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
Headers: map[string]string{"apikey": "auth-two"},
ExpectStatus: http.StatusOK,
Sleep: sleepTime, //sleep x millisecond before verify route
},
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My purpose is to prevent the two use cases from coupling.

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

//CASE 4: delete consumer
func TestConsumer_delete_consumer(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "delete consumer",
Object: APISIXExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/jack",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use admin api to do controller plane job?

Headers: map[string]string{"X-API-KEY": "edd1c9f034335f136f87ad84b625c8f1"},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "verify route",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusUnauthorized,
Sleep: sleepTime, //sleep x millisecond before verify route
},
}

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

//CASE 5: Teardown
func TestConsumer_teardown(t *testing.T) {
_ = []HttpTestCase{
{
caseDesc: "delete route",
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/routes/r1",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "delete consumer",
Object: APISIXExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/jack",
Headers: map[string]string{"X-API-KEY": "edd1c9f034335f136f87ad84b625c8f1"},
ExpectStatus: http.StatusOK,
},
}
}