Chef Inspec抛出Rspec弃用错误

时间:2016-08-19 22:39:47

标签: ruby rspec chef inspec

我在Jenkins作业中运行Chef Inspec exec配置文件,并返回Rspec弃用警告。问题是这些警告包含在inspec exec命令创建的json输出中,并且我已尝试使用sed / awk删除json对象之外的所有字符,使用begin和end pattern / {/,/} /但没有每次文本仍在文件中时,工作正常。我在spec_helper.rb文件中添加了一个rspec异常,以允许两者的语法,但仍会出现警告。我留下的文件包含:

Invalid expiration date or inactive date or both
Invalid expiration date or inactive date or both

1 deprecation warning total
{"version":"0.30.0","profiles":{  ..... }- this part is actually valid json, excluding from this post to keep it short.

使用Inspec 0.30.0,Ruby 2.0.0p645,Rspec 3.5.2。

这是我用来通过ssh在本地目标上执行inspec配置文件的命令:

    VmIp=$(vmrun getGuestIPAddress output-vmware-iso/${templateName}.vmx -wait)
    inspec exec  ./inspecRepo/ --format json-min -t ssh://vagrant@${VmIp} --password vagrant --sudo | tee Test_Results.json

以下是Jenkins作业输出的片段:

+ VmPath=/app_2/jenkins-work/workspace/xl-release/output-vmware-iso/rhel7.vmware.template.vmx
++ vmrun getGuestIPAddress output-vmware-iso/rhel7.vmware.template.vmx -wait
+ VmIp=172.16.81.159
+ inspec exec ./inspecRepo/ --format json -t ssh://vagrant@172.16.81.159 --password vagrant --sudo
+ tee Test_Results.json
./inspecRepo/controls/filesystem.rb:60: warning: already initialized constant #<Class:0x00000001a25ac0>::BASELINE
./inspecRepo/controls/filesystem.rb:60: warning: previous definition of BASELINE was here
./inspecRepo/controls/filesystem.rb:60: warning: already initialized constant #<Class:0x00000001a25ac0>::BASELINE
./inspecRepo/controls/filesystem.rb:60: warning: previous definition of BASELINE was here
./inspecRepo/controls/filesystem.rb:60: warning: already initialized constant #<Class:0x00000001a25ac0>::BASELINE
./inspecRepo/controls/filesystem.rb:60: warning: previous definition of BASELINE was here

Deprecation Warnings:

Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` with `config.expect_with(:rspec) { |c| c.syntax = :should }` instead. Called from ./inspecRepo/controls/app_config.rb:466:in `block (4 levels) in load'.


If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the
deprecation warnings into errors, giving you the full backtrace.
Invalid expiration date or inactive date or both
Invalid expiration date or inactive date or both

1 deprecation warning total
{"version":"0.30.0","profiles":{"rhel7-baseline":{ ..............

以下是我添加的spec_helper.rb的片段:should&amp; :期望语法包含:

   Spec_helper.rb :
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.expect_with(:rspec){|c| c.syntax = [:should, :expect]}
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|

以下是生成语法警告的测试: \

    control 'password-inactive' do

  impact 0.5

  title "Ensure password inactivity is configured"

  desc "The password inactive in /etc/shadow is configured"



  DATE_FMT = '%b %d, %Y' # date format used by chage(1) as specified by strptime(3)

  command('egrep ^[^:]+:[^\!*] /etc/shadow | cut -d: -f1').stdout.each_line do |line|   # extract all users(username) with password



    pwExipre = command("chage --list #{line}").stdout.split("\n")[1].split(":")[1].strip  # extract date and convert to right format

    pwInactive = command("chage --list #{line}").stdout.split("\n")[2].split(":")[1].strip

    if pwExipre == 'never' || pwInactive == 'never'

      puts "Invalid expiration date or inactive date or both"

      describe true do

        it { should eq false }

      end

    else

      expire = Date.strptime(pwExipre, DATE_FMT) # convert to desired format

      inactive = Date.strptime(pwInactive, DATE_FMT)

      describe (inactive - expire) do

        it { should eq 30 } # 30 days' difference

      end

    end

  end

end

1 个答案:

答案 0 :(得分:0)

只需修改代码即可使用新的expect()is_expected语法。似乎更容易,无论如何都是标准的前进。如果您包含失败的实际测试代码,我可能会更具体。