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 https://www.inspec.io/docs/reference/resources/
# Verifying java installation
describe package('openjdk-11-jdk') do
it { should be_installed }
end
# verifying tomcat installation
describe package('tomcat9') do
it { should be_installed }
end
# 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 }
end
# verify if the tomcat-users.xml file exists
describe file('/etc/tomcat9/tomcat-users.xml') do
it { should exist }
end
# verify if 8080 port is listening
describe port(8080) do
it { should be_listening }
end
- Now executing the following commands
kitchen create
kitchen converge
kitchen verify

- 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}]" end- 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 } end - Now converge and verify with the inspec tests

- 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
