Test Kitchen for beginners

  • Tutorial
imageHi, Habrausers! Today I would like to talk about tools that facilitate testing of cookbooks. What is the actual problem? As the scale of the infrastructure grows, it becomes more difficult to monitor the dependencies and consequences of making changes to cookies and the question of testing them becomes an agenda. To solve these problems, tools such as foodcritics, chefspec, minitest, test kitchen help ...
In this post I would like to share my experience in mastering test kitchen in “glorified 21 days”. I tried to make this post as simple and convenient as possible for an initial introduction to test kitchen.


To get started, you need the following software: vagrant, virualbox, and actually the test kitchen itself.
As a working OS, I used windows 7 x64. Why windows? Let's just say this was the statement of the problem.
It took me 3 hours. Therefore, if you want to go through all the steps yourself, it makes sense to reserve half a day for this.
So, let's go:

STEP 1. Preparation of the environment


VirtualBox & Vagrant

First, we need installed VirtualBox and vagrant.
After installing vagrant, we need to reboot.

After the reboot, we check the vagrant performance:

1. Create a folder for our test instance:

mkdir C:\vagrant && cd C:\vagrant

2. Add precise32 box:

C:\vagrant>vagrant box add precise32 files.vagrantup.com/precise32.box
Downloading box from URL: files.vagrantup.com/precise32.box
Extracting box...ate: 3089k/s, Estimated time remaining: --:--:--)
Successfully added box 'precise32' with provider 'virtualbox'!

3. We initialize the configuration. This command will create the Vagrantfile configuration file:

C:\vagrant>vagrant init

4. Editing the Vagrantfile file:

-- config.vm.box = "base"
++ config.vm.box = "precise32"

5. Now we are ready to run our instance:

C:\vagrant>vagrant up

6. After the command completes, we connect via ssh to localhost: 2222:

login as: vagrant
vagrant@localhost's password: vagrant
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)
* Documentation: help.ubuntu.com
Welcome to your Vagrant-built virtual machine.
If you managed to connect, then everything was done correctly.

Ruby & Ruby DevKit

Now you need to install ruby .
If you have chef-client installed, you must uninstall it, because we will use the installed via gem.
During installation, the “Add Ruby executables to your PATH” item should be checked so that ruby ​​can be run freely from the command line.
Next, you need to install DevKit, because it contains the necessary set of software (compiler, libraries - in one word, Devkit). I want to draw your attention to the fact that it is advisable to install in the default directory (both ruby ​​and DevKit), otherwise problems may arise.
To install, follow these steps:

1. Create a folder:
mkdir C:\devkit && cd C:\devkit

2. Download the archive into it and unpack it into the current directory:
Devkit-tdm
cd C:\devkit

3. Initialize the installation:
C:\devkit>ruby dk.rb init
[INFO] found RubyInstaller v1.9.3 at C:/Ruby193

Initialization complete! Please review and modify the auto-generated
'config.yml' file to ensure it contains the root directories to all
of the installed Rubies you want enhanced by the DevKit.
We see that the path and version of the installed ruby ​​were determined

4. Install:

C:\DevKit>ruby dk.rb install
[INFO] Updating convenience notice gem override for 'C:/Ruby193'
[INFO] Installing 'C:/Ruby193/lib/ruby/site_ruby/devkit.rb'

After executing ruby ​​dk.rb install, the paths to DevKit should be added to PATH,
but, unfortunately, this did not happen:
C:\DevKit>echo %PATH%
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Ruby193\bin
So you need to manually add the path to the DevKit directory in PATH.
There are several options for how to do this:
1. At the command prompt, execute the command;
setx PATH "C:\DevKit\mingw\bin\;C:\DevKit\bin\"
in this case, the PATH variable will be created in user variables;
2. We open cmd on behalf of the Administrator and execute:
setx PATH "%PATH%;C:\DevKit\mingw\bin\;C:\DevKit\bin\" /M
in this case, the paths to Devkit will be added to the end of the PATH
3 system variable . This method is recommended if you are unsure of the first two:
Мой компьютер > Свойства > Дополнительные параметры системы > Переменные среды.
Edit PATH and add C: \ DevKit \ bin \ and C: \ DevKit \ mingw \ bin \
Open a new terminal (cmd) and check:
C:\Users\cc>echo %PATH%
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Ruby193\bin;C:\DevKit\mingw\bin\;C:\DevKit\bin\


STEP 2. Installing Test Kitchen


And finally, we can start the test kitchen setup:

1. Create a folder for working with test kitchen:

C:\>mkdir C:\kitchen && cd C:\kitchen

2. Install bundler

C:\DevKit>gem install bundler
Fetching: bundler-1.5.2.gem (100%)
Successfully installed bundler-1.5.2
1 gem installed
Installing ri documentation for bundler-1.5.2...
Installing RDoc documentation for bundler-1.5.2...

3. In a text editor, create a Gemfile file with the contents:

source 'https://rubygems.org'

gem 'berkshelf', '~> 2.0.0' # use for manage cookbook dependencies
gem 'chef', '~> 11.6.0'
gem 'chef-zero'
gem 'json', '1.7.7' # needed for conflict resolution
gem 'kitchen-vagrant'
gem 'test-kitchen'

4. Run the installation:

C:\kitchen>bundle install
Fetching gem metadata from rubygems.org.......
Fetching additional metadata from rubygems.org..
Resolving dependencies...
Installing i18n (0.6.9)
Installing multi_json (1.8.4)
Installing activesupport (3.2.16)
...
Installing test-kitchen (1.0.0)
Installing kitchen-vagrant (0.11.3)
Using bundler (1.5.2)
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

5. Download and unzip to the current directory. cookbook for the test (I took sendmail for example):

mkdir C:\kitchen\cookbooks && cd C:\kitchen\cookbooks && knife cookbook site download sendmail

7. We initialize the test kitchen settings:

C:\kitchen>kitchen init
create .kitchen.yml
create test/integration/default
create .gitignore
append .gitignore
append .gitignore

8. To run one instance, we bring .kitchen.yml to the form:


---
driver_plugin: vagrant
driver_config:
require_chef_omnibus: true

platforms:
- name: ubuntu-12.04
driver_config:
box: opscode-ubuntu-12.04
box_url: opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box

suites:
- name: default
run_list: ["recipe[sendmail]"]
attributes: {}

9. Run the test:

C:\kitchen>kitchen test
.....................
←[0m[2014-01-11T10:17:15+00:00] INFO: *** Chef 11.8.2 ***←[36m
←[0m[2014-01-11T10:17:15+00:00] INFO: Chef-client pid: 1169←[36m
←[0m[2014-01-11T10:17:16+00:00] INFO: Setting the run_list to ["recipe[sendmail]"] from JSON←[36m
←[0m[2014-01-11T10:17:16+00:00] INFO: Run List is [recipe[sendmail]]←[36m
←[0m[2014-01-11T10:17:16+00:00] INFO: Run List expands to [sendmail]←[36m
←[0m[2014-01-11T10:17:16+00:00] INFO: Starting Chef Run for default-ubuntu-1204←[36m
←[0m[2014-01-11T10:17:16+00:00] INFO: Running start handlers←[36m
←[0m[2014-01-11T10:17:16+00:00] INFO: Start handlers complete.←[36m
←[0mCompiling Cookbooks...←[0m←[36m
←[0mConverging 2 resources←[0m←[36m
←[0mRecipe: sendmail::default←[0m←[36m
←[0m * package[sendmail] action install←[0m[2014-01-11T10:17:16+00:00] INFO: Processing package[sendmail] action install(sendmail::default line 1)←[36m
←[0m←[32m←[36m
←[0m - install version 8.14.4-2ubuntu2 of package sendmail←[0m←[36m
←[0m←[0m←[36m
←[0m * service[sendmail] action enable←[0m[2014-01-11T10:17:27+00:00] INFO: Processing service[sendmail] action enable (sendmail::default line 6)←[36m
←[0m[2014-01-11T10:17:27+00:00] INFO: service[sendmail] enabled←[36m
←[0m←[32m←[36m
←[0m - enable service service[sendmail]←[0m←[36m
←[0m←[0m←[36m
←[0m * service[sendmail] action start←[0m[2014-01-11T10:17:27+00:00] INFO: Processing service[sendmail] action start (sendmail::default line 6)←[36m
←[0m (up to date)←[0m←[36m
←[0m[2014-01-11T10:17:27+00:00] INFO: Chef Run complete in 11.695990777 seconds←[36m
←[0m[2014-01-11T10:17:27+00:00] INFO: Running report handlers←[36m
←[0m[2014-01-11T10:17:27+00:00] INFO: Report handlers complete←[36m
←[0mChef Client finished, 2 resources updated←[0m←[36m
←[0m←[36m Finished converging (0m29.54s).
←[0m←[36m-----> Setting up
←[0m←[36m Finished setting up (0m0.00s).
←[0m←[36m-----> Verifying
←[0m←[36m Finished verifying (0m0.00s).
←[0m←[36m-----> Destroying
←[0m←[36m [kitchen::driver::vagrant command] BEGIN (vagrant destroy -f)
←[0m←[36m [default] Forcing shutdown of VM...
←[0m←[36m [default] Destroying VM and associated drives...
←[0m←[36m [kitchen::driver::vagrant command] END (0m0.00s)
←[0m←[36m Vagrant instance destroyed.
←[0m←[36m Finished destroying (0m4.85s).
←[0m←[36m Finished testing (1m16.33s).
←[0m-----> Kitchen is finished. (1m16.69s)

We see that chef-client worked without errors and the sendmail package was successfully installed.
After successful execution, the instance is deleted.
In case of an error, the instance lags behind running.
This is done so that you can analyze the problem within the instance.


STEP 3. Berkshelf


We need Berkshelf to solve cookie dependency problems.
Because we installed it at the last stage - we proceed to configure:
1. Install the plugin for vagrant:

C:\kitchen>vagrant plugin install vagrant-berkshelf
Installing the 'vagrant-berkshelf' plugin. This can take a few minutes...
Installed the plugin 'vagrant-berkshelf (1.3.7)'!

2. Create a chef-cook project:

C:\kitchen>berks init chef-cook
create chef-cook/Berksfile
create chef-cook/Thorfile
create chef-cook/.gitignore
run git init from "./chef-cook"
C:/Ruby193/bin/berks: No such file or directory - git init
create chef-cook/Gemfile
create .kitchen.yml
append Thorfile
create test/integration/default
append Gemfile
append Gemfile
You must run `bundle install' to fetch any new gems.
create chef-cook/Vagrantfile
Successfully initialized

3. Run the installation (most likely the gem data is already installed):
cd C:\kitchen\chef-cook && bundle install

4. Create a cookbook:

mkdir C:\kitchen\chef-cook\cookbooks && cd C:\kitchen\chef-cook\cookbooks && berks cookbook mytest

5. Add our recipe to runlist .kitchen.yml:

C:\kitchen\chef-cook\.kitchen.yml
---
suites:
- name: default
run_list: ["recipe[mytest]"]
attributes: {}

6. Run the test:
C:\kitchen\chef-cook>kitchen test
and we get an error:
ERROR: Cookbook mytest not found. If you're loading mytest from another cookbook, make sure you configure the dependency in your metadata

in order to fix it, you need to add the following line to Berksfile:

cookbook 'mytest', :path => 'cookbooks/mytest'
this entry says where Berkshelf can find this cookie.


Further, we will complicate the task a bit by adding a dependency on a cookie that we do not have locally, for example sendmail.
1. Add the following line to the recipe C: \ kitchen \ chef-cook \ mytest \ default.rb:
include_recipe 'sendmail'

2. And also add the dependency in C: \ kitchen \ chef-cook \ mytest \ metadata.rb:

depends 'sendmail'

3. And finally, again, edit the Berksfile and bring it to this form:

site :opscode
cookbook 'sendmail'
cookbook 'mytest', :path => 'cookbooks/mytest'

Those. we added “cookbook 'sendmail'”, this entry suggests that, if necessary, Berkshelf can download the sendmail cookbook from the community repository.

4. Check the work of our cookbook:
C:\kitchen\chef-cook>kitchen conerge
....
←[0m←[36m Resolving cookbook dependencies with Berkshelf...
←[0mInstalling sendmail (0.1.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Using mytest (0.1.0) at './cookbooks/mytest'
....
←[0m * package[sendmail] action install←[0m[2014-01-15T08:00:44+00:00] INFO: Processing package[sendmail] action install (sendmail::default line 1)←[
....
←[0mChef Client finished, 2 resources updated←[0m←[36m
←[0m←[36m Finished converging (0m18.25s).
←[0m-----> Kitchen is finished. (0m18.68s)

This log shows that the sendmail cookbook successfully downloaded and the sendmail package was installed.


I repeat, this post is aimed at beginners in this direction to show the very basics of these tools. But, nevertheless, using them you can control a large number of cookbooks. Perhaps in the future I will write in more detail about the use of Berkshelf

useful links

The HomePage Chef
the Test Kitchen the HomePage
Berkshelf the HomePage
Opscode community cookbooks
For more details about the chef'e writes MistiC here

Also popular now: