Merge pull request #694 from chef/ap/symlinks-refactor2

update to train's new file interface: symlink + uid + gid
This commit is contained in:
Dominik Richter 2016-04-29 02:26:03 +02:00
commit 86b79035b8
3 changed files with 14 additions and 26 deletions

View file

@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib'] spec.require_paths = ['lib']
spec.add_dependency 'r-train', '~> 0.10.5' spec.add_dependency 'r-train', '~> 0.11'
spec.add_dependency 'thor', '~> 0.19' spec.add_dependency 'thor', '~> 0.19'
spec.add_dependency 'json', '~> 1.8' spec.add_dependency 'json', '~> 1.8'
spec.add_dependency 'rainbow', '~> 2' spec.add_dependency 'rainbow', '~> 2'

View file

@ -22,17 +22,17 @@ module Inspec::Resources
" "
include MountParser include MountParser
attr_reader :file, :path, :mount_options attr_reader :file, :mount_options
def initialize(path) def initialize(path)
@path = path @file = inspec.backend.file(path)
@file = inspec.backend.file(@path)
end end
%w{ %w{
type exist? file? block_device? character_device? socket? directory? type exist? file? block_device? character_device? socket? directory?
symlink? pipe? mode mode? owner owned_by? group grouped_into? link_target symlink? pipe? mode mode? owner owned_by? group grouped_into?
link_path linked_to? mtime size selinux_label immutable? link_path linked_to? mtime size selinux_label immutable?
product_version file_version version? md5sum sha256sum product_version file_version version? md5sum sha256sum
path source source_path uid gid
}.each do |m| }.each do |m|
define_method m.to_sym do |*args| define_method m.to_sym do |*args|
file.method(m.to_sym).call(*args) file.method(m.to_sym).call(*args)
@ -74,7 +74,7 @@ module Inspec::Resources
return file.mounted? if expected_options.nil? return file.mounted? if expected_options.nil?
# deprecation warning, this functionality will be removed in future version # deprecation warning, this functionality will be removed in future version
warn "[DEPRECATION] `be_mounted.with and be_mounted.only_with` are deprecated. Please use `mount('#{path}')` instead." warn "[DEPRECATION] `be_mounted.with and be_mounted.only_with` are deprecated. Please use `mount('#{source_path}')` instead."
# we cannot read mount data on non-Linux systems # we cannot read mount data on non-Linux systems
return nil if !inspec.os.linux? return nil if !inspec.os.linux?
@ -91,22 +91,8 @@ module Inspec::Resources
end end
end end
# TODO: This is temporary and must be moved to train
def uid
res = inspec.command('stat '+Shellwords.escape(@path)+' -c %u')
return nil if res.exit_status != 0 || res.stdout.empty?
res.stdout.to_i
end
# TODO: This is temporary and must be moved to train
def gid
res = inspec.command('stat '+Shellwords.escape(@path)+' -c %u')
return nil if res.exit_status != 0 || res.stdout.empty?
res.stdout.to_i
end
def to_s def to_s
"File #{path}" "File #{source_path}"
end end
private private
@ -133,13 +119,13 @@ module Inspec::Resources
def check_file_permission_by_user(user, flag) def check_file_permission_by_user(user, flag)
if inspec.os.linux? if inspec.os.linux?
perm_cmd = "su -s /bin/sh -c \"test -#{flag} #{path}\" #{user}" perm_cmd = "su -s /bin/sh -c \"test -#{flag} #{source_path}\" #{user}"
elsif inspec.os.bsd? || inspec.os.solaris? elsif inspec.os.bsd? || inspec.os.solaris?
perm_cmd = "sudo -u #{user} test -#{flag} #{path}" perm_cmd = "sudo -u #{user} test -#{flag} #{source_path}"
elsif inspec.os.aix? elsif inspec.os.aix?
perm_cmd = "su #{user} -c test -#{flag} #{path}" perm_cmd = "su #{user} -c test -#{flag} #{source_path}"
elsif inspec.os.hpux? elsif inspec.os.hpux?
perm_cmd = "su #{user} -c \"test -#{flag} #{path}\"" perm_cmd = "su #{user} -c \"test -#{flag} #{source_path}\""
else else
return skip_resource 'The `file` resource does not support `by_user` on your OS.' return skip_resource 'The `file` resource does not support `by_user` on your OS.'
end end

View file

@ -22,8 +22,10 @@ describe file('/proc/version') do
end end
describe file('/dev/stdout') do describe file('/dev/stdout') do
its(:type) { should eq :symlink } its(:type) { should eq :pipe }
its('source.type') { should eq :symlink }
it { should be_symlink } it { should be_symlink }
it { should be_pipe }
it { should_not be_file } it { should_not be_file }
it { should_not be_directory } it { should_not be_directory }
end end