【问题标题】:How to parse multiline stdout with ruby, in Inspec test如何在 Inspec 测试中使用 ruby​​ 解析多行标准输出
【发布时间】:2017-12-18 15:46:34
【问题描述】:

我有一个测试:

control "cis-0-0-7" do
  impact 1.0
  title "verify chkconfig"
  desc "verify chkconfig"
  stdout, stderr, status = Open3.capture3('chkconfig | grep active')
     puts stdout
      #stdout { should match /activemq-instance-EL2-ext/ }
   #end
end

这会在标准输出上显示以下内容:

$inspec exec cookbooks/activemq7/test/linuxcommon_test.rb
activemq-instance-EL2-ext   0:off   1:off   2:on    3:on    4:on    5:on    6:off
activemq-instance-EL2-int   0:off   1:off   2:on    3:on    4:on    5:on    6:off

我如何使用 Inspec(如果可能)或使用 ruby​​ 来解析和验证(断言)这些多行。

正如@coderanger 建议我使用的那样:

control "cis-0-0-7" do
  impact 1.0
  title "verify chkconfig"
  desc "verify chkconfig"
  #stdout, stderr, status = Open3.capture3('chkconfig | grep active')
  output = command('chkconfig | grep active')
     describe output do
      its('stdout')  { should match /activemq-instance-EL2-ext.*\n/ }
      its('stdout')  { should match /activemq-instance-EL2-int.*\n/ }
   end
end

有效!!谢谢

【问题讨论】:

  • 你仍然不需要变量,只需要describe command(...),现在我费心阅读你的代码,你为什么 1) 不使用 service() 资源和 2) 不使用现有的CIS 基准测试模块?

标签: ruby parsing chef-infra inspec


【解决方案1】:

您为什么使用Open3?您想使用 command 资源,而不是直接执行 Ruby 命令。也就是说,您只需与其中包含 \n 的字符串进行比较。

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 2010-11-17
    相关资源
    最近更新 更多