如何在 Inspec 测试中使用 ruby 解析多行标准输出
How to parse multiline stdout with ruby, in Inspec test
我有一个测试:
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
有效!!谢谢
为什么要使用 Open3
?您希望使用 command
资源,而不是直接执行 Ruby 命令。也就是说,您只需与其中包含 \n
的字符串进行比较。
我有一个测试:
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
有效!!谢谢
为什么要使用 Open3
?您希望使用 command
资源,而不是直接执行 Ruby 命令。也就是说,您只需与其中包含 \n
的字符串进行比较。