1.

Explain with an example of why ChefSpec is used in Chef.

Answer»

ChefSpec is an in-built testing framework for testing resources and recipes. It’s a PART of Chef DEVELOPMENT Kit. Usually, unit tests are written in ChefSpec and is an extension of Behavior Driven Development framework called RSpec for Ruby.

A SAMPLE unit test to check if “Nginx” package is installed or not is written as follows:

DESCRIBE 'nginx::default' do  context 'When all attributes are default, on Ubuntu 16.04' do let(:chef_run) do runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '16.04') runner.converge(described_recipe) end it 'install a package' do expect(chef_run).to install_package('nginx') end end

where 

  • describe block denotes a scenario
  • context block defines test within tests and each context block is executed individually.
  • ChefSpec::ServerRunner is used to simulate Chef Client run without actually configuring anything. In our example without actually installing “Nginx” SERVER
  • “It” is where we actually write the test using the Ruby DSL.


Discussion

No Comment Found