inspec/lib/vulcano/backend/mock.rb
Dominik Richter 32a6f01d1f simplify mock backend association
Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
2015-09-22 01:23:08 +02:00

72 lines
1.4 KiB
Ruby

# encoding: utf-8
module Vulcano::Backends
class Mock < Vulcano.backend(1)
name 'mock'
attr_accessor :files, :commands
def initialize(conf)
@conf = conf
@files = {}
@commands = {}
end
def file(path)
@files[path] ||= File.new(self, path)
end
def run_command(cmd)
@command[cmd] || Command.new(self, cmd)
end
def to_s
'Mock Backend Runner'
end
end
class Mock
class File
def initialize(runtime, path)
@path = path
# mock dataset
@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
@size
end
def content
@content
end
def file?
@is_file
end
def exists?
@exists
end
end
class Command
attr_accessor :stdout, :stderr, :exit_status
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