fails loudly with unsupported options

Signed-off-by: Kerry Vance <vancelot@osuosl.org>
This commit is contained in:
Kerry Vance 2019-09-05 11:55:00 -07:00
parent 540515f796
commit 729d9965ed
2 changed files with 43 additions and 3 deletions

View file

@ -41,6 +41,24 @@ The following examples show how to use this Chef InSpec audit resource.
<br>
### Compare content to hostname
describe file('/path/to/some/file') do
its('content') { should match sys_info.hostname }
end
<br>
Options can be passed as arguments to hostname as well.
describe file('/path/to/some/file') do
its('content') { should match sys_info.hostname('full') }
end
<br>
Currently supported arguments to `hostname` on Linux platforms are 'full'|'f'|'fqdn'|'long', 'domain'|'d', 'ip_address'|'i', and 'short'|'s'. Mac currently supports 'full'|'f'|'fqdn'|'long' and 'short'|'s'
## Matchers
For a full list of available matchers, please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).

View file

@ -29,7 +29,8 @@ module Inspec::Resources
# returns the hostname of the local system
def hostname(opt = nil)
os = inspec.os
if os.linux? || os.darwin?
if os.linux?
if !opt.nil?
opt = case opt
when "f", "long", "fqdn", "full"
" -f"
@ -40,9 +41,30 @@ module Inspec::Resources
when "s", "short"
" -s"
else
nil
"ERROR"
end
inspec.command("hostname#{opt}").stdout.chomp
end
if opt == "ERROR"
skip_resource "The `sys_info.hostname` resource is not supported with that option on your OS."
else
inspec.command("hostname#{opt}").stdout.chomp
end
elsif os.darwin?
if !opt.nil?
opt = case opt
when "f", "long", "fqdn", "full"
" -f"
when "s", "short"
" -s"
else
"ERROR"
end
end
if opt == "ERROR"
skip_resource "The `sys_info.hostname` resource is not supported with that option on your OS."
else
inspec.command("hostname#{opt}").stdout.chomp
end
elsif os.windows?
if !opt.nil?
skip_resource "The `sys_info.hostname` resource is not supported with that option on your OS."