Robert
Robert

Reputation: 10943

How create context with InSpec?

I am creating my tests with InSpec. It is my test for Apache:

require 'inspec'

if os[:family] == 'redhat'

  describe package 'httpd' do
    it { should be_installed }
  end

  describe service 'httpd' do
    it { should be_enabled }
    it { should be_running }
  end

  describe file '/etc/httpd/conf/httpd.conf' do
    it { should be_file }
  end
end
describe port(80) do
  it { should_not be_listening }
end

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

My question is related with context. Before InSpec I used ChefSpec and I like how you can create context and the output show your context. For the example above the output is this:

System Package
     ✔  httpd should be installed
  Service httpd
     ✔  should be enabled
     ✔  should be running
  File /etc/httpd/conf/httpd.conf
     ✔  should be file
  Port 80
     ✔  should not be listening
  Port 9000
     ✔  should be listening

I will like to include the family flavor or version or arch in the output in order to know and get a more clear output for my tests.

Any suggestion?

Upvotes: 0

Views: 1145

Answers (2)

james.garriss
james.garriss

Reputation: 13397

I think you are looking for a way to better control your output. If so, Chef recommends that you use the expect syntax. Although they say it's less readable than the should syntax, it's the only way to customize your output.

For example, if you run this control:

control 'Services' do
  title 'Check that the required services are installed and running.'
  if os['family'] == 'darwin'
    describe 'For Mac OS, the Apache service' do
      it 'should be installed and running.' do
        expect(service('httpd')).to(be_installed)
        expect(service('httpd')).to(be_running)
      end
    end
    describe 'For Mac OS, the Docker service' do
      it 'should be installed and running.' do
        expect(service('docker')).to(be_installed)
        expect(service('docker')).to(be_running)
      end
    end
  end
end

You will get this output:

  ×  Services: Check that the required services are installed and running. (1 failed)
     ×  For Mac OS, the Apache service should be installed and running.
     expected that `Service httpd` is installed
     ✔  For Mac OS, the Docker service should be installed and running.

Upvotes: 0

coderanger
coderanger

Reputation: 54211

First off, ChefSpec and InSpec do totally different things so the two are really comparable. Second, while InSpec supports some level of RSpec syntax compatibility it is not nearly as complete as ChefSpec or ServerSpec which are both full RSpec helper libraries. As @Tensibai mentioned, InSpec offers its own custom syntax for more complex tests. If you specifically want to use the RSpec describe and context block system or custom RSpec formatters, I would recommend using ServerSpec instead.

Upvotes: 3

Related Questions