DevOps Classroom Series – 04/Sept/2020

Testing chef cookbooks with InSpec

  • We will be using Test-Driven cookbook development for the tomcat using apt-get cookbook
  • Written the following in the ubuntu_test.rb in test/default folder
# InSpec test for recipe .::ubuntu

# The InSpec reference, with examples and extensive documentation, can be
# found at

# Verifying java installation
describe package('openjdk-11-jdk') do
  it { should be_installed }

# verifying tomcat installation
describe package('tomcat9') do
  it { should be_installed }

# verify if the tomcat9 service is installed,enable and running

describe service('tomcat9') do
  it { should be_installed }
  it { should be_enabled }
  it { should be_running }

# verify if the tomcat-users.xml file exists 

describe file('/etc/tomcat9/tomcat-users.xml') do
  it { should exist }

# verify if 8080 port is listening

describe port(8080) do
  it { should be_listening }

  • Now executing the following commands
kitchen create
kitchen converge
kitchen verify

Preview Preview Preview

  • Lets create a deployment recipe for deploying war file into tomcat
    • Create a new recipe called as deploy
    # Cookbook:: .
    # Recipe:: deploy
    # Copyright:: 2020, The Authors, All Rights Reserved.
    war_file_url = node['tomcat_flavor2']['war']
    war_file_location = node['tomcat_flavor2']['warlocation']
    tomcat_package = node['tomcat_flavor2']['tomcat_package']
    remote_file war_file_location do
        source war_file_url
        action :create
        notifies :restart, "service[#{tomcat_package}]"
    • navigate to deploy_test.rb and add the following
    # InSpec test for recipe .::deploy
    describe file('/var/lib/tomcat9/webapps/gameoflife.war') do
    it { should exist }
  • Now converge and verify with the inspec tests Preview
  • Refer Here for the latest version of the cookbook

Lets use chef inspec and test kitchen to write a tomcat 9 installation not using package-manager

  • For ubuntu steps Refer Here

  • Lets create a cookbook tomcat9

  • Exercise: Try installing mongo db on centos7 using test-driven development

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin