viernes, mayo 19, 2017

Running Parallel Tests in Docker

Sometimes when you are running your tests on your CI environment, you want to run tests in parallel. This parallelism is programmed in build tool such as Maven or Gradle or by using Jenkins plugin. 

If you are using Docker as a testing tool for providing external dependencies to the application (for example databases, mail servers, ftp servers, ....) you might find a big problem and it is that probably Docker Host used is one and when running tests in parallel, all of them are going to try to start a container with same name. So when you start the second test (in parallel) you will get a failure regarding that a conflict container name because of trying to start at the same Docker Host two containers with same name or having same binding port in two containers.

So arrived at this point you can do two things:
  • You can have one Docker Host for each parallel test.
  • You can reuse the same Docker Host and use Arquillian Cube Star Operator.

Arquillian Cube is an Arquillian extension that can be used to manager Docker containers in your tests.

To use Arquillian Cube you need a Docker daemon running on a computer (it can be local or not), but probably it will be at local.

Arquillian Cube offers three different ways to define container(s):

  • Defining a docker-compose file.
  • Defining a Container Object.
  • Using Container Object DSL.
In this example I am going to show you how to use docker-compose and Container Object DSL.

Star operator let’s you indicate to Arquillian Cube that you want to generate cube names randomly and can adapt links as well. In this way when you execute your tests in parallel there will be no conflicts because of names or binding ports.

Let’s see an example:

You can see in docker-compose.yml file an important change on a typical docker-compose file, and it is that the name ends up with star (*) operator [redis*]. This is how you are instructing Arquillian Cube that this name should be generated dynamically for each execution.

Then there are three tests (here only showed the first one) that all of them looks like the same. Basically it prints to console the binding port to connect to the server.

Finally there is build.gradle file, which executes two tests in parallel. So if you run the tests in Gradle (./gradlew test) you'll see that two tests are executed at the same time and when it finish one of them, the remaining test is executed. Then if you inspect the output you'll see next output:

So as you can see in the log, container name is not redis nor redis*, but redis followed by a UUID. Also you can see that when the output is printed the binding port is different in each case.

But if you don't want to use docker-compose approach, you can also define container programmatically by using Container Object DSL which also supports star operator.  In this case it the example looks like:

The approach is the same, but using Container Objects (you need Arquillian Cube 1.4.0 to run it with Container Objects).

Notice that thanks of this feature you can run the tests with any degree of parallel execution, since Arquillian Cube takes care of naming or port binding issues. Notice that in case of linking between containers, you still need to use the star operator, and it will be resolved at runtime.

To read more about star operator just check

Source code:

We keep learning,
I can show you the world, Shining, shimmering, splendid, Tell me, princess, now when did, You last let your heart decide? (A Whole New World - Aladdin)

2 comentarios:

Ace Starlit dijo...

Nice and Wonderful article. Keep sharing and keep posting. if you have any queries please visit our site;- Ace Starlit Price List

kingfariko dijo...

An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers
Click Here
Visit Web