Integrating Docker testing into Jenkins

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.

Preparing the Jenkins environment

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:

  1. We begin with adding the Jenkins' trusted PGP public key:
    $ 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.

  2. Add the Debian package location to the 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'
    
  3. Having added the package source, continue to run the apt-get command update option to resynchronize the package index from the sources:
    $ sudo apt-get update
    
  4. Now, install jenkins using the apt-get command install option, as demonstrated here:
    $ sudo apt-get install jenkins
    
  5. Finally, activate the jenkins service using the service command:
    $ sudo service jenkins start
    
  6. The 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:
    Preparing the Jenkins environment
  7. In this example, we are going to use GitHub as the source code repository. Jenkins does not support GitHub by default and hence, we need to install the GitHub plugin. During the installation, sometimes Jenkins does not populate the plugin availability list, and hence, you have to force it to download the list of available plugins. You can do so by performing the following steps:
    1. Select Manage Jenkins on the left-hand side of the screen, which will take us to a Manage Jenkins page, as shown in the following screenshot:
      Preparing the Jenkins environment
    2. On the Manage Jenkins page, select Manage Plugins and this will take us to the Plugin Manager page, as shown in the following screenshot:
      Preparing the Jenkins environment
    3. Here, on the Plugin Manager page, select the Advanced tab, go to the bottom of this page, and you will find the Check now button in the right-hand side corner of the page. Click on the Check now button to start the plugin updates. Alternatively, you can directly go to the Check now button on the Advanced page by navigating to http://<jenkins-server>:8080/pluginManager/advanced, wherein <jenkins-server> is the IP address of the system in which Jenkins is installed.

    Note

    NOTE: If Jenkins does not update the available plugin list, it is most likely a mirror site issue, so modify the Update Site field with a working mirror URL.

  8. Having updated the available plugin list, let's continue to install the GitHub plugin, as depicted in the following substeps:
    1. Select the Available tab in the Plugin Manager page, which will list all the available plugins.
    2. Type GitHub plugin as the filter, which will list just the GitHub plugin, as shown in the following screenshot:
      Preparing the Jenkins environment
    3. Select the checkbox, and click on Download now and install after restart. You will be taken to a screen that will show you the progress of the plugin installation:
      Preparing the Jenkins environment
    4. After all the plugins have successfully downloaded, go ahead and restart Jenkins using http://< jenkins-server >:8080/restart, where <jenkins-server> is the IP address of the system in which Jenkins is installed.
  9. Ensure that the git package is installed, otherwise install the git package using the apt-get command:
    $ sudo apt-get install git
    
  10. So far, we have been running the Docker client using the 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
    
  11. Restart the 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.

Preparing the Jenkins environment

Automating the Docker testing process

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:

  1. Configure Jenkins to trigger a build when a file is modified in the GitHub repository, which is illustrated in the following substeps:
    1. Connect to the Jenkins server again.
    2. Select either New Item or create new jobs:
      Automating the Docker testing process
    3. In the following screenshot, give a name to the project (for example, Docker-Testing), and select the Freestyle project radio button:
      Automating the Docker testing process
    4. In the next screenshot, select the Git radio button under Source Code Management, and specify the GitHub repository URL in the Repository URL text field:
      Automating the Docker testing process
    5. Select Poll SCM under Build Triggers to schedule GitHub polling for every 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:
      Automating the Docker testing process
    6. Scroll down the screen a little further, and select the Add build step button under Build. In the drop-down list, select Execute shell and type in the text, as shown in the following screenshot:
      Automating the Docker testing process
    7. Finally, save the configuration by clicking on the Save button.
  2. Go back to the Jenkins Dashboard, and you can find your test listed on the dashboard:
    Automating the Docker testing process
  3. You can either wait for the Jenkins schedule to kick-start the build, or you can click on the clock icon on the right-hand side of the screen to kick-start the build immediately. As soon as the build is done, the Dashboard is updated with the build status as a success or failure, and the build number:
    Automating the Docker testing process
  4. If you hover the mouse closer to the build number, you will get a drop-down button with options, such as Changes, Console Output, and so on, as shown in the following screenshot:
    Automating the Docker testing process
  5. The Console Output option will show the details highlighted for the build, as follows:
    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
    
  6. Evidently, the test failed because of the wrong module name error_hitcount, which we deliberately introduced. Now, let's experiment a negative scenario by deliberately introducing a bug in test_hitcount.py and observe the effect on Jenkins build. As we have configured Jenkins, it faithfully polls the GitHub and kick-starts the build:
    Automating the Docker testing process

    Apparently, the build failed as we expected.

  7. As a final step, open Console Output of the failed build:
    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.

Automating the Docker testing process
Automating the Docker testing process
Automating the Docker testing process
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset