Add percent_free property

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2019-01-31 17:18:39 -05:00
parent 497035a65b
commit fef637a6c6
3 changed files with 19 additions and 2 deletions

View file

@ -26,6 +26,7 @@ A `filesystem` resource block declares tests for disk space in a partition:
describe filesystem('/') do
its('size') { should be >= 32000 }
its('free') { should be >= 5000000 }
its('percent_free') { should be >= 20 }
its('type') { should cmp 'ext4' }
end
@ -34,6 +35,7 @@ where
* `filesystem('/')` states that the resource will look at the root (/) partition.
* `size` is the total partition size and is measured in kilobytes (KB).
* `free` is the available space on the partition and is measured in kilobytes (KB).
* `percent_free` is the percentage of available free space, and ranges from 0 to 100.
<br>
@ -65,6 +67,12 @@ The following examples show how to use this InSpec audit resource.
<br>
### Test if the /var partition has sufficient free space
describe filesystem('/var') do
its('percent_free') { should be >= 20 }
end
## Matchers
For a full list of available matchers, please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).

View file

@ -9,11 +9,13 @@ module Inspec::Resources
its('size') { should be >= 32000 }
its('free') { should be >= 3200 }
its('type') { should cmp 'ext4' }
its('percent_free') { should be >= 20 }
end
describe filesystem('c:') do
its('size') { should be >= 90 }
its('free') { should be >= 3200 }
its('type') { should cmp 'NTFS' }
its('percent_free') { should be >= 20 }
end
"
attr_reader :partition
@ -54,6 +56,11 @@ module Inspec::Resources
info[:free]
end
def percent_free
info = @fsman.info(@partition)
100 * info[:free] / info[:size]
end
def type
info = @fsman.info(@partition)
info[:type]

View file

@ -4,13 +4,14 @@ require 'inspec/resource'
describe 'Inspec::Resources::FileSystemResource' do
# arch linux
it 'verify filesystem on linux' do
resource = MockLoader.new(:ubuntu1404).load_resource('filesystem','/')
resource = MockLoader.new(:ubuntu1404).load_resource('filesystem','/')
_(resource.size).must_be :>=, 1
_(resource.name).must_equal '/'
_(resource.type).must_equal 'ext4'
_(resource.free).must_be :>=, 1
_(resource.percent_free).must_equal 68
end
# arch windows
it 'verify filesystem on windows' do
resource = MockLoader.new(:windows).load_resource('filesystem','c:')
@ -18,6 +19,7 @@ describe 'Inspec::Resources::FileSystemResource' do
_(resource.name).must_equal 'c:'
_(resource.type).must_equal 'NTFS'
_(resource.free).must_be :>=, 1
_(resource.free).must_equal 50000 # Test fixture reports size 100 with 50000 free
end
# unsuported os