Skip to content

Instantly share code, notes, and snippets.

@rcherara
Last active December 21, 2018 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcherara/291cffd95f4862f3027b0b8a022403e5 to your computer and use it in GitHub Desktop.
Save rcherara/291cffd95f4862f3027b0b8a022403e5 to your computer and use it in GitHub Desktop.
Test Kitchen is an integration tool for developing and testing infrastructure code and software on isolated target platforms.

Introduction

How do I run Kitchen? There are five basic commands to provision platforms and test infrastructure code.

$ kitchen create

$ kitchen converge

$ kitchen setup

$ kitchen verify

$ kitchen destroy

help : https://github.com/test-kitchen/test-kitchen

rcherara$VBoxManage --version
rcherara$vagrant --version
rcherara$ kitchen help
Commands:
  kitchen console                                 # Kitchen Console!
  kitchen converge [INSTANCE|REGEXP|all]          # Change instance state to converge. Use a provisioner to configure one or more instances
  kitchen create [INSTANCE|REGEXP|all]            # Change instance state to create. Start one or more instances
  kitchen destroy [INSTANCE|REGEXP|all]           # Change instance state to destroy. Delete all information for one or more instances
  kitchen diagnose [INSTANCE|REGEXP|all]          # Show computed diagnostic configuration
  kitchen doctor INSTANCE|REGEXP                  # Check for common system problems
  kitchen exec INSTANCE|REGEXP -c REMOTE_COMMAND  # Execute command on one or more instance
  kitchen help [COMMAND]                          # Describe available commands or one specific command
  kitchen init                                    # Adds some configuration to your cookbook so Kitchen can rock
  kitchen list [INSTANCE|REGEXP|all]              # Lists one or more instances
  kitchen login INSTANCE|REGEXP                   # Log in to one instance
  kitchen package INSTANCE|REGEXP                 # package an instance
  kitchen setup [INSTANCE|REGEXP|all]             # Change instance state to setup. Prepare to run automated tests. Install busser and related gems on one or more instances
  kitchen test [INSTANCE|REGEXP|all]              # Test (destroy, create, converge, setup, verify and destroy) one or more instances
  kitchen verify [INSTANCE|REGEXP|all]            # Change instance state to verify. Run automated tests on one or more instances
  kitchen version                                 # Print Kitchen's version information

Creating a Cookbook

macbook-pro-rcherara:git_cookbook rcherara$chef generate cookbook git_cookbook
macbook-pro-rcherara:git_cookbook rcherara$ ls
Berksfile	CHANGELOG.md	LICENSE		README.md	chefignore	metadata.rb	recipes		spec		test

kitchen.yml

Turn your attention to the .kitchen.yml

macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance             Driver   Provisioner  Verifier  Transport  Last Action    Last Error
default-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
default-centos-7     Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>

Instances

macbook-pro-rcherara:git_cookbook rcherara$ kitchen create default-ubuntu-1604
-----> Starting Kitchen (v1.22.0)
-----> Creating <default-ubuntu-1604>...
       ==> vagrant: A new version of Vagrant is available: 2.2.2 (installed version: 2.2.0)!
       ==> vagrant: To upgrade visit: https://www.vagrantup.com/downloads.html
       
       Bringing machine 'default' up with 'virtualbox' provider...
       ==> default: Box 'bento/ubuntu-16.04' could not be found. Attempting to find and install...
           default: Box Provider: virtualbox
           default: Box Version: >= 0
       ==> default: Loading metadata for box 'bento/ubuntu-16.04'
           default: URL: https://vagrantcloud.com/bento/ubuntu-16.04
       ==> default: Adding box 'bento/ubuntu-16.04' (v201812.15.0) for provider: virtualbox
           default: Downloading: https://vagrantcloud.com/bento/boxes/ubuntu-16.04/versions/201812.15.0/providers/virtualbox.box
    default: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
==> default: Successfully added box 'bento/ubuntu-16.04' (v201812.15.0) for 'virtualbox'!
       ==> default: Importing base box 'bento/ubuntu-16.04'...
==> default: Matching MAC address for NAT networking...
       ==> default: Checking if box 'bento/ubuntu-16.04' is up to date...
       ==> default: Setting the name of the VM: default-ubuntu-1604_default_1545362003973_35874
       ==> default: Clearing any previously set network interfaces...
       ==> default: Preparing network interfaces based on configuration...
           default: Adapter 1: nat
       ==> default: Forwarding ports...
           default: 22 (guest) => 2222 (host) (adapter 1)
       ==> default: Booting VM...
       ==> default: Waiting for machine to boot. This may take a few minutes...
           default: SSH address: 127.0.0.1:2222
           default: SSH username: vagrant
           default: SSH auth method: private key
           default: Warning: Connection reset. Retrying...
           default: 
           default: Vagrant insecure key detected. Vagrant will automatically replace
           default: this with a newly generated keypair for better security.
           default: 
           default: Inserting generated public key within guest...
           default: Removing insecure key from the guest if it's present...
           default: Key inserted! Disconnecting and reconnecting using new SSH key...
       ==> default: Machine booted and ready!
       ==> default: Checking for guest additions in VM...
       ==> default: Setting hostname...
       ==> default: Mounting shared folders...
           default: /tmp/omnibus/cache => /Users/rcherara/.kitchen/cache
       ==> default: Machine not provisioned because `--no-provision` is specified.
       [SSH] Established
       Vagrant instance <default-ubuntu-1604> created.
       Finished creating <default-ubuntu-1604> (2m16.82s).
-----> Kitchen is finished. (2m21.02s)

Writing a Recipe

macbook-pro-rcherara:git_cookbook rcherara$ vi recipes/default.rb
*edit and add the following: package "git"

kitchen converge

macbook-pro-rcherara:git_cookbook rcherara$ kitchen converge

*Here’s what happened at a high level:

* Chef was installed on the instance
* The git_cookbook files and a minimal Chef configuration were built and uploaded to the instance
* A Chef run was initiated using the run-list and node attributes specified in the .kitchen.yml file

*A converge will leave the machine running and kitchen automatically uploads changes each converge so that one can iterate rapidly on configuration code. A lot of time and effort has gone into ensuring that the exit code of kitchen is always appropriate. Here is the Kitchen Command Guarantee:

* I will always exit with code 0 if my operation was successful.
* I will always exit with a non-zero code if any part of my operation was not successful.
* Any behavior to the contrary is a bug.

Writing a Test

macbook-pro-rcherara:git_cookbook rcherara$  vi test/integration/default/default_test.rb

describe package('git') do
  it { should be_installed }
end

kitchen verify

macbook-pro-rcherara:git_cookbook rcherara$ kitchen verify default-ubuntu-1604
-----> Starting Kitchen (v1.22.0)
-----> Setting up <default-ubuntu-1604>...
       Finished setting up <default-ubuntu-1604> (0m0.00s).
-----> Verifying <default-ubuntu-1604>...
       Loaded tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.default"} 

Profile: tests from {:path=>"/Users/rcherara/Projects-Kitchen/git_cookbook/test/integration/default"} (tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.default"})
Version: (not specified)
Target:  ssh://vagrant@127.0.0.1:2222

  User root
     ↺  
  Port 80
     ↺  
  System Package git
     ✔  should be installed

Test Summary: 1 successful, 0 failures, 2 skipped
       Finished verifying <default-ubuntu-1604> (0m0.71s).
-----> Kitchen is finished. (0m5.22s)
macbook-pro-rcherara:git_cookbook rcherara$
  
macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance             Driver   Provisioner  Verifier  Transport  Last Action  Last Error
default-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
default-centos-7     Vagrant  ChefZero     Inspec    Ssh        Converged    <None>
macbook-pro-rcherara:git_cookbook rcherara$ 

kitchen test


macbook-pro-rcherara:git_cookbook rcherara$ kitchen test
-----> Starting Kitchen (v1.22.0)
-----> Cleaning up any prior instances of <default-ubuntu-1604>
-----> Destroying <default-ubuntu-1604>...
       ==> default: Forcing shutdown of VM...
       ==> default: Destroying VM and associated drives...
       Vagrant instance <default-ubuntu-1604> destroyed.
       Finished destroying <default-ubuntu-1604> (0m7.90s).
-----> Testing <default-ubuntu-1604>
-----> Creating <default-ubuntu-1604>...
       Bringing machine 'default' up with 'virtualbox' provider...
       ==> default: Importing base box 'bento/ubuntu-16.04'...
==> default: Matching MAC address for NAT networking...
       ==> default: Checking if box 'bento/ubuntu-16.04' is up to date...
       ==> default: Setting the name of the VM: default-ubuntu-1604_default_1545363795601_58272
       ==> default: Clearing any previously set network interfaces...
       ==> default: Preparing network interfaces based on configuration...
           default: Adapter 1: nat
       ==> default: Forwarding ports...
           default: 22 (guest) => 2222 (host) (adapter 1)
       ==> default: Booting VM...
       ==> default: Waiting for machine to boot. This may take a few minutes...
           default: SSH address: 127.0.0.1:2222
           default: SSH username: vagrant
           default: SSH auth method: private key
           default: 
           default: Vagrant insecure key detected. Vagrant will automatically replace
           default: this with a newly generated keypair for better security.
           default: 
           default: Inserting generated public key within guest...
           default: Removing insecure key from the guest if it's present...
           default: Key inserted! Disconnecting and reconnecting using new SSH key...
       ==> default: Machine booted and ready!
       ==> default: Checking for guest additions in VM...
       ==> default: Setting hostname...
       ==> default: Mounting shared folders...
           default: /tmp/omnibus/cache => /Users/rcherara/.kitchen/cache
       ==> default: Machine not provisioned because `--no-provision` is specified.
       [SSH] Established
       Vagrant instance <default-ubuntu-1604> created.
       Finished creating <default-ubuntu-1604> (1m7.99s).
-----> Converging <default-ubuntu-1604>...
       Preparing files for transfer
       Preparing dna.json
       Resolving cookbook dependencies with Berkshelf 7.0.4...
       Removing non-cookbook files before transfer
       Preparing validation.pem
       Preparing client.rb
-----> Installing Chef Omnibus (install only if missing)
       Downloading https://omnitruck.chef.io/install.sh to file /tmp/install.sh
       Trying wget...
       Download complete.
       ubuntu 16.04 x86_64
       Getting information for chef stable  for ubuntu...
       downloading https://omnitruck.chef.io/stable/chef/metadata?v=&p=ubuntu&pv=16.04&m=x86_64
         to file /tmp/install.sh.1546/metadata.txt
       trying wget...
       sha1	
       sha256	
       url	https://packages.chef.io/files/stable/chef/14.8.12/ubuntu/16.04/chef_14.8.12-1_amd64.deb
       version	14.8.12
       downloaded metadata file looks valid...
       /tmp/omnibus/cache/chef_14.8.12-1_amd64.deb exists
       Comparing checksum with sha256sum...
       
       WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
       
       You are installing an omnibus package without a version pin.  If you are installing
       on production servers via an automated process this is DANGEROUS and you will
       be upgraded without warning on new releases, even to new major releases.
       Letting the version float is only appropriate in desktop, test, development or
       CI/CD environments.
       
       WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
       
       Installing chef 
       installing with dpkg...
       Selecting previously unselected package chef.
(Reading database ... 38412 files and directories currently installed.)
       Preparing to unpack .../cache/chef_14.8.12-1_amd64.deb ...
       Unpacking chef (14.8.12-1) ...
       Setting up chef (14.8.12-1) ...
       Thank you for installing Chef!
       Transferring files to <default-ubuntu-1604>
       Starting Chef Client, version 14.8.12
       Creating a new client identity for default-ubuntu-1604 using the validator key.
       resolving cookbooks for run list: ["git_cookbook::default"]
       Synchronizing Cookbooks:
         - git_cookbook (0.1.0)
       Installing Cookbook Gems:
       Compiling Cookbooks...
       Converging 1 resources
       Recipe: git_cookbook::default
         * apt_package[git] action install (up to date)
       
       Running handlers:
       Running handlers complete
       Chef Client finished, 0/1 resources updated in 03 seconds
       Downloading files from <default-ubuntu-1604>
       Finished converging <default-ubuntu-1604> (0m24.55s).
-----> Setting up <default-ubuntu-1604>...
       Finished setting up <default-ubuntu-1604> (0m0.00s).
-----> Verifying <default-ubuntu-1604>...
       Loaded tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.default"} 

Profile: tests from {:path=>"/Users/rcherara/Projects-Kitchen/git_cookbook/test/integration/default"} (tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.default"})
Version: (not specified)
Target:  ssh://vagrant@127.0.0.1:2222

  User root
     ↺  
  Port 80
     ↺  
  System Package git
     ✔  should be installed

Test Summary: 1 successful, 0 failures, 2 skipped
       Finished verifying <default-ubuntu-1604> (0m0.58s).
-----> Destroying <default-ubuntu-1604>...
       ==> default: Forcing shutdown of VM...
       ==> default: Destroying VM and associated drives...
       Vagrant instance <default-ubuntu-1604> destroyed.
       Finished destroying <default-ubuntu-1604> (0m9.07s).
       Finished testing <default-ubuntu-1604> (1m50.11s).
-----> Cleaning up any prior instances of <default-centos-7>
-----> Destroying <default-centos-7>...
       ==> default: Forcing shutdown of VM...
       ==> default: Destroying VM and associated drives...
       Vagrant instance <default-centos-7> destroyed.
       Finished destroying <default-centos-7> (0m8.07s).
-----> Testing <default-centos-7>
-----> Creating <default-centos-7>...
       Bringing machine 'default' up with 'virtualbox' provider...
       ==> default: Importing base box 'bento/centos-7'...
==> default: Matching MAC address for NAT networking...
       ==> default: Checking if box 'bento/centos-7' is up to date...
       ==> default: Setting the name of the VM: default-centos-7_default_1545363903535_90655
       ==> default: Clearing any previously set network interfaces...
       ==> default: Preparing network interfaces based on configuration...
           default: Adapter 1: nat
       ==> default: Forwarding ports...
           default: 22 (guest) => 2222 (host) (adapter 1)
       ==> default: Booting VM...
       ==> default: Waiting for machine to boot. This may take a few minutes...
           default: SSH address: 127.0.0.1:2222
           default: SSH username: vagrant
           default: SSH auth method: private key
           default: 
           default: Vagrant insecure key detected. Vagrant will automatically replace
           default: this with a newly generated keypair for better security.
           default: 
           default: Inserting generated public key within guest...
           default: Removing insecure key from the guest if it's present...
           default: Key inserted! Disconnecting and reconnecting using new SSH key...
       ==> default: Machine booted and ready!
       ==> default: Checking for guest additions in VM...
       ==> default: Setting hostname...
       ==> default: Mounting shared folders...
           default: /tmp/omnibus/cache => /Users/rcherara/.kitchen/cache
       ==> default: Machine not provisioned because `--no-provision` is specified.
       [SSH] Established
       Vagrant instance <default-centos-7> created.
       Finished creating <default-centos-7> (1m14.37s).
-----> Converging <default-centos-7>...
       Preparing files for transfer
       Preparing dna.json
       Resolving cookbook dependencies with Berkshelf 7.0.4...
       Removing non-cookbook files before transfer
       Preparing validation.pem
       Preparing client.rb
-----> Installing Chef Omnibus (install only if missing)
       Downloading https://omnitruck.chef.io/install.sh to file /tmp/install.sh
       Trying wget...
       Download complete.
       el 7 x86_64
       Getting information for chef stable  for el...
       downloading https://omnitruck.chef.io/stable/chef/metadata?v=&p=el&pv=7&m=x86_64
         to file /tmp/install.sh.3091/metadata.txt
       trying wget...
       sha1	
       sha256	
       url	https://packages.chef.io/files/stable/chef/14.8.12/el/7/chef-14.8.12-1.el7.x86_64.rpm
       version	14.8.12
       downloaded metadata file looks valid...
       /tmp/omnibus/cache/chef-14.8.12-1.el7.x86_64.rpm exists
       Comparing checksum with sha256sum...
       
       WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
       
       You are installing an omnibus package without a version pin.  If you are installing
       on production servers via an automated process this is DANGEROUS and you will
       be upgraded without warning on new releases, even to new major releases.
       Letting the version float is only appropriate in desktop, test, development or
       CI/CD environments.
       
       WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
       
       Installing chef 
       installing with rpm...
       attention : /tmp/omnibus/cache/chef-14.8.12-1.el7.x86_64.rpm: Entête V4 DSA/SHA1 Signature, clé ID 83ef826a: NOKEY
       Préparation...                       ################################# [100%]
       Mise à jour / installation...
          1:chef-14.8.12-1.el7               ################################# [100%]
       Thank you for installing Chef!
       Transferring files to <default-centos-7>
       Starting Chef Client, version 14.8.12
       Creating a new client identity for default-centos-7 using the validator key.
       resolving cookbooks for run list: ["git_cookbook::default"]
       Synchronizing Cookbooks:
         - git_cookbook (0.1.0)
       Installing Cookbook Gems:
       Compiling Cookbooks...
       Converging 1 resources
       Recipe: git_cookbook::default
         * yum_package[git] action install
           - install version 0:1.8.3.1-20.el7.x86_64 of package git
       
       Running handlers:
       Running handlers complete
       Chef Client finished, 1/1 resources updated in 16 seconds
       Downloading files from <default-centos-7>
       Finished converging <default-centos-7> (0m35.31s).
-----> Setting up <default-centos-7>...
       Finished setting up <default-centos-7> (0m0.00s).
-----> Verifying <default-centos-7>...
       Loaded tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.default"} 

Profile: tests from {:path=>"/Users/rcherara/Projects-Kitchen/git_cookbook/test/integration/default"} (tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.default"})
Version: (not specified)
Target:  ssh://vagrant@127.0.0.1:2222

  User root
     ↺  
  Port 80
     ↺  
  System Package git
     ✔  should be installed

Test Summary: 1 successful, 0 failures, 2 skipped
       Finished verifying <default-centos-7> (0m1.97s).
-----> Destroying <default-centos-7>...
       ==> default: Forcing shutdown of VM...
       ==> default: Destroying VM and associated drives...
       Vagrant instance <default-centos-7> destroyed.
       Finished destroying <default-centos-7> (0m7.25s).
       Finished testing <default-centos-7> (2m6.99s).
-----> Kitchen is finished. (4m1.23s)
macbook-pro-rcherara:git_cookbook rcherara$ 

With this in mind, here’s what kitchen is doing in the Test Action:

  • Destroys the instance if it exists
  • Creates the instance
  • Converges the instance
  • Verifies the instance with InSpec
  • Destroys the instance

Finally, let’s check the status of the instance:

macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance             Driver   Provisioner  Verifier  Transport  Last Action    Last Error
default-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
  

Adding a Platform

macbook-pro-rcherara:git_cookbook rcherara$ kitchen verify 7
macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance             Driver   Provisioner  Verifier  Transport  Last Action    Last Error
default-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
default-centos-7     Vagrant  ChefZero     Inspec    Ssh        Verified       <None>

Adding a Suite

We’re going to call our new suite "rcherara-server" by editing .kitchen.yml

macbook-pro-rcherara:git_cookbook rcherara$ cat .kitchen.yml 
---
driver:
  name: vagrant

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

verifier:
  name: inspec

platforms:
  - name: ubuntu-16.04
  - name: centos-7

suites:
  - name: default
    run_list:
      - recipe[git_cookbook::default]
    verifier:
      inspec_tests:
        - test/integration/default
    attributes:

  - name: rcherara-server
    run_list:
      - recipe[git_cookbook::rcherara-server]
    verifier:
      inspec_tests:
        - test/integration/rcherara-server
    attributes:
macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance                     Driver   Provisioner  Verifier  Transport  Last Action    Last Error
default-ubuntu-1604          Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
default-centos-7             Vagrant  ChefZero     Inspec    Ssh        Verified       <None>
rcherara-server-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
rcherara-server-centos-7     Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
macbook-pro-rcherara:git_cookbook rcherara$ 

Adding a Test

macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance                     Driver   Provisioner  Verifier  Transport  Last Action    Last Error
default-ubuntu-1604          Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
default-centos-7             Vagrant  ChefZero     Inspec    Ssh        Verified       <None>
rcherara-server-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
rcherara-server-centos-7     Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
macbook-pro-rcherara:git_cookbook rcherara$ mkdir -p test/integration/rcherara-server
macbook-pro-rcherara:git_cookbook rcherara$ vi test/integration/rcherara-server/git_daemon_test.rb
macbook-pro-rcherara:git_cookbook rcherara$ cat test/integration/rcherara-server/git_daemon_test.rb
# # encoding: utf-8

  Inspec test for recipe git_cookbook::rcherara-server

describe port(9418) do
  it { should be_listening }
end

describe service('git-daemon') do
  it { should be_enabled }
  it { should be_running }
end
macbook-pro-rcherara:git_cookbook rcherara$ kitchen verify
-----> Starting Kitchen (v1.22.0)
-----> Creating <default-ubuntu-1604>...
       Bringing machine 'default' up with 'virtualbox' provider...
       ==> default: Importing base box 'bento/ubuntu-16.04'...



 >>>>>> ------Exception-------
>>>>>> Class: Kitchen::ActionFailed
>>>>>> Message: 2 actions failed.
>>>>>>     Converge failed on instance <rcherara-server-ubuntu-1604>.  Please see .kitchen/logs/rcherara-server-ubuntu-1604.log for more details
>>>>>>     Converge failed on instance <rcherara-server-centos-7>.  Please see .kitchen/logs/rcherara-server-centos-7.log for more details
>>>>>> ----------------------
>>>>>> Please see .kitchen/logs/kitchen.log for more details
>>>>>> Also try running `kitchen diagnose --all` for configuration

macbook-pro-rcherara:git_cookbook rcherara$ vi .kitchen/logs/rcherara-server-ubuntu-1604.log
macbook-pro-rcherara:git_cookbook rcherara$ cat test/integration/rcherara-server/git_daemon_test.rb
# # encoding: utf-8

 Inspec test for recipe git_cookbook::rcherara-server

describe port(9418) do
  it { should be_listening }
end

describe service('git-daemon') do
  it { should be_enabled }
  it { should be_running }
end
macbook-pro-rcherara:git_cookbook rcherara$ vi .kitchen/logs/rcherara-server-ubuntu-1604.log
macbook-pro-rcherara:git_cookbook rcherara$ vi .kitchen/logs/rcherara-server-centos-7.log
macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance                     Driver   Provisioner  Verifier  Transport  Last Action  Last Error
default-ubuntu-1604          Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
default-centos-7             Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
rcherara-server-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        Created      Kitchen::ActionFailed
rcherara-server-centos-7     Vagrant  ChefZero     Inspec    Ssh        Created      Kitchen::ActionFailed
macbook-pro-rcherara:git_cookbook rcherara$  

Adding a Recipe

we’ll create a file called recipes/rcherara-server.rb with the following:

# the default recipe is implied if only the cookbook name is provided
# effectively `include_recipe "git_cookbook::default"`
include_recipe "git_cookbook"

# install the above `daemon_pkg`
package 'git-daemon-run'

# create our data directory
directory '/opt/git'

# setup the systemd unit (service) with the above `daemon_bin`, enable, and
# start it
systemd_unit 'git-daemon.service' do
content <<-EOU.gsub(/^\s+/, '')
  [Unit]
  Description=Git Repositories Server Daemon
  Documentation=man:git-daemon(1)

  [Service]
  ExecStart=/usr/bin/git daemon \
  --reuseaddr \
  --base-path=/opt/git/ \
  /opt/git/

  [Install]
  WantedBy=getty.target
  DefaultInstance=tty1
  EOU

action [ :create, :enable, :start ]
end
macbook-pro-rcherara:git_cookbook rcherara$ vi recipes/rcherara-server.rb
macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance                     Driver   Provisioner  Verifier  Transport  Last Action  Last Error
default-ubuntu-1604          Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
default-centos-7             Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
rcherara-server-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        Created      Kitchen::ActionFailed
rcherara-server-centos-7     Vagrant  ChefZero     Inspec    Ssh        Created      Kitchen::ActionFailed
macbook-pro-rcherara:git_cookbook rcherara$ kitchen verify
-----> Starting Kitchen (v1.22.0)
-----> Verifying <default-ubuntu-1604>...
       Loaded tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.default"} 

Profile: tests from {:path=>"/Users/rcherara/Projects-Kitchen/git_cookbook/test/integration/default"} (tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.default"})
Version: (not specified)
Target:  ssh://vagrant@127.0.0.1:2200

  User root
     ↺  
  Port 80
     ↺  
  System Package git
     ✔  should be installed

Test Summary: 1 successful, 0 failures, 2 skipped
       Finished verifying <default-ubuntu-1604> (0m0.55s).
-----> Verifying <default-centos-7>...
       Loaded tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.default"} 

Profile: tests from {:path=>"/Users/rcherara/Projects-Kitchen/git_cookbook/test/integration/default"} (tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.default"})
Version: (not specified)
Target:  ssh://vagrant@127.0.0.1:2222

  User root
     ↺  
  Port 80
     ↺  
  System Package git
     ✔  should be installed

Test Summary: 1 successful, 0 failures, 2 skipped
       Finished verifying <default-centos-7> (0m1.29s).
-----> Converging <rcherara-server-ubuntu-1604>...
       Preparing files for transfer
       Preparing dna.json
       Resolving cookbook dependencies with Berkshelf 7.0.4...
       Removing non-cookbook files before transfer
       Preparing validation.pem
       Preparing client.rb
-----> Chef Omnibus installation detected (install only if missing)
       Transferring files to <rcherara-server-ubuntu-1604>
       Starting Chef Client, version 14.8.12
       resolving cookbooks for run list: ["git_cookbook::rcherara-server"]
       Synchronizing Cookbooks:
         - git_cookbook (0.1.0)
       Installing Cookbook Gems:
       Compiling Cookbooks...
       Converging 4 resources
       Recipe: git_cookbook::default
         * apt_package[git] action install (up to date)
       Recipe: git_cookbook::rcherara-server
         * apt_package[git-daemon-run] action install
           - install version 1:2.7.4-0ubuntu1.6 of package git-daemon-run
         * directory[/opt/git] action create
           - create new directory /opt/git
         * systemd_unit[git-daemon.service] action create
         Recipe: <Dynamically Defined Resource>
           * file[/etc/systemd/system/git-daemon.service] action create
             - create new file /etc/systemd/system/git-daemon.service
             - update content in file /etc/systemd/system/git-daemon.service from none to 29fe1a
             --- /etc/systemd/system/git-daemon.service	2018-12-21 04:48:55.403102000 +0000
             +++ /etc/systemd/system/.chef-git-daemon20181221-2433-15nuilb.service	2018-12-21 04:48:55.182992000 +0000
             @@ -1 +1,9 @@
             +[Unit]
             +Description=Git Repositories Server Daemon
             +Documentation=man:git-daemon(1)
             +[Service]
             +ExecStart=/usr/bin/git daemon     --reuseaddr     --base-path=/opt/git/     /opt/git/
             +[Install]
             +WantedBy=getty.target
             +DefaultInstance=tty1
             - change mode from '' to '0644'
             - change owner from '' to 'root'
             - change group from '' to 'root'
           - creating unit: git-daemon.service
       Recipe: git_cookbook::rcherara-server
         * systemd_unit[git-daemon.service] action enable
           - enabling unit: git-daemon.service
         * systemd_unit[git-daemon.service] action start
           - starting unit: git-daemon.service
       
       Running handlers:
       Running handlers complete
       Chef Client finished, 6/7 resources updated in 19 seconds
       Downloading files from <rcherara-server-ubuntu-1604>
       Finished converging <rcherara-server-ubuntu-1604> (0m26.80s).
-----> Setting up <rcherara-server-ubuntu-1604>...
       Finished setting up <rcherara-server-ubuntu-1604> (0m0.00s).
-----> Verifying <rcherara-server-ubuntu-1604>...
       Loaded tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.rcherara-server"} 

Profile: tests from {:path=>"/Users/rcherara/Projects-Kitchen/git_cookbook/test/integration/rcherara-server"} (tests from {:path=>".Users.rcherara.Projects-Kitchen.git_cookbook.test.integration.rcherara-server"})
Version: (not specified)
Target:  ssh://vagrant@127.0.0.1:2201

  Port 9418
     ✔  should be listening
  Service git-daemon
     ✔  should be enabled
     ✔  should be running

Test Summary: 3 successful, 0 failures, 0 skipped
       Finished verifying <rcherara-server-ubuntu-1604> (0m0.56s).
-----> Converging <rcherara-server-centos-7>...
       Preparing files for transfer
       Preparing dna.json
       Resolving cookbook dependencies with Berkshelf 7.0.4...
       Removing non-cookbook files before transfer
       Preparing validation.pem
       Preparing client.rb
-----> Chef Omnibus installation detected (install only if missing)
       Transferring files to <rcherara-server-centos-7>
       Starting Chef Client, version 14.8.12
       resolving cookbooks for run list: ["git_cookbook::rcherara-server"]
       Synchronizing Cookbooks:
         - git_cookbook (0.1.0)
       Installing Cookbook Gems:
       Compiling Cookbooks...
       Converging 4 resources
       Recipe: git_cookbook::default
         * yum_package[git] action install
           - install version 0:1.8.3.1-20.el7.x86_64 of package git
       Recipe: git_cookbook::rcherara-server
         * yum_package[git-daemon-run] action install
           * No candidate version available for git-daemon-run
           ================================================================================
           Error executing action `install` on resource 'yum_package[git-daemon-run]'
           ================================================================================
           
           Chef::Exceptions::Package
           -------------------------
           No candidate version available for git-daemon-run
           
           Resource Declaration:
           ---------------------
           # In /tmp/kitchen/cache/cookbooks/git_cookbook/recipes/rcherara-server.rb
           
             6: package 'git-daemon-run'
             7: 
           
           Compiled Resource:
           ------------------
           # Declared in /tmp/kitchen/cache/cookbooks/git_cookbook/recipes/rcherara-server.rb:6:in `from_file'
           
           yum_package("git-daemon-run") do
             package_name "git-daemon-run"
             action [:install]
             default_guard_interpreter :default
             declared_type :package
             cookbook_name "git_cookbook"
             recipe_name "rcherara-server"
           end
           
           System Info:
           ------------
           chef_version=14.8.12
           platform=centos
           platform_version=7.5.1804
           ruby=ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
           program_name=/opt/chef/bin/chef-client
           executable=/opt/chef/bin/chef-client
           
       
       Running handlers:
       [2018-12-21T04:49:19+00:00] ERROR: Running exception handlers
       Running handlers complete
       [2018-12-21T04:49:19+00:00] ERROR: Exception handlers complete
       Chef Client failed. 1 resources updated in 15 seconds
       [2018-12-21T04:49:19+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
       [2018-12-21T04:49:19+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
       [2018-12-21T04:49:19+00:00] FATAL: Chef::Exceptions::Package: yum_package[git-daemon-run] (git_cookbook::rcherara-server line 6) had an error: Chef::Exceptions::Package: No candidate version available for git-daemon-run
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::ActionFailed
>>>>>> Message: 1 actions failed.
>>>>>>     Converge failed on instance <rcherara-server-centos-7>.  Please see .kitchen/logs/rcherara-server-centos-7.log for more details
>>>>>> ----------------------
>>>>>> Please see .kitchen/logs/kitchen.log for more details
>>>>>> Also try running `kitchen diagnose --all` for configuration

macbook-pro-rcherara:git_cookbook rcherara$ vi recipes/rcherara-server.rb
macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance                     Driver   Provisioner  Verifier  Transport  Last Action  Last Error
default-ubuntu-1604          Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
default-centos-7             Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
rcherara-server-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
rcherara-server-centos-7     Vagrant  ChefZero     Inspec    Ssh        Created      Kitchen::ActionFailed
macbook-pro-rcherara:git_cookbook rcherara$ 

Excluding Platforms

---
driver:
  name: vagrant

provisioner:
  name: chef_zero

verifier:
  name: inspec

platforms:
  - name: ubuntu-16.04
  - name: centos-7

suites:
  - name: default
    run_list:
      - recipe[git_cookbook::default]
    verifier:
      inspec_tests:
        - test/integration/default
    attributes:
  - name: server
    run_list:
      - recipe[git_cookbook::rcherara-server]
    verifier:
      inspec_tests:
        - test/integration/rcherara-server
    attributes:
    excludes:
      - centos-7
macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance                     Driver   Provisioner  Verifier  Transport  Last Action  Last Error
default-ubuntu-1604          Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
default-centos-7             Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
rcherara-server-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
rcherara-server-centos-7     Vagrant  ChefZero     Inspec    Ssh        Created      Kitchen::ActionFailed
macbook-pro-rcherara:git_cookbook rcherara$ vi .kitchen.yml
macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance                     Driver   Provisioner  Verifier  Transport  Last Action  Last Error
default-ubuntu-1604          Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
default-centos-7             Vagrant  ChefZero     Inspec    Ssh        Verified     <None>
rcherara-server-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        Verified     <None>

kitchen destroy

Finally let’s destroy our running instances:

macbook-pro-rcherara:git_cookbook rcherara$ kitchen destroy
-----> Starting Kitchen (v1.22.0)
-----> Destroying <default-ubuntu-1604>...
       ==> default: Forcing shutdown of VM...
       ==> default: Destroying VM and associated drives...
       Vagrant instance <default-ubuntu-1604> destroyed.
       Finished destroying <default-ubuntu-1604> (0m8.29s).
-----> Destroying <default-centos-7>...
       ==> default: Forcing shutdown of VM...
       ==> default: Destroying VM and associated drives...
       Vagrant instance <default-centos-7> destroyed.
       Finished destroying <default-centos-7> (0m8.60s).
-----> Destroying <rcherara-server-ubuntu-1604>...
       ==> default: Forcing shutdown of VM...
       ==> default: Destroying VM and associated drives...
       Vagrant instance <rcherara-server-ubuntu-1604> destroyed.
       Finished destroying <rcherara-server-ubuntu-1604> (0m8.45s).
-----> Kitchen is finished. (0m29.78s)
macbook-pro-rcherara:git_cookbook rcherara$ kitchen list
Instance                     Driver   Provisioner  Verifier  Transport  Last Action    Last Error
default-ubuntu-1604          Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
default-centos-7             Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
rcherara-server-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
macbook-pro-rcherara:git_cookbook rcherara$ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment