serverspec如果条件错误

时间:2015-12-09 10:51:24

标签: puppet serverspec

我在puppet中有以下代码

if $::operatingsystemmajrelease < 7 {

     file { '/etc/sudoers' :
      ensure  => present,
      owner   => 'root',
      group   => 'root',
      mode    => '0440',
      content => template('sudo/sudoers.erb'),
    }
  }

我为此编写了serverspec

require 'spec_helper'

if os[:release] == '6'

  describe file('/etc/sudoers') do
    it { should exist }
    it { should be_owned_by 'root' }
    it { should be_grouped_into 'root' }
    it { should be_mode 440 }
    its(:content) { should match /Defaults    requiretty/}
  end
end

1)为什么我收到消息

No examples found.

Finished in 0.00038 seconds (files took 0.44176 seconds to load)
0 examples, 0 failures

我的操作系统版本是6所以它应该编译我的serverspec代码,但为什么它无法执行所有的例子。

如果我将其更改为if os[:release] < '7'

,则serverspec正在运行

2)如果我提供it { should be_mode 440 }} 4个八位字节匹配,it { should be_mode 0440也不支持。

低于错误

File "/etc/sudoers" should be mode 288
     On host `localhost'
     Failure/Error: it { should be_mode 0440 }
       expected `File "/etc/sudoers".mode?(288)` to return true, got false
       /bin/sh -c stat\ -c\ \%a\ /etc/sudoers\ \|\ grep\ --\ \\\^288\\\$

     # ./spec/localhost/sudo_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.05873 seconds (files took 0.48314 seconds to load)

1 个答案:

答案 0 :(得分:2)

1)通常os[:release]包含'6.5'等值。因此,进行比较的正确方法是:例如:

if os[:release].to_i == 6

或者:

if os[:release].split('.')[0] == '6'

2)因为带有前导零的数字由Ruby转换为八进制(0440)到十进制(288),而Serverspec(Specinfra)将它传递给 decimal-to-string 格式为the underlying grep command