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 Docker testing:

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 create new jobs.
  1. As shown in the following screenshot, give a name to the project (for example, Docker-Testing), and select Freestyle project:
  1. As shown 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:
  1. 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:
  1. Scroll down the screen a little further and click on 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:
  1. 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:
  1. 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:
  1. If you hover the mouse closer to the build number, you will get a drop-down button with options, such as Changes and Console Output, as shown in the following screenshot:
  1. The Console Output option will show the details highlighted for the build, as follows:
      Started by user Vinod Singh
Building in workspace
/var/lib/jenkins/workspace/Docker-testing

Cloning the remote Git repository
Cloning repository
https://github.com/thedocker/testing

> git init
/var/lib/jenkins/workspace/Docker-testing
# timeout=10

Fetching upstream changes from
https://github.com/thedocker/testing

> git --version # timeout=10
Removing intermediate container 76a53284f1e3
Successfully built d9e22d1d52c6
+ docker run --rm docker_testing_using_jenkins
.
--------------------------------------------
--------------------------

Ran 1 test in 0.000s

OK
Finished: SUCCESS
  1. Now, let's test the failed case 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 the Jenkins build. As we have configured Jenkins, it faithfully polls the GitHub and kick-starts the build.

Apparently, the build failed as we expected:

  1. 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.

..................Content has been hidden....................

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