Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Basic Chef + Test Kitchen: Setting up Ubuntu with Vagrant + Virtual Box + the Test-Kitchen Ruby Gem on a MacBook

Let's say we wanted to test something out on Ubuntu, a popular Linux distribution, on a MacBook: How would we be able to set it up locally, configuring the workstation? We set it up in Docker before. What other ways are there?

One way to do it is with a Chef.io tool to spin up a test environment, Test-Kitchen, and its Ruby gem. And, yes, this cooking metaphor is extended all throughout the Chef toolset with its cookbooks and recipes for environment configurations.

Adventures in Automation started looking at The Ruby Behind Chef, the History Behind Test Kitchen, and exploring the free tutorials that Chef provides, the extensive Learn Chef Rally.

For this article we will be using:

  • Test-Kitchen on GitHub as a primary source: builds a Vagrantfile, allowing you to create or destroy the instances of your virtual machines
  • Vagrant: "a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the "works on my machine" excuse a relic of the past".
  • Oracle's VirtualBox: "VirtualBox is a cross-platform virtualization application. What does that mean? For one thing, it installs on your existing Intel or AMD-based computers, whether they are running Windows, Mac, Linux or Solaris operating systems. Secondly, it extends the capabilities of your existing computer so that it can run multiple operating systems (inside multiple virtual machines) at the same time."

Setting Up The Environment

Once you have Ruby and VirtualBox installed, setting up a new virtual machine only takes four commands, according to the Test Kitchen - Quick Start guide at https://github.com/test-kitchen/test-kitchen:
  • gem install test-kitchen
  • kitchen init
  • kitchen list
  • kitchen converge default-ubuntu-1604

Step One: Setup Pre-Requisites



1.1.  Install Ruby: Follow the instructions here. Check that it is working by typing in using the Mac Terminal, or the Linux command line interface: ruby -v

1.2.  Install VirtualBox: Get the latest version of VirtualBox, and download the Mac or Linux version you need. 

1.3. Create New Directory: Create a new subfolder in the home directory such as test-kitchen-demo, by:  mkdir ~/test-kitchen-demo

1.4. Enter the New Directory: Change to the new directory with: cd  ~/test-kitchen-demo

Step Two: Install and Run Kitchen


2.1.  Install the test-kitchen gem: The last release at the time of this article was May 7, 2018 with 1.21.2, according to RubyGems.org:  gem install test-kitchen
  • This will install all the Ruby dependencies that the Test-Kitchen project needs to execute. 
2.2. Intialize a new Kitchen projectkitchen init
  • The kitchen-vagrant Ruby gem will install. According to its GitHub site, "[t]his driver works by generating a single Vagrantfile for each instance in a sandboxed directory. Since the Vagrantfile is written out on disk, Vagrant needs absolutely no knowledge of Test Kitchen. So no Vagrant plugins are required". It has built in support for VirtualBox, VMware and many others. 

2.3. List All Virtual Machines:  Want to see a listing of all virtual environments you can bring up? kitchen list
  • Two virtual machines can be found: default-ubuntu-1604 and default-centos-7.
2.4. Bring Up A Virtual Machine: We can use the command: kitchen converge default-ubuntu-1604
  • The base VirtualBox is downloaded from bento/ubuntu-16.04 and updated
  • The virtual machine is booted up
  • Vagrant instance is created for default-ubuntu-1604
  • Chef Omnibus is downloaded and installed from https://omnitruck.chef.io/install.sh
  • The virtual images is now up. 
2.5. Verify Server in VirtualBox:  If you bring up VirtualBox, you can see that a virtual machine is now running


... After you are done, makes sure to destroy your servers: kitchen destroy

How Does This All Work?

Vagrant Boxes: 


Have you ever ordered at a Japanese restaurant a Bento box for lunch, an all-in-one assortment of rice, veggies, and sushi, everything neatly organized in its own compartment?

Chef.io has a Bento project at https://github.com/chef/bento, creating templates for building Vagrant boxes. If you go to Vagrant Cloud / Bento, at https://app.vagrantup.com/bento, you can see environments for:
  • bento/centos-6.7
  • bento/fedora-28
  • bento/centos-7.5
  • bento/debian-9.4
  • bento/ubuntu-14.04
... And many more Linux distributions.

From VagrantUp's documentation about Boxes:

"Boxes are the package format for Vagrant environments. A box can be used by anyone on any platform that Vagrant supports to bring up an identical working environment.

"The vagrant box utility provides all the functionality for managing boxes. You can read the documentation on the vagrant box command for more information.

"The easiest way to use a box is to add a box from the publicly available catalog of Vagrant boxes. You can also add and share your own customized boxes on this website.

"Boxes also support versioning so that members of your team using Vagrant can update the underlying box easily, and the people who create boxes can push fixes and communicate these fixes efficiently".


You can search for more boxes on the VagrantCloud, https://app.vagrantup.com/boxes/search

Kitchen-Vagrant Ruby Gem



Installing the Kitchen Driver for Vagrant and running Kitchen Init creates a new kitchen.yml file:

kitchen.yml
 driver:  
name: vagrant
provisioner:
name: chef_solo
platforms:
- name: ubuntu-16.04
- name: centos-7
suites:
- name: default
run_list:
attributes:

What is Chef-Solo? It is a Chef product that allows you to install and configure all packages needed without setting up a Chef server. "Chef Solo is ideal for people who are already experienced with Chef, already have Chef cookbooks, or are looking to learn Chef", and is part of the Chef Development Toolkit at https://downloads.chef.io/chefdk.

What is converge?

"The chef-client configures the system based on the information that has been collected. Each resource is executed in the order identified by the run-list, and then by the order in which each resource is listed in each recipe. Each resource in the resource collection is mapped to a provider. The provider examines the node, and then does the steps necessary to complete the action. And then the next resource is processed. Each action configures a specific part of the system. This process is also referred to as convergence. This is also referred to as the 'execution phase'". - Chef.io / Chef Client Overview

Check Out Learn Chef Rally!


Want to dig a bit deeper? Take a look at Learn.Chef.io, and Applying a Cookbook Locally. There, you can learn about:
  • Writing a web server cookbook that prints out a "Hello World" message to the screen.
  • Installing an Apache webserver with a cookbook.
  • Examining the parts of a Test Kitchen file.
  • Creating a test instance of kitchen.
  • Applying a cookbook to a Test Kitchen instance.
  • Verifying the instance is converged as expected.
  • Deleting the Test Kitchen instance when done.


Enjoy! Happy Testing!

-T.J. Maher
Sr. QA Engineer, Software Engineer in Test
Meetup Organizer, Ministry of Testing - Boston

Twitter | YouTube | LinkedIn | Articles


This post first appeared on Adventures In Automation, please read the originial post: here

Share the post

Basic Chef + Test Kitchen: Setting up Ubuntu with Vagrant + Virtual Box + the Test-Kitchen Ruby Gem on a MacBook

×

Subscribe to Adventures In Automation

Get updates delivered right to your inbox!

Thank you for your subscription

×