Skip to content

Instantly share code, notes, and snippets.

@3bit-techs
Created February 28, 2020 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3bit-techs/d673ae0d3aef686835f56ead54f4d30a to your computer and use it in GitHub Desktop.
Save 3bit-techs/d673ae0d3aef686835f56ead54f4d30a to your computer and use it in GitHub Desktop.

Importing an API into API Gateway

For integration tests in the integration tests, we will import our API into the API Gateway locally, to validate the import flow and enable the use of our test with the gateway. We will have something similar to the diagram below:

Topologia de testes

Provisioning the API gateway

To facilitate the provisioning of resources, we will start to use a Makefile to centralize the scripts and enable reuse of them.

Below is the initial structure of our Makefile with the provisioning of the API Gateway (Kong):

#!make

run-integration-tests: integration-tests clean

integration-tests:
        # we created a network to be shared by Kong, Postgres, Newman and Prism
        docker network create kong-net
        # we created the Kong database
        docker run -d --name kong-database --network=kong-net -p 5432:5432 -e "POSTGRES_USER=kong" -e "POSTGRES_DB=kong" postgres:9.6
        sleep 5
        # we set up the kong database (bootstrap)
        docker run --rm --network=kong-net -e "KONG_DATABASE=postgres" -e "KONG_PG_HOST=kong-database" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" kong:latest kong migrations bootstrap
        # kong
        docker run -d --name kong --network=kong-net -e "KONG_DATABASE=postgres" -e "KONG_ANONYMOUS_REPORTS=off" -e "KONG_PG_HOST=kong-database" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" -e "KONG_PROXY_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" -p 8000:8000 -p 8443:8443 -p 8001:8001 -p 8444:8444 kong:latest

clean:
        -docker rm --force kong
        -docker rm --force kong-database
        -docker network rm kong-net

From this point on, we can already run our Makefile to check if the provisioning will be successful:

make -k run-integration-tests

If everything happened successfully, the output of the command should have looked like the image below:

Makefile Kong

Basically what we've done in this Makefile so far:

  • We created a shared network that will be used by Kong and its database (Postgres), the backend of our API (prism) and the client of our API (newman)
  • Provision the postgres database and bootstrap the Kong
  • We provision Kong using database
  • We clean by removing the created containers and the network, this is because we want them to live only during the execution of the tests

Provisioning the API backend (prism)

Now we will use the same command (with some minor changes) that we used to create the Prism Mock and add it to our Makefile, to automatically provision the mock API backend.

First, we will remove the container from the prism that we had previously provisioned:

docker rm --force prism

With that, we changed our Makefile:

#!make

run-integration-tests: integration-tests clean

integration-tests:
	# we created a network to be shared by Kong, Postgres, Newman and Prism
	docker network create kong-net
	# we created the Kong database
	docker run -d --name kong-database --network=kong-net -p 5432:5432 -e "POSTGRES_USER=kong" -e "POSTGRES_DB=kong" postgres:9.6
	sleep 5
	# we set up the kong database (bootstrap)
	docker run --rm --network=kong-net -e "KONG_DATABASE=postgres" -e "KONG_PG_HOST=kong-database" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" kong:latest kong migrations bootstrap
	# kong
	docker run -d --name kong --network=kong-net -e "KONG_DATABASE=postgres" -e "KONG_ANONYMOUS_REPORTS=off" -e "KONG_PG_HOST=kong-database" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" -e "KONG_PROXY_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" -p 8000:8000 -p 8443:8443 -p 8001:8001 -p 8444:8444 kong:latest
	# prism
	docker run --network=kong-net --rm --name prism -d -p 4010:4010 -v "$$(pwd)/petstore-v3.0.yaml":/etc/openapi/spec.file stoplight/prism:3 mock -h 0.0.0.0  "/etc/openapi/spec.file"

clean:
	-docker rm --force prism
	-docker rm --force kong
	-docker rm --force kong-database
	-docker network rm kong-net

We can run our Makefile again:

make -k run-integration-tests

The output of the command must be the error-free execution of the suite.

Importing the API

We will now import our API definition for the Kong gateway.

For this, we will increase our Makefile with additional commands to run the tool swagger-to-kong, developed specifically for this purpose.

Adding the registry as insecure (Optional - self-generated certificates)

Before using the tool, we will have to configure docker's registry as an insecure in the docker daemon, to avoid problems due to self-generated certificates.

To do this, change the /etc/docker/daemon.json file with the changes below:

{
    "insecure-registries" : [ "registry.domain.com" ]
}

To take effect, restart the docker daemon:

systemctl restart docker

Pulling the swagger-to-kong image:latest

To ensure that we have the last image available for the swagger-to-kong component, pull the image as shown below:

docker pull registry.domain.com/ci-tools/swagger-to-kong:latest

With the registry configured, we can use the swagger-to-kong tool properly.

Below is the Makefile with the necessary changes:

#!make

run-integration-tests: integration-tests clean

integration-tests:
	# we created a network to be shared by Kong, Postgres, Newman and Prism
	docker network create kong-net
	# we created the Kong database
	docker run -d --name kong-database --network=kong-net -p 5432:5432 -e "POSTGRES_USER=kong" -e "POSTGRES_DB=kong" postgres:9.6
	sleep 5
	# we set up the kong database (bootstrap)
	docker run --rm --network=kong-net -e "KONG_DATABASE=postgres" -e "KONG_PG_HOST=kong-database" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" kong:latest kong migrations bootstrap
	# kong
	docker run -d --name kong --network=kong-net -e "KONG_DATABASE=postgres" -e "KONG_ANONYMOUS_REPORTS=off" -e "KONG_PG_HOST=kong-database" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" -e "KONG_PROXY_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" -p 8000:8000 -p 8443:8443 -p 8001:8001 -p 8444:8444 kong:latest
	# prism
	docker run --network=kong-net --rm --name prism -d -p 4010:4010 -v "$$(pwd)/petstore-v3.0.yaml":/etc/openapi/spec.file stoplight/prism:3 mock -h 0.0.0.0  "/etc/openapi/spec.file"
	# swagger-to-kong
	docker run --rm --name swagger-to-kong --network kong-net -e OPENAPI_SERVER=http://prism:4010 -e KONG_HOST=http://kong:8001 -v "$$(pwd)/petstore-v3.0.yaml":/etc/openapi/spec.file registry.domain.com/ci-tools/swagger-to-kong:latest python3 swaggertokong.py /etc/openapi/spec.file
clean:
	-docker rm --force prism
	-docker rm --force kong
	-docker rm --force kong-database
	-docker network rm kong-net

Configuring the swagger-to-kong component performs the following actions:

  • Import the openapi petstore-v3.0.yaml definition
  • Changes the address of the item servers in openapi with the value of the environment variable OPENAPI_SERVER, which in turn points to the backend (Mock). This setting is applied in Service Kong.
  • Invokes the kong Admin API to create the appropriate Service and Route.

Executing the collection via Makefile

With everything working properly, we can now invoke our collection with Newman using our Makefile.

We will use the same command used previously, with minor adjustments to the container network to share the same network as Kong.

Follows Makefile with the changes:

#!make

run-integration-tests: integration-tests clean

integration-tests:
	# we created a network to be shared by Kong, Postgres, Newman and Prism
	docker network create kong-net
	# we created the Kong database
	docker run -d --name kong-database --network=kong-net -p 5432:5432 -e "POSTGRES_USER=kong" -e "POSTGRES_DB=kong" postgres:9.6
	sleep 5
	# we set up the kong database (bootstrap)
	docker run --rm --network=kong-net -e "KONG_DATABASE=postgres" -e "KONG_PG_HOST=kong-database" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" kong:latest kong migrations bootstrap
	# kong
	docker run -d --name kong --network=kong-net -e "KONG_DATABASE=postgres" -e "KONG_ANONYMOUS_REPORTS=off" -e "KONG_PG_HOST=kong-database" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" -e "KONG_PROXY_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" -p 8000:8000 -p 8443:8443 -p 8001:8001 -p 8444:8444 kong:latest
	# prism
	docker run --network=kong-net --rm --name prism -d -p 4010:4010 -v "$$(pwd)/petstore-v3.0.yaml":/etc/openapi/spec.file stoplight/prism:3 mock -h 0.0.0.0  "/etc/openapi/spec.file"
	# swagger-to-kong
	docker run --rm --name swagger-to-kong --network kong-net -e OPENAPI_SERVER=http://prism:4010 -e KONG_HOST=http://kong:8001 -v "$$(pwd)/petstore-v3.0.yaml":/etc/openapi/spec.file registry.domain.com/ci-tools/swagger-to-kong:latest python3 swaggertokong.py /etc/openapi/spec.file
	# newman
	docker run --net kong-net --rm -v "$$(pwd)/localhost.postman_environment.json:/etc/newman/env.json" -v "$$(pwd)/petstore-collection.json":/etc/newman/collection.json -t postman/newman:alpine run -e env.json collection.json
clean:
	-docker rm --force prism
	-docker rm --force kong
	-docker rm --force kong-database
	-docker network rm kong-net

If we try to run the Makefile now, we will have an error because the environment localhost.postman_environment.json used by the collection petstore-collection.json has been configured with the Variable baseUrl pointed at the computer local (localhost) and now we need it to be configured to point to Kong (API Gateway) on the network known to him in Docker.

To change the environment, we must go back to Postman and follow the same steps that we already did in the section Creating an environment and exporting, changing the value of Variable baseUrl to http://kong:8000.

The result should be the same as below:

Exporting the corrected environment

Don't forget to export the environment with the same name as the previous one!

We can then run our Makefile, now with integrated tests using newman!

make -k run-integration-tests

Integrated tests with Gateway and Prism

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment