Home  > Resources  > Blog

Unit Testing in Chef

 
October 1, 2021 by Bibhas Bhattacharya
Category: DevOps

Author: Fahim Javed

Disclaimer- You will need a specific setup in order to follow all the steps in the tutorial. At Web Age, this setup is provided to students by setting up the VMs and sharing them with students. Sharing VMs in the blog is not possible as it is proprietary, but this tutorial will give you a good sense to understand the topic.

In this lab, you will test the cookbook with ChefSpec and use the Cookstyle linting tool to check for style and errors.

Part 1 – Launch terminal

In this part, you will launch the Linux terminal.

1. Open the Terminal window.

Alternatively, you can press Ctrl+Alt+T to access the terminal

or

In the taskbar, click Search. In the search text box, type in terminal and hit enter key on the keyboard.

2. Switch to root: 

sudo -i

Part 2 – Create Unit Tests

In this part, you will create unit tests to ensure Apache web server will be installed and the configuration file will be created by the recipes you created in the previous lab.

1. Switch to the cookbook directory:

cd /home/wasadmin/LabWorks/cookbooks/my_cookbook

2. Edit the spec_helper.rb file:

gedit spec/spec_helper.rb

3. Modify the file as shown below:

require 'chefspec'
require 'chefspec/policyfile'

Note: You are using the new Policyfile instead of the old technique that utilized Berksfile.

4. Save and close the file.

5. View the auto-generated test specifications:

ls spec/unit/recipes

Notice there are two files: (1) apache_spec.rb and misc_spec.rb. The test specifications are automatically generated whenever you create a recipe by using the following command:

chef generate recipe {RECIPE_NAME}

6. Edit the apache_spec.rb test specification:

gedit spec/unit/recipes/apache_spec.rb

7. Delete the two context blocks from the file.

Your file should look like this:

describe ‘my_cookbook::apache’ do

end

8. Inside the describe block, add the following code:

  context 'Tests on Ubuntu 18.04' do
    platform 'ubuntu', '18.04'
    # test for the apache2 package
    it 'apache2 package installed' do
      expect(chef_run).to install_package 'apache2'
    end
    # test for the apach2 configuration
    it 'creates the configuration file' do  
      expect(chef_run).to create_file('/etc/apache2/ports.conf')  
    end  
  end

The context block lets you group tests and specifies a platform that the tests are meant for. For example, you can have a context for Ubuntu and CentOS.

it let’s you create the unit test. There are two unit tests here: (1) ensure apache2 is installed (2) ensure ports.conf is created.

9. Save and close the file.

10. Execute the following command to run unit tests:

chef exec rspec

Notice there are 0 failures displayed.

11. Let’s purposefully mess-up with the unit tests. Edit the apache_spec.rb:

gedit spec/unit/recipes/apache_spec.rb

12. Modify the two it blocks as shown in bold below:

    it 'apache2 package installed' do
      expect(chef_run).to remove_package 'apache2'
      # expect { chef_run }.to_not raise_error
    end
    it 'creates the configuration file' do  
      expect(chef_run).to delete_file('/etc/apache2/ports.conf')  
    end

13. Save and close the file.

14. Execute the following command to run unit tests again:

chef exec rspec

Notice there are 2 failures. The prominent details are highlighted in the screenshot below.

Part 3 – Use the Cookstyle code linting tool

In this part, you will use the Cookstyle code linting tool. Cookstyle helps you write better Chef Infra cookbooks by detecting and automatically correcting style, syntax, and logic mistakes in your code.

1. From the terminal, execute the following command to install the Cookstyle Ruby gem:

gem install cookstyle

2. Execute the Cookstyle tool from the cookbook directory:

cookstyle

Depending on the coding convention you have used to write the recipes, you will see recommendations regarding:

– Indentation should involve 2 spaces.

– Use single-quotes instead of double when you are not using string interpolation.

– Unnecessary trailing spaces.

– You might also see warnings related to metadata.rb that maintainer and maintainer_email should be configured with custom values.

3. If you encountered indentation and quotes related issues, they can be automatically fixed by using the following command:

cookstyle --auto-correct

Part 4 – Review

In this tutorial, you tested the cookbook with ChefSpec and used the Cookstyle linting tool to check for style and errors.

Follow Us

Blog Categories