Test Kitchen with an Ubuntu 12.0.4 / Vagrant VM

Test Kitchen (http://kitchen.ci/docs/getting-started) is a excellent automation framework.  I have split this into two parts.  Part one involves installing Test Kitchen (including all dependencies on an Ubuntu Linux “blank box”).  Part two is the Test Kitchen “getting started tutorial”.

I have used a Vagrant Ubuntu 12.0.4 image for installing Test Kitchen and Dependencies.  Part 2 is run_** locally_ (as you cannot run a VM _inside** _a VM).

**1.  Test Kitchen (including all dependencies on an Ubuntu Linux "blank box")**

If applicable, **install a Vagrant Ubuntu 12.0.4 (we are assuming that virtual box and vagrant are **already installed)

Add a Vagrant 12.0.4 box

$ vagrant box add ubuntu/precise32 ==> box: Loading metadata for box ‘ubuntu/precise32’ box: URL: https://vagrantcloud.com/ubuntu/precise32 ==> box: Adding box ‘ubuntu/precise32’ (v12.04.4) for provider: virtualbox box: Downloading: https://vagrantcloud.com/ubuntu/precise32/version/1/provider/virtualbox.box ==> box: Successfully added box ‘ubuntu/precise32’ (v12.04.4) for ‘virtualbox’!


Vagrant Init

$ vagrant init ubuntu/precise32 A Vagrantfile has been placed in this directory. You are now ready to vagrant up your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on vagrantup.com for more information on using Vagrant.


$ vagrant up Bringing machine ‘default’ up with ‘virtualbox’ provider… ==> default: Importing base box ‘ubuntu/precise32’… ==> default: Matching MAC address for NAT networking… ==> default: Checking if box ‘ubuntu/precise32’ is up to date… ==> default: Setting the name of the VM: ubuntu1204TestKitchen_default_1405534764509_8315 ==> default: Clearing any previously set forwarded ports… ==> default: Fixed port collision for 22 => 2222. Now on port 2200. ==> default: Clearing any previously set network interfaces… ==> default: Preparing network interfaces based on configuration… default: Adapter 1: nat ==> default: Forwarding ports… default: 22 => 2200 (adapter 1) ==> default: Booting VM… ==> default: Waiting for machine to boot. This may take a few minutes… default: SSH address: 127.0.0.1:2200 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection timeout. Retrying… ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM… default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 4.1.12 default: VirtualBox Version: 4.3 ==> default: Mounting shared folders… default: /vagrant => *****


$ vagrant ssh Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.8.0-42-generic i686)

  • Documentation: https://help.ubuntu.com/

System information as of Wed Jul 16 18:22:25 UTC 2014

System load: 0.02 Processes: 74 Usage of /: 2.8% of 39.34GB Users logged in: 0 Memory usage: 6% IP address for eth0: 10.0.2.15 Swap usage: 0%

Graph this data and manage this system at: https://landscape.canonical.com/

Get cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud

0 packages can be updated. 0 updates are security updates.

Your current Hardware Enablement Stack (HWE) is going out of support on 08/07/14. After this date security updates for critical parts (kernel and graphics stack) of your system will no longer be available.

For more information, please see: http://wiki.ubuntu.com/1204_HWE_EOL

To upgrade to a supported (or longer supported) configuration:

  • Upgrade from Ubuntu 12.04 LTS to Ubuntu 14.04 LTS by running: sudo do-release-upgrade

OR

  • Install a newer HWE version by running: sudo apt-get install linux-generic-lts-trusty linux-image-generic-lts-trusty

and reboot your system.

vagrant@vagrant-ubuntu-precise-32:~$

—————–

RVM installation (and dependencies) —————–

vagrant@vagrant-ubuntu-precise-32:~$ sudo apt-get install build-essential git-core Reading package lists… Done Building dependency tree Reading state information… Done build-essential is already the newest version. build-essential set to manually installed. The following extra packages will be installed: git git-man liberror-perl Suggested packages: git-daemon-run git-daemon-sysvinit git-doc git-el git-arch git-cvs git-svn git-email git-gui gitk gitweb The following NEW packages will be installed: git git-core git-man liberror-perl 0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded. Need to get 6,618 kB of archives. After this operation, 15.3 MB of additional disk space will be used. Do you want to continue [Y/n]? Y Get:1 http://archive.ubuntu.com/ubuntu/ precise/main liberror-perl all 0.17-1 [23.8 kB] Get:2 http://archive.ubuntu.com/ubuntu/ precise/main git-man all 1:1.7.9.5-1 [630 kB] Get:3 http://archive.ubuntu.com/ubuntu/ precise/main git i386 1:1.7.9.5-1 [5,963 kB] Get:4 http://archive.ubuntu.com/ubuntu/ precise/main git-core all 1:1.7.9.5-1 [1,384 B] Fetched 6,618 kB in 22s (290 kB/s) Selecting previously unselected package liberror-perl. (Reading database … 86164 files and directories currently installed.) Unpacking liberror-perl (from …/liberror-perl_0.17-1_all.deb) … Selecting previously unselected package git-man. Unpacking git-man (from …/git-man_1%3a1.7.9.5-1_all.deb) … Selecting previously unselected package git. Unpacking git (from …/git_1%3a1.7.9.5-1_i386.deb) … Selecting previously unselected package git-core. Unpacking git-core (from …/git-core_1%3a1.7.9.5-1_all.deb) … Processing triggers for man-db … Setting up liberror-perl (0.17-1) … Setting up git-man (1:1.7.9.5-1) … Setting up git (1:1.7.9.5-1) … Setting up git-core (1:1.7.9.5-1) … vagrant@vagrant-ubuntu-precise-32:~$


vagrant@vagrant-ubuntu-precise-32:~$ sudo apt-get install curl Reading package lists… Done Building dependency tree Reading state information… Done curl is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.


vagrant@vagrant-ubuntu-precise-32:~$ curl -L https://get.rvm.io | bash -s stable % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 184 100 184 0 0 134 0 0:00:01 0:00:01 –:–:– 362 100 20758 100 20758 0 0 11093 0 0:00:01 0:00:01 –:–:– 11093 Downloading https://github.com/wayneeseguin/rvm/archive/stable.tar.gz

Installing RVM to /home/vagrant/.rvm/ Adding rvm PATH line to /home/vagrant/.profile /home/vagrant/.bashrc /home/vagrant/.zshrc. RVM sourcing line found in /home/vagrant/.bashrc. RVM sourcing line not found for Zsh, rerun this command with ‘–auto-dotfiles’ flag to fix it. Installation of RVM in /home/vagrant/.rvm/ is almost complete:

  • To start using RVM you need to run source /home/vagrant/.rvm/scripts/rvm in all your open shell windows, in rare cases you need to reopen all shell windows.

vagrant,

#

Thank you for using RVM!

We sincerely hope that RVM helps to make your life easier and more enjoyable!!!

#

~Wayne, Michal & team.

In case of problems: http://rvm.io/help and https://twitter.com/rvm_io vagrant@vagrant-ubuntu-precise-32:~$


$ echo ‘[[ -s “/home/vagrant/.rvm/scripts/rvm” ]] && source “/home/vagrant/.rvm/scripts/rvm”’ » ~/.bashrc


$ type rvm | head -1 If the output of the above command is equivalent to rvm is a function, then we now have a working RVM installation. Now our Ruby Version Manager is installed and we can go ahead and install all the Rubies we want. To extend this article a little more I will go over how to install an actual Ruby version and also a Ruby Gem. So here we go…


vagrant@vagrant-ubuntu-precise-32:~$ rvm install 1.9.3 Searching for binary rubies, this might take some time. Found remote file https://rvm.io/binaries/ubuntu/12.04/i386/ruby-1.9.3-p547.tar.bz2 Checking requirements for ubuntu. Installing requirements for ubuntu. Updating system…… Installing required packages: gawk, libreadline6-dev, zlib1g-dev, libssl-dev, libyaml-dev, libsqlite3-dev, sqlite3, autoconf, libgdbm-dev, libncurses5-dev, automake, libtool, bison, pkg-config, libffi-dev…………. Requirements installation successful. ruby-1.9.3-p547 - #configure ruby-1.9.3-p547 - #download % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 11.5M 100 11.5M 0 0 346k 0 0:00:34 0:00:34 –:–:– 412k ruby-1.9.3-p547 - #validate archive ruby-1.9.3-p547 - #extract ruby-1.9.3-p547 - #validate binary ruby-1.9.3-p547 - #setup ruby-1.9.3-p547 - #gemset created /home/vagrant/.rvm/gems/ruby-1.9.3-p547@global ruby-1.9.3-p547 - #importing gemset /home/vagrant/.rvm/gemsets/global.gems……………………………… ruby-1.9.3-p547 - #generating global wrappers…….. ruby-1.9.3-p547 - #gemset created /home/vagrant/.rvm/gems/ruby-1.9.3-p547 ruby-1.9.3-p547 - #importing gemsetfile /home/vagrant/.rvm/gemsets/default.gems evaluated to empty gem list ruby-1.9.3-p547 - #generating default wrappers…….. vagrant@vagrant-ubuntu-precise-32:~$ ————

vagrant@vagrant-ubuntu-precise-32:~$ rvm use –default 1.9.3 Using /home/vagrant/.rvm/gems/ruby-1.9.3-p547 vagrant@vagrant-ubuntu-precise-32:~$ ————-

vagrant@vagrant-ubuntu-precise-32:~$ vagrant@vagrant-ubuntu-precise-32:~$ gem install bundler Fetching: bundler-1.6.3.gem (100%) Successfully installed bundler-1.6.3 Installing ri documentation for bundler-1.6.3 1 gem installed vagrant@vagrant-ubuntu-precise-32:~$

**2.  Test Kitchen "getting started tutorial" (http://kitchen.ci/docs/getting-started/installing)** (files available from https://github.com/chocksaway/testkitchen.git) --------------

$ gem install test-kitchen Fetching: mixlib-shellout-1.4.0.gem (100%) Successfully installed mixlib-shellout-1.4.0 Fetching: net-ssh-2.9.1.gem (100%) Successfully installed net-ssh-2.9.1 Fetching: net-scp-1.2.1.gem (100%) Successfully installed net-scp-1.2.1 Fetching: safe_yaml-1.0.3.gem (100%) Successfully installed safe_yaml-1.0.3 Fetching: thor-0.19.1.gem (100%) Successfully installed thor-0.19.1 Fetching: test-kitchen-1.2.1.gem (100%) Successfully installed test-kitchen-1.2.1 Installing ri documentation for mixlib-shellout-1.4.0 Installing ri documentation for net-scp-1.2.1 Installing ri documentation for net-ssh-2.9.1 Installing ri documentation for safe_yaml-1.0.3 Installing ri documentation for test-kitchen-1.2.1 Installing ri documentation for thor-0.19.1 6 gems installed vagrant@vagrant-ubuntu-precise-32:~$


vagrant@vagrant-ubuntu-precise-32:~$ kitchen version Test Kitchen version 1.2.1 vagrant@vagrant-ubuntu-precise-32:~$

$ kitchen list Instance Driver Provisioner Last Action default-ubuntu-1204 Vagrant ChefSolo vagrant@vagrant-ubuntu-precise-32:~/git-cookbook$

$ git init git-cookbook Initialized empty Git repository in /**/env/vagrant/git-cookbook/.git/


Create metadata.rb

name “git” version “0.1.0”


mkdir recipes rhubarb:git-cookbook** $** touch recipes/default.rb rhubarb:git-cookbook $ git add metadata.rb recipes/default.rb rhubarb:git-cookbook $ git commit -m “Git Chef cookbook - the lean version” [master (root-commit) 6f64d62] Git Chef cookbook - the lean version 2 files changed, 2 insertions(+) create mode 100644 metadata.rb create mode 100644 recipes/default.rb


$ kitchen init –driver=kitchen-vagrant create .kitchen.yml run gem install kitchen-vagrant from “.” Successfully installed kitchen-vagrant-0.15.0 Parsing documentation for kitchen-vagrant-0.15.0 Done installing documentation for kitchen-vagrant after 0 seconds 1 gem installed


Remove the centos reference:


driver: name: vagrant

provisioner: name: chef_solo

platforms:

  • name: ubuntu-12.04

suites:

  • name: default run_list:
  • recipe[git::default] attributes: ——–

$ kitchen list Instance Driver Provisioner Last Action default-ubuntu-1204 Vagrant ChefSolo -------

$ kitchen create default-ubuntu-1204 —–> Starting Kitchen (v1.2.1) —–> Creating ... Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'opscode-ubuntu-12.04'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: default-ubuntu-1204_default_1405543665149_15318 ==> default: Fixed port collision for 22 => 2222. Now on port 2201. ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 => 2201 (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2201 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection timeout. Retrying... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... ==> default: Setting hostname... ==> default: Machine not provisioning because `--no-provision` is specified. Vagrant instance created. Finished creating (0m33.77s). -----> Kitchen is finished. (0m34.01s)


$ kitchen list Instance Driver Provisioner Last Action default-ubuntu-1204 Vagrant ChefSolo Created ————- $ git add .gitignore .kitchen.yml $ vi recipes/default.rb


package “git”

log “Well, that was too easy”

$ kitchen converge default-ubuntu-1204 —–> Starting Kitchen (v1.2.1) —–> Converging ... Preparing files for transfer Preparing current project directory as a cookbook Removing non-cookbook files before transfer -----> Installing Chef Omnibus (true) downloading https://www.getchef.com/chef/install.sh to file /tmp/install.sh trying wget... Downloading Chef for ubuntu... downloading https://www.getchef.com/chef/metadata?v=&prerelease=false&nightlies=false&p=ubuntu&pv=12.04&m=x86_64 to file /tmp/install.sh.1195/metadata.txt trying wget... url https://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/12.04/x86_64/chef_11.12.8-2_amd64.deb downloaded metadata file looks valid... downloading https://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/12.04/x86_64/chef_11.12.8-2_amd64.deb to file /tmp/install.sh.1195/chef_11.12.8-2_amd64.deb trying wget... Comparing checksum with sha256sum... Installing Chef installing with dpkg... Selecting previously unselected package chef. (Reading database ... 56035 files and directories currently installed.) Unpacking chef (from .../chef_11.12.8-2_amd64.deb) ... Setting up chef (11.12.8-2) ... Thank you for installing Chef! Transfering files to [2014-07-16T20:52:54+00:00] INFO: Forking chef instance to converge... [2014-07-16T20:52:54+00:00] WARN:


SSL validation of HTTPS requests is disabled. HTTPS connections are still encrypted, but chef is not able to detect forged replies or man in the middle attacks.

To fix this issue add an entry like this to your configuration file:

# Verify all HTTPS connections (recommended)
ssl_verify_mode :verify_peer

# OR, Verify only connections to chef-server
verify_api_cert true

To check your SSL configuration, or troubleshoot errors, you can use the knife ssl check command like so:

knife ssl check -c /tmp/kitchen/solo.rb

Starting Chef Client, version 11.12.8 [2014-07-16T20:52:54+00:00] INFO: ** Chef 11.12.8 ** [2014-07-16T20:52:54+00:00] INFO: Chef-client pid: 1276 [2014-07-16T20:52:56+00:00] INFO: Setting the run_list to [“recipe[git::default]”] from CLI options [2014-07-16T20:52:56+00:00] INFO: Run List is [recipe[git::default]] [2014-07-16T20:52:56+00:00] INFO: Run List expands to [git::default] [2014-07-16T20:52:56+00:00] INFO: Starting Chef Run for default-ubuntu-1204 [2014-07-16T20:52:56+00:00] INFO: Running start handlers [2014-07-16T20:52:56+00:00] INFO: Start handlers complete. Compiling Cookbooks… Converging 2 resources Recipe: git::default

  • package[git] action install[2014-07-16T20:52:56+00:00] INFO: Processing package[git] action install (git::default line 1)

  • install version 1:1.7.9.5-1 of package git

  • log[Well, that was too easy] action write[2014-07-16T20:53:29+00:00] INFO: Processing log[Well, that was too easy] action write (git::default line 3) [2014-07-16T20:53:29+00:00] INFO: Well, that was too easy

[2014-07-16T20:53:29+00:00] INFO: Chef Run complete in 32.752295737 seconds

Running handlers: [2014-07-16T20:53:29+00:00] INFO: Running report handlers Running handlers complete

[2014-07-16T20:53:29+00:00] INFO: Report handlers complete Chef Client finished, 2/2 resources updated in 34.729096181 seconds Finished converging (2m48.79s). -----> Kitchen is finished. (2m49.03s)


$ kitchen list Instance Driver Provisioner Last Action default-ubuntu-1204 Vagrant ChefSolo Converged —————

$ mkdir -p test/integration/default/bats

$ vi test/integration/default/bats/git_installed.bats:


#!/usr/bin/env bats

@test “git binary is found in PATH” { run which git [ “$status” -eq 0 ] }


$ kitchen verify default-ubuntu-1204 —–> Starting Kitchen (v1.2.1) —–> Setting up ... Fetching: thor-0.19.0.gem (100%) Fetching: busser-0.6.2.gem (100%) Successfully installed thor-0.19.0 Successfully installed busser-0.6.2 2 gems installed -----> Setting up Busser Creating BUSSER_ROOT in /tmp/busser Creating busser binstub Plugin bats installed (version 0.2.0) -----> Running postinstall for bats plugin Installed Bats to /tmp/busser/vendor/bats/bin/bats Finished setting up (0m47.46s). -----> Verifying ... Suite path directory /tmp/busser/suites does not exist, skipping. Uploading /tmp/busser/suites/bats/git_installed.bats (mode=0644) -----> Running bats test suite **✓** **git binary is found in PATH**

1 test, 0 failures Finished verifying (0m0.69s). -----> Kitchen is finished. (0m48.41s) ---------- **$** git add test/integration/default/bats/git_installed.bats **$** git commit -m "Add bats test for default suite." [master bfcfde1] Add bats test for default suite. 1 file changed, 6 insertions(+) create mode 100644 test/integration/default/bats/git_installed.bats ---------

Author | Miles Davenport

Career programmer, who designs, assembles, fixes, and supports customers, software and systems.