【发布时间】:2019-09-11 16:13:06
【问题描述】:
假设在路径/tmp/foo 处部署到我的服务器的检查器脚本具有以下内容...
#!/bin/bash
a=`cat /tmp/a`
b=`cat /tmp/b`
echo -n $( expr $a - $b )
...我有一个 InSpec 测试来评估 a 和 b 之间的差异是否在可接受的范围内。
describe command('/tmp/foo') do
its('stdout') { should be >= 0 }
its('stdout') { should be < 120 }
end
遗憾的是,be matcher 不会将输入强制转换为可与数学运算符一起使用的类型。
如何将此值强制转换为整数?
到目前为止我已经尝试过
('stdout').to_i
undefined method `to_i' for #<Class:0x00007f8a86b91f00> (NoMethodError) Did you mean? to_s
-
('stdout').to_s和('stdout').to_s.to_i
Command: `/tmp/foo`
↺
↺
Test Summary: 0 successful, 0 failures, 2 skipped
(('stdout').to_i)
undefined method `0' for Command: `/tmp/foo`:#<Class:0x00007f9a52b912e0>
对于背景,/tmp/a 和 /tmp/b 是来自先前 InSpec 测试的一对 epoch 输出。
如果值在可接受的范围内,我可以检查客户端,但如果可能的话,我希望通过 InSpec 评估检查并报告这种差异,而不用正则表达式向导代替人类可读的数学表达式。
【问题讨论】:
-
我对 InSpec 一点也不熟悉,但很明显你必须查看它的文档,看看如何才能访问命令的标准输出。
标签: ruby chef-infra test-kitchen inspec