📜 ⬆️ ⬇️

Test Kitchen for beginners

image Hi, Habrayuzer! Today I would like to talk about tools that make it easier to test kukbuk. What is the actual problem? As the scale of infrastructure grows, it becomes more difficult to monitor dependencies and the consequences of making changes to kukbooks, and the question of testing them is on the agenda. Tools such as foodcritics, chefspec, minitest, test kitchen help solve these problems ...
In this post I would like to share my experience in mastering the test kitchen in the “glorious 21 days”. I tried to make this post as simple and convenient as possible for the initial introduction to the test kitchen.


To get started, you will need the following software: vagrant, virualbox and the actual test kitchen itself.
As a working OS, I used windows 7 x64. Why windows? Let's just say that 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. Preparing the environment


VirtualBox & Vagrant

First we need the installed VirtualBox and vagrant
After installing vagrant, you must reboot.
')
After the reboot, we check the operation of vagrant:

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. Initialize the configuration. This command will create a Vagrantfile configuration file:

C:\vagrant>vagrant init

4. Edit the Vagrantfile file:

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

5. Now we are ready to launch our instance:

C:\vagrant>vagrant up

6. After the command has completed, 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 can connect - then everything is done correctly.

Ruby & Ruby DevKit

Now you need to install ruby .
If you have a chef-client installed, you must remove it, because we will use the installed gem.
During installation, select the “Add Ruby executables to your PATH” option so that you can run ruby ​​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 the installation is desirable to produce in the default directory (both ruby ​​and DevKit), otherwise problems may arise.
To install, perform the following 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 ​​are defined.

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 doing the ruby ​​dk.rb install, the paths to the DevKit should be added to the 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 the PATH.
There are several options for how to do this:
1. On the command line, run the command
setx PATH "C:\DevKit\mingw\bin\;C:\DevKit\bin\"
in this case, the PATH variable will be created in user variables;
2. Open cmd on behalf of the Administrator and execute:
setx PATH "%PATH%;C:\DevKit\mingw\bin\;C:\DevKit\bin\" /M
in this case, the Devkit paths will be added to the end of the PATH system variable.
3. This method is recommended if you are not sure about the first two:
> > > .
Edit the 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. Install Test Kitchen


And finally, we can proceed to the installation of the test kitchen:

1. Create a folder for working with test kitchen:

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

2. Install the 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 content:

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. Start 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 unpack to the current directory. cookbook for the test (I took sendmail as an example):

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

7. 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 <default-ubuntu-1204> (0m29.54s).
←[0m←[36m-----> Setting up <default-ubuntu-1204>
←[0m←[36m Finished setting up <default-ubuntu-1204> (0m0.00s).
←[0m←[36m-----> Verifying <default-ubuntu-1204>
←[0m←[36m Finished verifying <default-ubuntu-1204> (0m0.00s).
←[0m←[36m-----> Destroying <default-ubuntu-1204>
←[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 <default-ubuntu-1204> destroyed.
←[0m←[36m Finished destroying <default-ubuntu-1204> (0m4.85s).
←[0m←[36m Finished testing <default-ubuntu-1204> (1m16.33s).
←[0m-----> Kitchen is finished. (1m16.69s)

We see that the chef-client worked without errors and the sendmail package was successfully installed.
After successful execution, the instance is deleted.
In the case of an error, the instance is lagging behind.
This is done in order to be able to analyze the problem inside the instance.


STEP 3. Berkshelf


Berkshelf we need to solve problems with dependencies kukbuk.
Since we installed it at the last stage - we proceed to setup:
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. Start the installation (most likely the gem data is already standing):
cd C:\kitchen\chef-cook && bundle install

4. Create a Kukbook:

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

5. Add our recipe to the 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 get the 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 tells where Berkshelf can find this Kukbook.


Next, let's complicate the task a bit by adding dependency on a workbook, which 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. Add a dependency to C: \ kitchen \ chef-cook \ mytest \ metadata.rb:

depends 'sendmail'

3. And finally, again we 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 message store from the repository community.

4. Check the work of our kukbuk:
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 <default-ubuntu-1204> (0m18.25s).
←[0m-----> Kitchen is finished. (0m18.68s)

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


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

useful links

Chef homepage
Test Kitchen HomePage
Berkshelf HomePage
Opscode community cookbooks
In more detail about the chef'e writes MistiC here

Source: https://habr.com/ru/post/209238/


All Articles