In my previous post I showed Jocker, a Jolie component able to interact with Docker by offering a subset of its functionalities as Jolie operations instead of REST ones. In this post I am going to exploit Jocker for automatizing a test on a simple Jolie microservice by orchestrating it from another jolie orchestrator. In order to do that I will use a simple example you can find in the jocker git repo under the folder: ExampleOrchestrator/TestingDBSystem
The system under test
The system under test is very simple and it is just represented by a microservice connected with a PostgreSQL database.
You can find the code of this simple Jolie microservice here. As you can see, this microservice has only one RequestResponse operation called getContent which is in charge to select the field of a row (field2) from table testTable of the DB depending on the value of column field1. Very simple.
Orchestrating the Test
Now, I'll show you how to test the microservice by checking if it properly returns the correct responses when some request messages are sent. In order to do so I use this simple orchestrator for interacting with Jocker which executes all the actions I need. In particular, the orchestrator performs three main activities:
- Preparation of the testing environment
- Test execution
- Removal of the testing environment
The main idea is to prepare the testing environment by creating a container for each basic component of the system. Here we have two basic components: the PostgreSQL database and the Jolie microservice.
The PostgreSQL Database container is obtained in the following way:
- pulling down the postgresql image from docker hub
- creating the corresponding container
- starting the container
- initializing the database we need by creating it from scratch
- initializing the required known data into the database
On the other hand, the Jolie microservice image can be built by following the same steps explained here. In particular:
- a temporary directory is created and all the content of the ServiceToTest folder is copied in.
- a Dockerfile is dynamically created and added to the temporary folder
- a tar file of the temporary folder is created
- a docker image of the microservice is created invoking Jocker
- a container is created starting from that image
- the container is started
Test execution
The test execution is a very simple phase because the orchestrator just sends all the requests I want to test to the microservice inputPort and checks if the results are like expected. For the sake of this example there is only one request to test but, clearly, they can be hundreds depending on the operations to test and the variety of data to be considered.
Removal of the testing environment
When the test is done, I don't need the test environment any more and I can destroy it. In particular:
- I stop the two containers
- I remove the two containers
- I remove the two images (in reality, as you can see in the code, I do not remove the postgresql image just because it takes time for pulling it down from the docker hub, but it is up to you).
Some interesting notes
- In this example we do not exploit the possibility to create links among different docker containers but we directly call the container on the ports they provide. In order to do this, once a container is created we also query Jocker in order to get their info details for extracting the local IP assigned by Docker to them. We will use these IPs as reference hosts for connecting the microservice with the PostgreSQL database and the tests to the microservice.
- In order to run the example it is sufficient to run the jocker container as described here and the run the orchestrator placed in the folder ExampleOrchestrator/TestingDBSystem by using the following command
jolie orchestrator.ol
Conclusions
I hope this post could be of inspirations for all those software engineers who are addressing testing issues with microservices and containers. Let me know if you have questions or doubts.