0

我正在使用 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

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

有什么建议吗?

4

2 回答 2

3

首先,ChefSpec 和 InSpec 做的事情完全不同,所以两者确实具有可比性。其次,虽然 InSpec 支持某种程度的 RSpec 语法兼容性,但它并不像 ChefSpec 或 ServerSpec 那样完整,它们都是完整的 RSpec 帮助程序库。正如@Tensibai 提到的,InSpec为更复杂的测试提供了自己的自定义语法。如果您特别想使用 RSpecdescribecontext块系统或自定义 RSpec 格式化程序,我建议您改用 ServerSpec。

于 2016-12-05T16:01:03.577 回答
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.
于 2019-01-28T17:27:26.480 回答