mirror of
https://github.com/inspec/inspec
synced 2024-11-23 21:23:29 +00:00
Merge pull request #33 from chef/windows_feature
add Windows feature resource
This commit is contained in:
commit
c36b6d2972
5 changed files with 102 additions and 0 deletions
75
lib/resources/windows_feature.rb
Normal file
75
lib/resources/windows_feature.rb
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
# check for a Windows feature
|
||||||
|
# Usage:
|
||||||
|
# describe windows_feature('DHCP Server') do
|
||||||
|
# it{ should be_installed }
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# deprecated serverspec syntax:
|
||||||
|
# describe windows_feature('IIS-Webserver') do
|
||||||
|
# it{ should be_installed.by("dism") }
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# describe windows_feature('Web-Webserver') do
|
||||||
|
# it{ should be_installed.by("powershell") }
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# This implementation uses the Get-WindowsFeature commandlet:
|
||||||
|
# Get-WindowsFeature | Where-Object {$_.Name -eq 'XPS Viewer' -or $_.DisplayName -eq 'XPS Viewe
|
||||||
|
# r'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json
|
||||||
|
# {
|
||||||
|
# "Name": "XPS-Viewer",
|
||||||
|
# "DisplayName": "XPS Viewer",
|
||||||
|
# "Description": "The XPS Viewer is used to read, set permissions for, and digitally sign XPS documents.",
|
||||||
|
# "Installed": false,
|
||||||
|
# "InstallState": 0
|
||||||
|
# }
|
||||||
|
class WindowsFeature < Vulcano.resource(1)
|
||||||
|
name 'windows_feature'
|
||||||
|
|
||||||
|
def initialize(feature)
|
||||||
|
@feature = feature
|
||||||
|
@cache = nil
|
||||||
|
|
||||||
|
# verify that this resource is only supported on Windows
|
||||||
|
return skip_resource 'The `windows_feature` resource is not supported on your OS.' if vulcano.os[:family] != 'windows'
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns true if the package is installed
|
||||||
|
def installed?(_provider = nil, _version = nil)
|
||||||
|
info[:installed] == true
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns the package description
|
||||||
|
def info
|
||||||
|
return @cache if !@cache.nil?
|
||||||
|
features_cmd = "Get-WindowsFeature | Where-Object {$_.Name -eq '#{@feature}' -or $_.DisplayName -eq '#{@feature}'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json"
|
||||||
|
cmd = vulcano.run_command(features_cmd)
|
||||||
|
|
||||||
|
@cache = {
|
||||||
|
name: @feature,
|
||||||
|
type: 'windows-feature',
|
||||||
|
}
|
||||||
|
|
||||||
|
# cannot rely on exit code for now, successful command returns exit code 1
|
||||||
|
# return nil if cmd.exit_status != 0
|
||||||
|
# try to parse json
|
||||||
|
begin
|
||||||
|
params = JSON.parse(cmd.stdout)
|
||||||
|
rescue JSON::ParserError => _e
|
||||||
|
return @cache
|
||||||
|
end
|
||||||
|
|
||||||
|
@cache = {
|
||||||
|
name: params['Name'],
|
||||||
|
description: params['Description'],
|
||||||
|
installed: params['Installed'],
|
||||||
|
type: 'windows-feature',
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
"Windows feature '#{@feature}'"
|
||||||
|
end
|
||||||
|
end
|
|
@ -52,4 +52,5 @@ require 'resources/registry_key'
|
||||||
require 'resources/security_policy'
|
require 'resources/security_policy'
|
||||||
require 'resources/service'
|
require 'resources/service'
|
||||||
require 'resources/ssh_conf'
|
require 'resources/ssh_conf'
|
||||||
|
require 'resources/windows_feature'
|
||||||
require 'resources/yum'
|
require 'resources/yum'
|
||||||
|
|
|
@ -69,6 +69,7 @@ def loadResource (resource, *args)
|
||||||
'pip show jinja2' => cmd.call('pip-show-jinja2'),
|
'pip show jinja2' => cmd.call('pip-show-jinja2'),
|
||||||
"Get-Package -Name 'Mozilla Firefox' | ConvertTo-Json" => cmd.call('get-package'),
|
"Get-Package -Name 'Mozilla Firefox' | ConvertTo-Json" => cmd.call('get-package'),
|
||||||
"New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name Service -Value (Get-Service -Name dhcp| Select-Object -Property Name, DisplayName, Status) -PassThru | Add-Member -MemberType NoteProperty -Name WMI -Value (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property StartMode) -PassThru | ConvertTo-Json" => cmd.call('get-service-dhcp'),
|
"New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name Service -Value (Get-Service -Name dhcp| Select-Object -Property Name, DisplayName, Status) -PassThru | Add-Member -MemberType NoteProperty -Name WMI -Value (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property StartMode) -PassThru | ConvertTo-Json" => cmd.call('get-service-dhcp'),
|
||||||
|
"Get-WindowsFeature | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json" => cmd.call('get-windows-feature'),
|
||||||
}
|
}
|
||||||
|
|
||||||
# load resource
|
# load resource
|
||||||
|
|
7
test/unit/mock/cmd/get-windows-feature
Normal file
7
test/unit/mock/cmd/get-windows-feature
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"Name": "DHCP",
|
||||||
|
"DisplayName": "DHCP Server",
|
||||||
|
"Description": "Dynamic Host Configuration Protocol (DHCP) Server enables you to centrally configure, manage, and provide temporary IP addresses and related information for client computers.",
|
||||||
|
"Installed": false,
|
||||||
|
"InstallState": 0
|
||||||
|
}
|
18
test/unit/resource_windows_feature.rb
Normal file
18
test/unit/resource_windows_feature.rb
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
# encoding utf-8
|
||||||
|
|
||||||
|
require 'helper'
|
||||||
|
require 'vulcano/resource'
|
||||||
|
|
||||||
|
describe 'Vulcano:Resources::Feature' do
|
||||||
|
describe 'feature' do
|
||||||
|
|
||||||
|
let(:resource) { loadResource('windows_feature', 'dhcp') }
|
||||||
|
|
||||||
|
# TODO: set windows as mock os
|
||||||
|
it 'verify windows feature parsing' do
|
||||||
|
pkg = { name: 'DHCP', description: 'Dynamic Host Configuration Protocol (DHCP) Server enables you to centrally configure, manage, and provide temporary IP addresses and related information for client computers.', installed: false, type: 'windows-feature' }
|
||||||
|
_(resource.info).must_equal pkg
|
||||||
|
_(resource.installed?).must_equal false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue