Integration Testing Chef Cookbooks With Serverspec

Sauce AI for Test Authoring: Move from aim to performance in minutes.|xBack to ResourcesBlogPosted December 28, 2016

Integration Testing Chef Cookbooks With Serverspec

quote

Writing a server consolidation test using the Serverspec screen fabric to verify your server configuration helps to ensure the consistency and dependability of Chef code as it is being developed. Serverspec tests will prove that the correct packages be installed, configured correctly, and tested on the various program(CentOS, Ubuntu, Windows, and more).

This article walks you through the operation of creating and running Serverspec tests. We & # x27; ll set up our workstation to run the Chef cookbook integration test on a local workstation surroundings. Then we & # x27; ll run through some of the integration tests to ensure everything is working properly.

Workstation Prerequisite

To set up your local ontogenesis surround, you & # x27; ll initiative need to download theChef Development Kit (DK), VirtualBox, and Vagrant. This prerequisite let you specifically download Chef DK 0.15.15, VirtualBox 5.0.26, and Vagrant 1.8.1, which are compatible with each other. The versions will change as Chef DK, VirtualBox and Vagrant get new updates and it & # x27; s your responsibility to validate the alteration are compatible. Once you & # x27; ve download and instal Chef DK and Vagrant, open up your terminal window and check the Chef and Vagrant versions.

$ chef -v
Chef Development Kit Version: 0.15.15
chef-client version: 12.11.18
bringing version: 0.0.23
berks version: 4.3.5
kitchen version: 1.10.0

Once you & # x27; ve downloaded and instal VirtualBox, open the application and check the VirtualBox variant(VirtualBox & gt; About VirtualBox).

Once you ’ ve download and extractednginx-pkg from Chef Supermarket, open up your terminal window and navigate to thenginx-pkgdirectory to cease ready your local environs.

Integration Testing

Test Kitchen is part of the Chef DK cortege, and make not involve to be installed severally. Install Serverspec by adding a line to your Chef cookbook gemfile. Then, it & # x27; s time to screen your Chef cookbook. Let & # x27; s take a closer look at each of the frameworks used for integration testing.

Serverspecis a quiz model project to see that your server has been configure correctly. Serverspec tryout are used to verify your distant cloud or virtualization server ’ s actual state (resource types, port, package, command - http position codification, and more) through an SSH link.

Test-Kitchenis a testing harness to accomplish and verify your infrastructure codification on one or more program in isolation. It allows you to run your codification on various cloud providers and virtualization technologies such as Amazon EC2, Docker, Vagrant + VirtualBox, and more.

Running kitchen examwill accomplish kitchen create, kitchen converge, kitchen verify, and kitchen destroy, in that order. Serverspec exam will run during thekitchen verifyform. The .kitchen.yml file Tell VirtualBox which type of architecture and operating system to use, then countenance you to bootstrap with Chef code.

$ chef exec bundle exec kitchen -- helper
Commands:
kitchen console # Kitchen console

kitchen converge # Change representative state to converge to provision instances
Kitchen destroy # Change instance province to destroy and delete all information


kitchen list # Lists one or more example
kitchen login # Log in to one instance

kitchen test # Test (destroy, create, converge, setup, verify, destroy)
kitchen verify # Verify and run automated test on one or more inst ...

Reviewing Serverspec Tests for the NGINX Cookbook

Kitchen and Serverspec allow us to converge, verify, and destroy your Chef cookbook on various existent virtualization engineering - no simulation hither. I want to continue motor this message across all my testing blog posts - it & # x27; s lots easier and cheaper to get glitch locally, as fight to having product apps crash and burn.

Every cookbook should include a suite of integration tests. The folder “ test/integration ” is where Serverspec integration tests live. Let & # x27; s reexamine thecookbook-nginx-pkgfile structure for serverspec testing:

|- cookbook-ngnix-pkg/
| -- recipes/
| -- spec/unit
|-- test/integration
| -- -- default/
| -- -- vendor/serverspec/
| -- -- -- serverspec/
| -- -- -- -- spec_helper.rb
| -- -- -- -- vendor_spec.rb

When pen Serverspec trial for your cookbook, the standard is to create a separate spec test for each formula in the test/integration folder.

Let & # x27; s guide a closer face at recipe/default.rb and attributes/default.rb which install the nginx package and latest stable version.

#install the package
packet node[& # x27; nginx-pkg & # x27;][& # x27; package & # x27;][& # x27; name & # x27;] do
version node[& # x27; nginx-pkg & # x27;][& # x27; packet & # x27;][& # x27; version & # x27;]
end

7 nonpayment[& # x27; nginx-pkg & # x27;][& # x27; package & # x27;][& # x27; name & # x27;]= & # x27; nginx & # x27;

12 default[& # x27; nginx-pkg & # x27;][& # x27; package & # x27;][& # x27; version & # x27;]= & # x27; & # x27;

It & # x27; s a simple formula, right? Checking if the practical machine convergence include the NGINX package shouldn & # x27; t be too difficult.

1 require & # x27; spec_helper & # x27;
2
3 describe packet (& # x27; nginx & # x27;) do
4 it {should be_instal}
5 end

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

Ask yourself if this is enough test coverage for this cookbook. It checks that the NGINX packet has be installed. If you ask me, this isn & # x27; t decent test coverage. Let & # x27; s add Serverspec trial to better test coverage.

Improving Serverspec Test Coverage

If you are using any of the popular conformation management tools available, they only get you halfway there by automating the server configuration. It ’ s important to think about all possible scenarios for testing the recipe. Untested recipe scenarios are destined to fail in production. It ’ s more than checking that the package was installed on the waiter. I selected the nginx-pkg cookbook to demo how to improve the integration testing by adding the following tests:

Service Resource Type- Checking that a service should not be enabled using thebe_enabled matcher.

describe service (& # x27; nginx & # x27;) do
it {should_not be_running }
end

Directory Resource Type- Checking that the package directory exists habituate thebe_directory matcher.

describe file (& # x27; /etc/nginx & # x27;) do
it {should be_directory}
end

File Resource Type- Checking that the file exists and that the file contains a given string, utilize thebe_file and containmatcher. You can use more hard-and-fast grammar syntax likebe_a_fileinstead ofbe_filewith all imagination types.

describe file (& # x27; /etc/nginx/conf.d/default.conf & # x27;) do
it {should be_a_file }
its (: content) {should match (% r {mind\s+80;}) }
end

Package Resource Type- Checking the given package version is installed, usingbe_installed and with_version matchers.

describe package (& # x27; nginx & # x27;) do
it {should be_installed.with_version (& # x27; 1.10.0 & # x27;)}
end

To prove a specific package edition, I extremely recommend determine the package version attribute within yourkitchen.yml file:

attributes: {
nginx-pkg: {
package: {
version: & quot; 1.10.0-1.el7.ngx & quot;
}
}
}

Command Resource Type: Executing command from the line to insure command results usingstdout, stderr and exit condition, along with RSpec matchers.

describe & # x27; NGINX Service - Status, Start, and Stop do

describe command (& # x27; systemctl status nginx.service & # x27;) do
its (: stdout) {should match (% r {Active:\sinactive\s. (beat.)})}
end

describe command (& # x27; sudo systemctl start nginx.service & # x27;) do
its(:exit_position) {should eq 0}
end

describe command (& # x27; systemctl status nginx.service & # x27;) do
its (: stdout) {should match (% r {Active:\sactive\s. (bunk.)})}
end

describe command (& # x27; sudo systemctl stop nginx.service & # x27;) do
its(:exit_condition) {should eq 0}
end

describe command (& # x27; systemctl status nginx.service & # x27;) do
its (: stdout) {should match (% r {Active:\sinactive\s. (dead.)})}
end

end

I recommend referencing the Serverspec resource types when writing consolidation tests, or as a reexamination of Chef cookbook. Getting familiar with the resource types will only improve your Chef integration test reportage.

Conclusion

In this blog situation, we set up a clean development environment and critique the consolidation screen for the nginx-pkg cookbook, and demonstrated how to meliorate consolidation test reportage by adding more Serverspec tests. Your infrastructure codification merit screen, and before deploying those servers, prove that your cookbook works.

To continue learning about Serverspec, Test Kitchen, and RSpec prove extensions, you can reference these corroboration links:

Greg Sypolt (@ gregsypolt) is a Older Engineer at Gannett – USA Today Network and co-founder of Quality Element. He has spent most of his calling work as a developer in test - concentrating on machine-driven try for web browsers, APIs, mobile, and more. He is focused on the enquiry, creation, and deployment of automated test strategy, quiz frameworks, tools, and uninterrupted integration. Passionate about # TestAutomation # TestCoverage # ContinuousIntegration # DevOps

Published:
Dec 28, 2016
Share this billet
Copy Share Link
LinkedIn
© 2026 Sauce Labs Inc., all right reserved. SAUCE and SAUCE LABS are registered trademark have by Sauce Labs Inc. in the United States, EU, and may be registered in early jurisdiction.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free