2015-08-27 20:59:15 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2015-08-29 23:14:28 +00:00
|
|
|
module Vulcano::Backends
|
|
|
|
class Mock < Vulcano.backend(1)
|
|
|
|
name 'mock'
|
2015-08-27 20:59:15 +00:00
|
|
|
|
2015-09-05 15:39:02 +00:00
|
|
|
def initialize(conf, mapping = {}, cmd_mapping = {})
|
2015-08-29 23:14:28 +00:00
|
|
|
@conf = conf
|
2015-08-27 20:59:15 +00:00
|
|
|
@files = {}
|
2015-09-03 15:09:39 +00:00
|
|
|
@mapping = mapping
|
2015-09-05 15:39:02 +00:00
|
|
|
@cmd_mapping = cmd_mapping
|
2015-08-27 20:59:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def file(path)
|
2015-09-03 15:51:08 +00:00
|
|
|
puts "--> get file #{path}"
|
2015-09-03 15:09:39 +00:00
|
|
|
newpath = @mapping[path]
|
2015-09-03 15:53:38 +00:00
|
|
|
@files[path] ||= File.new(self, newpath || path, newpath.nil?)
|
2015-08-27 20:59:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_command(cmd)
|
2015-09-03 15:51:08 +00:00
|
|
|
puts "--> run command #{cmd}"
|
2015-09-05 15:39:02 +00:00
|
|
|
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)
|
2015-08-27 20:59:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
'Mock Backend Runner'
|
|
|
|
end
|
|
|
|
end
|
2015-08-29 23:14:28 +00:00
|
|
|
|
|
|
|
class Mock
|
|
|
|
class File
|
2015-09-03 15:09:39 +00:00
|
|
|
def initialize(runtime, path, mock = true)
|
2015-08-29 23:14:28 +00:00
|
|
|
@path = path
|
2015-09-03 15:09:39 +00:00
|
|
|
# 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
|
2015-09-05 14:07:54 +00:00
|
|
|
@size = (rand**3 * 1000).to_i
|
2015-09-03 15:09:39 +00:00
|
|
|
@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)
|
2015-08-29 23:14:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def size
|
|
|
|
puts "----> get file #{@path} size: #{@size}"
|
|
|
|
@size
|
|
|
|
end
|
|
|
|
|
|
|
|
def content
|
|
|
|
puts "----> get file #{@path} content: #{@content}"
|
|
|
|
@content
|
|
|
|
end
|
|
|
|
|
2015-08-30 00:13:53 +00:00
|
|
|
def file?
|
2015-08-29 23:14:28 +00:00
|
|
|
puts "----> is file #{@path} a file? #{@is_file}"
|
|
|
|
@is_file
|
|
|
|
end
|
|
|
|
|
|
|
|
def exists?
|
|
|
|
puts "----> does file #{@path} exist? #{@exists}"
|
|
|
|
@exists
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Command
|
2015-09-05 15:39:02 +00:00
|
|
|
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
|
2015-08-29 23:14:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-08-27 20:59:15 +00:00
|
|
|
end
|