📜 ⬆️ ⬇️

How to test docker image for half a second


This article provides an easy way to create and test docker images. In the course of the story, using Goss , we will write tests, with which you can test the official Nginx image in just ~ 0.5 s .


As a result, we will have a small YAML file that describes the desired state of the image, with which we can test the nginx container:


docker run -p 8080:80 nginx 

running a simple command:


 dgoss run -p 8080:80 nginx 

The procedure takes about 500 milliseconds and gives the following result:


 INFO: Starting docker container INFO: Container ID: 25d23a24 INFO: Running Tests File: /var/log/nginx/error.log: exists: matches expectation: [true] File: /var/log/nginx/error.log: linkedto: matches expectation: ["/dev/stderr"] File: /var/log/nginx/error.log: filetype: matches expectation: ["symlink"] File: /var/log/nginx/access.log: exists: matches expectation: [true] File: /var/log/nginx/access.log: linkedto: matches expectation: ["/dev/stdout"] File: /var/log/nginx/access.log: filetype: matches expectation: ["symlink"] Process: nginx: running: matches expectation: [true] Port: tcp:80: listening: matches expectation: [true] Port: tcp:80: ip: matches expectation: [["0.0.0.0"]] HTTP: http://localhost: status: matches expectation: [200] HTTP: http://localhost: Body: matches expectation: [Welcome to nginx!] Package: nginx: installed: matches expectation: [true] Package: nginx: version: matches expectation: [["1.11.10-1~jessie"]] Total Duration: 0.012s Count: 13, Failed: 0, Skipped: 0 INFO: Deleting container 

Note : if you prefer to watch, rather than read, at the end of the article you can find a link to the video version (in English).


Required Tools


To be able to execute the commands given in the article, you will need a Docker .


Installing the test toolkit


As a tool for testing, we take Goss . Installing it is simple and takes a few seconds.


 curl -fsSL https://goss.rocks/install | sh 

Note: if you do not like curl | sh curl | sh , you can install Goss manually using these instructions .


What is Goss ?


Goss is a YAML-using tool for checking server configuration, an alternative to serverspec . It simplifies writing tests that can be generated based on the current state of the system. Written tests can be launched for execution or used as a desired final state of the system.

For more information, see the project page .


Writing tests


dgoss is a tool that comes with Goss that drastically simplifies writing tests for docker. Suppose we run a container like this:


 docker run -p 8080:80 nginx:1.11.10 

To start creating tests, replace docker run with dgoss edit :


 dgoss edit -p 8080:80 nginx:1.11.10 

This command will launch the container, install Goss and open the container console:


 INFO: Starting docker container INFO: Container ID: 9468c0c3 INFO: Run goss add/autoadd to add resources 

Now we can start writing tests. Paste the following commands into the console that will generate tests based on the current state of the system:


 goss a file /var/log/nginx/access.log /var/log/nginx/error.log goss a process nginx goss a port 80 goss a package nginx goss a http http://localhost exit 

Note : “a” is the abbreviated form of “add”.


After executing the exit container will be deleted, and the goss.yaml file will appear in the current directory:


 file: /var/log/nginx/access.log: exists: true mode: "0777" size: 11 owner: root group: root linked-to: /dev/stdout filetype: symlink contains: [] /var/log/nginx/error.log: exists: true mode: "0777" size: 11 owner: root group: root linked-to: /dev/stderr filetype: symlink contains: [] package: nginx: installed: true versions: - 1.11.10-1~jessie port: tcp:80: listening: true ip: - 0.0.0.0 process: nginx: running: true http: http://localhost: status: 200 allow-insecure: false no-follow-redirects: false timeout: 5000 body: [] 

These tests can be performed using the command:


 dgoss run nginx 

Manual test changes


The previously generated YAML file can also be written manually. The dgoss edit command is designed to make writing tests easier. But in most cases, the generated YAML file still has to be tweaked. Next we will do it.


In the file section, it is only important for us that the logs be linked to stdout / stderr with symbolic links. So let's remove the mode, size, owner, group and contains checks. The file section should look like this:


 file: /var/log/nginx/access.log: exists: true linked-to: /dev/stdout filetype: symlink /var/log/nginx/error.log: exists: true linked-to: /dev/stderr filetype: symlink 

Next, remove the flags associated with https and add a check that the response body has the string “Welcome to nginx!”:


 http: http://localhost: status: 200 timeout: 5000 body: - Welcome to nginx! 

Our YAML file should now have 28 lines:


 file: /var/log/nginx/access.log: exists: true linked-to: /dev/stdout filetype: symlink /var/log/nginx/error.log: exists: true linked-to: /dev/stderr filetype: symlink package: nginx: installed: true versions: - 1.11.10-1~jessie port: tcp:80: listening: true ip: - 0.0.0.0 process: nginx: running: true http: http://localhost: status: 200 timeout: 5000 body: - Welcome to nginx! 

Running these tests is still very simple:


 dgoss run nginx 

What's next?


This article is designed for a quick start and demonstrates a simple test script. More information and advanced uses can be found on the following pages:






References:


  1. Original: Tutorial: How to test your docker image in half a second .

')

Source: https://habr.com/ru/post/325570/


All Articles