The workflow for connecting the Continuous Integration (CI) system

As people working with code and writing tests for it, there's no reason not to see those tests executed in CI. The same way every program has language requirements, ours need to be able to build Docker containers and execute some Ruby code. Being able to fully execute a whole pile of tests automatically, upon any code check-in, is a major quality improvement step. No one can test each and every possibility and regression and special cases from months or maybe years ago. It's true in software code, and it's the same in infrastructure code as well. Let's find an elegant and automated way to execute our infrastructure code tests in CI systematically so this could be another dot connected to the bigger map.

Getting ready

To step through this recipe, you will need:

  • A working Docker installation
  • A free Travis CI account

How to do it…

We'd like our RSpec integration tests to be executed automatically each time we commit a change on Git. This is the perfect job for a CI system, such as Jenkins, the Circle CI, or the Travis CI. Our only requirement is that the CI platform should build and execute Docker containers and run RSpec tests. Docker support is good with Travis, and it works out of the box. Jenkins would work equally well behind the firewall when properly configured, like most other CI systems. Here's how to configure our CI platform to automatically execute tests on a new commit:

  1. Create a free account for the Travis CI or use your own (https://travis-ci.org/).
  2. Click on the + button to add a new GitHub repository:
    How to do it…
  3. Enable the watching of the repository by Travis:
    How to do it…
  4. Now add a configuration file for Travis named .travis.yml at the root of the repository. This file can contain a lot of information to do many things, but for now, it should simply tell Travis that we need a Ruby environment in a recent Linux distribution running Docker. Also, it should simply execute make test for Makefile. In our case, this command will execute the RSpec tests:
    sudo: required
    language: ruby
    dist: trusty
    services:
      - docker
    script: make test
    
  5. Commit and push this file and it will trigger our first test on Travis:
    $ git add .travis.yml
    $ git commit -m "added travis.yml"
    $ git push
    
  6. Navigating back to the Travis CI, we can see the tests begin:
    How to do it…
  7. A few seconds later, the tests pass successfully, assuring us the build is consistent with our expectations. Travis even gives easy access to the output of the commands:
    How to do it…

We just initiated new steps for integrating automated tests in our workflow. This is getting increasingly important as every project or team grows, and it's getting riskier to ship untested containers into production.

Note

It's also highly recommended that you include any other test that can be done in this CI system, such as the Docker linters check from earlier in this book. Quality can only go higher: the more the checks, the better. Building quicker tests for a faster feedback loop will then be a new subject.

As with every CI system, the final step after the tests are completed is to package, ship, and deploy the containers. As exciting as this step is, it's also unfortunately far beyond the scope of this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.216.229