You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/06/13 08:39:21 UTC

[incubator-shenyu-nginx] branch main updated: adjustment directories of test cases for adopting multi-cases (#12)

This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu-nginx.git


The following commit(s) were added to refs/heads/main by this push:
     new 26f79aa  adjustment directories of test cases for adopting multi-cases (#12)
26f79aa is described below

commit 26f79aa666e8fc4b49b7d3bb866d862fe23fcbda
Author: Luke.Z <10...@users.noreply.github.com>
AuthorDate: Mon Jun 13 16:39:17 2022 +0800

    adjustment directories of test cases for adopting multi-cases (#12)
    
    * adjustment directories of test cases for adopting multi-cases
    
    * fix
---
 .github/workflows/it.yaml                          | 24 +++---
 lib/shenyu/register/etcd.lua                       |  6 +-
 lib/shenyu/register/nacos.lua                      |  7 +-
 .../nginx.conf => case/etcd/conf/gateway.conf}     |  5 +-
 .../nginx.conf => case/etcd/conf/mock-shenyu.conf} |  0
 test/it/{ => case/etcd}/docker-compose.yml         | 23 +++---
 test/it/consumer/bin/entrypoint.sh                 | 91 ++++++++++------------
 test/it/gateway/Dockerfile                         |  3 -
 test/it/mock-shenyu/Dockerfile                     |  3 -
 9 files changed, 77 insertions(+), 85 deletions(-)

diff --git a/.github/workflows/it.yaml b/.github/workflows/it.yaml
index e769f3b..8a84f69 100644
--- a/.github/workflows/it.yaml
+++ b/.github/workflows/it.yaml
@@ -32,15 +32,17 @@ jobs:
       - uses: actions/checkout@v3
         with:
           submodules: true
-      - uses: actions/setup-java@v1
-        with:
-          java-version: 8
-      - run: |
-          docker-compose -f ./test/it/docker-compose.yml run -d consumer
-          docker wait $(docker ps -qa --filter Name=it_consumer)
-          status="$?"
-          docker-compose -f ./test/it/docker-compose.yml logs
-          if [ $status -eq 0 ]; then
-            docker-compose -f test/it/docker-compose.yml logs consumer
+      - name: run testcase
+        run: |
+          set -x
+          docker-compose -f ./test/it/case/etcd/docker-compose.yml run -d consumer --build
+          status=$(docker wait $(docker ps -qa --filter Name=etcd_consumer))
+          if [[ $status -ne 200 ]]; then
+            docker-compose -f ./test/it/case/etcd/docker-compose.yml logs
+          fi
+          docker logs $(docker ps -qa --filter Name=etcd_consumer)
+          docker-compose -f ./test/it/case/etcd/docker-compose.yml kill
+          docker-compose -f ./test/it/case/etcd/docker-compose.yml rm -f
+          if [[ $status -ne 200 ]]; then
+            exit 1
           fi
-          docker-compose -f ./test/it/docker-compose.yml kill
diff --git a/lib/shenyu/register/etcd.lua b/lib/shenyu/register/etcd.lua
index 370d647..4fd7c4f 100644
--- a/lib/shenyu/register/etcd.lua
+++ b/lib/shenyu/register/etcd.lua
@@ -221,7 +221,7 @@ local function sync(premature)
     local lock = storage:get("_lock")
     local ver = storage:get("revision")
 
-    if lock and ver > _M.revision then
+    if not lock and ver > _M.revision then
         local server_list = storage:get("server_list")
         local servers = json.decode(server_list)
         if _M.revision <= 1 then
@@ -246,7 +246,7 @@ local function watch(premature, watching)
 
     if not watching then
         if not _M.etcd_conf then
-            _M.storage:set("_lock", false)
+            _M.storage:set("_lock", true)
 
             local conf, err = parse_base_url(_M.etcd_base_url)
             if not conf then
@@ -262,7 +262,7 @@ local function watch(premature, watching)
             _M.time_at = 3
         else
             watching = true
-            _M.storage:set("_lock", true)
+            _M.storage:set("_lock", false)
         end
     else
         local conf = _M.etcd_conf
diff --git a/lib/shenyu/register/nacos.lua b/lib/shenyu/register/nacos.lua
index 5f6901b..2c40154 100644
--- a/lib/shenyu/register/nacos.lua
+++ b/lib/shenyu/register/nacos.lua
@@ -36,6 +36,7 @@ local INFO = ngx.INFO
 _M.access_token = nil
 
 local function login(username, password)
+    local httpc = http.new()
     local res, err = httpc:request_uri(nacos_base, {
         method = "POST",
         path = "/nacos/v1/auth/login",
@@ -156,7 +157,7 @@ local function subscribe(premature, initialized)
         _M.storage:set("server_list", server_list_in_json)
         _M.storage:set("revision", revision)
 
-        initialized = false
+        initialized = true
     else
         local server_list, revision, err = get_server_list(_M.service_name, _M.group_name, _M.namespace, _M.clusters)
         if not server_list then
@@ -181,7 +182,7 @@ local function subscribe(premature, initialized)
         end
 
         if not updated then
-            goto contiue
+            goto continue
         end
 
         _M.balancer:reinit(server_list)
@@ -211,7 +212,7 @@ local function sync(premature)
     if ver > _M.revision then
         local server_list = storage:get("server_list")
         local servers = json.decode(server_list)
-        if _M.revision <= 1 then
+        if _M.revision < 1 then
             _M.balancer:init(servers)
         else
             _M.balancer:reinit(servers)
diff --git a/test/it/gateway/conf/nginx.conf b/test/it/case/etcd/conf/gateway.conf
similarity index 90%
rename from test/it/gateway/conf/nginx.conf
rename to test/it/case/etcd/conf/gateway.conf
index dc5795f..2d9863d 100644
--- a/test/it/gateway/conf/nginx.conf
+++ b/test/it/case/etcd/conf/gateway.conf
@@ -26,10 +26,13 @@ events {
 env ETCD_SERVER_URL;
 
 http {
+    lua_shared_dict shenyu_storage 1m;
+
     init_worker_by_lua_block {
         local register = require("shenyu.register.etcd")
         register.init({
-            balancer_type = "chash",
+            shenyu_storage = ngx.shared.shenyu_storage,
+            balancer_type = "roundrobin",
             etcd_base_url = os.getenv("ETCD_SERVER_URL"),
         })
     }
diff --git a/test/it/mock-shenyu/conf/nginx.conf b/test/it/case/etcd/conf/mock-shenyu.conf
similarity index 100%
rename from test/it/mock-shenyu/conf/nginx.conf
rename to test/it/case/etcd/conf/mock-shenyu.conf
diff --git a/test/it/docker-compose.yml b/test/it/case/etcd/docker-compose.yml
similarity index 86%
rename from test/it/docker-compose.yml
rename to test/it/case/etcd/docker-compose.yml
index ae4944e..140001d 100644
--- a/test/it/docker-compose.yml
+++ b/test/it/case/etcd/docker-compose.yml
@@ -35,14 +35,15 @@ services:
 
   instance1:
     build:
-      context: ./mock-shenyu
+      context: ../../mock-shenyu
     expose:
       - 9090
     environment:
       - APP_NAME=mock-shenyu-instance-1
       - ETCD_SERVER_URL=http://172.16.238.10:2379
-    ports:
-      - 9090:9090
+    volumes:
+      - ./conf/mock-shenyu.conf:/var/run/nginx.conf
+    entrypoint: ["openresty", "-c", "/var/run/nginx.conf"]
     restart: on-failure
     healthcheck:
       test: [ "CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/9090" ]
@@ -58,12 +59,15 @@ services:
 
   instance2:
     build:
-      context: ./mock-shenyu
+      context: ../../mock-shenyu
     expose:
       - 9090
     environment:
       - APP_NAME=mock-shenyu-instance-2
       - ETCD_SERVER_URL=http://172.16.238.10:2379
+    volumes:
+      - ./conf/mock-shenyu.conf:/var/run/nginx.conf
+    entrypoint: ["openresty", "-c", "/var/run/nginx.conf"]
     restart: on-failure
     healthcheck:
       test: [ "CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/9090" ]
@@ -79,14 +83,13 @@ services:
 
   gateway:
     build:
-      context: ../..
+      context: ../../../..
       dockerfile: ./test/it/gateway/Dockerfile
-    ports:
-      - 8080:8080
-    volumes:
-      - ./gateway/conf:/conf
     environment:
       - ETCD_SERVER_URL=http://172.16.238.10:2379
+    volumes:
+      - ./conf/gateway.conf:/var/run/nginx.conf
+    entrypoint: ["openresty", "-c", "/var/run/nginx.conf"]
     restart: on-failure
     healthcheck:
       test: [ "CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/8080" ]
@@ -102,7 +105,7 @@ services:
 
   consumer:
     build:
-      context: ../..
+      context: ../../../..
       dockerfile: ./test/it/consumer/Dockerfile
     depends_on:
       gateway:
diff --git a/test/it/consumer/bin/entrypoint.sh b/test/it/consumer/bin/entrypoint.sh
index a78b8e3..9fbba7e 100644
--- a/test/it/consumer/bin/entrypoint.sh
+++ b/test/it/consumer/bin/entrypoint.sh
@@ -16,68 +16,55 @@
 # See the License for the specific language governing permissions and
 # limitations under the License
 
-
-inst_addr_1=$(curl -s http://instance1:9090/get)
-if [[ -z "$inst_addr_1" ]]; then
-  echo "failed to start instance 1."
-  exit 129
-fi
-
-inst_addr_2=$(curl -s http://instance2:9090/get)
-if [[ -z "$inst_addr_2" ]]; then
-  echo "failed to start instance 2."
-  exit 129
-fi
-
-rst=$(curl -Is http://gateway:8080/get | grep "HTTP/1.1 500")
-if [[ -z "$rst" ]]; then
-  echo "failed to set new environment for testing."
-  exit 128
-fi
-
-rst=$(curl -Is http://instance1:9090/register | grep "HTTP/1.1 200")
-echo $rst
-if [[ -z "$rst" ]]; then
-  echo "failed to register instance1 to etcd."
-  exit 128
-fi
-
-rst=$(curl -Is http://instance2:9090/register | grep "HTTP/1.1 200")
-echo $rst
-if [[ -z "$rst" ]]; then
-  echo "failed to register instance2 to etcd."
-  exit 128
-fi
+# 1: url, 2: condition, 3: message
+send() {
+  echo "send request to $1"
+  curl -s -D ./head -o response $1
+  rst=$(grep "$2" ./head)
+  if [[ -z "rst" ]]; then
+    echo "send $1 error: $3"
+    cat ./head
+    cat ./response
+    exit 100
+  fi
+}
+
+register() {
+  inst=$1
+  echo "${inst} registering..."
+  inst_addr=$(curl -s http://${inst}:9090/register)
+  if [[ -z "$inst_addr" ]]; then
+    echo "failed to start ${inst}."
+    exit 129
+  fi
+  echo "${inst} registered."
+}
+
+register "instance1"
+register "instance2"
+
+send "http://gateway:8080/get" "HTTP/1.1 500" "failed to set new environment for testing."
+
+send "http://gateway:8080/get" "HTTP/1.1 200" "failed to register instance1 to etcd."
+send "http://gateway:8080/get" "HTTP/1.1 200" "failed to register instance2 to etcd."
 
 sleep 5
 
-curl -s http://gateway:8080/get
-
-rst=$(curl -Is http://gateway:8080/get | grep "HTTP/1.1 200")
-if [[ -z "$rst" ]]; then
-  echo "shenyu nginx module did not work."
-  exit 128
-fi
+send "http://gateway:8080/get" "HTTP/1.1 200" "shenyu nginx module did not work."
 
 inst1=$(curl -s http://gateway:8080/get)
 inst2=$(curl -s http://gateway:8080/get)
-
-[[ "$inst1" == "$inst2" ]] || (echo "validation failed" && exit 128)
-
-# remove instance 1
-rst=$(curl -Is http://instance1:9090/unregister | grep "HTTP/1.1 200")
-if [[ -z "$rst" ]]; then
-  echo "failed to unregister instance1 to etcd."
+if [[ "$inst1" == "$inst2" ]]; then
+  echo "validation failed, inst1: ${inst1}, inst2: ${inst2}"
   exit 128
 fi
 
+# remove instance 1
+send "http://instance1:9090/unregister" "HTTP/1.1 200" "failed to unregister instance1 to etcd."
+
 sleep 5
 
-rst=$(curl -Is http://gateway:8080/get | grep "HTTP/1.1 200")
-if [[ -z "$rst" ]]; then
-  echo "shenyu nginx module did not work right"
-  exit 128
-fi
+send "http://gateway:8080/get" "HTTP/1.1 200" "shenyu nginx module did not work right"
 
 rst=$(curl -s http://gateway:8080/get)
 if [[ "$rst" == "$inst_addr_1" ]]; then
@@ -91,3 +78,5 @@ if [[ "$rst" == "$inst_addr_1" ]]; then
 fi
 
 echo "validation successful"
+
+exit 200
diff --git a/test/it/gateway/Dockerfile b/test/it/gateway/Dockerfile
index 8942dd2..9975844 100644
--- a/test/it/gateway/Dockerfile
+++ b/test/it/gateway/Dockerfile
@@ -20,8 +20,5 @@ WORKDIR /shenyu-nginx
 
 COPY ./lib lib
 COPY ./rockspec rockspec
-COPY ./test/it/gateway/conf/nginx.conf conf/nginx.conf
 
 RUN luarocks make ./rockspec/shenyu-nginx-main-0.rockspec
-
-ENTRYPOINT openresty -c /shenyu-nginx/conf/nginx.conf
diff --git a/test/it/mock-shenyu/Dockerfile b/test/it/mock-shenyu/Dockerfile
index 46ca147..ff4c8ff 100644
--- a/test/it/mock-shenyu/Dockerfile
+++ b/test/it/mock-shenyu/Dockerfile
@@ -17,6 +17,3 @@
 FROM openresty/openresty:1.17.8.2-5-alpine-fat
 
 RUN luarocks install lua-resty-http
-COPY ./conf/nginx.conf /conf/nginx.conf
-
-ENTRYPOINT openresty -c /conf/nginx.conf