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

json.rb produces stacktrace when a target file is unreadable #1382

Closed
makotots opened this issue Dec 29, 2016 · 1 comment
Closed

json.rb produces stacktrace when a target file is unreadable #1382

makotots opened this issue Dec 29, 2016 · 1 comment

Comments

@makotots
Copy link
Contributor

Description

When ini resource tries to open an unreadable file (file mode = 000), InSpec dies with backtrace.

InSpec and Platform Version

InSpec 1.8.0, Debian 7.10

Replication Case

% ls -l /tmp/unreadable
---------- 1 makoto group1 10 Dec 28 21:43 /tmp/unreadable
% cat ../test_unreadable.rb
control test_unreadable do
  describe ini('/tmp/unreadable') do
    its('foo') { should eq 'abc'}
  end
end
% bin/inspec exec ../test_unreadable.rb
/home/makoto/inspec/lib/resources/json.rb:52:in `initialize': undefined method `empty?' for nil:NilClass (NoMethodError)
....
(complete stacktrace below)

Possible Solutions

When a given file is unreadable, Train returns file.content = nil.

Expected result is returned with a patch at #1381:

% bin/inspec exec ../test_unreadable.rb

Target:  local://

  ↺  test_unreadable: INI /tmp/unreadable
     ↺  Can't read file "/tmp/unreadable"

Profile Summary: 0 successful, 0 failures, 1 skipped
Test Summary: 0 successful, 0 failures, 1 skipped
%

Happy to add a unit test if you could guide me to the standard way to mock Train objects.

Stacktrace

% bin/inspec exec ../test_unreadable.rb
/home/makoto/inspec/lib/resources/json.rb:52:in `initialize': undefined method `empty?' for nil:NilClass (NoMethodError)
	from /home/makoto/inspec/lib/inspec/plugins/resource.rb:48:in `initialize'
	from /home/makoto/inspec/lib/inspec/resource.rb:48:in `new'
	from /home/makoto/inspec/lib/inspec/resource.rb:48:in `block (3 levels) in create_dsl'
	from ../test_unreadable.rb:2:in `block in load_with_context'
	from /home/makoto/inspec/lib/inspec/rule.rb:51:in `instance_eval'
	from /home/makoto/inspec/lib/inspec/rule.rb:51:in `initialize'
	from /home/makoto/inspec/lib/inspec/control_eval_context.rb:73:in `new'
	from /home/makoto/inspec/lib/inspec/control_eval_context.rb:73:in `block (2 levels) in create'
	from ../test_unreadable.rb:1:in `load_with_context'
	from /home/makoto/inspec/lib/inspec/profile_context.rb:146:in `instance_eval'
	from /home/makoto/inspec/lib/inspec/profile_context.rb:146:in `load_with_context'
	from /home/makoto/inspec/lib/inspec/profile_context.rb:130:in `load_control_file'
	from /home/makoto/inspec/lib/inspec/profile.rb:144:in `block in collect_tests'
	from /home/makoto/inspec/lib/inspec/profile.rb:141:in `each'
	from /home/makoto/inspec/lib/inspec/profile.rb:141:in `collect_tests'
	from /home/makoto/inspec/lib/inspec/runner.rb:90:in `block in load'
	from /home/makoto/inspec/lib/inspec/runner.rb:79:in `each'
	from /home/makoto/inspec/lib/inspec/runner.rb:79:in `load'
	from /home/makoto/inspec/lib/inspec/runner.rb:100:in `run'
	from /home/makoto/inspec/lib/inspec/base_cli.rb:82:in `run_tests'
	from /home/makoto/inspec/lib/inspec/cli.rb:157:in `exec'
	from /home/makoto/local/lib/ruby/gems/2.2.0/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
	from /home/makoto/local/lib/ruby/gems/2.2.0/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
	from /home/makoto/local/lib/ruby/gems/2.2.0/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'
	from /home/makoto/local/lib/ruby/gems/2.2.0/gems/thor-0.19.4/lib/thor/base.rb:444:in `start'
	from bin/inspec:12:in `<main>'
%
@chris-rock
Copy link
Contributor

@makotots This is a great idea. Are you going to open a PR to fix this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants