lib/serverspec/backend/exec.rb in serverspec-0.2.19 vs lib/serverspec/backend/exec.rb in serverspec-0.2.20
- old
+ new
@@ -9,11 +9,11 @@
def commands
@commands
end
- def do_check(cmd, opts={})
+ def run_command(cmd, opts={})
# In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
#stdout, stderr, status = Open3.capture3(cmd)
# So get exit status with `command`
`#{cmd} 2>&1`
ret = { :exit_status => $?, :exit_signal => nil }
@@ -24,11 +24,11 @@
ret[:stderr] = stderr.read
ret
end
def check_zero(cmd, *args)
- ret = do_check(commands.send(cmd, *args))
+ ret = run_command(commands.send(cmd, *args))
ret[:exit_status] == 0
end
# Default action is to call check_zero with args
def method_missing(meth, *args, &block)
@@ -36,33 +36,33 @@
args.shift
check_zero(meth, *args)
end
def check_installed_by_gem(example, package, version)
- ret = do_check(commands.check_installed_by_gem(package))
+ ret = run_command(commands.check_installed_by_gem(package))
res = ret[:exit_status] == 0
if res && version
res = false if not ret[:stdout].match(/\(#{version}\)/)
end
res
end
def check_running(example, process)
- ret = do_check(commands.check_running(process))
+ ret = run_command(commands.check_running(process))
if ret[:exit_status] == 1 || ret[:stdout] =~ /stopped/
- ret = do_check(commands.check_process(process))
+ ret = run_command(commands.check_process(process))
end
ret[:exit_status] == 0
end
def check_running_under_supervisor(example, process)
- ret = do_check(commands.check_running_under_supervisor(process))
+ ret = run_command(commands.check_running_under_supervisor(process))
ret[:exit_status] == 0 && ret[:stdout] =~ /RUNNING/
end
def check_readable(example, file, by_whom)
- mode = sprintf('%04s',do_check(commands.get_mode(file))[:stdout].strip)
+ mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
mode = mode.split('')
mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
case by_whom
when nil
mode_octal & 0444 != 0
@@ -74,11 +74,11 @@
mode_octal & 0004 != 0
end
end
def check_writable(example, file, by_whom)
- mode = sprintf('%04s',do_check(commands.get_mode(file))[:stdout].strip)
+ mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
mode = mode.split('')
mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
case by_whom
when nil
mode_octal & 0222 != 0
@@ -90,11 +90,11 @@
mode_octal & 0002 != 0
end
end
def check_executable(example, file, by_whom)
- mode = sprintf('%04s',do_check(commands.get_mode(file))[:stdout].strip)
+ mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
mode = mode.split('')
mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
case by_whom
when nil
mode_octal & 0111 != 0
@@ -105,17 +105,51 @@
when 'others'
mode_octal & 0001 != 0
end
end
+ def check_mounted(example, path, expected_attr, only_with)
+ ret = run_command(commands.check_mounted(path))
+ if expected_attr.nil? || ret[:exit_status] != 0
+ return ret[:exit_status] == 0
+ end
+
+ mount = ret[:stdout].scan(/\S+/)
+ actual_attr = { :device => mount[0], :type => mount[4] }
+ mount[5].gsub(/\(|\)/, '').split(',').each do |option|
+ name, val = option.split('=')
+ if val.nil?
+ actual_attr[name.to_sym] = true
+ else
+ val = val.to_i if val.match(/^\d+$/)
+ actual_attr[name.to_sym] = val
+ end
+ end
+
+ if ! expected_attr[:options].nil?
+ expected_attr.merge!(expected_attr[:options])
+ expected_attr.delete(:options)
+ end
+
+ if only_with
+ actual_attr == expected_attr
+ else
+ match = true
+ expected_attr.each do |key, val|
+ match = actual_attr[key] == val
+ end
+ match
+ end
+ end
+
def check_os
- if do_check('ls /etc/redhat-release')[:exit_status] == 0
+ if run_command('ls /etc/redhat-release')[:exit_status] == 0
'RedHat'
- elsif do_check('ls /etc/debian_version')[:exit_status] == 0
+ elsif run_command('ls /etc/debian_version')[:exit_status] == 0
'Debian'
- elsif do_check('ls /etc/gentoo-release')[:exit_status] == 0
+ elsif run_command('ls /etc/gentoo-release')[:exit_status] == 0
'Gentoo'
- elsif do_check('uname -s')[:stdout] =~ /SunOS/i
+ elsif run_command('uname -s')[:stdout] =~ /SunOS/i
'Solaris'
end
end
end