From a78bad6094a56b3d02328c19b1cbd2c2d0bcf0f4 Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Thu, 15 Dec 2022 08:30:26 +0100 Subject: [PATCH 1/3] Update to 2.15 Updating to the latest v2 minor version to ensure all works as expected. --- ci/utils/linux-common-runnner.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/utils/linux-common-runnner.sh b/ci/utils/linux-common-runnner.sh index 4e6a75e..1c7eae2 100755 --- a/ci/utils/linux-common-runnner.sh +++ b/ci/utils/linux-common-runnner.sh @@ -19,7 +19,7 @@ export_or_prefix() { get_apisix_code() { # ${1} branch name # ${2} checkout path - git_branch=${1:-release/2.12} + git_branch=${1:-release/2.15} git_checkout_path=${2:-workbench} git clone --depth 1 --recursive https://github.com/apache/apisix.git \ -b "${git_branch}" "${git_checkout_path}" && cd "${git_checkout_path}" || exit 1 From 603f671dcf85386015125b8a7f585f59cf68340f Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Thu, 15 Dec 2022 09:24:59 +0100 Subject: [PATCH 2/3] Update demo test to work with 2.15 As of 2.13.0 public API's are no longer exposed by default so we do it manually in the test now. See https://github.com/apache/apisix/blob/2.13.0/CHANGELOG.md#2130 --- t/demo/demo.t | 59 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/t/demo/demo.t b/t/demo/demo.t index 55bd01d..aa417ab 100644 --- a/t/demo/demo.t +++ b/t/demo/demo.t @@ -11,6 +11,7 @@ add_block_preprocessor(sub { my $extra_yaml_config = $block->extra_yaml_config // <<_EOC_; plugins: - demo + - public-api _EOC_ $block->set_value("extra_yaml_config", $extra_yaml_config); @@ -46,33 +47,45 @@ hello, world! --- config location = /t { content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "plugins": { - "demo": { - "body": "test" - } - }, - "upstream": { - "nodes": { - "127.0.0.1:1980": 1 + local data = { + { + url = "/apisix/admin/routes/1", + data = [[{ + "plugins": { + "demo": { + "body": "test" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" }, - "type": "roundrobin" - }, - "uri": "/demo" - }]] - ) - - if code >= 300 then - ngx.status = code + "uri": "/demo" + }]], + }, + { + url = "/apisix/admin/routes/r1", + data = [[{ + "plugins": { + "public-api": {} + }, + "uri": "/apisix/plugin/demo/public_api" + }]] + }, + } + + local t = require("lib.test_admin").test + + for _, data in ipairs(data) do + local code, body = t(data.url, ngx.HTTP_PUT, data.data) + ngx.say(code..body) end - ngx.say(body) } } ---- response_body -passed +--- response_body eval +"201passed\n" x 2 From 2216ff63dc4b7963ede87be15a135cf44f0e2d70 Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Thu, 15 Dec 2022 10:13:57 +0100 Subject: [PATCH 3/3] Use docker-compose for local development This adds a docker file and docker-compose to enable developers to run tests locally without installing a bunch of dependencies. --- Dockerfile | 81 ++++++++++++++++++++++++++++++++++++++++++++++ README.md | 7 ++++ docker-compose.yml | 36 +++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..736bdb9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,81 @@ +FROM api7/apisix-base:dev + +ARG APISIX_VERSION=2.15.1 +ENV APISIX_VERSION=${APISIX_VERSION} + +ENV DEBIAN_FRONTEND noninteractive + +# Dependencies where found in +# https://github.com/apache/apisix-docker/blob/release/apisix-2.15.1/debian-dev/Dockerfile +# https://github.com/apache/apisix/blob/2.15.1/ci/common.sh#L130 +RUN set -x \ + && apt-get -y update --fix-missing \ + && apt-get install -y curl \ + gawk \ + git \ + libldap2-dev \ + liblua5.1-0-dev \ + lua5.1 \ + make \ + sudo \ + unzip \ + wget \ + cpanminus \ + build-essential \ + libncurses5-dev \ + libreadline-dev \ + libssl-dev \ + perl \ + libpcre3 \ + libpcre3-dev \ + libldap2-dev \ + && curl "https://raw.githubusercontent.com/apache/apisix/${APISIX_VERSION}/utils/install-dependencies.sh" -sL | bash - \ + && curl "https://raw.githubusercontent.com/apache/apisix/${APISIX_VERSION}/utils/linux-install-etcd-client.sh" -sL | bash - \ + && cpanm --notest "Test::Nginx" "IPC::Run" + +# Clone the repository and update submodule's +RUN git clone --depth 1 --branch "${APISIX_VERSION}" "https://github.com/apache/apisix.git" "/usr/local/apisix-plugin-test" \ + && cd "/usr/local/apisix-plugin-test" \ + && git submodule update --init --recursive + +WORKDIR /usr/local/apisix-plugin-test + +# Install Dependencies and create log directory +RUN make deps \ + && mkdir logs + +ENV TEST_NGINX_BINARY="/usr/local/openresty/bin/openresty" + +# Ensure we use the remote etcd +ENV ETCD_VERSION="3.4.15" \ + ETCD_HOST="etcd" \ + ETCDCTL_ENDPOINTS="http://etcd:2379" \ + ENABLE_LOCAL_DNS=true + +# Replace hardcoded etcd locations +RUN set +x \ + && sed -i 's~127.0.0.1:2379~etcd:2379~g' ./conf/config-default.yaml \ + && sed -i 's~127.0.0.1:2379~etcd:2379~g' ./t/**/*.t +# && sed -i 's~127.0.0.1:2379~etcd:2379~g' ./t/**/*.sh + +# Copy Plugin code and tests +COPY ./apisix ./apisix +COPY ./t ./t + +# Install the plugin into apisix +RUN make install \ + && ./bin/apisix test + +ENV FLUSH_ETCD=1 + +RUN printf '#!/bin/sh \n\ +set -ex \n\ +./bin/apisix init \n\ +./bin/apisix init_etcd \n\ +prove -Itest-nginx/lib -I./ -r "$@" \n\ +' > /run-tests.sh \ + && chmod +x /run-tests.sh + +ENTRYPOINT ["/run-tests.sh"] + +CMD [ "t/demo" ] diff --git a/README.md b/README.md index 659f27a..afc34d0 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,13 @@ which contains an introduction to the essential parts of the plugin development. ## Testing [Testing plugins][apisix-testing-framework] is an essential part of the plugin development to make sure that everything works as expected. +### Local with docker +In order to run the test locally a [Docker Compose](https://docs.docker.com/compose/) file is provided. + +To start the services needed to run the test execute `docker-compose up -d`. +Once these services are running you can run the tests by executing `docker-compose run --rm apisix`. +If you would like to run a specific test for example `demo` you can execute `docker-compose run --rm apisix 't/demo'`. + ### Continuous integration [Continuous integration][continuous-integration] (CI) depends on [GitHub Actions][github-actions], a set of workflows that make it possible to automate your testing process. Thanks to such automation, you can delegate the testing and verification phases to the CI and instead focus on development (and writing more tests). diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6595161 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,36 @@ +version: '3' +services: + apisix: + profiles: ["test"] + build: + context: . + volumes: + - ./t/demo:/usr/local/apisix-plugin-test/t/demo + - ./apisix/plugins/demo.lua:/usr/local/apisix-plugin-test/apisix/plugins/demo.lua + depends_on: + - etcd + networks: + apisix: + + etcd: + image: bitnami/etcd:3.4.15 + restart: always + volumes: + - etcd_data:/bitnami/etcd + environment: + ETCD_ENABLE_V2: "true" + ALLOW_NONE_AUTHENTICATION: "yes" + ETCD_ADVERTISE_CLIENT_URLS: "http://etcd:2379" + ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379" + ports: + - "2379:2379/tcp" + networks: + apisix: + +networks: + apisix: + driver: bridge + +volumes: + etcd_data: + driver: local