Merge pull request #4388 from Vancelot11/vancelot/sys_info.hostname

Expand sys_info resource functionality
This commit is contained in:
Clinton Wolfe 2019-09-16 20:46:25 -04:00 committed by GitHub
commit f1f5840fa7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 4 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/).
@ -51,6 +69,31 @@ The `hostname` matcher tests the host for which standard output is returned:
its('hostname') { should eq 'value' }
### fqdn
The `fqdn` property tests the 'fully qualified domain name' of the system:
its('fqdn') { should eq 'value' }
### domain
The `domain` property tests the name of the DNS domain:
its('domain') { should eq 'value' }
### ip-address
The `ip-address` property tests all network addresses of the host:
its('ip-address') { should eq 'value' }
### short
The `short` property tests the host name cut at the first dot:
its('short') { should eq 'value' }
### manufacturer
The `manufacturer` matcher tests the host for which standard output is returned:

View file

@ -13,20 +13,77 @@ module Inspec::Resources
describe sys_info do
its('hostname') { should eq 'example.com' }
end
describe sys_info do
its('fqdn') { should eq 'user.example.com' }
end
EXAMPLE
%w{ domain fqdn ip_address short }.each do |opt|
define_method(opt.to_sym) do
hostname(opt)
end
end
# returns the hostname of the local system
def hostname
def hostname(opt = nil)
os = inspec.os
if os.linux? || os.darwin?
inspec.command("hostname").stdout.chomp
if os.linux?
linux_hostname(opt)
elsif os.darwin?
mac_hostname(opt)
elsif os.windows?
inspec.powershell("$env:computername").stdout.chomp
if !opt.nil?
skip_resource "The `sys_info.hostname` resource is not supported with that option on your OS."
else
inspec.powershell("$env:computername").stdout.chomp
end
else
skip_resource "The `sys_info.hostname` resource is not supported on your OS yet."
end
end
def linux_hostname(opt = nil)
if !opt.nil?
opt = case opt
when "f", "long", "fqdn", "full"
" -f"
when "d", "domain"
" -d"
when "i", "ip_address"
" -I"
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
end
def mac_hostname(opt = nil)
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
end
# returns the Manufacturer of the local system
def manufacturer
os = inspec.os