Merge pull request #4558 from inspec/mj/4517

Add name property to interface resource
This commit is contained in:
Miah Johnson 2019-10-03 16:24:58 -07:00 committed by GitHub
commit 302abaaa85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 19 deletions

View file

@ -17,38 +17,34 @@ module Inspec::Resources
its('ipv6_cidrs') { should include '::1/128' }
end
EXAMPLE
def initialize(iface)
@iface = iface
@interface_provider = nil
if inspec.os.linux?
@interface_provider = LinuxInterface.new(inspec)
elsif inspec.os.windows?
@interface_provider = WindowsInterface.new(inspec)
else
return skip_resource "The `interface` resource is not supported on your OS yet."
end
end
def exists?
!interface_info.nil? && !interface_info[:name].nil?
!!(interface_info && interface_info[:name])
end
def up?
interface_info.nil? ? false : interface_info[:up]
!!(interface_info && interface_info[:up])
end
def name
interface_info[:name]
end
# returns link speed in Mbits/sec
def speed
interface_info.nil? ? nil : interface_info[:speed]
interface_info && interface_info[:speed]
end
def ipv4_address?
!ipv4_addresses.nil? && !ipv4_addresses.empty?
ipv4_addresses && !ipv4_addresses.empty?
end
def ipv6_address?
!ipv6_addresses.nil? && !ipv6_addresses.empty?
ipv6_addresses && !ipv6_addresses.empty?
end
def ipv4_addresses
@ -72,11 +68,11 @@ module Inspec::Resources
end
def ipv4_cidrs
interface_info.nil? ? [] : interface_info[:ipv4_addresses]
interface_info && Array(interface_info[:ipv4_addresses])
end
def ipv6_cidrs
interface_info.nil? ? [] : interface_info[:ipv6_addresses]
interface_info && Array(interface_info[:ipv6_addresses])
end
def to_s
@ -86,9 +82,11 @@ module Inspec::Resources
private
def interface_info
return @cache if defined?(@cache)
@cache = @interface_provider.interface_info(@iface) unless @interface_provider.nil?
@cache ||= begin
provider = LinuxInterface.new(inspec) if inspec.os.linux?
provider = WindowsInterface.new(inspec) if inspec.os.windows?
Hash(provider && provider.interface_info(@iface))
end
end
end

View file

@ -10,6 +10,7 @@ describe "Inspec::Resources::Interface" do
_(resource.exists?).must_equal true
_(resource.up?).must_equal true
_(resource.speed).must_equal 10000
_(resource.name).must_equal "eth0"
_(resource.ipv4_cidrs).must_include "127.0.0.1/8"
_(resource.ipv4_addresses).must_include "127.0.0.1"
_(resource.ipv4_addresses_netmask).must_include "127.0.0.1/255.0.0.0"
@ -23,6 +24,7 @@ describe "Inspec::Resources::Interface" do
resource = MockLoader.new(:ubuntu1404).load_resource("interface", "eth1")
_(resource.exists?).must_equal false
_(resource.up?).must_equal false
_(resource.name).must_be_nil
_(resource.speed).must_be_nil
_(resource.ipv4_cidrs).must_be_empty
_(resource.ipv4_addresses).must_be_empty
@ -38,6 +40,7 @@ describe "Inspec::Resources::Interface" do
resource = MockLoader.new(:windows).load_resource("interface", "ethernet0")
_(resource.exists?).must_equal true
_(resource.up?).must_equal false
_(resource.name).must_equal "Ethernet0"
_(resource.speed).must_equal 0
_(resource.ipv4_address?).must_equal false
_(resource.ipv6_address?).must_equal false
@ -52,6 +55,7 @@ describe "Inspec::Resources::Interface" do
resource = MockLoader.new(:windows).load_resource("interface", "vEthernet (Intel(R) PRO 1000 MT Network Connection - Virtual Switch)")
_(resource.exists?).must_equal true
_(resource.up?).must_equal true
_(resource.name).must_equal "vEthernet (Intel(R) PRO 1000 MT Network Connection - Virtual Switch)"
_(resource.speed).must_equal 10000000
_(resource.ipv4_cidrs).must_include "127.0.0.1/8"
_(resource.ipv4_addresses).must_include "127.0.0.1"
@ -66,6 +70,7 @@ describe "Inspec::Resources::Interface" do
resource = MockLoader.new(:windows).load_resource("interface", "eth1")
_(resource.exists?).must_equal false
_(resource.up?).must_equal false
_(resource.name).must_be_nil
_(resource.speed).must_be_nil
_(resource.ipv4_address?).must_equal false
_(resource.ipv6_address?).must_equal false
@ -81,6 +86,7 @@ describe "Inspec::Resources::Interface" do
resource = MockLoader.new(:undefined).load_resource("interface", "eth0")
_(resource.exists?).must_equal false
_(resource.up?).must_equal false
_(resource.name).must_be_nil
_(resource.speed).must_be_nil
end