From 4bf213026b953de984135e25203764845c6b0f36 Mon Sep 17 00:00:00 2001 From: idbeta Date: Mon, 9 Nov 2020 17:21:11 +0800 Subject: [PATCH 1/4] add consumer e2e test add public method "PartialBody" --- api/test/e2e/base.go | 6 + api/test/e2e/consumer_test.go | 274 ++++++++++++++++++++++++++++++++++ 2 files changed, 280 insertions(+) create mode 100644 api/test/e2e/consumer_test.go diff --git a/api/test/e2e/base.go b/api/test/e2e/base.go index 71b61b5609..45deb4cbe3 100644 --- a/api/test/e2e/base.go +++ b/api/test/e2e/base.go @@ -80,6 +80,7 @@ type HttpTestCase struct { ExpectCode int ExpectMessage string ExpectBody string + PartialBody string Sleep time.Duration //ms } @@ -131,4 +132,9 @@ func testCaseCheck(tc HttpTestCase) { if tc.ExpectBody != "" { resp.Body().Equal(tc.ExpectBody) } + + //Partial body + if tc.PartialBody != "" { + resp.Body().Contains(tc.PartialBody) + } } diff --git a/api/test/e2e/consumer_test.go b/api/test/e2e/consumer_test.go new file mode 100644 index 0000000000..a66c9764e8 --- /dev/null +++ b/api/test/e2e/consumer_test.go @@ -0,0 +1,274 @@ +/* + * 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" + ) + +//TEST 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 + }, + } + + for _, tc := range tests { + testCaseCheck(tc) + } +} + +//TEST 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-one" + } + }, + "desc": "test description" + }`, + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusBadRequest, + }, + } + + for _, tc := range tests { + testCaseCheck(tc) + } +} + +//TEST 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": "jack", + "labels": { + "build":"16", + "env":"production", + "version":"v2" + }, + "plugins": { + "key-auth": { + "key": "auth-one" + } + }, + "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/jack", + Headers: map[string]string{"X-API-KEY": "edd1c9f034335f136f87ad84b625c8f1"}, + ExpectStatus: http.StatusOK, + PartialBody: "\"env\":\"production\"", + Sleep: sleepTime, //sleep x millisecond before verify route + }, + { + 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 + }, + } + + for _, tc := range tests { + testCaseCheck(tc) + } +} + +//TEST 4: delete consumer +func TestConsumer_delete_consumer(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: "delete consumer", + Object: APISIXExpect(t), + Method: http.MethodDelete, + Path: "/apisix/admin/consumers/jack", + 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) + } +} + + +//Test 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, + }, + } +} \ No newline at end of file From 9d1dd7702ae0a219c0fd338a31e27e1dc299ab57 Mon Sep 17 00:00:00 2001 From: idbeta Date: Mon, 9 Nov 2020 17:29:47 +0800 Subject: [PATCH 2/4] test: add end with EOL --- api/test/e2e/consumer_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/test/e2e/consumer_test.go b/api/test/e2e/consumer_test.go index a66c9764e8..e73f203957 100644 --- a/api/test/e2e/consumer_test.go +++ b/api/test/e2e/consumer_test.go @@ -271,4 +271,4 @@ func TestConsumer_teardown(t *testing.T) { ExpectStatus: http.StatusOK, }, } -} \ No newline at end of file +} From 3e3ccdb273d439b418e6880dd3a5c84a7be2383e Mon Sep 17 00:00:00 2001 From: idbeta Date: Mon, 9 Nov 2020 18:23:13 +0800 Subject: [PATCH 3/4] add test data plane to case 2 delete some useless code --- api/test/e2e/consumer_test.go | 85 ++++++++++++----------------------- 1 file changed, 28 insertions(+), 57 deletions(-) diff --git a/api/test/e2e/consumer_test.go b/api/test/e2e/consumer_test.go index e73f203957..ede9fb4d03 100644 --- a/api/test/e2e/consumer_test.go +++ b/api/test/e2e/consumer_test.go @@ -21,7 +21,7 @@ "testing" ) -//TEST 1: add consumer with username +//CASE 1: add consumer with username func TestConsumer_add_consumer_with_username(t *testing.T) { tests := []HttpTestCase{ { @@ -70,6 +70,14 @@ 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, + }, } for _, tc := range tests { @@ -77,7 +85,7 @@ } } -//TEST 2: add consumer without username +//CASE 2: add consumer without username func TestConsumer_add_consumer_without_username(t *testing.T) { tests := []HttpTestCase{ { @@ -88,7 +96,7 @@ func TestConsumer_add_consumer_without_username(t *testing.T) { Body: `{ "plugins": { "key-auth": { - "key": "auth-one" + "key": "auth-new" } }, "desc": "test description" @@ -96,6 +104,15 @@ func TestConsumer_add_consumer_without_username(t *testing.T) { 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, + }, } for _, tc := range tests { @@ -103,7 +120,7 @@ func TestConsumer_add_consumer_without_username(t *testing.T) { } } -//TEST 3: add consumer with labels +//CASE 3: add consumer with labels func TestConsumer_add_consumer_with_labels(t *testing.T) { tests := []HttpTestCase{ { @@ -112,7 +129,7 @@ func TestConsumer_add_consumer_with_labels(t *testing.T) { Path: "/apisix/admin/consumers", Method: http.MethodPut, Body: `{ - "username": "jack", + "username": "jack2", "labels": { "build":"16", "env":"production", @@ -120,7 +137,7 @@ func TestConsumer_add_consumer_with_labels(t *testing.T) { }, "plugins": { "key-auth": { - "key": "auth-one" + "key": "auth-two" } }, "desc": "test description" @@ -132,11 +149,11 @@ func TestConsumer_add_consumer_with_labels(t *testing.T) { caseDesc: "verify consumer", Object: APISIXExpect(t), Method: http.MethodGet, - Path: "/apisix/admin/consumers/jack", + Path: "/apisix/admin/consumers/jack2", Headers: map[string]string{"X-API-KEY": "edd1c9f034335f136f87ad84b625c8f1"}, ExpectStatus: http.StatusOK, PartialBody: "\"env\":\"production\"", - Sleep: sleepTime, //sleep x millisecond before verify route + Sleep: sleepTime, }, { caseDesc: "create route", @@ -163,7 +180,7 @@ func TestConsumer_add_consumer_with_labels(t *testing.T) { Object: APISIXExpect(t), Method: http.MethodGet, Path: "/hello", - Headers: map[string]string{"apikey": "auth-one"}, + Headers: map[string]string{"apikey": "auth-two"}, ExpectStatus: http.StatusOK, Sleep: sleepTime, //sleep x millisecond before verify route }, @@ -174,55 +191,9 @@ func TestConsumer_add_consumer_with_labels(t *testing.T) { } } -//TEST 4: delete consumer +//CASE 4: delete consumer func TestConsumer_delete_consumer(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: "delete consumer", Object: APISIXExpect(t), @@ -251,7 +222,7 @@ func TestConsumer_delete_consumer(t *testing.T) { } -//Test 5: Teardown +//CASE 5: Teardown func TestConsumer_teardown(t *testing.T) { _ = []HttpTestCase{ { From c77895c46124acd91d423e9ea3ba7ec4e2a510ca Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 10 Nov 2020 14:12:28 +0800 Subject: [PATCH 4/4] modify code style --- api/test/e2e/consumer_test.go | 40 ++++++++++++++++------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/api/test/e2e/consumer_test.go b/api/test/e2e/consumer_test.go index ede9fb4d03..b8af9e296a 100644 --- a/api/test/e2e/consumer_test.go +++ b/api/test/e2e/consumer_test.go @@ -14,15 +14,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package e2e +package e2e - import ( - "net/http" - "testing" - ) +import ( + "net/http" + "testing" +) //CASE 1: add consumer with username - func TestConsumer_add_consumer_with_username(t *testing.T) { +func TestConsumer_add_consumer_with_username(t *testing.T) { tests := []HttpTestCase{ { caseDesc: "create consumer", @@ -36,8 +36,8 @@ "key": "auth-one" } }, - "desc": "test description" - }`, + "desc": "test description" + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, }, @@ -57,7 +57,7 @@ "172.16.238.20:1980": 1 } } - }`, + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, }, @@ -76,7 +76,7 @@ Method: http.MethodGet, Path: "/hello", ExpectStatus: http.StatusUnauthorized, - Sleep: sleepTime, + Sleep: sleepTime, }, } @@ -99,8 +99,8 @@ func TestConsumer_add_consumer_without_username(t *testing.T) { "key": "auth-new" } }, - "desc": "test description" - }`, + "desc": "test description" + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusBadRequest, }, @@ -140,9 +140,9 @@ func TestConsumer_add_consumer_with_labels(t *testing.T) { "key": "auth-two" } }, - "desc": "test description" - }`, - Headers: map[string]string{"Authorization": token}, + "desc": "test description" + }`, + Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, }, { @@ -152,8 +152,8 @@ func TestConsumer_add_consumer_with_labels(t *testing.T) { Path: "/apisix/admin/consumers/jack2", Headers: map[string]string{"X-API-KEY": "edd1c9f034335f136f87ad84b625c8f1"}, ExpectStatus: http.StatusOK, - PartialBody: "\"env\":\"production\"", - Sleep: sleepTime, + PartialBody: "\"env\":\"production\"", + Sleep: sleepTime, }, { caseDesc: "create route", @@ -171,7 +171,7 @@ func TestConsumer_add_consumer_with_labels(t *testing.T) { "172.16.238.20:1980": 1 } } - }`, + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, }, @@ -211,9 +211,6 @@ func TestConsumer_delete_consumer(t *testing.T) { ExpectStatus: http.StatusUnauthorized, Sleep: sleepTime, //sleep x millisecond before verify route }, - - - } for _, tc := range tests { @@ -221,7 +218,6 @@ func TestConsumer_delete_consumer(t *testing.T) { } } - //CASE 5: Teardown func TestConsumer_teardown(t *testing.T) { _ = []HttpTestCase{