How do i create a basic inspec test for chef?



  • Inspec is a test suite. The main page link: https://www.chef.io/inspec/

    This is a my basic chef code to create a file with contents.

    file 'example.txt' do
      content 'This is my example'
    end
    

    To write a simple test create a file called example_test.rb

    Add the following:

    describe file('example.txt') do
      it { should be_file }
      its('content') { should match('This is my example') }
    end
    

    This tests that file file example.txt exists and has the contents "This is my example" in the file.

    To run the test do the following, the inspec command comes with the chefDK. Out two tests passed successfully.

    $ inspec exec example_test.rb
    
    Profile: tests from example_test.rb (tests from example_test.rb)
    Version: (not specified)
    Target:  local://
    
      File example.txt
         ✔  should be file
         ✔  content should match "This is my example"
    
    Test Summary: 2 successful, 0 failures, 0 skipped
    

Log in to reply
 

© Lightnetics 2024