如何使用InSpec创建上下文?

时间:2016-12-05 14:15:04

标签: chef inspec

我正在使用InSpec创建测试。这是我对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

我的问题与背景有关。在InSpec之前,我使用了ChefSpec,我喜欢你如何创建上下文,输出显示你的上下文。对于上面的示例,输出为:

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

我想在输出中包含家族风味或版本或拱形,以便了解并获得更清晰的测试输出。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找一种更好地控制输出的方法。如果是这样,Chef建议您使用expect语法。尽管他们说它比should语法可读性差,但这是定制输出的唯一方法。

例如,如果您运行此控件:

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

您将获得以下输出:

  ×  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.