- Lab
- 
                        Libraries: If you want this lab, consider one of these libraries.
- Cloud
 
            
        Chef - Available Testing Frameworks - InSpec and ChefSpec
In this lab, we will demonstrate an understanding of Chef Testing Frameworks by using InSpec and ChefSpec with ChefDK and then using Chef Kitchen. First, we'll install the ChefDK tools on a server and make sure that Docker is working. Then, we need to ensure that Git is installed and has some basic configuration. Next, we'll see how much we know about local cookbook development by creating one that installs software and uses InSpec and ChefSpec. At the end of this hands-on Lab, we will have installed ChefDK tools, configured it, and have a custom-developed cookbook that we've tested with Docker and Kitchen.
 
            
        Lab Info
Table of Contents
- 
                
                Challenge On the Provided Server, Install Version 2.4.17 of the ChefDK Tools- 
We need to download the correct version of ChefDK, which is 2.4.17. In a command line, run: wget https://packages.chef.io/files/stable/chefdk/2.4.17/el/7/chefdk-2.4.17-1.el7.x86_64.rpm
- 
Now install it: sudo rpm -ivh chefdk-2.4.17-1.el7.x86_64.rpm
- 
Once it's installed, we need to make sure Chef uses the correct system locations for things: echo 'eval "$(chef shell-init bash)"' >> ~/.bash_profilesource ~/.bash_profile
- 
Now check to see if it worked with which rubyand we should see/opt/chefdk/embedded/bin/rubyget returned.
 
- 
- 
                
                Challenge Install What docker-ce Requires on This Server- 
We need Docker on our workstation so we will need to run the following commands: sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.reposudo yum-config-manager --setopt="docker-ce-stable.baseurl=https://download.docker.com/linux/centos/7/x86_64/stable" --savesudo yum-config-manager --add-repo=http://mirror.centos.org/centos/7/extras/x86_64/sudo rpm --import https://www.centos.org/keys/RPM-GPG-KEY-CentOS-7sudo yum install -y container-selinux docker-ce
- 
Now, let's start Docker and enable it to start on system boot: sudo systemctl start dockersudo systemctl enable docker
- 
We should also set your user to be able to use Docker without using the sudocommand. Once we've done this, we have to log out and back in again:sudo usermod -aG docker $USERexit
- 
Use SSH to get back in again and check to make sure Docker is running: docker psIf we can do that without prefacing it with sudo, we're ready to continue.
 
- 
- 
                
                Challenge Install Git and Set Some Global Defaults for Our User, Email Address, and EditorWhen 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. - 
Install it: sudo yum -y install git
- 
Configure some Git basics like the username, email address, and a default text editor: git config --global user.name "USERNAME"git config --global user.email "[email protected]"git config --global core.editor vimNote: We can use fake email information since we don't actually use it for this lab. It just needs to be set or we will get errors later. 
 
- 
- 
                
                Challenge 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
 
- 
- 
                
                Challenge Update SELinux to Be Permissive- 
To perform the tasks properly, you should change SELinux so that it is permissive: sudo setenforce permissive
- 
We should also edit /etc/selinux/configand change things to permissive there too:sudo vim /etc/selinux/configChange the SELINUXline fromSELINUX=enforcingtoSELINUX=permissive.
 
- 
- 
                
                Challenge Create a Cookbook for Use with These Tasks, and Call It la_testing- 
Create the cookbook with this command: chef generate cookbook la_testing
- 
Now, change to the newly created directory: cd la_testing
 
- 
- 
                
                Challenge Edit Our kitchen.yml File- 
Edit kitchen.yml:vim .kitchen.yml
- 
In the top section, replace vagrantwithdockerand add some things. When we're done, that top section should look like this:--- driver: name: docker privileged: true use_sudo: falseRemember that this is YAML, so we need to make sure we have the right number of spaces in spots. Those lines under driverare all indented two spaces.
- 
Down in the provisionersection, we need to add a name and version. It should look like this when we're done:provisioner: name: chef_zero # You may wish to disable always updating cookbooks in CI or other testing environments. # For example: # always_update_cookbooks: <%= !ENV['CI'] %> always_update_cookbooks: true product_name: "chef" product_version: "13.8.5"
- 
We're on a CentOS machine so we need to get rid of ubuntufrom the file. Just delete (or comment out) the line in theplatformssection that reads:- name: ubuntu-16.04
 That's it for kitchen.ymledits. Write and quit so that we can get back to the command prompt.
- 
- 
                
                Challenge Edit the Default Recipe to Install net-tools and httpd- 
Edit the default recipe: vim recipes/default.rb
- 
Add this to the end of the file: ['net-tools','httpd'].each do |pkg| package pkg do action :install end end
 
- 
- 
                
                Challenge Update the InSpec Test to Test for net-tools and httpd- 
Edit the InSpec test: vim test/smoke/default/default_test.rb
- 
At the end of this file, we need to add: ['net-tools','httpd'].each do |pkg| describe package(pkg) do it { should be_installed } end end
 
- 
- 
                
                Challenge Update the ChefSpec Test, Removing Ubuntu Tests and Adding Tests for net-tools and httpd- 
We've finished the InSpec portion of our tests. Now, let's edit the ChefSpec test so that it does what we need it to: vim spec/unit/recipes/default_spec.rb
- 
Here, we want to remove any reference to Ubuntu tests. Let's erase lines 10-22 (the whole first contextsection):context 'When all attributes are default, on Ubuntu 16.04' do let(:chef_run) do # for a complete list of available platforms and versions see: # https://github.com/customink/fauxhai/blob/master/PLATFORMS.md runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '16.04') runner.converge(described_recipe) end it 'converges successfully' do expect { chef_run }.to_not raise_error end end
- 
We should just have a CentOS section now. Let's add our test to this (for net-tools and httpd). The whole section should look like this: describe 'la_testing::default' do context 'When all attributes are default, on CentOS 7.4.1708' do let(:chef_run) do # for a complete list of available platforms and versions see: # https://github.com/customink/fauxhai/blob/master/PLATFORMS.md runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '7.4.1708') runner.converge(described_recipe) end it 'converges successfully' do expect { chef_run }.to_not raise_error end it 'installs httpd' do expect(chef_run).to install_package('httpd') end it 'installs net-tools' do expect(chef_run).to install_package('net-tools') end end end
- 
Save this file and let's run some tests! 
 
- 
- 
                
                Challenge Run the ChefSpec Test- 
We're going to run the ChefSpec test that we just altered with this command: chef exec rspec --color -fdIf everything passes, we can move on. 
 
- 
- 
                
                Challenge Trust, but Verify, with Kitchen- 
To test if this cookbook works, we'll run: kitchen verify
- 
You may also notice, in the end of the output, that our InSpec test gets run. If that all looks good, we're done. Clean up the mess with this: kitchen destroy
 
- 
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.
 
           
                 
                