2016-08-19 65 views
0

我在Jenkins工作中运行厨师Inspec可执行文件并返回Rspec弃用警告。问题在于,这些警告包含在inspec exec命令创建的json输出中,而且我试过sed/awk使用开始和结束模式/ {/,/} /删除json对象之外的所有字符,但没有任何工作,每次文本仍然在文件中。我向spec_helper.rb文件添加了一个rspec异常,以允许两种语法,但警告仍然出现。我剩下的文件包含:厨师Inspec抛出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://[email protected]${VmIp} --password vagrant --sudo | tee Test_Results.json 

这里是詹金斯作业输出的一个片段:

+ 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://[email protected] --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的一段代码:&:期望语法包含:

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 

回答

0

只是解决您的代码以使用新的expect()is_expected语法来代替。看起来更容易,无论如何这是标准的前进。如果包含失败的实际测试代码,我可能会更具体。

+0

我已经更新了包含测试的问题,但测试使用的是标准Inspec dsl。 – hedda

+0

用'is_expected.to'或'expect(subject).to'替换'should'。这与InSpec无关,这是RSpec的正常工作。 – coderanger

+0

嗯,但是在使用Inspec dsl语法'should'的配置文件中有数百个测试,并且他们不会抛出此错误。 – hedda