2017-10-18 83 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