Test Kitchen – Working with Generator and Community Cookbooks
In this lab we use a generator to create a cookbook generator with several defaults. We then use this to generate a cookbook which makes use of a community cookbook locally via Test Kitchen.
ChefDK Tools - Docker, Chef Generators, Test Kitchen and Community Cookbooks
We have been tasked with examining the memory and default disk space used by several Docker images.
A small problem we have is that our workstation is being repaired. We'll have to start from scratch with a new Linux computer that is being provided while ours is being repaired.
First, we'll need to install the ChefDK tools, the same version (2.4.17) that the rest of the team uses.
We've got to install Docker on the computer as well, and set up Git as the repository tool.
Next we need to create a new cookbook containing the default information that Chef generates.
We'll have to modify this new cookbook so that we can test three separate operating system instances:
- ubuntu 16.04
In the end, we want the ability to start the instances with Test Kitchen, so that we can log into them to gather the information we need. And once we have the information, we need to stop the running images and remove them with
We need to download the correct version of ChefDK, which we can browse to here: https://downloads.chef.io/chefdk/stable/2.4.17, or just grab it with
The server provided for this Lab is a Centos 7 server, so let's be sure to download the appropriate install file (RHEL 7) if we're using a web browser.
Install it with these commands (note the included
wget command) and make sure we're using the Ruby that comes with ChefDK:
wget https://packages.chef.io/files/stable/chefdk/2.4.17/el/7/chefdk-2.4.17-1.el7.x86_64.rpm sudo rpm -ivh chefdk-2.4.17-1.el7.x86_64.rpm echo 'eval "$(chef shell-init bash)"' >> ~/.bash_profile
Once we're done, we'll need to either log out and back in (so that our
.bash_profile change takes effect) or use the following command to source the new profile information:
We need Docker on our workstation, but there are a couple of tasks that we need to complete ahead of time:
- Install YUM utilities:
sudo yum install yum-utils<br/>
- Add the Docker repository:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- Install Docker-CE
sudo yum -y install docker-ce
Enable Docker to start at boot time, and then fire it up right now:
sudo systemctl enable docker
sudo systemctl start docker
Allow our user to use Docker without using
sudo usermod -aG docker $USER
Note: You will need to log out and back in for the
usermod to take effect.
Test that Docker is working with
docker ps after logging out and back in again.
Install Git and Set Some Defaults
When we use Kitchen, later in the lab, we need Git to be installed, and set up with some basic information. These commands will make that happen:
- If Git is not already installed:
sudo yum -y install git
- Now we can configure some basics for git:
- Change the username:
git config --global user.name "USERNAME"
- Edit the email address:
git config --global user.email "YOUREMAIL@DOMAIN.COM"
- We can use fake information, we don't actually use it for this lab. It just needs to be set or we will get errors later.
- Set the default text editor:
git config --global core.editor vim
- Change the username:
Install the Required Gem
Docker requires a gem for this all to work. Install it with this:
gem install kitchen-docker
Create a Generator
We'll create a folder named
generator in our home directory with a quick
mkdir ~/generator. Then we've got to use the
chef generate command to create the generator:
chef generate generator generator/corp_origin
Edit the README Template
Let's get into the default templates folder in our
Now we can edit
README.md.erb (using whichever text editor is most comfortable). Add a line above the "TODO" section with the text
Created using the generator corp_origin
Edit the Kitchen File
kitchen.yml.erb file and replace the defaults with Docker specific items.
It should look like this when we're done:
driver: name: docker privileged: true use_sudo: false provisioner: name: chef_zero always_update_cookbooks: true product_name: "chef" product_version: "13.8.5" verifier: name: inspec platforms: - name: centos-7.2 driver_config: run_command: /usr/lib/systemd/systemd suites: - name: default run_list: - recipe[<%= cookbook_name %>::default] verifier: inspect_tests: - test/integration/default attributes:
Note: Be careful of spacing in
yml files. This one is available for download in the GitHub repository mentioned above, under Additional Information and Resources.
Create a Cookbook Folder
We need a place to store our cookbooks. Make one with this:
mkdir -p ~/chef/cookbooks
Create a Configuration File
We need to create a directory and configuration file, in our own home directory:
config.rb file needs to look like this:
cookbook_path ['~/chef/cookbooks'] local_mode true if File.basename($PROGRAM_NAME).eql?('chef') && ARGV.eql?('generate') chefdk.generator.license = "all_rights" chefdk.generator.copyright_holder = "Student Name" chefdk.generator.email = "email@example.com" chefdk.generator_cookbook = "~/generator/corp_origin" end
Generate a New Cookbook
From within the
~/chef directory that we created earlier, we can generate a cookbook with this:
chef generate cookbook cookbooks/corp_haproxy
We'll want to check that our template modifications are showing in the new cookbook:
We need to utilize HAProxy. To make Chef do this, we need to edit the
metadata.rb file located in the new cookbook. Add the following entry (under the
chef_version line is a good spot for it), then save the file:
depends 'haproxy', '=3.0.2'
Check for and Download Dependencies
From within the
corp_haproxy folder, run:
and the dependencies should download. We can see them if we look in our new
ls -la ~/.berkshelf/cookbooks
Test the Cookbook
kitchen test command to verify the cookbook.
An error similar to the following, means it's likely a problem with the format of the yml in the
>>>>>> ------Exception------- >>>>>> Class: Kitchen::UserError >>>>>> Message: Error parsing /home/cloud_user/chef/cookbooks/corp_haproxy/.kitchen.yml as YAML. Please run `kitchen diagnose --no-instances --loader' to help debug your issue. >>>>>> ----------------------
Fixing it ourselves is preferable, but if it's absolutely necessary, we can download a copy of the
kitchen.yml from the GitHub location mentioned above under Additional Information and Resources.
Converge the Cookbook and Check Docker
To converge the cookbook, we'll just run
kitchen converge. Then we can use
docker ps to see if Docker is running.
kitchen by itself will show us all of the different commands we can run. Let's run one of them:
kitchen login. We'll land in a shell, where we can peek around the filesystem, look at
top numbers, etc. The
exit command will get us back out.
Destroy the Instance
One of the other commands that
kitchen listed out was
kitchen destroy. We can run that to delete this instance.
Well, it was rough but we made it. We were able sit down at a brand new workstation and install everything we need in order to start looking at the the Docker images. Now we can get to work. Congratulations!