- Lab
- A Cloud Guru
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.
Path Info
Table of Contents
-
Challenge
Install and configure what is necessary to use the specified version (2.4.17) of the ChefDK Tools on the Provided Server, including docker-ce, git and the docker gem.
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
wget
.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, in case we didn't want to use a browser to get the ChefDK package):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:source ~/.bash_profile
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
:
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 Global Defaults for Our User, Email Address, and Editor
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 "[email protected]"
- 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 Gem Required for Using Docker with the Test Kitchen
Docker requires a gem for this all to work. Install it with this:
gem install kitchen-docker
- Install YUM utilities:
-
Challenge
Generate a generator cookbook and modify it for use with Docker. Under provisioner a product name of `chef` and product version of `15.3` is required.
Use chef generate to Create a Generator Under the Folder ~/generator, and Call It corp_origin
We'll create a folder named
generator
in our home directory with a quickmkdir ~/generator
. Then we've got to use thechef generate
command to create the generator:
chef generate generator generator/corp_origin
Edit the Template for the README.md.erb and Add "Created using the generator corp_origin"
Let's get into the default templates folder in our
generator
directory:
cd ~/generator/corp_origin/templates/default/
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 kitchen.yml.erb and Update the File to Use Docker, Adding 'product_name: "chef"' and 'product_version: "15.3"'
Edit the kitchen.yml.erb file and replace the default shown with docker specific items.
Below is an example of an updated kitchen.yml.erb file that is set to use docker:
driver: name: docker privileged: true use_sudo: false provisioner: name: chef_zero always_update_cookbooks: true product_name: "chef" product_version: "15.3" client_rb: chef_license: accept 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. -
Challenge
Configure the system so that the generator cookbook is used when generating cookbooks. Configure the system to use ~/chef/cookbooks for cookbooks.
Create a chef/cookbooks Folder In Our Home Directory
We need a place to store our cookbooks. Make one with this:
mkdir -p ~/chef/cookbooks
Create and Configure a config.rb File In ~/.chef, Then Add the New Cookbook Path and Set local_mode to "true"
We need to create a directory and configuration file, in our own home directory:
mkdir ~/.chef
vim ~/.chef/config.rb
And that
config.rb
file needs to look like this:cookbook_path ['~/chef/cookbooks'] local_mode true if File.basename($PROGRAM_NAME).eql?('chef') && ARGV[0].eql?('generate') chefdk.generator.license = "all_rights" chefdk.generator.copyright_holder = "Student Name" chefdk.generator.email = "[email protected]" chefdk.generator_cookbook = "~/generator/corp_origin" end
-
Challenge
Generate a New Cookbook Using the Generator Template created previously, call it corp_haproxy. Ensure template changes have been included and add haproxy so it will download.
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.
Add the HAProxy (Version 3.0.2) to the Cookbook so It Will Download to the Local Computer
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, then save the file:
depends 'haproxy', '=3.0.2'
-
Challenge
Use berks to download dependencies, Use test kitchen to verify the cookbook.
Use berks to Check and Download the Cookbook Dependencies
From within the
corp_haproxy
folder, run:berks
and the dependencies should download.
Use test kitchen to Test the Cookbook
Use the
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
.kitchen.yml
file:>>>>>> ------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.Using test kitchen, Converge the Cookbook and See If It Is Running in Docker Afterwards
To converge the cookbook, we'll just run
kitchen converge
. Then we can usedocker ps
to see if Docker is running. -
Challenge
Clean up the instance created with the kitchen command.
Use
kitchen destroy
to destroy the instance.
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.