Dockerize your unit tests for faster feedback

1,559 views

Published on

Ever got the message "Fatal error: Out of memory" when running your unit tests? Or do you have to wait for more than 5 minutes for all of your unit tests to complete? We're using Docker to mitigate these and other issues. In this session I will show you how we have split up a single testsuite of 3K+ tests into manageable unit modules which are executed by Docker micro instances in parallel. And with this set up we test our application on the current PHP version, but it also allows us to test against a new major PHP version the moment it is released or even is available in beta.

Published in: Engineering

Dockerize your unit tests for faster feedback

  1. 1. DOCKERIZE YOUR UNIT TESTS FOR FASTER FEEDBACK Let’s speed up your tests! https://goo.gl/images/OdSqB4
  2. 2. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 2 FotobyAnnaFilinaatBGPHP16
  3. 3. Michelangelo van Dam β€£ Pro PHP consultant at in2it β€£ President of PHPBenelux β€£ Open-Source contributor β€£ Conference Speaker β€£ ZCE and ZF-CE in itPROFESSIONAL PHP SERVICES PHPUnit + Docker = πŸš—πŸ’¨ 3
  4. 4. PHPUNIT www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 4 https://goo.gl/images/eBa0vw
  5. 5. EXTRAS TO RUN PHPUNIT www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 5 β€£ Command-line? β€£ Run from IDE? β€£ Test server? β€£ Jenkins, TeamCity, Bamboo? β€£ Vagrant? β€£ Docker? https://goo.gl/images/l0HXj7
  6. 6. DRAWBACKS www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 6 β€£ Takes long time for many tests β€£ Environment not always ready for testing β€£ Tests don’t provide good feedback https://goo.gl/images/TajZrP
  7. 7. SYSTEM FAILURES www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 7 β€£ Memory Exhausted β€£ Disk full β€£ No network or network latency β€£ Systems crash https://goo.gl/images/ruod6b
  8. 8. DOCKER www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 8 https://goo.gl/images/vhDEGy
  9. 9. WHAT IS DOCKER? www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 9 β€£ Technology platform β€£ Run processes in separate containers β€£ Scales fast and seemlessly β€£ Containers are easy shippable β€£ Best next thing since sliced bread https://goo.gl/images/xEfLk5
  10. 10. DOCKER USAGE www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 10 β€£ Mimic distributed architectures β€£ Isolate applications β€£ Run single purpose commands https://goo.gl/images/LIfWVb
  11. 11. DEMO TIME www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 11
  12. 12. RUN THEM ALL www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 12 https://goo.gl/images/grh4mu
  13. 13. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 13
  14. 14. USING @GROUP www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 14 https://www.flickr.com/photos/bgphp/21210790293
  15. 15. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 15
  16. 16. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 16
  17. 17. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 17 for i in `./vendor/bin/phpunit --list-groups | grep "^ -" | awk {'print $2'}`; do ./vendor/bin/phpunit --group $i; done;
  18. 18. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 18
  19. 19. SHIP IT WITH DOCKER!!! www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 19 https://goo.gl/images/Fo0dVi
  20. 20. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 20 docker run -ti --rm -v "$PWD":/usr/src/myapp -w /usr/ src/myapp php:7.0-cli ./vendor/bin/phpunit
  21. 21. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 21
  22. 22. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 22 https://www.gnu.org/software/parallel/
  23. 23. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 23 for i in `./vendor/bin/phpunit β€”list-groups | grep "^ -β€œ | awk {'print $2'}`; do echo $i; done | /usr/local/ bin/parallel docker run -v "$PWD":/usr/src/myapp -w / usr/src/myapp php:7.0-cli ./vendor/bin/phpunit --log- junit=build/junit-{}.xml --group {}
  24. 24. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 24
  25. 25. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 25 for i in `./vendor/bin/phpunit β€”list-groups | grep "^ -β€œ | awk {'print $2'}`; do echo $i; done | time /usr/ local/bin/parallel docker run -d -v "$PWD":/usr/src/ myapp -w /usr/src/myapp php:7.0-cli ./vendor/bin/ phpunit --log-junit=build/junit-{}.xml --group {}
  26. 26. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 26
  27. 27. WHERE ARE MY FAILURES? www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 27 https://goo.gl/images/VWpYFO
  28. 28. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 28 for i in `./vendor/bin/phpunit --list-groups | grep "^ -" | awk {'print $2'}`; do echo $i; done | time parallel docker run -d -v "$PWD":/var/run/phpunit -w / var/run/phpunit --name pu-docker-{} php:7.0-cli /var/ run/phpunit/vendor/bin/phpunit --group {} && for i in `./vendor/bin/phpunit --list-groups | grep "^ -" | awk {'print $2'}`; do docker wait pu-docker-$i | grep -c 0 > /dev/null || docker logs pu-docker-$i && docker rm -f pu-docker-$i > /dev/null; done;
  29. 29. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 29 β€£ loop over our @group list entries β€£ run these in docker (daemonized) β€£ loop over our @group list entries β€£ fetch the exit status of phpunit from the containers β€£ if not 0 -> show us the failure
  30. 30. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 30
  31. 31. #FTW #PARTY #AWESOME www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 31 https://goo.gl/images/uWs8N0
  32. 32. OTHER SCENARIOS www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 32 β€£ Test on different PHP versions β€£ Test separation based on TestSuite, Directory, … β€£ Testing long running integration tests separately β€£ Resilience testing https://goo.gl/images/JnY8yr
  33. 33. QUESTIONS www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 33 https://goo.gl/images/mM3D3j
  34. 34. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 34 PHPUnit/Unit Testing Ensure your apps are of high quality Zend Framework 2/3 Build robust web applications Azure PHP Quick time to market Scale up and out jQuery Professional jQuery PHP 7 Learn the web language that powers 80% of the internet On-site Training Personal coaching and mentoring on-site Training courses 2016 - 2017
  35. 35. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 35 phpcon.eu Ticket sales start soon! January 27 & 28 in Antwerp (Belgium)
  36. 36. www.in2it.be - @in2itvof PHPUnit + Docker = πŸš—πŸ’¨ 36 https://goo.gl/images/dKsFdO

Γ—