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 e2e test for config route with service_id or upstream_id #810

Merged
merged 10 commits into from
Nov 18, 2020
209 changes: 209 additions & 0 deletions api/test/e2e/route_service_upstream_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
* 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"
)

func TestRoute_Invalid_Service_And_Service(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "use service that not exist",
Object: MangerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uri": "/hello_",
"service_id": "not-exists"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
},
Copy link
Member

Choose a reason for hiding this comment

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

only test controller plane(MangerApiExpect) is not enough, we MUST test data plane(APISIXExpect) at the same time.

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 check whether this route is valid on the data plane

{
caseDesc: "use upstream that not exist",
Object: MangerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uri": "/hello_",
"upstream_id": "not-exists"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
},
Copy link
Member

Choose a reason for hiding this comment

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

ditto

{
caseDesc: "create service and upstream together at the same time",
Object: MangerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uri": "/hello_",
"service_id": "not-exists-service",
"upstream_id": "not-exists-upstream"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
Copy link
Member

Choose a reason for hiding this comment

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

ditto

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

func TestRoute_Create_Upstream(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create upstream",
Object: MangerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Body: `{
"nodes": [{
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 check nodes, not only one node.

"host": "172.16.238.20",
"port": 1980,
"weight": 1
}],
"type": "roundrobin"
}`,
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "create route using the upstream just created",
Object: MangerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uri": "/server_port",
Copy link
Member

Choose a reason for hiding this comment

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

if we only enabled one node, please use "/hello". for /server_port, it mainly used to test multiple nodes

"upstream_id": "1"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
{
caseDesc: "hit the route just created",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/server_port",
ExpectStatus: http.StatusOK,
ExpectBody: "1980",
Copy link
Member

Choose a reason for hiding this comment

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

the response body is 1980?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After studying the current test sample and realizing that the same response was used here[1], I wrote this as well.
Thanks for your review, I read nginx.conf[2] and docker-compose.yaml[3] and found that this server.lua[4] script is used, there is a server_port function in the server.lua script, so the printout here is port

[1] https://github.com/apache/apisix-dashboard/blob/v2.0/api/test/e2e/upstream_test.go#L73
[2] https://github.com/apache/apisix-dashboard/blob/v2.0/api/test/docker/upstream.conf#L47
[3] https://github.com/apache/apisix-dashboard/blob/v2.0/api/test/docker/docker-compose.yaml#L116
[4] https://github.com/apache/apisix/blob/master/t/lib/server.lua#L59

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

image

I'm not sure if Golang will convert this type?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not family with Go test, I'm confused why the response body is only the port number?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After studying the current test sample and realizing that the same response was used here[1], I wrote this as well.
Thanks for your review, I read nginx.conf[2] and docker-compose.yaml[3] and found that this server.lua[4] script is used, there is a server_port function in the server.lua script, so the printout here is port

[1] https://github.com/apache/apisix-dashboard/blob/v2.0/api/test/e2e/upstream_test.go#L73
[2] https://github.com/apache/apisix-dashboard/blob/v2.0/api/test/docker/upstream.conf#L47
[3] https://github.com/apache/apisix-dashboard/blob/v2.0/api/test/docker/docker-compose.yaml#L116
[4] https://github.com/apache/apisix/blob/master/t/lib/server.lua#L59

Copy link
Member

Choose a reason for hiding this comment

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

I'm not family with Go test, I'm confused why the response body is only the port number?

here is the reason: #810 (comment)

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

func TestRoute_Create_Service(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create service",
Object: MangerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/services/200",
Body: `{
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "172.16.238.20",
"port": 1980,
"weight": 1
}]
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "create route using the service just created",
Object: MangerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r2",
Body: `{
"uri": "/hello",
"service_id": "200"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
{
caseDesc: "hit the route just created",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
ExpectStatus: http.StatusOK,
ExpectBody: "hello world\n",
Sleep: sleepTime,
},
}
for _, tc := range tests {
testCaseCheck(tc)
}
}

func TestRoute_Delete_Upstream(t *testing.T) {
tests := []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: "remove upstream",
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/upstreams/1",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
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 check whether this route deleted on the data plane

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

func TestRoute_Delete_Service(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "delete route",
Object: MangerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/routes/r2",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "remove service",
Object: MangerApiExpect(t),
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Method: http.MethodDelete,
Path: "/apisix/admin/services/200",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
}
for _, tc := range tests {
testCaseCheck(tc)
}
}