【问题标题】:How create context with InSpec?如何使用 InSpec 创建上下文?
【发布时间】:2019-01-29 01:27:26
【问题描述】:

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

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

有什么建议吗?

【问题讨论】:

标签: chef-infra inspec


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我认为您正在寻找一种更好地控制输出的方法。如果是这样,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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      相关资源
      最近更新 更多