lib/serverspec/backend/exec.rb in serverspec-0.5.3 vs lib/serverspec/backend/exec.rb in serverspec-0.5.4
- old
+ new
@@ -1,12 +1,20 @@
+require 'singleton'
+
module Serverspec
module Backend
class Exec
- def initialize(commands)
- @commands ||= commands
+ include Singleton
+
+ def set_commands(c)
+ @commands = c
end
+ def set_example(e)
+ @example = e
+ end
+
def commands
@commands
end
def run_command(cmd, opts={})
@@ -14,11 +22,11 @@
cmd = add_pre_command(cmd)
stdout = `#{build_command(cmd)} 2>&1`
# In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
#stdout, stderr, status = Open3.capture3(cmd)
- if ! @example.nil?
+ if @example
@example.metadata[:command] = cmd
@example.metadata[:stdout] = stdout
end
{ :stdout => stdout, :stderr => nil,
@@ -47,32 +55,27 @@
ret[:exit_status] == 0
end
# Default action is to call check_zero with args
def method_missing(meth, *args, &block)
- # Remove example object from *args
- @example = args.shift
check_zero(meth, *args)
end
- def check_running(example, process)
- @example = example
+ def check_running(process)
ret = run_command(commands.check_running(process))
if ret[:exit_status] == 1 || ret[:stdout] =~ /stopped/
ret = run_command(commands.check_process(process))
end
ret[:exit_status] == 0
end
- def check_running_under_supervisor(example, process)
- @example = example
+ def 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)
- @example = example
+ def check_readable(file, by_whom)
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
@@ -84,12 +87,11 @@
when 'others'
mode_octal & 0004 != 0
end
end
- def check_writable(example, file, by_whom)
- @example = example
+ def check_writable(file, by_whom)
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
@@ -101,12 +103,11 @@
when 'others'
mode_octal & 0002 != 0
end
end
- def check_executable(example, file, by_whom)
- @example = example
+ def check_executable(file, by_whom)
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
@@ -118,12 +119,11 @@
when 'others'
mode_octal & 0001 != 0
end
end
- def check_mounted(example, path, expected_attr, only_with)
- @example = example
+ def check_mounted(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
@@ -152,11 +152,10 @@
end
true
end
end
- def check_routing_table(example, expected_attr)
- @example = example
+ def check_routing_table(expected_attr)
return false if ! expected_attr[:destination]
ret = run_command(commands.check_routing_table(expected_attr[:destination]))
return false if ret[:exit_status] != 0
ret[:stdout] =~ /^(\S+)(?: via (\S+))? dev (\S+).+\r\n(?:default via (\S+))?/