2017-12-06 21:22:11 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2017, Chef Software Inc.
|
|
|
|
|
|
|
|
require 'helper'
|
|
|
|
require 'thor'
|
|
|
|
|
|
|
|
describe 'BaseCLI' do
|
|
|
|
let(:cli) { Inspec::BaseCLI.new }
|
|
|
|
|
2018-08-23 17:47:26 +00:00
|
|
|
describe 'opts' do
|
|
|
|
it 'raises if `--password/--sudo-password` are used without value' do
|
|
|
|
default_options = { mock: { sudo_password: -1 } }
|
|
|
|
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
|
|
|
|
|
|
|
|
e = proc { cli.send(:opts, :mock) }.must_raise(ArgumentError)
|
2018-10-15 22:25:27 +00:00
|
|
|
e.message.must_match(/Please provide a value for --sudo-password/)
|
2018-08-23 17:47:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'assumes `--sudo` if `--sudo-password` is used without it' do
|
|
|
|
default_options = { mock: { sudo_password: 'p@ssw0rd' } }
|
|
|
|
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
|
|
|
|
|
|
|
|
opts = {}
|
|
|
|
out, err = capture_io do
|
|
|
|
cli.send(:opts, :mock)[:sudo].must_equal true
|
|
|
|
end
|
2018-10-15 22:25:27 +00:00
|
|
|
err.must_match /WARN: `--sudo-password` used without `--sudo`/
|
2018-08-23 17:47:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls `Compliance::API.login` if `opts[:compliance] is passed`' do
|
|
|
|
default_options = { mock: { compliance: 'mock' } }
|
|
|
|
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
|
|
|
|
|
2018-10-04 18:31:39 +00:00
|
|
|
InspecPlugins::Compliance::API.expects(:login).with('mock')
|
2018-08-23 17:47:26 +00:00
|
|
|
|
|
|
|
cli.send(:opts, :mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-06 21:22:11 +00:00
|
|
|
describe 'merge_options' do
|
2018-02-09 16:46:09 +00:00
|
|
|
let(:default_options) do
|
|
|
|
{ exec: { 'reporter' => ['json'], 'backend_cache' => false }}
|
|
|
|
end
|
|
|
|
|
2017-12-06 21:22:11 +00:00
|
|
|
it 'cli defaults populate correctly' do
|
|
|
|
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
|
|
|
|
|
2017-12-07 12:19:36 +00:00
|
|
|
opts = cli.send(:merged_opts, :exec)
|
2018-04-05 12:51:51 +00:00
|
|
|
expected = {"backend_cache"=>false, "reporter"=>{"json"=>{"stdout"=>true}}, "type"=>:exec}
|
2017-12-06 21:22:11 +00:00
|
|
|
opts.must_equal expected
|
|
|
|
end
|
|
|
|
|
2018-02-20 12:37:23 +00:00
|
|
|
it 'verify platform detect' do
|
|
|
|
hash = { name: 'test-os', families: 'aws, cloud', release: 'aws-sdk-v1' }
|
|
|
|
expect = <<EOF
|
|
|
|
Name: \e[1m\e[35mtest-os\e[0m
|
|
|
|
Families: \e[1m\e[35maws, cloud\e[0m
|
|
|
|
Release: \e[1m\e[35maws-sdk-v1\e[0m
|
|
|
|
EOF
|
|
|
|
_(Inspec::BaseCLI.detect(params: hash, indent: 2, color: 35)).must_equal expect
|
|
|
|
end
|
|
|
|
|
2017-12-06 21:22:11 +00:00
|
|
|
it 'json-config options override cli defaults' do
|
|
|
|
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
|
|
|
|
|
|
|
|
parsed_json = { 'backend_cache' => true }
|
|
|
|
cli.expects(:options_json).returns(parsed_json)
|
|
|
|
|
2017-12-07 12:19:36 +00:00
|
|
|
opts = cli.send(:merged_opts, :exec)
|
2018-04-05 12:51:51 +00:00
|
|
|
expected = {"backend_cache"=>true, "reporter"=>{"json"=>{"stdout"=>true}}, "type"=>:exec}
|
2017-12-06 21:22:11 +00:00
|
|
|
opts.must_equal expected
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cli options override json-config and default' do
|
|
|
|
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
|
|
|
|
|
|
|
|
parsed_json = { 'backend_cache' => false }
|
|
|
|
cli.expects(:options_json).returns(parsed_json)
|
|
|
|
|
|
|
|
cli_options = { 'backend_cache' => true }
|
|
|
|
cli.instance_variable_set(:@options, cli_options)
|
|
|
|
|
2017-12-07 12:19:36 +00:00
|
|
|
opts = cli.send(:merged_opts, :exec)
|
2018-04-05 12:51:51 +00:00
|
|
|
expected = {"backend_cache"=>true, "reporter"=>{"json"=>{"stdout"=>true}}, "type"=>:exec}
|
2017-12-06 21:22:11 +00:00
|
|
|
opts.must_equal expected
|
|
|
|
end
|
2017-12-07 12:19:36 +00:00
|
|
|
|
|
|
|
it 'make sure shell does not get exec defaults' do
|
|
|
|
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
|
|
|
|
|
|
|
|
opts = cli.send(:merged_opts)
|
|
|
|
expected = {}
|
|
|
|
opts.must_equal expected
|
|
|
|
end
|
2018-02-09 16:46:09 +00:00
|
|
|
|
2018-10-15 22:25:27 +00:00
|
|
|
it 'make sure default reporter is overriden by json-config reporter' do
|
2018-02-09 16:46:09 +00:00
|
|
|
default_options['reporter'] = ['cli']
|
|
|
|
Inspec::BaseCLI.stubs(:default_options).returns(default_options)
|
2018-10-15 22:25:27 +00:00
|
|
|
|
|
|
|
parsed_json = { 'reporter' => ['json'] }
|
2018-02-09 16:46:09 +00:00
|
|
|
cli.expects(:options_json).returns(parsed_json)
|
|
|
|
|
|
|
|
opts = cli.send(:merged_opts, :exec)
|
2018-04-05 12:51:51 +00:00
|
|
|
expected = {"backend_cache"=>false, "reporter"=>{"json"=>{"stdout"=>true}}, "type"=>:exec}
|
2018-02-09 16:46:09 +00:00
|
|
|
opts.must_equal expected
|
|
|
|
end
|
2017-12-06 21:22:11 +00:00
|
|
|
end
|
2018-02-08 09:06:58 +00:00
|
|
|
|
2018-02-09 14:14:40 +00:00
|
|
|
describe 'configure_logger' do
|
|
|
|
let(:options) do
|
|
|
|
o = {
|
|
|
|
'log_location' => STDERR,
|
|
|
|
'log_level' => 'debug',
|
2018-02-09 16:19:52 +00:00
|
|
|
'reporter' => {
|
|
|
|
'json' => {
|
|
|
|
'stdout' => true,
|
|
|
|
},
|
|
|
|
},
|
2018-02-09 14:14:40 +00:00
|
|
|
}
|
|
|
|
Thor::CoreExt::HashWithIndifferentAccess.new(o)
|
|
|
|
end
|
2018-02-09 16:19:52 +00:00
|
|
|
let(:format) do
|
|
|
|
device = options[:logger].instance_variable_get(:"@logdev")
|
|
|
|
device.instance_variable_get(:"@dev")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets to stderr for log_location' do
|
|
|
|
cli.send(:configure_logger, options)
|
|
|
|
format.must_equal STDERR
|
|
|
|
end
|
2018-02-09 14:14:40 +00:00
|
|
|
|
2018-02-09 16:46:09 +00:00
|
|
|
it 'sets to stderr for json' do
|
2018-02-09 16:19:52 +00:00
|
|
|
options.delete('log_location')
|
|
|
|
options.delete('log_level')
|
|
|
|
cli.send(:configure_logger, options)
|
2018-02-09 16:46:09 +00:00
|
|
|
format.must_equal STDERR
|
2018-02-09 16:19:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets defaults to stdout for everything else' do
|
|
|
|
options.delete('log_location')
|
|
|
|
options.delete('log_level')
|
|
|
|
options.delete('reporter')
|
|
|
|
|
2018-02-09 14:14:40 +00:00
|
|
|
cli.send(:configure_logger, options)
|
2018-02-09 16:19:52 +00:00
|
|
|
format.must_equal STDOUT
|
2018-02-09 14:14:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-08 09:06:58 +00:00
|
|
|
describe 'parse_reporters' do
|
|
|
|
it 'parse cli reporters' do
|
|
|
|
opts = { 'reporter' => ['cli'] }
|
|
|
|
parsed = Inspec::BaseCLI.parse_reporters(opts)
|
2018-09-05 19:07:34 +00:00
|
|
|
expected_value = { 'reporter' => { 'cli' => { 'stdout' => true }}}
|
|
|
|
parsed.must_equal expected_value
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses cli report and attaches target_id' do
|
|
|
|
opts = { 'reporter' => ['cli'], 'target_id' => '1d3e399f-4d71-4863-ac54-84d437fbc444' }
|
|
|
|
parsed = Inspec::BaseCLI.parse_reporters(opts)
|
|
|
|
expected_value = {"reporter"=>{"cli"=>{"stdout"=>true, "target_id"=>"1d3e399f-4d71-4863-ac54-84d437fbc444"}}, "target_id"=>"1d3e399f-4d71-4863-ac54-84d437fbc444"}
|
|
|
|
parsed.must_equal expected_value
|
2018-02-08 09:06:58 +00:00
|
|
|
end
|
2018-10-15 22:25:27 +00:00
|
|
|
end
|
2018-02-08 09:06:58 +00:00
|
|
|
|
|
|
|
describe 'validate_reporters' do
|
|
|
|
it 'valid reporter' do
|
|
|
|
stdout = { 'stdout' => true }
|
|
|
|
reporters = { 'json' => stdout }
|
|
|
|
Inspec::BaseCLI.validate_reporters(reporters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'invalid reporter type' do
|
|
|
|
reporters = ['json', 'magenta']
|
|
|
|
proc { Inspec::BaseCLI.validate_reporters(reporters) }.must_raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'two reporters outputting to stdout' do
|
|
|
|
stdout = { 'stdout' => true }
|
|
|
|
reporters = { 'json' => stdout, 'cli' => stdout }
|
|
|
|
proc { Inspec::BaseCLI.validate_reporters(reporters) }.must_raise ArgumentError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'suppress_log_output?' do
|
|
|
|
it 'suppresses json' do
|
|
|
|
opts = { 'reporter' => { 'json' => { 'stdout' => true }}}
|
|
|
|
cli.send(:suppress_log_output?, opts).must_equal true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'do not suppresses json-min when going to file' do
|
|
|
|
opts = { 'reporter' => { 'json-min' => { 'file' => '/tmp/json' }}}
|
|
|
|
cli.send(:suppress_log_output?, opts).must_equal false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'suppresses json-rspec' do
|
|
|
|
opts = { 'reporter' => { 'json-rspec' => { 'stdout' => true }}}
|
|
|
|
cli.send(:suppress_log_output?, opts).must_equal true
|
|
|
|
end
|
|
|
|
|
2018-08-23 17:43:48 +00:00
|
|
|
it 'suppresses json-automate' do
|
|
|
|
opts = { 'reporter' => { 'json-automate' => { 'stdout' => true }}}
|
|
|
|
cli.send(:suppress_log_output?, opts).must_equal true
|
|
|
|
end
|
|
|
|
|
2018-02-08 09:06:58 +00:00
|
|
|
it 'suppresses junit' do
|
|
|
|
opts = { 'reporter' => { 'junit' => { 'stdout' => true }}}
|
|
|
|
cli.send(:suppress_log_output?, opts).must_equal true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'do not suppresses cli' do
|
|
|
|
opts = { 'reporter' => { 'cli' => nil } }
|
|
|
|
cli.send(:suppress_log_output?, opts).must_equal false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'do not suppresses cli' do
|
|
|
|
opts = { 'reporter' => { 'cli' => nil, 'json' => {'file' => '/tmp/json' }}}
|
|
|
|
cli.send(:suppress_log_output?, opts).must_equal false
|
|
|
|
end
|
|
|
|
end
|
2017-12-06 21:22:11 +00:00
|
|
|
end
|