improvement: extend mock to support simulated cmds

This commit is contained in:
Christoph Hartmann 2015-09-05 17:39:02 +02:00 committed by Dominik Richter
parent cdf15b9dd1
commit f0ac64cf31

View file

@ -4,10 +4,11 @@ module Vulcano::Backends
class Mock < Vulcano.backend(1) class Mock < Vulcano.backend(1)
name 'mock' name 'mock'
def initialize(conf, mapping = {}) def initialize(conf, mapping = {}, cmd_mapping = {})
@conf = conf @conf = conf
@files = {} @files = {}
@mapping = mapping @mapping = mapping
@cmd_mapping = cmd_mapping
end end
def file(path) def file(path)
@ -18,7 +19,12 @@ module Vulcano::Backends
def run_command(cmd) def run_command(cmd)
puts "--> run command #{cmd}" puts "--> run command #{cmd}"
Command.new(self, cmd) result = Command.new(self, cmd)
mapping = @cmd_mapping[cmd]
# read simulation data
return result if mapping.nil?
Command.new(self, cmd, File.new(self, mapping, false).content, '', 0)
end end
def to_s def to_s
@ -74,11 +80,11 @@ module Vulcano::Backends
end end
class Command class Command
attr_reader :stdout, :stderr, :exit_status attr_accessor :stdout, :stderr, :exit_status
def initialize(runtime, cmd) def initialize(runtime, cmd, stdout = nil, stderr = nil, exit_code = nil)
@exit_code = (rand < 0.7) ? 0 : (100 * rand).to_i @exit_code = exit_code || (rand < 0.7) ? 0 : (100 * rand).to_i
@stdout = (0...50).map { ('a'..'z').to_a[rand(26)] }.join @stdout = stdout || (0...50).map { ('a'..'z').to_a[rand(26)] }.join
@stderr = (0...50).map { ('a'..'z').to_a[rand(26)] }.join @stderr = stderr || (0...50).map { ('a'..'z').to_a[rand(26)] }.join
end end
end end
end end