Skip to content

Commit

Permalink
feat(tests) test the artifact outputs (#7)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hutchic authored Nov 9, 2018
1 parent 6364ca9 commit 38eb267
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) \
Expand Down Expand Up @@ -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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions fpm-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -e

cd /tmp/build

FPM_PARAMS=""
Expand All @@ -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
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"

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"]
20 changes: 20 additions & 0 deletions test/Dockerfile.centos
Original file line number Diff line number Diff line change
@@ -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"]
20 changes: 20 additions & 0 deletions test/Dockerfile.ubuntu
Original file line number Diff line number Diff line change
@@ -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"]
19 changes: 19 additions & 0 deletions test/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
68 changes: 68 additions & 0 deletions test/run_tests.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 38eb267

Please sign in to comment.