$30 off During Our Annual Pro Sale. View Details »

[Atlanta JUG] Testing containers with TestContainers

[Atlanta JUG] Testing containers with TestContainers

You never know how your application performs once deployed to the production. Sure, you have unit tests, and your test coverage is sky high. However, you might depend on external resources like databases, web services, and distributed caches. Moreover, without a proper integration testing, you cannot be confident about the stability of your production environment. A long time ago, long before the invention of Docker, configuring the environment for integration testing was incredibly complicated and tedious. Developers and Ops were using fake database
implementations, mocking servers, and all kinds of weird tricks to implement the environment without the real environment, but it was ugly, bulky, faulty and not cross-platform as well. Thanks to Docker, those days are long gone, now we can quickly prepare the environment with Docker for our tests.

In this talk, I would like to spread the word about awesome project TestContainers – an open source library that exposes API for JUnit tests. It provides lightweight, disposable instances of shared databases, distributed caches or grids, or anything else that can run in a Docker container, all securely and reliably downloaded from your Docker Hub.

Viktor Gamov

July 18, 2017
Tweet

More Decks by Viktor Gamov

Other Decks in Programming

Transcript

  1. @gamussa @hazelcast @testcontainers
    Testing containers
    with testcontainers

    View Slide

  2. @gamussa @hazelcast @testcontainers
    HELLO!

    View Slide

  3. @gamussa @hazelcast @testcontainers
    HELLO!
    I am Viktor Gamov

    View Slide

  4. @gamussa @hazelcast @testcontainers
    HELLO!
    I am Viktor Gamov
    I am here because I love to give presentations.

    View Slide

  5. @gamussa @hazelcast @testcontainers
    HELLO!
    I am Viktor Gamov
    I am here because I love to give presentations.
    You can find me in internetz @gamussa

    View Slide

  6. @gamussa @hazelcast @testcontainers
    HELLO!
    I am Viktor Gamov
    I am here because I love to give presentations.
    You can find me in internetz @gamussa
    Detailed dossier http:/
    /gamov.io

    View Slide

  7. @gamussa @hazelcast @testcontainers
    Disclaimer:


    I build highly


    View Slide

  8. @gamussa @hazelcast @testcontainers
    A quick roadmap of
    today’s journey

    View Slide

  9. View Slide

  10. @gamussa @hazelcast @testcontainers
    Development
    How Docker helps Java developers

    View Slide

  11. View Slide

  12. @gamussa @hazelcast @testcontainers
    Let’s start with why?

    View Slide

  13. @gamussa @hazelcast @testcontainers
    The Promise

    View Slide

  14. @gamussa @hazelcast @testcontainers

    View Slide

  15. @gamussa @hazelcast @testcontainers

    View Slide

  16. @gamussa @hazelcast @testcontainers
    Which image?

    View Slide

  17. @gamussa @hazelcast @testcontainers
    Which image?
    • java

    View Slide

  18. @gamussa @hazelcast @testcontainers
    Which image?
    • java
    • openjdk / openjdk-alpine

    View Slide

  19. @gamussa @hazelcast @testcontainers
    Which image?
    • java
    • openjdk / openjdk-alpine
    • Oracle Container Repo

    View Slide

  20. @gamussa @hazelcast @testcontainers
    Which image?
    • java
    • openjdk / openjdk-alpine
    • Oracle Container Repo
    • Custom Repository
    • ECS repo
    • Docker EE
    • Artifactory

    View Slide

  21. @gamussa @hazelcast @testcontainers
    How?

    View Slide

  22. @gamussa @hazelcast @testcontainers
    How?
    Designated images per set of tasks

    View Slide

  23. @gamussa @hazelcast @testcontainers
    How?
    Designated images per set of tasks
    jdk image for builds

    View Slide

  24. @gamussa @hazelcast @testcontainers
    How?
    Designated images per set of tasks
    jdk image for builds
    jre image for runtime

    View Slide

  25. @gamussa @hazelcast @testcontainers
    How?
    Designated images per set of tasks
    jdk image for builds
    jre image for runtime
    Debug?

    View Slide

  26. @gamussa @hazelcast @testcontainers
    Tooling
    Automation for image creation

    View Slide

  27. @gamussa @hazelcast @testcontainers
    Maven
    com.spotify
    docker-maven-plugin
    ${docker-maven-plugin.version}

    View Slide

  28. @gamussa @hazelcast @testcontainers
    Maven

    View Slide

  29. @gamussa @hazelcast @testcontainers
    Gradle
    import org.gradle.internal.os.OperatingSystem
    task buildDocker(type: Exec) {
    doFirst {
    copy {
    from "src/main/docker/"
    into "build/docker/"
    include "*"
    }
    copy {
    from "build/libs"
    into "build/docker/"
    include "*.war*"
    }
    }
    if (OperatingSystem.current().isWindows()) {
    commandLine 'cmd', '/c', 'docker', 'image', 'build', '-f', 'build/docker/Dockerfile', '-t',
    'myhipsterapp', 'build/docker/'
    } else {
    commandLine 'docker', 'image', 'build', '-f', 'build/docker/Dockerfile', '-t', 'myhipsterapp',
    'build/docker/'
    }
    }

    View Slide

  30. @gamussa @hazelcast @testcontainers
    Gradle

    View Slide

  31. @gamussa @hazelcast @testcontainers
    Multi-container
    application
    on the single host

    View Slide

  32. @gamussa @hazelcast @testcontainers
    Why compose?
    Why I can’t do all in one image?
    Script file to run them all?

    View Slide

  33. Images are like onions
    Images have layers

    View Slide

  34. @gamussa @hazelcast @testcontainers
    Production
    How Docker help to be in production

    View Slide

  35. View Slide

  36. @gamussa @hazelcast @testcontainers
    Production check list

    View Slide

  37. @gamussa @hazelcast @testcontainers
    Production check list
    Reproducible environment

    View Slide

  38. @gamussa @hazelcast @testcontainers
    Production check list
    Reproducible environment
    Deployment of complex stacks without
    complicated scripts

    View Slide

  39. @gamussa @hazelcast @testcontainers
    Production check list
    Reproducible environment
    Deployment of complex stacks without
    complicated scripts
    High Availability

    View Slide

  40. @gamussa @hazelcast @testcontainers
    Production check list
    Reproducible environment
    Deployment of complex stacks without
    complicated scripts
    High Availability
    Monitoring

    View Slide

  41. @gamussa @hazelcast @testcontainers
    Monitoring

    View Slide

  42. @gamussa @hazelcast @testcontainers
    Monitoring
    docker stats

    View Slide

  43. @gamussa @hazelcast @testcontainers
    Monitoring
    docker stats
    Docker remote API

    View Slide

  44. @gamussa @hazelcast @testcontainers
    Monitoring
    docker stats
    Docker remote API
    docker service logs

    View Slide

  45. @gamussa @hazelcast @testcontainers
    Monitoring
    docker stats
    Docker remote API
    docker service logs
    Prometheus endpoint (since 1.13)

    View Slide

  46. @gamussa @hazelcast @testcontainers
    Monitoring
    docker stats
    Docker remote API
    docker service logs
    Prometheus endpoint (since 1.13)
    cAdvisor

    View Slide

  47. @gamussa @hazelcast @testcontainers
    Monitoring
    docker stats
    Docker remote API
    docker service logs
    Prometheus endpoint (since 1.13)
    cAdvisor
    ELK stack

    View Slide

  48. @gamussa @hazelcast @testcontainers
    Monitoring
    docker stats
    Docker remote API
    docker service logs
    Prometheus endpoint (since 1.13)
    cAdvisor
    ELK stack
    Splunk, Sumologic, DataDog, etc

    View Slide

  49. @gamussa @hazelcast @testcontainers
    One more thing…

    View Slide

  50. @gamussa @hazelcast @testcontainers

    View Slide

  51. @gamussa @hazelcast @testcontainers

    View Slide

  52. @gamussa @hazelcast @testcontainers

    View Slide

  53. @gamussa @hazelcast @testcontainers

    View Slide

  54. @gamussa @hazelcast @testcontainers
    Integration Testing
    How Docker helps easy transition from
    Development to Production

    View Slide

  55. View Slide

  56. @gamussa @hazelcast @testcontainers
    Verify how software product will
    behave in real-world conditions
    Why?

    View Slide

  57. @gamussa @hazelcast @testcontainers
    Pros and cons

    View Slide

  58. @gamussa @hazelcast @testcontainers
    Pros and cons
    ✓ Real-world isolated
    testing

    View Slide

  59. @gamussa @hazelcast @testcontainers
    Pros and cons
    ✓ Real-world isolated
    testing
    ✓ Can be run during
    development

    View Slide

  60. @gamussa @hazelcast @testcontainers
    Pros and cons
    ✓ Real-world isolated
    testing
    ✓ Can be run during
    development
    ✓ Spot the issues before
    they apear in prod

    View Slide

  61. @gamussa @hazelcast @testcontainers
    Pros and cons
    ✓ Real-world isolated
    testing
    ✓ Can be run during
    development
    ✓ Spot the issues before
    they apear in prod
    ❌ Have to start real
    databases

    View Slide

  62. @gamussa @hazelcast @testcontainers
    Pros and cons
    ✓ Real-world isolated
    testing
    ✓ Can be run during
    development
    ✓ Spot the issues before
    they apear in prod
    ❌ Have to start real
    databases
    ❌ Should be cross
    platform

    View Slide

  63. @gamussa @hazelcast @testcontainers
    Pros and cons
    ✓ Real-world isolated
    testing
    ✓ Can be run during
    development
    ✓ Spot the issues before
    they apear in prod
    ❌ Have to start real
    databases
    ❌ Should be cross
    platform
    ❌ Slower than unit testing

    View Slide

  64. @gamussa @hazelcast @testcontainers
    Solution – Docker!

    View Slide

  65. @gamussa @hazelcast @testcontainers
    Solution – Docker!
    Abstraction layer

    View Slide

  66. @gamussa @hazelcast @testcontainers
    Solution – Docker!
    Abstraction layer
    CI friendly

    View Slide

  67. @gamussa @hazelcast @testcontainers
    Solution – Docker!
    Abstraction layer
    CI friendly
    Cross-platform

    View Slide

  68. @gamussa @hazelcast @testcontainers
    Solution – Docker!
    Abstraction layer
    CI friendly
    Cross-platform
    Docker Compose FTW!
    services:
    hazelcast-client:
    image: hazelcast_client_app
    volumes:
    - ../resources:/configFolder
    environment:
    - JAVA_OPTS=-Dhazelcast.client.config=/configFolder/hazelcast-
    client.xml -Dhazelcast.member.address=hazelcast -Dgroup.name=hz-
    compose -Dgroup.password=s3crEt
    hazelcast:
    image: hazelcast/hazelcast
    volumes:
    - ../resources:/configFolder
    environment:
    - JAVA_OPTS=-Dhazelcast.config=/configFolder/hazelcast.xml -
    Dhazelcast.mancenter.url=http://mancenter:8080/mancenter -
    Dgroup.name=hz-compose -Dgroup.password=s3crEt
    links:
    - "management-center:mancenter"
    management-center:
    image: hazelcast/management-center
    volumes:
    - ~/mancenter3.8:/mancenter-3.8
    environment:
    - MANCENTER_DATA=/mancenter-3.8
    - JAVA_OPTS=-Dhazelcast.mc.rest.enabled=true
    ports:
    - 8080:8080

    View Slide

  69. @gamussa @hazelcast @testcontainers
    But…

    View Slide

  70. @gamussa @hazelcast @testcontainers
    But…
    No port randomization

    View Slide

  71. @gamussa @hazelcast @testcontainers
    But…
    No port randomization
    Fighting with docker env

    View Slide

  72. @gamussa @hazelcast @testcontainers
    But…
    No port randomization
    Fighting with docker env
    localhost?

    View Slide

  73. View Slide

  74. Remove the
    database

    View Slide

  75. @gamussa @hazelcast @testcontainers

    View Slide

  76. @gamussa @hazelcast @testcontainers
    Demo

    View Slide

  77. @gamussa @hazelcast @testcontainers
    What to expect

    View Slide

  78. @gamussa @hazelcast @testcontainers
    What to expect
    More testing frameworks support

    View Slide

  79. @gamussa @hazelcast @testcontainers
    What to expect
    More testing frameworks support
    Chaos Testing via Pumba

    View Slide

  80. @gamussa @hazelcast @testcontainers
    What to expect
    More testing frameworks support
    Chaos Testing via Pumba
    Integration / Merge with Arquillian Cube

    View Slide

  81. View Slide

  82. DEVELOPMENT AND
    DEPLOYMENT
    WITH
    CLASSIC TOOLS

    View Slide

  83. DEVELOPMENT AND
    DEPLOYMENT
    WITH
    CLASSIC TOOLS
    EXPLORE 

    THE

    WORLD OF CONTAINERS

    View Slide

  84. @gamussa @hazelcast @testcontainers
    Thanks!
    QA?
    @gamussa
    http:/
    /gamov.io

    View Slide