【问题标题】:Achieving skip condition in chef inspec在厨师检查中实现跳过条件
【发布时间】:2021-08-21 18:29:14
【问题描述】:

我试图达到这样的条件,如果组存在于服务器中,它应该通过,如果不存在,它应该跳过并且不应该显示为失败

describe.one do 
  describe 'testgroup'  do 
    expect(bash("ipa group-show testgroup").exit_status).to eq 0 
    it { should exist }
  end 

  describe 'testgroup' do 
    expect(bash("ipa group-show testgroup").exit_status).to_not eq 0 
    it { should_not exist }
  end
end 

执行后使用:

# inspec exec testgroup_test.rb

它抛出错误输出为:

undefined method 'bash' for Rspec::ExampleGroups (No method Error). 

请指教,如何在inspec中实现这样的条件测试。

【问题讨论】:

  • 如果组不存在也没关系,为什么要测试它呢?如果条件不匹配,不确定是否要编写一个不会失败的测试。
  • 该脚本在 2 个不同的环境中是通用的,在一个环境中,如果它可用,则测试应该通过,而在另一个环境中,该组不可用,它应该跳过并且不会显示失败

标签: chef-infra inspec


【解决方案1】:

要在解决特定环境测试之前先解决错误消息,测试命令结果的正确方法是使用command resource

让我们考虑一个案例,当命令执行成功时我想通过测试,否则失败:

describe command('ipa group-show testgroup') do
  its('exit_status') { should eq 0 }
end

现在,假设我们有 2 个环境 - “dev”和“qa”。我们可以使用inputs 有条件地执行测试。

例如:我们可以定义一个名为env 的输入。在“dev”环境中,此命令可能没有必要成功。在“qa”中它应该会成功。

# In "dev" any return code from command is ok
if input('env') == 'dev'
  control "ipa-group-dev" do
    describe command('ipa group-show testgroup') do
      its('exit_status') { should be >= 0 }
    end
  end
end

# In "qa" this command must succeed
if input('env') == 'qa'
  control "ipa-group-qa" do
    describe command('ipa group-show testgroup') do
      its('exit_status') { should eq 0 }
    end
  end
end

我们可以通过将env 的值作为输入来运行测试,例如:

inspec exec testgroup_test.rb --input env=dev

【讨论】:

  • sesadri_c 感谢您的宝贵意见,顺便说一句,是否可以在您的代码中获得自定义输出。表示如果条件抛出输出为“testgroup present”或“testgroup not available”
  • 我认为你试图用 inspec 实现的目标不符合它的设计。请查看custom inspec resources 是否适合您。否则,您应该查看其他工具(甚至是初级的 Shell 脚本)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多