2015-08-27 20:59:15 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2015-08-29 23:14:28 +00:00
|
|
|
module Vulcano::Backends
|
2015-09-09 10:13:13 +00:00
|
|
|
Struct.new('MockCommand', :stdout, :stderr, :exit_status)
|
2015-09-06 18:40:13 +00:00
|
|
|
|
2015-08-29 23:14:28 +00:00
|
|
|
class Mock < Vulcano.backend(1)
|
|
|
|
name 'mock'
|
2015-08-27 20:59:15 +00:00
|
|
|
|
2015-09-21 10:40:25 +00:00
|
|
|
attr_accessor :files, :commands, :os
|
2015-09-06 17:35:19 +00:00
|
|
|
def initialize(conf)
|
2015-08-29 23:14:28 +00:00
|
|
|
@conf = conf
|
2015-08-27 20:59:15 +00:00
|
|
|
@files = {}
|
2015-09-21 10:40:25 +00:00
|
|
|
@os = {}
|
2015-09-06 17:35:19 +00:00
|
|
|
@commands = {}
|
2015-09-04 20:54:52 +00:00
|
|
|
trace_calls unless @conf[:quiet]
|
2015-08-27 20:59:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def file(path)
|
2015-09-06 17:35:19 +00:00
|
|
|
@files[path] ||= File.new(self, path)
|
2015-08-27 20:59:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_command(cmd)
|
2015-09-06 18:26:23 +00:00
|
|
|
@commands[cmd] || Command.new(self, cmd)
|
2015-08-27 20:59:15 +00:00
|
|
|
end
|
|
|
|
|
2015-09-06 18:40:13 +00:00
|
|
|
def mock_command(stdout, stderr, exit_status)
|
|
|
|
Struct::MockCommand.new(stdout, stderr, exit_status)
|
|
|
|
end
|
|
|
|
|
2015-08-27 20:59:15 +00:00
|
|
|
def to_s
|
|
|
|
'Mock Backend Runner'
|
|
|
|
end
|
2015-09-04 20:54:52 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def trace_calls
|
|
|
|
interface_methods = {
|
|
|
|
'Vulcano::Backends::Mock' => Vulcano::Backends::Mock.instance_methods(false),
|
|
|
|
'Vulcano::Backends::Mock::File' => FileCommon.instance_methods(false),
|
|
|
|
}
|
|
|
|
|
2015-09-09 08:08:54 +00:00
|
|
|
# rubocop:disable Metrics/ParameterLists
|
|
|
|
# rubocop:disable Lint/Eval
|
|
|
|
set_trace_func proc { |event, _file, _line, id, binding, classname|
|
|
|
|
unless classname.to_s.start_with?('Vulcano::Backends::Mock') and
|
|
|
|
event == 'call' and
|
|
|
|
interface_methods[classname.to_s].include?(id)
|
|
|
|
next
|
|
|
|
end
|
2015-09-04 20:54:52 +00:00
|
|
|
# kindly borrowed from the wonderful simple-tracer by matugm
|
2015-09-09 08:08:54 +00:00
|
|
|
arg_names = eval(
|
|
|
|
'method(__method__).parameters.map { |arg| arg[1].to_s }',
|
|
|
|
binding)
|
|
|
|
args = eval("#{arg_names}.map { |arg| eval(arg) }", binding).join(', ')
|
2015-09-04 20:54:52 +00:00
|
|
|
prefix = '-' * (classname.to_s.count(':') - 2) + '> '
|
2015-09-09 08:08:54 +00:00
|
|
|
puts("#{prefix}#{id} #{args}")
|
2015-09-04 20:54:52 +00:00
|
|
|
}
|
2015-09-09 08:08:54 +00:00
|
|
|
# rubocop:enable all
|
2015-09-04 20:54:52 +00:00
|
|
|
end
|
2015-08-27 20:59:15 +00:00
|
|
|
end
|
2015-08-29 23:14:28 +00:00
|
|
|
|
|
|
|
class Mock
|
2015-09-04 20:54:52 +00:00
|
|
|
class File < FileCommon
|
2015-09-09 08:08:54 +00:00
|
|
|
def initialize(_runtime, path)
|
2015-08-29 23:14:28 +00:00
|
|
|
@path = path
|
2015-09-03 15:09:39 +00:00
|
|
|
# mock dataset
|
2015-09-18 10:35:32 +00:00
|
|
|
@exist = (rand < 0.8) ? true : false
|
|
|
|
@is_file = (@exist && rand < 0.7) ? true : false
|
2015-09-06 17:35:19 +00:00
|
|
|
@size = 0
|
|
|
|
@content = ''
|
2015-09-18 10:35:32 +00:00
|
|
|
if @exist && @is_file
|
2015-09-06 17:35:19 +00:00
|
|
|
@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
|
2015-08-29 23:14:28 +00:00
|
|
|
end
|
|
|
|
@content
|
|
|
|
end
|
|
|
|
|
2015-09-18 10:35:32 +00:00
|
|
|
%w{ size content file? exist? }.each do |m|
|
2015-09-09 08:08:54 +00:00
|
|
|
define_method m.to_sym do
|
|
|
|
instance_variable_get(m.sub('?', '').to_sym)
|
|
|
|
end
|
2015-08-29 23:14:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Command
|
2015-09-05 15:39:02 +00:00
|
|
|
attr_accessor :stdout, :stderr, :exit_status
|
2015-09-09 08:08:54 +00:00
|
|
|
def initialize(_runtime, _cmd)
|
2015-09-06 17:35:19 +00:00
|
|
|
@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
|
2015-08-29 23:14:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-08-27 20:59:15 +00:00
|
|
|
end
|