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



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

def initialize(ip = nil, port) # rubocop:disable OptionalArguments
  @ip = ip
  @port = port
  @port_manager = nil
  @cache = nil

  case inspec.os[:family]
  when 'ubuntu', 'debian', 'redhat', 'fedora', 'centos', 'arch', 'wrlinux'
    @port_manager = LinuxPorts.new(inspec)
  when 'darwin', 'aix'
    # 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)
  when 'windows'
    @port_manager = WindowsPorts.new(inspec)
  when 'freebsd'
    @port_manager = FreeBsdPorts.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)


51
52
53
# File 'lib/resources/port.rb', line 51

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

#pidsObject



65
66
67
68
# File 'lib/resources/port.rb', line 65

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

#processesObject



60
61
62
63
# File 'lib/resources/port.rb', line 60

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

#protocolsObject



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

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

#to_sObject



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

def to_s
  "Port  #{@port}"
end