2019-10-05 00:31:50 +00:00
|
|
|
class Minitest::Test
|
|
|
|
##
|
|
|
|
# This creates a real resource with default config/backend.
|
|
|
|
#
|
|
|
|
# Use this whenever possible. Let's phase out the MockLoader pain.
|
|
|
|
|
|
|
|
def quick_resource(name, platform = :linux, *args, &block)
|
|
|
|
backend = Inspec::Backend.create(Inspec::Config.new)
|
|
|
|
backend.extend Fake::Backend
|
|
|
|
|
|
|
|
os = MockLoader::OPERATING_SYSTEMS[platform]
|
|
|
|
raise "Unknown platform: %p" % [platform] unless os
|
|
|
|
|
|
|
|
# mock.mock_os(@platform)
|
|
|
|
platform = Train::Platforms.name(os[:name])
|
|
|
|
platform.find_family_hierarchy # TODO: remove? UGH! adds platform=
|
|
|
|
platform.platform = os
|
2019-10-23 23:49:49 +00:00
|
|
|
platform.add_platform_methods
|
2019-10-05 00:31:50 +00:00
|
|
|
# TODO: this should have a setter
|
|
|
|
# TODO: backend.backend is the WORST name
|
|
|
|
backend.backend.instance_variable_set :@platform, platform
|
|
|
|
# end mock.mock_os
|
|
|
|
|
2019-10-23 23:49:49 +00:00
|
|
|
klass = Inspec::Resource.registry[name.to_s]
|
2019-10-05 00:31:50 +00:00
|
|
|
|
|
|
|
instance = klass.new(backend, name, *args)
|
|
|
|
instance.extend Fake::Resource
|
|
|
|
instance.mock_command(&block) if block
|
|
|
|
instance
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Fake
|
|
|
|
Command = Struct.new(:stdout, :stderr, :exit_status)
|
|
|
|
|
|
|
|
module Backend
|
2019-10-23 23:49:49 +00:00
|
|
|
def string(str)
|
|
|
|
::Fake::Command.new(str, "", 0)
|
|
|
|
end
|
|
|
|
|
2019-10-05 00:31:50 +00:00
|
|
|
def stdout_file(path)
|
|
|
|
result(path, nil, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stderr_file(path)
|
|
|
|
result(nil, path, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def result(stdout_path, stderr_path, exit)
|
|
|
|
stdout = stdout_path ? File.read(stdout_path) : ""
|
|
|
|
stderr = stderr_path ? File.read(stderr_path) : ""
|
|
|
|
|
2022-03-22 07:37:28 +00:00
|
|
|
exit_code = exit || 0
|
|
|
|
::Fake::Command.new(stdout, stderr, exit_code)
|
2019-10-05 00:31:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Resource
|
|
|
|
def mock_command(&block)
|
|
|
|
inspec.define_singleton_method :command, &block
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|