0

我在 Jenkins 作业中运行 Chef Inspec exec 配置文件,它返回 Rspec 弃用警告。问题是这些警告包含在 inspec exec 命令创建的 json 输出中,我尝试 sed/awk 使用开始和结束模式 /{/ , /}/ 删除 json 对象之外的所有字符,但没有任何效果,每次文本仍在文件中。我在 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 在本地目标上执行检查配置文件的命令:

    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 & :expect 语法包含:

   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
4

1 回答 1

0

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

于 2016-08-20T01:32:52.023 回答