Use the kitchen-docker to do automated testing

With docker to enable the application and micro-service is a lot of people with the purpose of docker, I introduced today, how to use docker to do automated testing.


Be sure to take the Kitchen test's Getting started and make sure that all the commands you have introduced are understood and used correctly.

Why automate the test?

You have not thought that every time you made changes to the code, add new features, fix a bug, how do you protect your code is still healthy, will not produce new problems. Manual one by one to check is very time-consuming, and easy to miss.

Automated testing is before you submit a new code, you can pre-test it. After the code is merged into the code repository, the CI / CD tool (jenkins, bamboo, etc.) automatically tests whether the new code is working. Because all the processes are automated, can save a lot of maintenance time, to ensure that the submitted code through the test, you can normally deployed to the instance.

Automated testing of the way

There are many ways to automate the test, and the type is also unit test, acceptance test, integration test, serverspec test, etc

What we focus on today is to use the kitchen-docker component to do automated testing.

What is kitchen-docker?

Kitchen-docker is an open source community plugin for chef's kitchen test. After reading the start of the start of the kitchen test, you can see what the kitchen test can do.

Usually we are in their own computer environment, with the vagrant virtual machine to do the test. After using the kitchen-docker, it is called the docker mirror and the container to do the test of the media. Speed ​​faster than vagrant, especially in the macbook, the installation of the native docker for mac

How do I use the kitchen-docker?

I made a demonstration for this, you move to the kitchen-test-demo

Here is the demo:

Go to the correct directory

$ Git clone
$ Cd kitchen-test-demo
$ Find. -name ".kitchen.yml"

$ Cd kitchen-docker-ansible-serverspec / roles / demo

List the test examples

$ Kitchen list
Instance Driver Provisioner Verifier Transport Last Action
Default-centos-72 Docker AnsiblePlaybook Serverspec Ssh Verified

# Create an instance
$ Kitchen create
-----> Starting Kitchen (v1.10.2)
-----> Creating <default-centos-72> ... 32790
[SSH] Established
Finished creating <default-centos-72> (0m0.10s).
-----> Kitchen is finished. (0m0.25s)

Prepare the environment

If the test is ansible playbook, then the kitchen test will run ansible-playbook install the role of all the application configuration

$ Kitchen converge
-----> Starting Kitchen (v1.10.2)
-----> Converging <default-centos-72> ...
Transferring files to <default-centos-72>
Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: default.yml ********************************************* *************
1 plays in /tmp/kitchen/default.yml
PLAY RECAP ************************************************ *********************
Localhost: ok = 2 changed = 0 unreachable = 0 failed = 0

Finished converging <default-centos-72> (0m6.56s).
-----> Kitchen is finished. (0m6.71s)


$ Kitchen verify
-----> Starting Kitchen (v1.10.2)
-----> Setting up <default-centos-72> ...
Installing ruby, bundler and serverspec remotely on server
Running Serverspec
/ Usr / local / bin / rspec

Package "httpd"
Should be installed

Finished in 0.07733 seconds (files took 0.40255 seconds to load)
1 example, 0 failures

Finished verifying <default-centos-72> (0m0.89s).
-----> Kitchen is finished. (0m1.41s)

You can add a new test code to the kitchen-docker-ansible-serverspec / roles / demo / test / integration / default / serverspec / default_spec.rb

If you want to quickly learn how to add new test code, see serverspec resources

Use jenkings / bamboo to do automated testing

Code submitted, we usually use jenkins or bamboo do build and deploy (can automatically trigger), in the process of building, you can add a new task to do the test:

  Cd kitchen-docker-ansible-serverspec / roles / demo 
Kitchen test

Kitchen test = kitchen make + kitchen converge + kitchen verify + kitchen destroy

Kitchen-docker defects

You can not use the kitchen-docker to test the running of the docker container. This can only switch to kitchen-vagrant

Heads up! This alert needs your attention, but it's not super important.