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/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 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 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