simplify mock backend association

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
Dominik Richter 2015-09-06 19:35:19 +02:00
parent ea91af6da1
commit 32a6f01d1f

View file

@ -4,27 +4,19 @@ module Vulcano::Backends
class Mock < Vulcano.backend(1)
name 'mock'
def initialize(conf, mapping = {}, cmd_mapping = {})
attr_accessor :files, :commands
def initialize(conf)
@conf = conf
@files = {}
@mapping = mapping
@cmd_mapping = cmd_mapping
@commands = {}
end
def file(path)
puts "--> get file #{path}"
newpath = @mapping[path]
@files[path] ||= File.new(self, newpath || path, newpath.nil?)
@files[path] ||= File.new(self, path)
end
def run_command(cmd)
puts "--> run command #{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)
@command[cmd] || Command.new(self, cmd)
end
def to_s
@ -34,58 +26,47 @@ module Vulcano::Backends
class Mock
class File
def initialize(runtime, path, mock = true)
def initialize(runtime, path)
@path = path
# mock dataset
if mock
@exists = (rand < 0.8) ? true : false
@is_file = (@exists && rand < 0.7) ? true : false
@size = 0
@content = ''
if @exists && @is_file
@size = (rand**3 * 1000).to_i
@size = 0 if rand < 0.2
end
if @size > 0
@content = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
end
# we have a mapping file
else
@exists = IO::File.exists?(path)
@is_file = IO::File.file?(path)
@size = IO::File.size(path)
@content = IO::File.read(path)
@exists = (rand < 0.8) ? true : false
@is_file = (@exists && rand < 0.7) ? true : false
@size = 0
@content = ''
if @exists && @is_file
@size = (rand**3 * 1000).to_i
@size = 0 if rand < 0.2
end
if @size > 0
@content = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
end
end
def size
puts "----> get file #{@path} size: #{@size}"
@size
end
def content
puts "----> get file #{@path} content: #{@content}"
@content
end
def file?
puts "----> is file #{@path} a file? #{@is_file}"
@is_file
end
def exists?
puts "----> does file #{@path} exist? #{@exists}"
@exists
end
end
class Command
attr_accessor :stdout, :stderr, :exit_status
def initialize(runtime, cmd, stdout = nil, stderr = nil, exit_code = nil)
@exit_code = exit_code || (rand < 0.7) ? 0 : (100 * rand).to_i
@stdout = stdout || (0...50).map { ('a'..'z').to_a[rand(26)] }.join
@stderr = stderr || (0...50).map { ('a'..'z').to_a[rand(26)] }.join
def initialize(runtime, cmd)
@exit_code = (rand < 0.7) ? 0 : (100 * rand).to_i
@stdout = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
@stderr = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
end
end
end
end