Documentation for version Alpha3 can be found here: https://github.com/arquillian/arquillian-cube/blob/1.0.0.Alpha3/README.adoc
Documentation for version Alpha2 can be found here: https://github.com/arquillian/arquillian-cube/blob/1.0.0.Alpha2/README.adoc
Documentation for version Alpha1 can be found here: https://github.com/arquillian/arquillian-cube/blob/1.0.0.Alpha1/README.adoc
Arquillian Cube is an Arquillian extension that can be used to manager Docker containers from Arquillian.
Extension is named Cube for two reasons:
-
Because Docker is like a cube
-
Because Borg starship is named cube and well because we are moving tests close to production we can say that "any resistance is futile, bugs will be assimilated".
With this extension you can start a Docker container with a server installed, deploy the required deployable file within it and execute Arquillian tests.
The key point here is that if Docker is used as deployable platform in production, your tests are executed in a the same container as it will be in production, so your tests are even more real than before.
But it also lets you start a container with every required service like database, mail server, … and instead of stubbing or using fake objects your tests can use real servers.
Warning
|
This extension has been developed and tested on a Linux machine with the Docker server already installed. It works with Boot2Docker as well in Windows and MacOS machines, but some parameters like host ip must be the Boot2Docker server instead of localhost (in case you have Docker server installed inside your own machine). One of the best resources to learn about why using Boot2Docker is different from using Docker in Linux can be read here http://viget.com/extend/how-to-use-docker-on-os-x-the-missing-guide |
Arquillian Cube relies on docker-java API.
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.
By default the Docker server uses UNIX sockets for communicating with the Docker client. Arquillian Cube will attempt to detect the operating system it is running on and either set the docker-java to use UNIX socket on Linux or to Boot2Docker on Windows/Mac as the default URI.
If you want to use TCP/IP to connect to the Docker server, you’ll need to make sure that your Docker server is listening on TCP port. To allow Docker server to use TCP add the following line to /etc/default/docker:
DOCKER_OPTS="-H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock"
After restarting the Docker daemon you need to make sure that Docker is up and listening on TCP.
$ docker -H tcp://127.0.0.1:2375 version
Client version: 0.8.0
Go version (client): go1.2
Git commit (client): cc3a8c8
Server version: 1.2.0
Git commit (server): fa7b24f
Go version (server): go1.3.1
If you cannot see the client and server versions then it means that something is wrong with the Docker installation.
After having a Docker server installed we can start using Arquillian Cube. In this case we are going to use a very simple example using a Docker image with Apache Tomcat and we are going to test a Servlet on it.
@WebServlet("/HelloWorld")
public class HelloWorldServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println("Hello World");
}
}
@RunWith(Arquillian.class)
public class HelloWorldServletTest {
@Deployment(testable=false) //(1)
public static WebArchive create() {
return ShrinkWrap.create(WebArchive.class, "hello.war").addClass(HelloWorldServlet.class); //(2)
}
@Test
public void should_parse_and_load_configuration_file(@ArquillianResource URL resource) throws IOException { //(3)
URL obj = new URL(resource, "HelloWorld");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
assertThat(response.toString(), is("Hello World"));//(4)
}
}
-
In this case we are running the test as a client. So in fact this test is executed against the container instead of inside the container.
-
No changes in this part, we need to create a deployable file, and because we are testing against Tomcat, a war file is created.
-
Because the test is run as client, we can use @ArquillianResource to get the URL where the file is deployed. Note that this will be the URL to access Tomcat running inside the Docker container.
-
Typical jUnit assertion of servlet response.
Now this test could be run in any container, there is nothing that ties this to Docker. Next step is adding some dependencies apart from the typical Arquillian dependencies.
<dependency>
<groupId>org.arquillian.cube</groupId>
<artifactId>arquillian-cube-docker</artifactId> <!--(1)-->
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-tomcat-remote-7</artifactId> <!--(2)-->
<version>1.0.0.CR7</version>
<scope>test</scope>
</dependency>
-
Adds Arquillian Cube dependency.
-
From the point of view of Arquillian, Tomcat is being executed in a remote host (in fact this is true because Tomcat is running inside Docker which is external to Arquillian), so we need to add the remote adapter.
And finally we need to configure Tomcat remote adapter and Arquillian Cube in arquillian.xml file.
<?xml version="1.0"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/schema/arquillian"
xsi:schemaLocation="http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<extension qualifier="docker"> <!--(1)-->
<property name="serverVersion">1.12</property> <!--(2)-->
<property name="serverUri">http://localhost:2375</property> <!--(3)-->
<property name="dockerContainers"> <!--(4)-->
tomcat:
image: tutum/tomcat:7.0
exposedPorts: [8089/tcp]
await:
strategy: polling
env: [TOMCAT_PASS=mypass, JAVA_OPTS=-Dcom.sun.management.jmxremote.port=8089 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false]
portBindings: [8089/tcp, 8080/tcp]
</property>
</extension>
<container qualifier="tomcat" default="true"> <!--(5)-->
<configuration>
<property name="host">localhost</property> <!--(6)-->
<property name="httpPort">8080</property> <!--(7)-->
<property name="user">admin</property> <!--(8)-->
<property name="pass">mypass</property>
</configuration>
</container>
</arquillian>
-
Arquillian Cube extension is registered.
-
Docker server version is required.
-
Docker server URI is required. In case you are using a remote Docker host or Boot2Docker here you need to set the remote host ip, but in this case Docker server is on same machine.
-
A Docker container contains a lot of parameters that can be configured. To avoid having to create one XML property for each one, a YAML content can be embedded directly as property.
-
Configuration of Tomcat remote adapter. Cube will start the Docker container when it is ran in the same context as an Arquillian container with the same name.
-
Host can be localhost because there is a port forwarding between container and Docker server.
-
Port is exposed as well.
-
User and password are required to deploy the war file to remote Tomcat.
And that’s all. Now you can run your test and you will see how tutum/tomcat:7.0 image is downloaded and started. Ports 8080 (Tomcat standard port) and 8089(JMX port used by Arquillian) are exposed. Finally in env section, environment variables are set. Read next link to understand why this is required https://docs.jboss.org/author/display/ARQ/Tomcat+7.0+-+Remote
Arquillian Cube requires some parameters to be configured, some related with Docker server and others related on the image that is being used. Let’s see valid attributes:
serverVersion |
Version of REST API provided by_Docker_ server. You should check on the Docker site which version of REST API is shipped inside installed Docker service. |
serverUri |
Uri of Docker server. If the Docker server is running natively on Linux then this will be localhost but if you are using Boot2Docker or a remote Docker server then the URI should be changed to point to the Docker remote URI. It can be a unix socket URI as well in case you are running Docker in Linux (unix:///var/run/docker.sock). |
dockerRegistry |
Sets the location of Docker registry. Default value is the official Docker registry located at https://registry.hub.docker.com |
dockerContainers |
Each Docker image (or container) can be configured with different parameters. This configuration is provided in YAML format. This property can be used to embed as YAML string value, all configuration. |
dockerContainersFile |
Instead of embedding YAML string, you can set YAML file location in this attribute. |
autoStartContainers |
Cube will normally only start a Docker container when it has the same name as an active Arquillian container. That works for things that are DeployableContainer's. For any other service, e.g. a database, you can use the autoStartContainers option to define which Docker containers to automatically start up. The option takes a comma separated list of Docker container ids. e.g. tomcat7, mysql. Arquillian Cube will attempt to start the containers in parallel if possible as well as start any linked containers. |
certPath |
Path where certificates are stored. If you are not using https protocol this parameter is not required. This parameter accepts starting with ~ as home directory |
boot2dockerPath |
Sets the full location (and program name) of boot2docker. For example /opt/boot2dockerhome/boot2docker. |
connectionMode |
Connection Mode to bypass the Create/Start Cube commands if the a Docker Container with the same name is already running on the target system. This parameter can receive three possible values. STARTANDSTOP which is the default one if not set any and simply creates and stops all Docker Containers. If a container is already running, an exception is thrown. STARTORCONNECT mode tries to bypass the Create/Start Cube commands if a container with the same name is already running, and if it is the case doesn’t stop it at the end. But if container is not already running, Cube will start one and stop it at the end of the execution. And last mode is STARTORCONNECTANDLEAVE which is exactly the same of STARTORCONNECT but if container is started by Cube it won’t be stopped at the end of the execution so it can be reused in next executions. |
In the next example you can see a whole YAML document with configuration properties. Keep in mind that almost all of them are configuration parameters provided by Docker remote API. In this example we are going to explain the attributes that are most used and special cases. Of course not all of them are mandatory:
Note
|
In YAML adding brackets ("[" "]") is for setting a list. |
tomcat: #1
image: tutum/tomcat:7.0 #2
exposedPorts: [8089/tcp] #3
await: #4
strategy: polling #5
workingDir: .
disableNetwork: true
hostName: host
portSpecs: [80,81]
user: alex
tty: true
stdinOpen: true
stdinOnce: true
memoryLimit: 1
memorySwap: 1
cpuShares: 1
attachStdin: true
attachStderr: true
env: [TOMCAT_PASS=mypass, JAVA_OPTS=-Dcom.sun.management.jmxremote.port=8089] #6
cmd: [] #7
dns: [127.0.0.1]
volumes: [/tmp]
volumesFrom: [tomcat]
binds:
- /host:/container:ro
links:
- name:alias
- name2:alias2
portBindings: [8089/tcp, 8081->8080/tcp] #8
privileged: true
publishAllPorts: true
networkMode: host
dnsSearch: [127.0.0.1]
devices:
cGroupPermissions: a
pathOnHost: b
pathInContainer: c
restartPolicy: #10
name: failure
maximumRetryCount: 1
capAdd: [a]
capDrop: [b]
extends: container-id #9
-
The name that are going to be assign to running container. It is mandatory.
-
The name of the image to be used. It is mandatory. If the image has not already been pulled by the Docker server, Arquillian Cube will pull it for you.
-
Sets exposed ports of the running container. It should follow the format port number slash(/) and _protocol (udp or tcp). Note that it is a list and it is not mandatory.
-
After a container is started, it starts booting up the defined services/commands. Depending on the nature of service, the lifecycle of these services are linked to start up or not. For example Tomcat, Wildlfy, TomEE and in general all Java servers must be started in foreground and this means that from the point of view of the client, the container never finishes to start. But on the other side other services like Redis are started in background and when the container is started you can be sure that Redis server is there. To avoid executing tests before the services are ready, you can set which await strategy should be used from Arquillian Cube side to accept that Docker container and all its defined services are up and ready. It is not mandatory and by default native strategy is used.
-
In strategy you set which strategy you want to follow. Currently three strategies are supported. static, native and polling.
-
You can pass environment variables by using
env
. In this section you can set specialdockerServerIp
string which at runtime will be replaced by Cube to current docker server ip. -
After the container is up, a list of commands can be executed within it.
-
Port forwarding is configured using
portBinding
section. It contains a list ofexposedPort
andport
separated by arrow (→). If only one port is provided, Arquillian Cube will expose the same port number. In this example the exposed port 8089 is mapped to 8089 and exposed port 8080 is mapped to 8081. -
You can extend another configuration. Any top level element and it’s children from the target container-id will be copied over to this configuration, unless they have been defined here already.
As we seen in basic example the definition Arquillian Cube scenarios are described in dockerContainers
property.
But if you want you can avoid using this property by simply creating a file called cube
at root of the classpath with same content.
Arquillian Cube will read it as it was defined in arquilllian.xml
file.
tomcat:
image: tomcat:7.0
exposedPorts: [8089/tcp]
await:
strategy: polling
env: [TOMCAT_PASS=mypass, JAVA_OPTS=-Dcom.sun.management.jmxremote.port=8089 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false]
portBindings: [8089/tcp, 8080/tcp]
After a container is started, it starts booting up the defined services/commands. Depending on the nature of service, the lifecycle of these services are linked to start up or not. For example Tomcat, Wildlfy, TomEE and in general all Java servers must be started in foreground and this means that from the point of view of the Docker client, the container never finishes to start. But on the other side other services like Redis are started in background and when the container is started you can be sure that Redis server is there. To avoid executing tests before the services are ready, you can set which await strategy should be used from Arquillian Cube side to accept that Docker container and all its defined services are up and ready.
Currently next await strategies are supported:
- native
-
it uses wait command. In this case current thread is waiting until the Docker server notifies that has started. In case of foreground services this is not the approach to be used.
- polling
-
in this case a polling (with ping or ss command) is executed for 5 seconds against all exposed ports. When communication to all exposed ports is acknowledged, the container is considered to be up. This approach is the one to be used in case of services started in foreground. By default polling executes ss command inside the running container to know if the server is already running. You can use a ping from client by setting type attribute to ping; Note that ping only works if you are running Docker daemon on localhost. In almost all cases the default behaviour matches all scenarios.
- static
-
similar to polling but it uses the host ip and specified list of ports provided as configuration parameter. This can be used in case of using Boot2Docker.
- sleeping
-
sleeps current thread for the specified amount of time. You can specify the time in seconds or milliseconds.
By default in case you don’t specify any await strategy, polling with ss command is used.
tomcat:
image: tutum/tomcat:7.0
exposedPorts: [8089/tcp]
await:
strategy: native
tomcat:
image: tutum/tomcat:7.0
exposedPorts: [8089/tcp]
await:
strategy: polling
sleepPollingTime: 200 s #1
iterations: 3 #2
-
Optional parameter to configure sleeping time between poling. You can set in seconds using s or miliseconds using ms. By default time unit is miliseconds and value 500.
-
Optional parameter to configure number of retries to be done. By default 10 iterations are done.
tomcat:
image: tutum/tomcat:7.0
exposedPorts: [8089/tcp]
await:
strategy: static
ip: localhost
ports: [8080, 8089]
tomcat:
image: tutum/tomcat:7.0
exposedPorts: [8089/tcp]
await:
strategy: sleeping
sleepTime: 200 s #1
-
Optional parameter to configure sleeping time between poling. You can set in seconds using s or miliseconds using ms. By default time unit is miliseconds and value 500.
When you are using Docker you can set two different parameters, exposedPort and portBinding. exposedPorts are ports that are available inside Docker infrastructure and they are used for communication between Docker containers but not from outside. On the other side portBindings are a way to instruct Docker container to publish a port to be available from outside (for example from our test).
It seems reasonable that if you set a port binding this port should automatically be exposed port as well. For this reason in Arquillian Cube you can use portBinding and it will automatically set to exposed port.
In next example we are only setting portBinding and Arquillian Cube will instruct Docker to expose port 8080 and of course bind the port 8080 so it can be accessible from outside.
daytime:
buildImage:
dockerfileLocation: src/test/resources/undertow
noCache: true
remove: true
await:
strategy: polling
portBindings: [8080/tcp]
With the configuration option connectionMode you can bypass the Create/Start Cube commands if the a Docker Container with the same name is already running on the target system. If so, Arquillian Cube will reuse that Container moving forward.
This allows you to prestart the containers manually during development and just connect to them to avoid the extra cost of starting the Docker Containers for each test run. This assumes you are not changing the actual definition of the Docker Container itself.
An example of this configuration could be:
<extension qualifier="docker">
<property name="serverVersion">1.12</property>
<property name="serverUri">http://localhost:2375</property>
<property name="connectionMode">STARTORCONNECT</property>
<property name="dockerContainers">
tomcat:
#more properties
</property>
</extension>
connectionMode is an attribute that can receive three parameters:
- STARTANDSTOP
-
it is the default one if not set any and simply creates and stops all Docker Containers. If a container is already running, an exception is thrown.
- STARTORCONNECT
-
it tries to bypass the Create/Start Cube commands if a container with the same name is already running, and if it is the case doesn’t stop it at the end. But if container is not already running, Cube will start one and stop it at the end of the execution.
- STARTORCONNECTANDLEAVE
-
it is exactly the same of STARTORCONNECT but if container is started by Cube it won’t be stopped at the end of the execution so it can be reused in next executions.
Sometimes when the tests has finished and container is stopped you want to inspect some data like container console or getting a file from the container to manual inspecting. In these cases you can configure each container to copy console log or copy a file/s from container to local machine just before container is stopped.
Next snippet shows how to copy a directory from container to local disk:
tomcat_default:
image: tutum/tomcat:7.0
beforeStop: # (1)
- copy: # (2)
from: /test
to: /tmp
- log: # (3)
to: /tmp/container.log
-
beforeStop goes into the container section and may contain a list of copy and log elements.
-
copy is used to notify that we want to copy some directories or files form from container location to to local location.
-
log is used to notify that we want to copy container log to to local location.
In case of log command the standard output and the error output are returned. log Docker command can receive some configuration paramters and you can set them too in configuration file.
beforeStop:
- log:
to: /tmp/container.log
follow: true
stdout: true
stderr: false
timestamps: true
tail: 10
serverUri parameter is where you configure the Uri of Docker server. This parameter is not mandatory and in case you don’t set it, Arquillian Cube will use next values:
Linux |
unix:///var/run/docker.sock |
Windows |
|
MacOS |
If you are using boot2docker there are some parameters that depends on the installation of the boot2docker. For example boot2docker ip is not localhost and may change every time you start a new boot2docker instance. Also every time you start boot2docker copies required certificates to home directory of local machine.
Arquillian Cube offers some automatic mechanisms to use boot2docker in Cube.
The first one is that serverUri parameter can contain the word boot2docker like for example https://boot2docker:2376. When Cube is started it will check if the serverUri contains the boot2docker word, and if it is the case it will run the command boot2docker ip to get the ip and substitute the boot2docker sentence to the ip returned by that command.
Note that by default Arquillian Cube assumes that boot2docker command is on PATH, but you can configure its location by using boot2dockerPath which is the full location (and program name) of boot2docker. For example /opt/boot2dockerhome/boot2docker.
boot2docker runs in https and you need to set the certificates path. These certificates are copied by boot2docker by default at <HOME>/.boot2docker/certs/boot2docker-vm. If this propery is not set and the serverUri contains boot2docker, then this property is automatically configured to <HOME>/.boot2docker/certs/boot2docker-vm so you don’t need to worry to set for each environment.
To build a container Docker uses a file called Dockerfile http://docs.docker.com/reference/builder/. Arquillian Cube also supports building and running a container from a Dockerfile.
To set that Arquillian Cube must build the container, the image property must be changed to buildImage and add the location of Dockerfile.
Let’s see previous example but instead of creating a container from a predefined image, we are going to build one:
<property name="dockerContainers">
tomcat:
buildImage: #1
dockerfileLocation: src/test/resources-tomcat-7-dockerfile/tomcat #2
noCache: true #3
remove: true #4
await:
strategy: polling
env: [JAVA_OPTS=-Dcom.sun.management.jmxremote.port=8089 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false]
portBindings: [8089/tcp, 8080/tcp]
</property>
-
buildImage section is used in front of image. In case of both sections present in a document, image section has preference over buildImage.
-
dockerfileLocation contains the location of Dockerfile and all files required to build the container.
-
Property to enable or disable the no cache attribute.
-
Property to enable or disable the remove attribute.
Tip
|
dockerfileLocation can be a directory that must contains Dockerfile in root directory, also a tar.gz file or a URL pointing to a tar.gz file. |
An example of Dockerfile is:
FROM tutum/tomcat:7.0
ENV JAVA_OPTS -Dcom.sun.management.jmxremote.port=8089 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
ADD tomcat-users.xml /tomcat/conf/ # (1)
EXPOSE 8089
CMD ["/tomcat/bin/catalina.sh","run"]
-
tomcat-users.xml file is located at same directory as Dockerfile.
Arquillian Cube comes with a few enrichers.
One for injecting the CubeID(containerId) of the current container created for executing the test, one that injects the CubeController to call lifecycle methods on any cube and one that injects com.github.dockerjava.api.DockerClient instance used to communicate with Docker server.
DockerClient injection only work if the tests are run in client mode, that is by using @RunAsClient or by setting the testable property to false @Deployment(testable = false).
These can be injected using the @ArquillianResource annotation.
As examples:
@ArquillianResource
CubeID containerId;
@ArquillianResource
DockerClient dockerClient;
@ArquillianResource
CubeController cubeController;
Probably any application you may write will need an application/servlet container but also other servers like database server or mail server. Each one will be placed on one Docker Container. So for example a full application may contain one Docker Container with an application server (for example Wildfly) and another container with a database (for example H2).
Arquillian Cube can orchestrate these containers as well.
An example of orchestration can be:
<property name="autoStartContainers">database</property> <!--(1)-->
<property name="dockerContainers">
wildfly_database:
extends: wildfly
links:
- database:database #2
database:
image: zhilvis/h2-db
exposedPorts: [81/tcp, 1521/tcp]
await:
strategy: polling
portBindings: [1521/tcp, 8181->81/tcp]
</property>
<container qualifier="wildfly_database">
<configuration>
<property name="target">wildfly:8.1.0.Final:remote</property>
<property name="managementPort">9991</property>
<property name="username">admin</property>
<property name="password">Admin#70365</property>
</configuration>
</container>
-
This property is used to start containers before any test is executed. In this case database container.
-
We use link property to connect Wildfly container to database container.
In this case when a test is started both containers are started and when both are ready to receive requests, the test will be executed.
And the database definition shall be:
@DataSourceDefinition(
name = "java:app/TestDataSource",
className = "org.h2.jdbcx.JdbcDataSource",
url = "jdbc:h2:tcp://database:1521/opt/h2-data/test",
user = "sa",
password = "sa"
)
@Stateless
public class UserRepository {
@PersistenceContext
private EntityManager em;
public void store(User user) {
em.persist(user);
}
}
Arquillian Cube can automatically configure default ports of container in case of port forwarding.
What Arquillian Cube does internally is remapping default DeployableContainer
port values to the ones configured in Docker Containers configuration.
Suppose you have a Docker Container configuration like:
<property name="dockerContainers">
tomcat_default:
image: tutum/tomcat:7.0
exposedPorts: [8089/tcp]
await:
strategy: polling
env: [TOMCAT_PASS=mypass, JAVA_OPTS=-Dcom.sun.management.jmxremote.port=8089 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false]
portBindings: [8089/tcp, 8081->8080/tcp] #1
</property>
-
Note that the exposed port is the 8081.
Then in theory you should configure the remote Tomcat adapter to port 8081 on your arquillian.xml file. But let’s say that you are using that remote adapter for a remote local machine Tomcat (outside Docker) too, and is configured to use 8080 port.
<container qualifier="tomcat_default">
configuration>
<property name="host">localhost</property>
<property name="user">admin</property>
<property name="pass">mypass</property>
</configuration>
</container>
Which basically uses default port (8080) to connect to remote server.
In this case you don’t need to create a new container
tag, Arquillian Cube is smart enough to change the default port value automatically; in case of Tomcat 8080 to 8081.
Arquillan Cube will apply autoremapping to all properties that contains port
as a substring of the property, and will remap if it is necessary.
Note
|
Automapping only works in case you want to change the default server port to a Docker port forwarded port. |
If you are using a remote docker server (not on localhost) or for example boot2docker you may want to set that ip to Arquillian remote adapter configuration so it can deploy the archive under test. In these cases you can hardcode this ip to arquillian container adapter configuration or you can use the special tag dockerServerIp. At runtime these tag will be replaced by Arquillian Cube to docker server ip configured in serverUri parameter. This replacement only works in properties that contains the string host or address in property name.
So for example:
<extension qualifier="docker">
<property name="serverUri">http://192.168.0.2:2756</property> <!--(1)-->
...
</extension>
<container qualifier="tomcat_default">
configuration>
<property name="host">dockerServerIp</property> <!--(2)-->
<property name="user">admin</property>
<property name="pass">mypass</property>
</configuration>
</container>
-
We set the serverUri as usually.
-
dockerServerIp is replaced at runtime.
The host property will be replaced automatically to 192.168.0.2.
Note
|
This also works in case you set serverUri using boot2docker special word or by using the defaults. Read more about it Boot2Docker section and Automatic serverUri resolution section. |
In case of using unix socket dockerServerUri is replaced to localhost.
Also Arquillian Cube can help you in another way inferring boot2docker ip.
In case you are running in MACOS or Windows with boot2docker, you may not need to set host property at all nor using dockerServerIp.
Arquillian Cube will inspect any property in configuration class that contains the word address or host that it is not overriden in arquillian.xml
and it will set the boot2docker server automatically.
So previous example could be modified to:
<container qualifier="tomcat_default">
configuration>
<property name="user">admin</property>
<property name="pass">mypass</property>
</configuration>
</container>
And in case you are running on Windows or MacOS, `host`property will be automatically set to the +boot2docker +ip.
In all previous sections we have seen that the application is deployed inside a container. For example in case of Tomcat application, resources are deployed inside a Servlet container or for example in case of Apache TomEE you can deploy EJBs inside an EJB container.
But nowadays there other kind of applications that contains the container (if they have one) embedded inside them. Typically these applications uses an embedded server and they are run as CLI applications. Some examples can be Spring Boot, Netty, SparkJava or Undertow.
If you are using some of these technologies with Docker, you can still use Arquillian Cube to write your tests.
Let’s suppose we are writing a service which should return as text the current day and time. To serve this service to the world we decide to use undertow embedded server.
The code looks like:
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DaytimeServer {
public static void main(String[] args) { //(1)
Undertow server = Undertow.builder()
.addHttpListener(8080, "0.0.0.0")
.setHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send(simpleDateFormat.format(new Date()) + System.lineSeparator()); //(2)
}
}).build();
server.start();
}
}
-
This class is a CLI application.
-
Returns a text with the day and time formatted with SimpleDateFormat.
See that this application is a CLI application which is pretty different from previous examples. Previously the packaged application was deployed inside an application server, which in fact means that Arquillian connects to the server and tells it to deploy that file.
In this example there is no application server nor servlet server waiting for Arquillian to deploy an archive but the application is self-contained, it contains everything. So in fact if you want to run the application probably you will end up by doing something like java -jar daytime.jar.
So how to write a test for these classes if we are using Docker as runtime container?
The first thing to do is add arquillian-cube-containerless dependency.
<dependency>
<groupId>org.arquillian.cube</groupId>
<artifactId>arquillian-cube-containerless</artifactId>
<version>${arquillian.cube.version}</version>
</dependency>
Next step is creating a Dockerfile. This is required because we need to set not only the container image to be used but how to run the application. But see that there is a problem on creating a Dockerfile in this case. The jar name is not static because it will depend on the name you give during the creation of the archive (using Shrinkwrap). So in fact Dockerfile should be templaterized. And this is something that Arquillian Cube can do for you. The idea is creating a file called DockerfileTemplate.
FROM java:7
WORKDIR /usr/src/server
COPY ${deployableFilename} /usr/src/server/${deployableFilename} #1
EXPOSE 8080
CMD ["java", "-jar", "${deployableFilename}"]
-
${deployableFilname} will be replaced at runtime by the name of the jar file created by Shrinkwrap.
Then we need to touch arquillian.xml file by setting an special container definition so Arquillian doesn’t crash because of trying to deploy the archive into a none defined container.
<?xml version="1.0"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/schema/arquillian"
xsi:schemaLocation="http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<extension qualifier="docker">
<property name="serverVersion">1.12</property>
<property name="serverUri">http://localhost:2375</property>
<property name="dockerContainers"> <!--(1)-->
daytime:
buildImage: <!--(2)-->
dockerfileLocation: src/test/resources/undertow <!--(3)-->
noCache: true
remove: true
await:
strategy: polling
portBindings: [8080/tcp]
</property>
</extension>
<container qualifier="containerless" default="true"> <!--(4)-->
<configuration>
<property name="containerlessDocker">daytime</property> <!--(5)-->
<property name="embeddedPort">8080</property> <!--(6)-->
</configuration>
</container>
</arquillian>
-
The Docker container is defined as per usual.
-
buildImage attribute is used to define the dockerfile location.
-
This attribute sets the directory where the Dockerfile is stored. In fact in this case it is the directory where DockerfileTemplate file is stored.
-
A container provided by Arquillian Cube must be defined.
-
This property is used to set which container must be started.
-
This property sets the exposed port by the embedded server.
And finally the test:
@RunWith(Arquillian.class)
public class DaytimeTest {
private static final String LINE_SEPARATOR = System
.getProperty("line.separator");
@Deployment(testable = false) //(1)
public static JavaArchive createDeployment() {
JavaArchive[] undertow = Maven.resolver().resolve("io.undertow:undertow-core:1.1.1.Final").withTransitivity().as(JavaArchive.class); //(2)
JavaArchive jar = ShrinkWrap
.create(JavaArchive.class, "daytime.jar")
.addClass(DaytimeServer.class); //(3)
for (JavaArchive javaArchive : undertow) { //(4)
jar.merge(javaArchive);
}
jar.addAsManifestResource(
new StringAsset(
"Main-Class: org.arquillian.cube.impl.containerless.DaytimeServer"
+ LINE_SEPARATOR), "MANIFEST.MF"); //(5)
return jar;
}
@Test
public void shouldReturnDateFromDaytimeServer(@ArquillianResource URL base) { //(6)
try (
BufferedReader in = new BufferedReader(new InputStreamReader(
base.openStream()));) {
String userInput = in.readLine();
assertThat(userInput, notNullValue());
} catch (UnknownHostException e) {
fail("Don't know about host ");
} catch (IOException e) {
fail("Couldn't get I/O for the connection to ");
}
}
}
-
Tests should be run as-client.
-
ShrinkWrap Maven resolver gets all dependencies for Undertow.
-
Create a jar file called daytime.jar with DaytimeServer class.
-
Undertow dependencies are merged inside jar.
-
Because it is a runnable jar, MANIFEST is created accordantly.
-
Simple test.
In previous section we have seen that we can test any java CLI application that offers a socket connection. But if you think clearly there is nothing that avoid Arquillian Cube to deploy applications developed in other languages like Node.js, Play, Ruby on Rails, …
Let’s see an example on how you can use Arquillian Cube to test a Node.js hello world application.
First thing to do is create the Node.js application.
{
"name": "helloworld-server",
"version": "0.0.1",
"description": "A NodeJS webserver to run inside a docker container",
"author": "asotobu@gmail.com",
"license": "APLv2",
"dependencies": {
"express": "*"
},
"scripts": {"start": "node index.js"}
}
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('Hello from inside a container!');
});
app.listen(8080);
Then we need to define a DockerfileTemplate as we did for Undertow.
FROM node:0.11.14
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ADD ${deployableFilename} /usr/src/app #1
RUN npm install
EXPOSE 8080
CMD [ "npm", "start" ]
-
We need to use ADD command adding the deployed file instead of COPY. We are going to see why below.
Finally the arquillian.xml configuration file.
<?xml version="1.0"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/schema/arquillian"
xsi:schemaLocation="http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<extension qualifier="docker">
<property name="serverVersion">1.12</property>
<property name="serverUri">http://localhost:2375</property>
<property name="dockerContainers">
node:
buildImage:
dockerfileLocation: src/test/resources/node
noCache: true
remove: true
await:
strategy: polling
portBindings: [8080/tcp]
</property>
</extension>
<container qualifier="containerless" default="true">
<configuration>
<property name="containerlessDocker">node</property> <!--(1)-->
<property name="embeddedPort">8080</property>
</configuration>
</container>
</arquillian>
-
This property is used to set which container must be started. In this case node.
And finally the Arquillian test.
@RunWith(Arquillian.class)
public class NodeTest {
@Deployment(testable = false) //(1)
public static GenericArchive createDeployment() {
return ShrinkWrap.create(GenericArchive.class, "app.tar") //(2)
.add(new ClassLoaderAsset("index.js"), "index.js")
.add(new ClassLoaderAsset("package.json"), "package.json");
}
@Test
public void shouldReturnMessageFromNodeJs(@ArquillianResource URL base) { //(3)
try (BufferedReader in = new BufferedReader(new InputStreamReader(
base.openStream()));) {
String userInput = in.readLine();
assertThat(userInput, is("Hello from inside a container!"));
} catch (UnknownHostException e) {
fail("Don't know about host ");
} catch (IOException e) {
fail("Couldn't get I/O for the connection to ");
}
}
}
-
Tests should be run as-client.
-
GenericArchive with tar extension must be created using Shrinkwrap.
-
Simple test.
Note
|
GenericArchive must end with tar extension because it is expected by Arquillian Cube. When you use ADD in Dockerfile, Docker will untar automatically the file to given location. |
API will continuously evolve to fit requirements of an enterprise application as well as providing integration with Kubernates and other Docker related tools. Also some configuration parameters will be modified to fix any possible requirements. Although we are going to try to not break compatibility with previous versions, we cannot guarantee until beta stage.
Feel free to use it and any missing feature, bug or anything you see , feel free to add a new issue.