2017-10-18 80 views
1

背景 我寫了一本食譜,安裝Windows功能。某些功能依賴於父功能。父功能可能沒有安裝該功能所需的源文件。廚師/ InSpec powershell命令結果內測試

在我的食譜中,我使用only_if調用Powershell命令來確定源文件是否存在。

(Get-WindowsFeature | Where Name -eq NET-Framework-Core | Select InstallState).InstallState -eq 'Removed' 

如果安裝狀態等於拆除,因功能不具備所需的源文件,並且無需提供他們無法安裝。因此,如果我的食譜確定缺少源文件,它將不會嘗試安裝這些功能。但是,如果源文件確實存在,cookbook將安裝這些功能。這部分工作完美。

問題 我有InSpec測試來驗證安裝了正確的Windows功能。我想使用Powershell命令的結果運行或跳過特定的測試。我找不到一種方法來調用上面的Powershell命令,獲取結果並運行或跳過InSpec中的測試。

回答

0

有兩個主要選項。一種是複製邏輯來檢查源文件是否存在於你的InSpec代碼中(毛重)。另一種是寫出一個令牌文件(即只是觸摸文件),如果不進行安裝,並檢查InSpec中的文件是否帶有file資源。

0

一些挖後,我發現這個InSpec issue on git hub

他們加入到內INSPEC使用only_if(我不知道)的能力。我使用powershell資源來調用我的powershell命令,將stdout轉換爲布爾值並將其返回。我將提供我想出的粗略代碼以供參考。我是新來的紅寶石,所以我相信有一個更好的方法來編碼。

control 'Recipe windows_features.rb .NET 3.5 Features' do 
    impact 1.0 
    title 'Required .NET 3.5 Windows Features Are Installed' 
    only_if do 
    powershell_command_script = <<-EOH 
    (Get-WindowsFeature | Where Name -eq NET-Framework-Core | Select InstallState).InstallState -ne 'Removed' 
    EOH 
    command_result = powershell(powershell_command_script) 
    case command_result.stdout 
    when true, "True\r\n" then true 
    when false, "False\r\n" then false 
    else 
     raise ArgumentError, "invalid value: #{command_result.stdout.inspect}" 
    end 

    end 
    describe windows_feature('WAS-NET-Environment') do 
    it { should be_installed } 
    end 
    describe windows_feature('Web-Asp-Net') do 
    it { should be_installed } 
    end 
    describe windows_feature('Web-Net-Ext') do 
    it { should be_installed } 
    end 
    describe windows_feature('Web-Mgmt-Console') do 
    it { should be_installed } 
    end 
end