Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable caching for backend calls #2309

Merged
merged 9 commits into from
Dec 4, 2017
2 changes: 1 addition & 1 deletion inspec.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 2.3'

spec.add_dependency 'train', '~> 0.29', '>= 0.29.2'
spec.add_dependency 'train', '~> 0.30'
spec.add_dependency 'thor', '~> 0.19'
spec.add_dependency 'json', '>= 1.8', '< 3.0'
spec.add_dependency 'rainbow', '~> 2'
Expand Down
12 changes: 12 additions & 0 deletions lib/inspec/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ def self.create(config)
raise "Can't connect to transport backend '#{name}'."
end

# Set caching settings. We always want to enable caching for
# the Mock transport for testing.
if config[:backend_cache] || config[:backend] == :mock
connection.enable_cache(:file)
connection.enable_cache(:command)
elsif config[:debug_shell]
connection.disable_cache(:file)
connection.disable_cache(:command)
else
connection.disable_cache(:command)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we explicitly enable file caching? so we do not depend on train defaults?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

end

cls = Class.new do
include Base

Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/base_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def self.exec_options
desc: 'Use the given path for caching dependencies. (default: ~/.inspec/cache)'
option :create_lockfile, type: :boolean, default: true,
desc: 'Write out a lockfile based on this execution (unless one already exists)'
option :backend_cache, type: :boolean, default: false,
desc: 'Allow caching for backend command output.'
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/inspec/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def detect
def shell_func
diagnose
o = opts.dup
o[:debug_shell] = true

json_output = ['json', 'json-min'].include?(opts['format'])
log_device = json_output ? nil : STDOUT
Expand Down
10 changes: 10 additions & 0 deletions test/functional/inspec_shell_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ def do_shell_c(code, exit_status, json = false, stderr = '')
out
end

it 'confirm file caching is disabled' do
out = do_shell_c('inspec.backend.cache_enabled?(:file)', 0)
out.stdout.chop.must_equal 'false'
end

it 'confirm command caching is disabled' do
out = do_shell_c('inspec.backend.cache_enabled?(:command)', 0)
out.stdout.chop.must_equal 'false'
end

it 'can run ruby expressions (json output)' do
x = rand
y = rand
Expand Down
23 changes: 23 additions & 0 deletions test/unit/runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@
Inspec::Runner.any_instance.stubs(:validate_attributes_file_readability!)
end

describe 'when backend caching is enabled' do
it 'returns a backend with caching' do
opts = { backend_cache: true }
runner = Inspec::Runner.new(opts)
backend = runner.instance_variable_get(:@backend)
backend.backend.cache_enabled?(:command).must_equal true
end
end

describe 'when backend caching is disabled' do
it 'returns a backend without caching' do
opts = { backend_cache: false }
runner = Inspec::Runner.new(opts)
backend = runner.instance_variable_get(:@backend)
backend.backend.cache_enabled?(:command).must_equal false
end

it 'returns a backend without caching as default' do
backend = runner.instance_variable_get(:@backend)
backend.backend.cache_enabled?(:command).must_equal false
end
end

describe 'when no attrs are specified' do
it 'returns an empty hash' do
options = {}
Expand Down