From 38eb2676678cf9a6829aa5cd49447f1be1548e71 Mon Sep 17 00:00:00 2001 From: Colin Hutchinson Date: Fri, 9 Nov 2018 16:17:41 -0500 Subject: [PATCH] feat(tests) test the artifact outputs (#7) * feat(tests) first pass at testing the resulting Kong asset * misc(tests) re-organize how to setup and run the tests * misc(tests) Makefile refactor and asset naming / versioning * feat(tests) test ubuntu artifact * tests(centos) test the centos package --- Makefile | 30 ++++++++++++++++- README.md | 15 +++++++++ fpm-entrypoint.sh | 10 ++++++ test/Dockerfile.alpine | 28 ++++++++++++++++ test/Dockerfile.centos | 20 ++++++++++++ test/Dockerfile.ubuntu | 20 ++++++++++++ test/docker-entrypoint.sh | 19 +++++++++++ test/run_tests.sh | 68 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 test/Dockerfile.alpine create mode 100644 test/Dockerfile.centos create mode 100644 test/Dockerfile.ubuntu create mode 100755 test/docker-entrypoint.sh create mode 100755 test/run_tests.sh diff --git a/Makefile b/Makefile index 34080861a866..aad85a581dbb 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +export SHELL:=/bin/bash +export SHELLOPTS:=$(if $(SHELLOPTS),$(SHELLOPTS):)pipefail:errexit + RESTY_IMAGE_BASE?=ubuntu RESTY_IMAGE_TAG?=xenial PACKAGE_TYPE?=debian @@ -18,7 +21,7 @@ KONG_LICENSE?="ASL 2.0" KONG_SOURCE_LOCATION?="$$PWD/../kong/" KONG_VERSION?="0.0.0" -release-kong: +release-kong: test RESTY_IMAGE_BASE=$(RESTY_IMAGE_BASE) \ RESTY_IMAGE_TAG=$(RESTY_IMAGE_TAG) \ KONG_PACKAGE_NAME=$(KONG_PACKAGE_NAME) \ @@ -90,3 +93,28 @@ else --build-arg RESTY_IMAGE_BASE=$(RESTY_IMAGE_BASE) \ -t kong:$(RESTY_IMAGE_BASE)-$(RESTY_IMAGE_TAG) . endif + +.PHONY: test +test: + microk8s.reset + sleep 3 + microk8s.enable storage dns registry + sleep 3 + /snap/bin/helm init + RESTY_IMAGE_BASE=$(RESTY_IMAGE_BASE) \ + RESTY_IMAGE_TAG=$(RESTY_IMAGE_TAG) \ + KONG_VERSION=$(KONG_VERSION) \ + KONG_PACKAGE_NAME=$(KONG_PACKAGE_NAME) \ + test/run_tests.sh + +cleanup_tests: + microk8s.reset + sudo snap unalias kubectl + sudo snap remove microk8s + sudo snap remove helm + +setup_tests: + sudo snap install microk8s --classic + sudo snap install helm --classic + sudo snap alias microk8s.kubectl kubectl + #sudo iptables -P FORWARD ACCEPT diff --git a/README.md b/README.md index 116a18d430ab..4745e87da11a 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,21 @@ export REDHAT_USERNAME=rhuser export REDHAT_PASSWORD=password ``` +## Testing + +*Prerequisites:* + +- Docker +- Microk8s +- Helm + +A Make task `setup_tests` exists that will install the prerequisites for you which assumes you're on a system that can +install microk8s via snap (aka Ubuntu). There's also a `cleanup_tests` make task to uninstall microk8s / helm + +``` +make test +``` + ## Releasing a Kong Distribution The same defaults that applied when creating a packaged version of Kong apply to releasing said package diff --git a/fpm-entrypoint.sh b/fpm-entrypoint.sh index 635ff0f251e7..cf6c78ab48ef 100755 --- a/fpm-entrypoint.sh +++ b/fpm-entrypoint.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -e + cd /tmp/build FPM_PARAMS="" @@ -20,6 +22,13 @@ elif [ "$RESTY_IMAGE_BASE" == "rhel" ]; then fi fi +ROCKSPEC_VERSION=`basename /tmp/build/build/usr/local/lib/luarocks/rocks/kong/*` + +echo "#!/bin/sh +mkdir -p /etc/kong +mv usr/local/lib/luarocks/rocks/kong/$ROCKSPEC_VERSION/kong.conf.default /etc/kong/kong.conf.default +" > /tmp/post_install_script + if [ "$RESTY_IMAGE_BASE" == "alpine" ]; then pushd /tmp/build mkdir -p etc/kong @@ -37,6 +46,7 @@ else --description 'Kong is a distributed gateway for APIs and Microservices, focused on high performance and reliability.' \ --vendor 'Kong Inc.' \ --license "$KONG_LICENSE" \ + --after-install /tmp/post_install_script \ --url 'https://getkong.org/' usr \ && mv kong*.* /output/${KONG_PACKAGE_NAME}-${KONG_VERSION}${OUTPUT_FILE_SUFFIX}.${PACKAGE_TYPE} fi diff --git a/test/Dockerfile.alpine b/test/Dockerfile.alpine new file mode 100644 index 000000000000..5a8b4fe4bdd9 --- /dev/null +++ b/test/Dockerfile.alpine @@ -0,0 +1,28 @@ +FROM alpine:3.6 + +ARG KONG_VERSION="0.0.0" +ARG KONG_PACKAGE_NAME="kong-community-edition" + +LABEL maintainer="Kong Core Team " + +RUN apk add --no-cache --virtual .build-deps tar ca-certificates \ + && apk add --no-cache libgcc openssl pcre perl tzdata bash + +COPY output/${KONG_PACKAGE_NAME}-${KONG_VERSION}.apk.tar.gz kong.apk.tar.gz + +RUN tar -xzf kong.apk.tar.gz -C /tmp \ + && rm -f kong.tar.gz \ + && cp -R /tmp/usr / \ + && rm -rf /tmp/usr \ + && cp -R /tmp/etc / \ + && rm -rf /tmp/etc \ + && apk del .build-deps + +COPY test/docker-entrypoint.sh /docker-entrypoint.sh +ENTRYPOINT ["/docker-entrypoint.sh"] + +EXPOSE 8000 8443 8001 8444 + +STOPSIGNAL SIGTERM + +CMD ["kong", "docker-start"] \ No newline at end of file diff --git a/test/Dockerfile.centos b/test/Dockerfile.centos new file mode 100644 index 000000000000..7299a1232b4d --- /dev/null +++ b/test/Dockerfile.centos @@ -0,0 +1,20 @@ +FROM centos:7 + +ARG KONG_VERSION="0.0.0" +ARG KONG_PACKAGE_NAME="kong-community-edition" +ARG RESTY_IMAGE_TAG="7" + +RUN yum -y install perl openssl + +COPY output/${KONG_PACKAGE_NAME}-${KONG_VERSION}.el${RESTY_IMAGE_TAG}.noarch.rpm /kong.rpm + +RUN rpm -i kong.rpm + +COPY test/docker-entrypoint.sh /docker-entrypoint.sh +ENTRYPOINT ["/docker-entrypoint.sh"] + +EXPOSE 8000 8443 8001 8444 + +STOPSIGNAL SIGTERM + +CMD ["kong", "docker-start"] \ No newline at end of file diff --git a/test/Dockerfile.ubuntu b/test/Dockerfile.ubuntu new file mode 100644 index 000000000000..85373b46bdd1 --- /dev/null +++ b/test/Dockerfile.ubuntu @@ -0,0 +1,20 @@ +FROM ubuntu:xenial + +ARG KONG_VERSION="0.0.0" +ARG KONG_PACKAGE_NAME="kong-community-edition" +ARG RESTY_IMAGE_TAG="xenial" + +RUN apt-get update && apt-get install -y perl openssl + +COPY output/${KONG_PACKAGE_NAME}-${KONG_VERSION}.${RESTY_IMAGE_TAG}.all.deb /kong.deb + +RUN dpkg -i kong.deb + +COPY test/docker-entrypoint.sh /docker-entrypoint.sh +ENTRYPOINT ["/docker-entrypoint.sh"] + +EXPOSE 8000 8443 8001 8444 + +STOPSIGNAL SIGTERM + +CMD ["kong", "docker-start"] \ No newline at end of file diff --git a/test/docker-entrypoint.sh b/test/docker-entrypoint.sh new file mode 100755 index 000000000000..37df5830faa6 --- /dev/null +++ b/test/docker-entrypoint.sh @@ -0,0 +1,19 @@ +#!/bin/bash +set -e + +export KONG_NGINX_DAEMON=off + +if [[ "$1" == "kong" ]]; then + PREFIX=${KONG_PREFIX:=/usr/local/kong} + mkdir -p $PREFIX + + if [[ "$2" == "docker-start" ]]; then + kong prepare -p $PREFIX + + exec /usr/local/openresty/nginx/sbin/nginx \ + -p $PREFIX \ + -c nginx.conf + fi +fi + +exec "$@" diff --git a/test/run_tests.sh b/test/run_tests.sh new file mode 100755 index 000000000000..8928775886f4 --- /dev/null +++ b/test/run_tests.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +set -e + +if [ "$RESTY_IMAGE_BASE" == "alpine" ]; then + DOCKER_FILE="Dockerfile.alpine" +elif [ "$RESTY_IMAGE_BASE" == "ubuntu" ]; then + DOCKER_FILE="Dockerfile.ubuntu" +elif [ "$RESTY_IMAGE_BASE" == "centos" ]; then + DOCKER_FILE="Dockerfile.centos" +else + echo "Unrecognized base image $RESTY_IMAGE_BASE" + exit 1 +fi + +microk8s.docker build --build-arg RESTY_IMAGE_TAG=$RESTY_IMAGE_TAG -f test/$DOCKER_FILE -t localhost:32000/kong . + +while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:32000)" != 200 ]]; do + echo "waiting for K8s registry to be ready" + sleep 5; +done + +microk8s.docker push localhost:32000/kong + +helm init --wait +helm install --name kong --set image.repository=localhost,image.tag=32000/kong stable/kong + +microk8s.kubectl get deployment kong-kong | tail -n +2 | awk '{print $5}' + +while [[ "$(microk8s.kubectl get deployment kong-kong | tail -n +2 | awk '{print $5}')" != 1 ]]; do + echo "waiting for Kong to be ready" + sleep 5; +done + +HOST="https://$(microk8s.kubectl get nodes --namespace default -o jsonpath='{.items[0].status.addresses[0].address}')" +echo $HOST +ADMIN_PORT=$(microk8s.kubectl get svc --namespace default kong-kong-admin -o jsonpath='{.spec.ports[0].nodePort}') +echo $ADMIN_PORT +PROXY_PORT=$(microk8s.kubectl get svc --namespace default kong-kong-proxy -o jsonpath='{.spec.ports[0].nodePort}') +echo $PROXY_PORT +CURL_COMMAND="curl -s -o /dev/null -w %{http_code} --insecure " +echo $CURL_COMMAND + +if ! [ `$CURL_COMMAND$HOST:$ADMIN_PORT` == "200" ]; then + echo "Can't invoke admin API" + exit 1 +fi + +echo "Admin API passed" + +RANDOM_API_NAME="randomapiname" +RESPONSE=`$CURL_COMMAND -d "name=$RANDOM_API_NAME&hosts=$RANDOM_API_NAME.com&upstream_url=http://mockbin.org" $HOST:$ADMIN_PORT/apis/` +if ! [ $RESPONSE == "201" ]; then + echo "Can't create API" + exit 1 +fi + +sleep 3 + +# Proxy Tests +RESPONSE=`$CURL_COMMAND -H "Host: $RANDOM_API_NAME.com" $HOST:$PROXY_PORT/request` +if ! [ $RESPONSE == "200" ]; then + echo "Can't invoke API on HTTP" + exit 1 +fi + +echo "Proxy and Admin smoke tests passed" +exit 0 \ No newline at end of file