Chapter 7. Testing and Writing Better Infrastructure Code with Chef and Puppet

In this chapter, we will cover the following recipes:

  • Linting Chef code with Foodcritic and Puppet code with puppet-lint
  • Unit testing with ChefSpec and rspec-puppet
  • Testing infrastructure with Test Kitchen for Chef and Beaker for Puppet
  • Integration testing with ServerSpec

Introduction

In the development world, good practices of testing software are widespread, such as unit and integration tests. Linters are also used daily for most languages by software developers. These techniques are fortunately brought to the infrastructure world through the tools we use; now as infrastructure is basically code, it can be analyzed, tested, and reported! Combined with CI systems, writing infrastructure code that is thoroughly tested at different levels helps hugely to achieve a very high quality of sustainable code and prevents unexpected regressions that would have otherwise broken things later.

In this chapter, you'll discover various techniques to write cleaner code using linters and styling tools, so our code follows high standards. You'll learn how to unit test infrastructure code such as Chef resources and achieve the highest code coverage possible, so we're sure nothing is there by error or is being modified unintentionally. Then we'll configure the testing environment Test Kitchen, which leverages the use of VMs through Vagrant (or other systems) to apply test suites. This will be our base to then write integration tests so we can make sure we achieve what we intended to achieve with multiple cookbooks and sources of code, really reaching the target and doing the job on a real system.

These tools and techniques are absolutely key to write the best infrastructure code possible, and they are as fun to use as they are powerful!

All recipes are based on Chef. However, when possible, we'll try to show how things work similarly with Puppet, Chef's direct alternative.

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

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