mirror of
https://github.com/inspec/inspec
synced 2024-11-27 07:00:39 +00:00
improvement: extend mock to support simulated cmds
This commit is contained in:
parent
cdf15b9dd1
commit
f0ac64cf31
1 changed files with 13 additions and 7 deletions
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue