Using Docker for Testing MUKTA APHALE ATA GTR 2016, PUNE @muktaa
Using Docker for TestingMUKTA APHALE
ATA GTR 2016, PUNE
@muktaa
Who Am I?• Developer, DevOps Enthusiast• Passion for technology, innovation and the thirst to keep
learning• Love to code• Love to design solutions• Love to network• Believe in dreaming big• Believe in work-life balance
@muktaa
Agenda•Where testing stands today, current challenges of
testing•What is Docker?•How can docker help in better automation testing?•A traditional way of doing integration testing•Demo of using Docker for testing•Demo of integrating dockerized tests with your CI
@muktaa
Pain Points of Testing•Unit tests, integration tests, complex integration tests•UI testing•Git branching• Environments• Failed tests pollute environment•Deployment dependencies, testing dependencies
@muktaa
Docker
@muktaa
Linux Container Docker Data Centre, Docker Cloud, DockerEngine, Docker Hub,
UCP
Benefits:Speed
PortabilityDensity
Open Source
“Can create lightweight, self sufficient containers from
any application”
Docker Applications•Testing•Microservices Applications• Simplifying Development• Simplifying Deployment•Production• CaaS
@muktaa
Docker for enabling Testing•Mimic production environment• Consistent Environments•Black Box Testing• Scalability Testing• Configuration Testing•Testing Simplified
@muktaa
Deploying for Integration Testing: Old Way• Build project• SCP the build artifact to the
server(s)• Ssh into the server• Stop webserver• Replace the build artifact at the
right path• Restart webserver
o SCP the tar.gz from the project to the server(s)
o ssh into the servero stop tomcato remove the “work”, “webapps”,
“shared” and “common” folderso extract the tar.gz into the
tomcat foldero start tomcat
@muktaa
Integration Testing: Old Way•Wait for deployment to complete◦Webserver comes up
• Ensure setup is clean◦DB Scripts, Env variables, etc
•Get your latest integration test code ready•Point your integration tests to your newly deployed
setup•Run integration tests
@muktaa
Let’s use Docker…
@muktaa
Example Scenario• Webserver
• NGiNX
• App Server• 3 load balanced instances• Node js
• DB Server• Redis
@muktaa
Output• Prints the number of times
the page was hit
• Source Code:
https://github.com/muktaa/docker-example/
@muktaa
Dockerizing Each Component• Dockerfile for each
component• Docker-compose.yml• Build Script
@muktaa
DemoDEPLOY APPLICATION FOR INTEGRATION TESTING
@muktaa
Steps•Git clone https://github.com/muktaa/docker-example/•Docker-compose build•Docker-compose up
@muktaa
Running Integration TestsLETS DOCKERIZE THE INTEGRATION TESTS TOO!
@muktaa
Example of Selenium Tests• https://github.com/muktaa/SeleniumExample•Ready docker image available:◦Docker pull selenium-example
• Customize the sample tests, refer to README: https://github.com/muktaa/SeleniumExample/blob/master/README.md•Headless Selenium tests
@muktaa
Demo• https://github.com/muktaa/SeleniumExample/blob/m
aster/Dockerfile•Docker pull muktaa/selenium-example:headless•Docker run -it -e
TEST_URL=http://54.254.223.84:5555/muktaa/selenium-example:headless
@muktaa
Sample Output
@muktaa
Nightly BuildPUTTING IT ALL TOGETHER TO WRITE NIGHTLY BUILD JOB FOR OUR DOCKERIZED INTEGRATION TESTS
@muktaa
Jenkins Job
@muktaa
Job Configuration
@muktaa
Job Build Step 1#/bin/bash +x
docker-compose --project-name=${JOB_NAME} stop &> /dev/null || true &> /dev/null
docker-compose --project-name=${JOB_NAME} rm --force &> /dev/null || true &> /dev/null
docker stop `docker ps -a -q -f status=exited` &> /dev/null || true &> /dev/null
docker rm -v `docker ps -a -q -f status=exited` &> /dev/null || true &> /dev/null
docker rmi `docker images --filter 'dangling=true' -q --no-trunc` &> /dev/null || true &> /dev/null
docker-compose build
docker-compose --project-name=${JOB_NAME} up &
@muktaa
Job Build Step 2, 3
docker run -itd -v /var/lib/jenkins/workspace/docker-example/test-report:/usr/share/app/target/surefire-reports -e TEST_URL=http://54.254.223.84:5555/ muktaa/selenium-example:headless
@muktaa
Job Build Steps 3, 4
@muktaa
What Next?• Lots More!•Running Selenium tests inside Docker•Using docker swarm to deploy•Using docker machine to manage hosts for testing
@muktaa
References• http://blogs.plos.org/tech/testing-made-awesome-with-docker/• https://www.wouterdanes.net/2014/04/11/continuous-integration-using-
docker-maven-and-jenkins.html• http://making.meetup.com/post/122890386432/steps-towards-automated-
testing-with-docker-and• https://divan.github.io/posts/integration_testing/• https://medium.com/@WoloxEngineering/ruby-on-rails-continuous-
integration-with-jenkins-and-docker-compose-8dfd24c3df57#.7vxoq2gfg• http://anandmanisankar.com/posts/docker-container-nginx-node-redis-
example/• http://www.conductor.com/nightlight/running-selenium-grid-using-docker-
compose/
@muktaa
Thank You!QUESTIONS?
@muktaa