Class: Port

Inherits:
Object
  • Object
show all
Defined in:
lib/resources/port.rb

Overview

Usage: describe port(80) do

it { should be_listening }
its('protocol') {should eq 'tcp'}

end

not supported serverspec syntax describe port(80) do

it { should be_listening.with('tcp') }

end

TODO: currently we return local ip only TODO: improve handling of same port on multiple interfaces

Instance Method Summary collapse

Constructor Details

#initialize(ip = nil, port) ⇒ Port

rubocop:disable OptionalArguments



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/resources/port.rb', line 30

def initialize(ip = nil, port) # rubocop:disable OptionalArguments
  @ip = ip
  @port = port
  @port_manager = nil
  @cache = nil
  os = inspec.os
  if os.linux?
    @port_manager = LinuxPorts.new(inspec)
  elsif %w{darwin aix}.include?(os[:family])
    # AIX: see http://www.ibm.com/developerworks/aix/library/au-lsof.html#resources
    #      and https://www-01.ibm.com/marketing/iwm/iwm/web/reg/pick.do?source=aixbp
    # Darwin: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/lsof.8.html
    @port_manager = LsofPorts.new(inspec)
  elsif os.windows?
    @port_manager = WindowsPorts.new(inspec)
  elsif ['freebsd'].include?(os[:family])
    @port_manager = FreeBsdPorts.new(inspec)
  elsif os.solaris?
    @port_manager = SolarisPorts.new(inspec)
  else
    return skip_resource 'The `port` resource is not supported on your OS yet.'
  end
end

Instance Method Details

#listening?(_protocol = nil, _local_address = nil) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/resources/port.rb', line 54

def listening?(_protocol = nil, _local_address = nil)
  info.size > 0
end

#pidsObject



68
69
70
71
# File 'lib/resources/port.rb', line 68

def pids
  res = info.map { |x| x[:pid] }.uniq.compact
  res.size > 0 ? res : nil
end

#processesObject



63
64
65
66
# File 'lib/resources/port.rb', line 63

def processes
  res = info.map { |x| x[:process] }.uniq.compact
  res.size > 0 ? res : nil
end

#protocolsObject



58
59
60
61
# File 'lib/resources/port.rb', line 58

def protocols
  res = info.map { |x| x[:protocol] }.uniq.compact
  res.size > 0 ? res : nil
end

#to_sObject



73
74
75
# File 'lib/resources/port.rb', line 73

def to_s
  "Port  #{@port}"
end