我编写了一个 serverspec 测试,旨在检查服务器上的 ram 数量是否大于或等于属性文件中列出的挂载
describe command('perl -n -e "if (/MemTotal:\s+(\d+)/) { print \$1; last; }" /proc/meminfo') do
its(:stdout){ should be >= "#{property['ram']}"}
end
在我正在测试的机器上,命令给出“7539944”
如果属性 ram 设置为“888”,则测试错误地失败,因为字符串比较“888”大于“7539944”
有没有办法可以对 serverspec 中的字符串类型进行数字比较?
这个 irb 会议展示了我认为 ruby 的行为方式以及我希望它的行为方式
$ irb
1.9.3-p484 :001 > prop="888"
=> "888"
1.9.3-p484 :002 > command="7539944"
=> "7539944"
1.9.3-p484 :003 > command > prop
=> false
1.9.3-p484 :004 > command.to_i > prop.to_i
=> true
如果我将描述更改为此
describe command('perl -n -e "if (/MemTotal:\s+(\d+)/) { print \$1; last; }" /proc/meminfo') do
its(:stdout){ should be >= "#{property['ram']}".to_i}
end
我收到这个错误
Failures:
1) std Command "perl -n -e "if (/MemTotal:\s+(\d+)/) { print \$1; last; }" /proc/meminfo" stdout should be >= 888
On host `foo.bar.org'
Failure/Error: its(:stdout){ should be >= "#{property['ram']}".to_i}
ArgumentError:
comparison of String with 888 failed
sudo -p 'Password: ' /bin/sh -c perl\ -n\ -e\ \"if\ \(/MemTotal:\\s\+\(\\d\+\)/\)\ \{\ print\ \\\$1\;\ last\;\ \}\"\ /proc/meminfo
7539944
Shared Example Group: "ram::init" called from ./spec/std/spec.rb:5
# ./spec/shared/ram/init.rb:4:in `block (3 levels) in <top (required)>'