Skip to main content

Chef: Test Kitchen

kitchen1.png

kitchen2.png

  1. Install docker with the following command:
    yum install docker
    systemctl start docker

  2. Then we need to install the kitchen-docker ruby gem.
    chef gem install kitchen-docker

  3. Create a cookbook:
    chef generate cookbook my_cookbook

  4. Edit the .kitchen.yml file in the generated cookbook.
    Change the driver name from vagrant to docker and delete the line
    - name: centos-7
    Save and close.

  5. Then execute kitchen converge. This command will create a docker container for us and put all the settings in place.
    The output will be something as follows:
    Synchronizing Cookbooks:
    - my_cookbook (0.1.0)
    Installing Cookbook Gems:
    Compiling Cookbooks...
    Converging 0 resourcesRunning handlers:
    Running handlers complete
    Chef Client finished, 0/0 resources updated in 18 seconds
    Finished converging <default-ubuntu-1604> (7m3.52s).

  6. Then run kitchen list
    The output will be something as follows:
    Instance Driver Provisioner Verifier Transport Last Action Last Error
    default-ubuntu-1604 Docker ChefZero Inspec Ssh Converged <None>

  7. Now we need to verify whether the test was successful or not. Execute
    kitchen verify
    -----> Starting Kitchen (v1.17.0)
    -----> Setting up <default-ubuntu-1604>...
    Finished setting up <default-ubuntu-1604> (0m0.00s).
    -----> Verifying <default-ubuntu-1604>...
    Loaded tests from test/smoke/default

    Profile: tests from test/smoke/default
    Version: (not specified)
    Target: ssh://kitchen@localhost:32768

     

    User root

    Port 80


    Test Summary: 0 successful, 0 failures, 2 skipped
    Finished verifying <default-ubuntu-1604> (0m3.62s).
    -----> Kitchen is finished. (0m6.58s)

  8. Edit the file vi test/smoke/default/default_test.rb
    Add the following lines to it:
    describe package('cowsay') do
      it {should be_installed }
    end

  9. Then run kitchen verify. This test should return failed:
    >>>>>> ------Exception-------
    >>>>>> Class: Kitchen::ActionFailed
    >>>>>> Message: 1 actions failed.
    >>>>>> Verify failed on instance <default-ubuntu-1604>. Please see .kitchen/logs/default-ubuntu-1604.log for more details
    >>>>>> ----------------------
    >>>>>> Please see .kitchen/logs/kitchen.log for more details
    >>>>>> Also try running `kitchen diagnose --all` for configuration

  10. Edit the following file vi recipes/default.rb
    Add the line package 'cowsay'
    Then execute kitchen converge and then kitchen test.
    The output should be something like
    System Package
    ✔ cowsay should be installed

    Test Summary: 1 successful, 0 failures, 1 skipped

Comments