Skip to content

Testing dockerized application

Simon Tsvilik edited this page Sep 7, 2020 · 4 revisions

Use Case

As a developer I want to run functional tests against my application which runs inside a Docker container.

How is this different from other scenarios?

In previous scenarios such as Selenium Docker: Testing local applications we demonstrated how Docker is used to run Selenium, where this example demonstrates how to run Docker to host your application. These scenarios are radically different since roles of Docker are reversed.

Test process flow explained

  1. wdio-docker-service will start your dockerized application.
  2. Once your application is running, tests will run in any other service available for WDIO (i.e. wdio-selenium-standalone-service).
  3. Once tests are finished wdio will report back results.

How do I configure that?

For demonstration purposes lets use Docker NGINX, which as you may have guessed runs NGINX server inside a Docker container (neat huh?). This would be our application.

To run functional tests for our application, we will use another great WDIO service wdio-selenium-standalone-service.

Example:

const path = require('path');

exports.config = {
    host: 'localhost',
    path: '/wd/hub', // Required to work with wdio v6
    specs: [
        './test/*.js'
    ],

    capabilities: [{
        browserName: 'chrome'
    }],

    sync: true,
    logLevel: 'debug',

    baseUrl: 'http://localhost:8080',

    waitforTimeout: 10000,
    connectionRetryTimeout: 90000,
    connectionRetryCount: 3,

    framework: 'mocha',
    mochaOpts: {
        ui: 'bdd'
    },
    reporters: ['spec'],
    services: [
        'selenium-standalone',
        'docker'
    ],
    dockerLogs: './',
    dockerOptions: {
        image: 'nginx',
        healthCheck: 'http://localhost:8080',
        options: {
            p: ['8080:8080'],
            v: [
                `${ path.join(__dirname, '/app/') }:/usr/share/nginx/html:ro`,
                `${ path.join(__dirname, '/nginx.conf') }:/etc/nginx/nginx.conf:ro`
            ]
        }
    }
};

Important parts above are baseUrl, services, dockerOptions.

  • baseUrl - We point our base url to an app which will run inside a Docker container
  • services - We now add two services standalone-selenium to run tests and docker to run application.
  • dockerOptions - this configuration will create a static site which runs on NGINX
    • image - this is a public version of NGINX docker
    • healthCheck - points to url of our website that will run on port 8080 once Docker is running
    • p - maps internal port of NGINX service to external one
    • v - maps directories and configuration necessary for NGINX to run.

Test website above consists of only one file /app/index.html.

NGINX config is pretty basic as well

# nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen 8080 default_server;
        listen [::]:8080 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
            try_files $uri $uri/ /index.html;
        }
    }
}