mirror of
https://github.com/inspec/inspec
synced 2025-02-16 22:18:38 +00:00
handle mount results with multiple entries
This commit is contained in:
parent
e1402a16d9
commit
a72ba94f10
5 changed files with 35 additions and 6 deletions
|
@ -10,6 +10,7 @@ class Mount < Inspec.resource(1)
|
|||
example "
|
||||
describe mount('/') do
|
||||
it { should be_mounted }
|
||||
its(:count) { should eq 1 }
|
||||
its('device') { should eq '/dev/mapper/VolGroup-lv_root' }
|
||||
its('type') { should eq 'ext4' }
|
||||
its('options') { should eq ['rw', 'mode=620'] }
|
||||
|
@ -29,14 +30,28 @@ class Mount < Inspec.resource(1)
|
|||
file.mounted?
|
||||
end
|
||||
|
||||
def count
|
||||
mounted = file.mounted
|
||||
|
||||
if !mounted.nil? && !mounted.stdout.nil? && !mounted.stdout.lines.nil?
|
||||
mounted.stdout.lines.count
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def method_missing(name)
|
||||
return nil if !file.mounted?
|
||||
|
||||
mounted = file.mounted
|
||||
return nil if mounted.nil? || mounted.stdout.nil?
|
||||
|
||||
line = mounted.stdout
|
||||
# if we got multiple lines, only use the last entry
|
||||
line = mounted.stdout.lines.last if mounted.stdout.lines.count > 1
|
||||
|
||||
# parse content if we are on linux
|
||||
@mount_options ||= parse_mount_options(mounted.stdout)
|
||||
|
||||
@mount_options ||= parse_mount_options(line)
|
||||
@mount_options[name]
|
||||
end
|
||||
|
||||
|
|
|
@ -198,6 +198,7 @@ class MockLoader
|
|||
'find /etc/apache2/conf-enabled/*.conf -maxdepth 1 -type f' => cmd.call('find-apache2-conf-enabled'),
|
||||
# mount
|
||||
"mount | grep -- ' on /'" => cmd.call("mount"),
|
||||
"mount | grep -- ' on /mnt/iso-disk'" => cmd.call("mount-multiple"),
|
||||
}
|
||||
|
||||
@backend
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
# instead of `.with` or `.only_with` we recommend to use the `mount` resource
|
||||
describe mount '/mnt/iso-disk' do
|
||||
it { should be_mounted }
|
||||
its('count') { should eq 1 }
|
||||
its('device') { should eq '/root/alpine-3.3.0-x86_64.iso' }
|
||||
its('type') { should eq 'iso9660' }
|
||||
its('options') { should eq ['ro'] }
|
||||
|
|
2
test/unit/mock/cmd/mount-multiple
Normal file
2
test/unit/mock/cmd/mount-multiple
Normal file
|
@ -0,0 +1,2 @@
|
|||
/root/alpine-3.3.0-x86_64.iso on /mnt/iso-disk type iso9660 (ro)
|
||||
/root/alpine-3.3.0-x86_64_2.iso on /mnt/iso-disk type iso9660 (ro)
|
|
@ -6,11 +6,21 @@ require 'helper'
|
|||
require 'inspec/resource'
|
||||
|
||||
describe Inspec::Resources::File do
|
||||
let(:resource) { load_resource('mount', '/') }
|
||||
let(:root_resource) { load_resource('mount', '/') }
|
||||
|
||||
it 'parses the mount data properly' do
|
||||
resource.send(:device).must_equal('/dev/xvda1')
|
||||
resource.send(:type).must_equal('ext4')
|
||||
resource.send(:options).must_equal(['rw','discard'])
|
||||
root_resource.send(:device).must_equal('/dev/xvda1')
|
||||
root_resource.send(:type).must_equal('ext4')
|
||||
root_resource.send(:options).must_equal(['rw','discard'])
|
||||
root_resource.send(:count).must_equal(1)
|
||||
end
|
||||
|
||||
let(:iso_resource) { load_resource('mount', '/mnt/iso-disk') }
|
||||
|
||||
it 'parses the mount data properly' do
|
||||
iso_resource.send(:device).must_equal('/root/alpine-3.3.0-x86_64_2.iso')
|
||||
iso_resource.send(:type).must_equal('iso9660')
|
||||
iso_resource.send(:options).must_equal(['ro'])
|
||||
iso_resource.send(:count).must_equal(2)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue