Skip to content

Commit

Permalink
Add hard disable for cache option and remove all cache from debug shell.
Browse files Browse the repository at this point in the history
Signed-off-by: Jared Quick <jquick@chef.io>
  • Loading branch information
jquick committed Nov 27, 2017
1 parent c75ab1b commit 33c66f9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
21 changes: 11 additions & 10 deletions lib/inspec/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def inspect
#
# @param [Hash] config for the transport backend
# @return [TransportBackend] enriched transport instance
def self.create(config)
def self.create(config) # rubocop:disable Metrics/AbcSize
conf = Train.target_config(config)
name = Train.validate_backend(conf)
transport = Train.create(name, conf)
Expand All @@ -53,19 +53,20 @@ def self.create(config)
raise "Can't connect to transport backend '#{name}'."
end

# set caching settings
if config[:backend_cache]
connection.enable_cache(:command)
elsif config[:debug_shell]
connection.disable_cache(:file)
connection.disable_cache(:command)
elsif !config[:backend_cache] && config[:backend] != :mock
connection.disable_cache(:command)
end

cls = Class.new do
attr_accessor :cache_resources
include Base

def initialize
@cache_resources = false
end

define_method :backend do
if @cache_resources
connection.enable_cache(:file)
connection.enable_cache(:command)
end
connection
end

Expand Down
4 changes: 2 additions & 2 deletions lib/inspec/base_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def self.target_options
desc: 'Choose a backend: local, ssh, winrm, docker.'
option :host, type: :string,
desc: 'Specify a remote host which is tested.'
option :backend_cache, type: :boolean, default: false,
desc: 'Allow caching for backend file and command calls.'
option :port, aliases: :p, type: :numeric,
desc: 'Specify the login port for a remote scan.'
option :user, type: :string,
Expand Down Expand Up @@ -69,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 @@ -190,6 +190,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
3 changes: 0 additions & 3 deletions lib/inspec/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ def initialize(conf = {})

load_attributes(@conf)
configure_transport

# set caching on backend
@backend.cache_resources = @conf[:backend_cache] || false
end

def tests
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
6 changes: 3 additions & 3 deletions test/unit/runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
opts = { backend_cache: true }
runner = Inspec::Runner.new(opts)
backend = runner.instance_variable_get(:@backend)
backend.cache_resources.must_equal true
backend.backend.cache_enabled?(:command).must_equal true
end
end

Expand All @@ -25,12 +25,12 @@
opts = { backend_cache: false }
runner = Inspec::Runner.new(opts)
backend = runner.instance_variable_get(:@backend)
backend.cache_resources.must_equal false
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.cache_resources.must_equal false
backend.backend.cache_enabled?(:command).must_equal false
end
end

Expand Down

0 comments on commit 33c66f9

Please sign in to comment.