In the previous section, we laid out a stimulating foundation on software testing, how to leverage the Docker technology for the software testing, and the unique benefits of the container technology during the testing phase. In this section, we will introduce you to the steps required to prepare the Jenkins environment for testing with Docker, and then, demonstrate how Jenkins can be extended to integrate and automate testing with Docker using the well-known hit count use case.
In this section, we will take you through the steps to install jenkins
, GitHub plugin for Jenkins and git
, and the revision control tool. These steps are as follows:
$ wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
Here, we are using wget
to download the PGP public key, and then we add it to the list of trusted keys using the apt-key tool. Since Ubuntu and Debian share the same software packaging, Jenkins provides a single common package for both Ubuntu and Debian.
apt
package source list, as follows:$ sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
apt-get
command update option to resynchronize the package index from the sources:$ sudo apt-get update
jenkins
using the apt-get
command install option, as demonstrated here:$ sudo apt-get install jenkins
jenkins
service using the service
command:$ sudo service jenkins start
jenkins
service can be accessed through any web browsers by specifying the IP address (10.1.1.13
) of the system in which Jenkins is installed. The default port number for Jenkins is 8080
. The following screenshot is the entry page or Dashboard of Jenkins:http://<jenkins-server>:8080/pluginManager/advanced
, wherein <jenkins-server>
is the IP address of the system in which Jenkins is installed.GitHub plugin
as the filter, which will list just the GitHub plugin, as shown in the following screenshot:http://< jenkins-server >:8080/restart
, where <jenkins-server>
is the IP address of the system in which Jenkins is installed.git
package is installed, otherwise install the git
package using the apt-get
command:$ sudo apt-get install git
sudo
command, but unfortunately, we could not invoke sudo
inside Jenkins because sometimes it prompts for a password. To overcome the sudo
password prompt issue, we can make use of the Docker group, wherein any user who is part of the Docker group can invoke the Docker client without using the sudo
command. Jenkins installation always sets up a user and group called jenkins
and runs the Jenkins server using that user and group. So, we just need to add the jenkins
user to the Docker group to get the Docker client working without the sudo
command:$ sudo gpasswd -a jenkins docker Adding user jenkins to group docker
jenkins
service for the group change to take effect using the following command:$ sudo service jenkins restart * Restarting Jenkins Continuous Integration Server jenkins [ OK ]
We have set up a Jenkins environment that is now capable of automatically pulling the latest source code from the http://github.com repository, packaging it as a Docker image, and executing the prescribed test scenarios.
In this section, we will explore how to automate testing using Jenkins and Docker. As mentioned earlier, we are going to use GitHub as our repository. We have already uploaded the Dockerfile
, test_hitcount.py
, and hitcount.py
files of our previous example to GitHub at https://github.com/thedocker/testing, which we are to use in the ensuing example. However, we strongly encourage you to set up your own repository at http://github.com, using the fork option that you can find at https://github.com/thedocker/testing, and substitute this address wherever applicable in the ensuing example.
The following are the detailed steps to automate the Docker testing:
Docker-Testing
), and select the Freestyle project radio button:15
minute interval. Type the following line of code H/15 * * * *
in the Schedule textbox, as shown in the following screenshot. For testing purposes, you can reduce the polling interval:Started by user anonymous Building in workspace /var/lib/jenkins/jobs/Docker-Testing/workspace Cloning the remote Git repository Cloning repository https://github.com/thedocker/testing/ . . . OUTPUT TRUNCATED . . . + docker build -t docker_testing_using_jenkins . Sending build context to Docker daemon 121.9 kB Sending build context to Docker daemon Step 0 : FROM python:latest . . . OUTPUT TRUNCATED . . . Successfully built ad4be4b451e6 + docker run --rm docker_testing_using_jenkins . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK Finished: SUCCESS
Apparently, the build failed as we expected.
Started by an SCM change Building in workspace /var/lib/jenkins/jobs/Docker-Testing/workspace . . . OUTPUT TRUNCATED . . . ImportError: No module named 'error_hitcount' ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (errors=1) Build step 'Execute shell' marked build as failure Finished: FAILURE
Evidently, the test failed because of the wrong module name error_hitcount
, which we deliberately introduced.
Cool, isn't it? We automated our testing using Jenkins and Docker. Besides, we are able to experience the power of testing automation using Jenkins and Docker. In a large-scale project, Jenkins and Docker can be combined together to automate the complete unit testing needs, and thus, to automatically capture any defects and deficiencies introduced by any developers.