Skip to content

Commit

Permalink
test: add e2e test for field hosts in route api (#612)
Browse files Browse the repository at this point in the history
* feat: ETCD cluster and APISIX cluster for CI

* feat: add test cases

* feat: add upstream service in docker compose

* fix: comment

* test: add test cases

* fix: code format

* test: add ci

* fix ci

* fix ci

* fix ci

* fix: remove consumer test

* test: sleep for sync

* test: e2e as an independent subproject

* fix CI error

* test: run docker

* fix: remove json schema generate script in docker build

* fix: check host and hosts config together

* fix ci

* test: add test cases for `host` in route

* remove useless code

* fix: using relative path to read conf (#617)

* fix: using  relative path

* fix path

* fix path

* fix conf path

* fix ci error

* fix etcd ip

* fix: code format

* run backend e2e test ci on v2 branch

* fix: code format

* fix: code format

* fix: CI error

* test: remove deploy CI again

* remove useless codes

* fix: go fmt

* test: don't use `go fmt`

* fix: code format

* fix: var name

* fix lint

* fix CI error

* debug

* fix: docker container name

* fix CI error

* fix CI error

* test: add more test cases

* fix CI error

* chore: remove useless code

* fix: go fmt

* fix: refactor test code

* fix: check body

* fix small issue

* fix: update docker compose

* fix CI

* test

* test

* test: build image first

* test: revert subnet ip in docker compose

* fix by review

* fix: docker compose

* test: add test cases

* fix: remove useless code

* fix: code format

* test: code format
  • Loading branch information
nic-chen authored Nov 2, 2020
1 parent 94d0245 commit 915ce83
Show file tree
Hide file tree
Showing 15 changed files with 887 additions and 260 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/e2e-test-backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Backend E2E Test

on:
push:
branches:
- master
pull_request:
branches:
- master
- v2.0

jobs:
run-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: install runtime
run: |
sudo apt-get update
sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update
export GO111MOUDULE=on
sudo apt install golang-1.14-go
- name: run docker compose
working-directory: ./api/test/docker
run: |
docker-compose up -d
sleep 5
docker logs docker_managerapi_1
- name: run test
working-directory: ./api/test/e2e
run: |
go test
2 changes: 1 addition & 1 deletion .github/workflows/test-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- name: run test
working-directory: ./api
run: go test ./...
run: go test $(go list ./... | grep -v /e2e)

- name: run with custom port
working-directory: ./api
Expand Down
14 changes: 1 addition & 13 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ RUN mkdir -p /go/manager-api \
&& mv /go/src/github.com/apisix/manager-api/entry.sh /go/manager-api/ \
&& mv /go/src/github.com/apisix/manager-api/build-tools/* /go/manager-api/build-tools/ \
&& mv /go/src/github.com/apisix/manager-api/conf/conf_preview.json /go/manager-api/conf.json \
&& mv /go/src/github.com/apisix/manager-api/conf/schema.json /go/manager-api/schema.json \
&& rm -rf /go/src/github.com/apisix/manager-api \
&& rm -rf /etc/localtime \
&& ln -s /usr/share/zoneinfo/Hongkong /etc/localtime \
Expand All @@ -37,13 +38,6 @@ RUN wget https://github.com/api7/dag-to-lua/archive/v1.1.tar.gz \
&& mkdir -p /go/manager-api/dag-to-lua \
&& mv -u ./dag-to-lua-1.1/lib/* /go/manager-api/dag-to-lua/

RUN wget https://github.com/apache/apisix/archive/master.zip \
&& apt-get update && apt-get install zip -y \
&& unzip master.zip \
&& rm -rf /go/manager-api/build-tools/apisix/ \
&& mkdir -p /go/manager-api/build-tools/apisix \
&& mv ./apisix-master/apisix/* /go/manager-api/build-tools/apisix/

FROM alpine:3.11

RUN mkdir -p /go/manager-api \
Expand All @@ -60,12 +54,6 @@ WORKDIR /go/manager-api
COPY --from=build-env /go/manager-api/ /go/manager-api/
COPY --from=build-env /usr/share/zoneinfo/Hongkong /etc/localtime

RUN cd /go/manager-api/build-tools \
&& lua schema-sync.lua > /go/manager-api/schema.json \
&& cd /go/manager-api/ \
&& rm -rf /go/manager-api/build-tools/
ADD ./dist /go/manager-api

EXPOSE 8080

RUN chmod +x ./entry.sh
Expand Down
6 changes: 3 additions & 3 deletions api/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const (
EnvDEV = "dev"
EnvLOCAL = "local"

confPath = "/go/manager-api/conf.json"
schemaPath = "/go/manager-api/schema.json"
confJsonPath = "/go/manager-api/conf.json"
schemaPath = "/go/manager-api/schema.json"
)

var (
Expand Down Expand Up @@ -103,7 +103,7 @@ func configurationPath() string {
} else if ENV == EnvLOCAL {
return filepath.Join(filepath.Dir(basePath), "conf.json")
} else {
return confPath
return confJsonPath
}
}

Expand Down
18 changes: 1 addition & 17 deletions api/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@
# limitations under the License.
#

pwd=`pwd`

# config
cp ${pwd}/api/conf/conf_preview.json ${pwd}/conf.json

# export APIX_DAG_LIB_PATH="${pwd}/dag-to-lua-1.1/lib/"
# export APIX_ETCD_ENDPOINTS="127.0.0.1:2379"

export SYSLOG_HOST=127.0.0.1

if [[ "$unamestr" == 'Darwin' ]]; then
sed -i '' -e "s%#syslogAddress#%`echo $SYSLOG_HOST`%g" ${pwd}/conf.json
else
sed -i -e "s%#syslogAddress#%`echo $SYSLOG_HOST`%g" ${pwd}/conf.json
fi

cp ${pwd}/conf.json ${pwd}/api/conf/conf.json
export ENV=prod

exec ./manager-api
226 changes: 0 additions & 226 deletions api/go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions api/internal/handler/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ func (h *Handler) Update(c droplet.Context) (interface{}, error) {
input.Route.ID = input.ID
}

if input.Route.Host != "" && len(input.Route.Hosts) > 0 {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
fmt.Errorf("only one of host or hosts is allowed")
}

//check depend
if input.ServiceID != "" {
_, err := h.svcStore.Get(input.ServiceID)
Expand Down
35 changes: 35 additions & 0 deletions api/test/docker/apisix_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# 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.
#
# If you want to set the specified configuration value, you can set the new
# in this file. For example if you want to specify the etcd address:
#
etcd:
host:
- "http://172.16.238.10:2379"
- "http://172.16.238.11:2379"
- "http://172.16.238.12:2379"

apisix:
admin_key:
-
name: "admin"
key: edd1c9f034335f136f87ad84b625c8f1 # using fixed API token has security risk, please
# update it when you deploy to production environment
role: admin

allow_admin: # http://nginx.org/en/docs/http/ngx_http_access_module.html#allow
- 0.0.0.0/0 # If we don't set any IP list, then any IP access is allowed by default.
183 changes: 183 additions & 0 deletions api/test/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#
# 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.
#
version: "3.6"

services:
node1:
image: quay.io/coreos/etcd:v3.4.0
ports:
- "2379:2379"
expose:
- 2379
- 2380
networks:
apisix_dashboard_e2e:
ipv4_address: 172.16.238.10
environment:
- ETCDCTL_API=3
command:
- /usr/local/bin/etcd
- --data-dir=/etcd-data
- --name
- node1
- --initial-advertise-peer-urls
- http://172.16.238.10:2380
- --listen-peer-urls
- http://0.0.0.0:2380
- --advertise-client-urls
- http://172.16.238.10:2379
- --listen-client-urls
- http://0.0.0.0:2379
- --initial-cluster
- node1=http://172.16.238.10:2380,node2=http://172.16.238.11:2380,node3=http://172.16.238.12:2380
- --initial-cluster-state
- new
- --initial-cluster-token
- docker-etcd

node2:
image: quay.io/coreos/etcd:v3.4.0
networks:
apisix_dashboard_e2e:
ipv4_address: 172.16.238.11
environment:
- ETCDCTL_API=3
expose:
- 2379
- 2380
command:
- /usr/local/bin/etcd
- --data-dir=/etcd-data
- --name
- node2
- --initial-advertise-peer-urls
- http://172.16.238.11:2380
- --listen-peer-urls
- http://0.0.0.0:2380
- --advertise-client-urls
- http://172.16.238.11:2379
- --listen-client-urls
- http://0.0.0.0:2379
- --initial-cluster
- node1=http://172.16.238.10:2380,node2=http://172.16.238.11:2380,node3=http://172.16.238.12:2380
- --initial-cluster-state
- new
- --initial-cluster-token
- docker-etcd

node3:
image: quay.io/coreos/etcd:v3.4.0
networks:
apisix_dashboard_e2e:
ipv4_address: 172.16.238.12
environment:
- ETCDCTL_API=3
expose:
- 2379
- 2380
command:
- /usr/local/bin/etcd
- --data-dir=/etcd-data
- --name
- node3
- --initial-advertise-peer-urls
- http://172.16.238.12:2380
- --listen-peer-urls
- http://0.0.0.0:2380
- --advertise-client-urls
- http://172.16.238.12:2379
- --listen-client-urls
- http://0.0.0.0:2379
- --initial-cluster
- node1=http://172.16.238.10:2380,node2=http://172.16.238.11:2380,node3=http://172.16.238.12:2380
- --initial-cluster-state
- new
- --initial-cluster-token
- docker-etcd

upstream:
image: johz/upstream:v2.0
restart: always
volumes:
- ./upstream.conf:/etc/nginx/conf.d/default.conf:ro
ports:
- '80:80/tcp'
- '1980:1980/tcp'
- '1981:1981/tcp'
- '1982:1982/tcp'
- '1983:1983/tcp'
- '1984:1984/tcp'
networks:
apisix_dashboard_e2e:
ipv4_address: 172.16.238.20

apisix:
image: apache/apisix:dev
restart: always
volumes:
- ./apisix_config.yaml:/usr/local/apisix/conf/config.yaml:ro
depends_on:
- node1
- node2
- node3
ports:
- '9080:9080/tcp'
- '9443:9443/tcp'
networks:
apisix_dashboard_e2e:
ipv4_address: 172.16.238.30

apisix2:
image: apache/apisix:dev
restart: always
volumes:
- ./apisix_config.yaml:/usr/local/apisix/conf/config.yaml:ro
depends_on:
- node1
- node2
- node3
ports:
- '9081:9080/tcp'
networks:
apisix_dashboard_e2e:
ipv4_address: 172.16.238.31

managerapi:
build:
context: ../../
dockerfile: Dockerfile
restart: always
volumes:
- ./manager-api-conf.json:/go/manager-api/conf.json:ro
depends_on:
- node1
- node2
- node3
ports:
- '8080:8080/tcp'
networks:
apisix_dashboard_e2e:
ipv4_address: 172.16.238.40

networks:
apisix_dashboard_e2e:
driver: bridge
ipam:
driver: default
config:
-
subnet: 172.16.238.0/24
31 changes: 31 additions & 0 deletions api/test/docker/manager-api-conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"conf": {
"syslog": {
"host": "127.0.0.1"
},
"listen": {
"host": "0.0.0.0",
"port": 8080
},
"dag-lib-path": "",
"etcd": {
"endpoints": "172.16.238.10:2379,172.16.238.11:2379,172.16.238.12:2379"
}
},
"authentication": {
"session": {
"secret": "secret",
"expireTime": 3600
},
"user": [
{
"username": "admin",
"password": "admin"
},
{
"username": "user",
"password": "user"
}
]
}
}
Loading

0 comments on commit 915ce83

Please sign in to comment.