2018-01-25 08:29:31 -06:00
module Inspec::Resources
class FileSystemResource < Inspec . resource ( 1 )
name 'filesystem'
2018-02-19 06:26:49 -08:00
supports platform : 'linux'
2018-11-19 19:32:59 +01:00
supports platform : 'windows'
2018-01-25 08:29:31 -06:00
desc 'Use the filesystem InSpec resource to test file system'
example "
describe filesystem ( '/' ) do
2019-02-01 00:46:18 -05:00
its ( 'size_kb' ) { should be > = 32000 }
its ( 'free_kb' ) { should be > = 3200 }
2019-01-29 15:23:05 -06:00
its ( 'type' ) { should cmp 'ext4' }
2019-01-31 17:18:39 -05:00
its ( 'percent_free' ) { should be > = 20 }
2018-11-19 19:32:59 +01:00
end
describe filesystem ( 'c:' ) do
2019-02-01 00:46:18 -05:00
its ( 'size_kb' ) { should be > = 9000 }
its ( 'free_kb' ) { should be > = 3200 }
2019-01-29 15:23:05 -06:00
its ( 'type' ) { should cmp 'NTFS' }
2019-01-31 17:18:39 -05:00
its ( 'percent_free' ) { should be > = 20 }
2018-01-25 08:29:31 -06:00
end
"
attr_reader :partition
def initialize ( partition )
@partition = partition
2018-11-19 19:32:59 +01:00
@cache = nil
# select file system manager
@fsman = nil
os = inspec . os
if os . linux?
@fsman = LinuxFileSystemResource . new ( inspec )
elsif os . windows?
@fsman = WindowsFileSystemResource . new ( inspec )
else
raise Inspec :: Exceptions :: ResourceSkipped , 'The `filesystem` resource is not supported on your OS yet.'
end
end
def info
return @cache if ! @cache . nil?
return { } if @fsman . nil?
@cache = @fsman . info ( @partition )
end
def to_s
" FileSystem #{ @partition } "
2018-01-25 08:29:31 -06:00
end
2019-02-01 00:39:52 -05:00
def size_kb
2018-11-19 19:32:59 +01:00
info = @fsman . info ( @partition )
2019-02-01 00:39:52 -05:00
info [ :size_kb ]
end
def size
2019-02-01 00:50:57 -05:00
Inspec . deprecate ( :filesystem_property_size , 'The `size` property did not reliably use the correct units. Please use `size_kb` instead.' )
2019-02-01 00:39:52 -05:00
if inspec . os . windows?
# On windows, we had a bug prior to #3767 in which the
2019-02-01 00:50:57 -05:00
# 'size' value was be scaled to GB in powershell.
2019-02-01 00:39:52 -05:00
# We now collect it in KB.
( size_kb / ( 1024 * 1024 ) ) . to_i
else
size_kb
end
2018-11-19 19:32:59 +01:00
end
2018-01-25 08:29:31 -06:00
2019-02-01 00:46:18 -05:00
def free_kb
2019-01-29 15:23:05 -06:00
info = @fsman . info ( @partition )
2019-02-01 00:39:52 -05:00
info [ :free_kb ]
2019-01-29 15:23:05 -06:00
end
2019-01-31 17:18:39 -05:00
def percent_free
2019-02-01 00:46:18 -05:00
100 * free_kb / size_kb
2019-01-31 17:18:39 -05:00
end
2018-11-19 19:32:59 +01:00
def type
info = @fsman . info ( @partition )
info [ :type ]
2018-01-25 08:29:31 -06:00
end
2018-11-19 19:32:59 +01:00
def name
info = @fsman . info ( @partition )
info [ :name ]
end
end
class FsManagement
attr_reader :inspec
def initialize ( inspec )
@inspec = inspec
end
end
class LinuxFileSystemResource < FsManagement
def info ( partition )
2019-01-29 15:23:05 -06:00
cmd = inspec . command ( " df #{ partition } -T " )
2018-11-19 19:32:59 +01:00
raise Inspec :: Exceptions :: ResourceFailed , " Unable to get available space for partition #{ partition } " if cmd . stdout . nil? || cmd . stdout . empty? || ! cmd . exit_status . zero?
2019-01-29 16:50:31 -06:00
value = cmd . stdout . split ( / \ n / ) [ 1 ] . strip . split ( ' ' )
2018-11-19 19:32:59 +01:00
{
name : partition ,
2019-02-01 00:39:52 -05:00
size_kb : value [ 2 ] . to_i ,
free_kb : value [ 4 ] . to_i ,
2019-01-29 15:23:05 -06:00
type : value [ 1 ] . to_s ,
2018-11-19 19:32:59 +01:00
}
end
end
class WindowsFileSystemResource < FsManagement
def info ( partition )
cmd = inspec . command <<-EOF.gsub(/^\s*/, '')
$disk = Get - WmiObject Win32_LogicalDisk - Filter " DeviceID=' #{ partition } ' "
2019-02-01 00:39:52 -05:00
$disk . Size = $disk . Size / 1 KB
$disk . FreeSpace = $disk . FreeSpace / 1 KB
2019-01-29 15:23:05 -06:00
$disk | select - property DeviceID , Size , FileSystem , FreeSpace | ConvertTo - Json
2018-11-19 19:32:59 +01:00
EOF
raise Inspec :: Exceptions :: ResourceSkipped , " Unable to get available space for partition #{ partition } " if cmd . stdout == '' || cmd . exit_status . to_i != 0
begin
fs = JSON . parse ( cmd . stdout )
rescue JSON :: ParserError = > e
raise Inspec :: Exceptions :: ResourceFailed ,
'Failed to parse JSON from Powershell. ' \
" Error: #{ e } "
end
{
name : fs [ 'DeviceID' ] ,
2019-02-01 00:39:52 -05:00
size_kb : fs [ 'Size' ] . to_i ,
free_kb : fs [ 'FreeSpace' ] . to_i ,
2018-11-19 19:32:59 +01:00
type : fs [ 'FileSystem' ] ,
}
2018-01-25 08:29:31 -06:00
end
end
end