mirror of
https://github.com/inspec/inspec
synced 2024-11-14 17:07:09 +00:00
Merge pull request #1035 from chef/chris-rock/sys_info
add sys_info resource to get information about the hostname
This commit is contained in:
commit
e44a3802dd
6 changed files with 58 additions and 0 deletions
|
@ -106,6 +106,7 @@ require 'resources/service'
|
|||
require 'resources/shadow'
|
||||
require 'resources/ssl'
|
||||
require 'resources/ssh_conf'
|
||||
require 'resources/sys_info'
|
||||
require 'resources/users'
|
||||
require 'resources/vbscript'
|
||||
require 'resources/windows_feature'
|
||||
|
|
26
lib/resources/sys_info.rb
Normal file
26
lib/resources/sys_info.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
# encoding: utf-8
|
||||
module Inspec::Resources
|
||||
# this resource returns additional system informatio
|
||||
class System < Inspec.resource(1)
|
||||
name 'sys_info'
|
||||
|
||||
desc 'Use the user InSpec system resource to test for operating system properties.'
|
||||
example "
|
||||
describe sysinfo do
|
||||
its('hostname') { should eq 'example.com' }
|
||||
end
|
||||
"
|
||||
|
||||
# returns the hostname of the local system
|
||||
def hostname
|
||||
os = inspec.os
|
||||
if os.linux?
|
||||
inspec.command('hostname').stdout.chomp
|
||||
elsif os.windows?
|
||||
inspec.powershell('$env:computername').stdout.chomp
|
||||
else
|
||||
skip_resource 'The `sys_info.hostname` resource is not supported on your OS yet.'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -254,6 +254,10 @@ class MockLoader
|
|||
'netstat -an -f inet' => cmd.call('hpux-netstat-inet'),
|
||||
#ipv6 ports on hpux
|
||||
'netstat -an -f inet6' => cmd.call('hpux-netstat-inet6'),
|
||||
# hostname linux
|
||||
'hostname' => cmd.call('hostname'),
|
||||
# hostname windows
|
||||
'$env:computername' => cmd.call('$env-computername'),
|
||||
}
|
||||
|
||||
@backend
|
||||
|
|
1
test/unit/mock/cmd/$env-computername
Normal file
1
test/unit/mock/cmd/$env-computername
Normal file
|
@ -0,0 +1 @@
|
|||
WIN-CIV7VMLVHLD
|
1
test/unit/mock/cmd/hostname
Normal file
1
test/unit/mock/cmd/hostname
Normal file
|
@ -0,0 +1 @@
|
|||
example.com
|
25
test/unit/resources/sys_info_test.rb
Normal file
25
test/unit/resources/sys_info_test.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
# encoding: utf-8
|
||||
# author: Christoph Hartmann
|
||||
# author: Dominik Richter
|
||||
|
||||
require 'helper'
|
||||
require 'inspec/resource'
|
||||
|
||||
describe 'Inspec::Resources::SysInfo' do
|
||||
describe 'sys_info' do
|
||||
it 'check ssh config parsing for Ubuntu' do
|
||||
resource = MockLoader.new(:ubuntu1504).load_resource('sys_info')
|
||||
_(resource.hostname).must_equal 'example.com'
|
||||
end
|
||||
|
||||
it 'check ssh config parsing for Ubuntu' do
|
||||
resource = MockLoader.new(:windows).load_resource('sys_info')
|
||||
_(resource.hostname).must_equal 'WIN-CIV7VMLVHLD'
|
||||
end
|
||||
|
||||
it 'check ssh config parsing for freebsd' do
|
||||
resource = MockLoader.new(:freebsd10).load_resource('sys_info')
|
||||
_(resource.hostname).must_equal 'The `sys_info.hostname` resource is not supported on your OS yet.'
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue