2015-09-10 11:35:15 +00:00
# encoding: utf-8
2015-10-02 12:55:13 +00:00
# author: Christoph Hartmann
2015-10-06 16:55:44 +00:00
# author: Dominik Richter
2015-09-10 11:35:15 +00:00
# This resource talks with OneGet (https://github.com/OneGet/oneget)
# Its part of Windows Management Framework 5.0 and part of Windows 10
#
# Usage:
# describe oneget('zoomit') do
# it { should be_installed }
# end
2016-03-08 18:06:55 +00:00
module Inspec::Resources
class OneGetPackage < Inspec . resource ( 1 )
name 'oneget'
desc 'Use the oneget InSpec audit resource to test if the named package and/or package version is installed on the system. This resource uses OneGet, which is part of the Windows Management Framework 5.0 and Windows 10. This resource uses the Get-Package cmdlet to return all of the package names in the OneGet repository.'
example "
describe oneget ( 'zoomit' ) do
it { should be_installed }
end
"
2015-09-10 11:35:15 +00:00
2016-03-08 18:06:55 +00:00
def initialize ( package_name )
@package_name = package_name
2015-10-02 08:31:46 +00:00
2016-03-08 18:06:55 +00:00
# verify that this resource is only supported on Windows
return skip_resource 'The `oneget` resource is not supported on your OS.' if inspec . os [ :family ] != 'windows'
end
2015-09-10 11:35:15 +00:00
2016-03-08 18:06:55 +00:00
def info
return @info if defined? ( @info )
2015-09-17 14:50:21 +00:00
2016-03-08 18:06:55 +00:00
@info = { }
@info [ :type ] = 'oneget'
@info [ :installed ] = false
2015-09-17 14:50:21 +00:00
2016-03-08 18:06:55 +00:00
cmd = inspec . command ( " Get-Package -Name ' #{ @package_name } ' | ConvertTo-Json " )
# cannot rely on exit code for now, successful command returns exit code 1
# return nil if cmd.exit_status != 0
# try to parse json
2015-09-17 14:50:21 +00:00
2016-03-08 18:06:55 +00:00
begin
pkgs = JSON . parse ( cmd . stdout )
@info [ :installed ] = true
2015-10-02 09:10:13 +00:00
2016-03-08 18:06:55 +00:00
# sometimes we get multiple values
if pkgs . is_a? ( Array )
# select the first entry
pkgs = pkgs . first
end
rescue JSON :: ParserError = > _e
return @info
2015-10-02 09:10:13 +00:00
end
2015-09-17 14:50:21 +00:00
2016-03-08 18:06:55 +00:00
@info [ :name ] = pkgs [ 'Name' ] if pkgs . key? ( 'Name' )
@info [ :version ] = pkgs [ 'Version' ] if pkgs . key? ( 'Version' )
@info
end
2015-09-10 11:35:15 +00:00
2016-03-08 18:06:55 +00:00
def installed?
info [ :installed ] == true
end
2015-09-10 11:35:15 +00:00
2016-03-08 18:06:55 +00:00
def version
info [ :version ]
end
2015-09-10 11:35:15 +00:00
2016-03-08 18:06:55 +00:00
def to_s
" OneGet Package #{ @package_name } "
end
2015-09-10 11:35:15 +00:00
end
end