Class: Serverspec::Type::Port

Inherits:
Base
  • Object
show all
Defined in:
lib/serverspec/type/port.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#initialize, #inspect, #to_ary, #to_s

Constructor Details

This class inherits a constructor from Serverspec::Type::Base

Instance Method Details

#listening?(protocol, local_address) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/serverspec/type/port.rb', line 30

def listening?(protocol, local_address)
  protocol_matcher(protocol) if protocol
  local_address_matcher(local_address) if local_address
  @runner.check_port_is_listening(@name, options)
end

#local_address_matcher(local_address) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/serverspec/type/port.rb', line 22

def local_address_matcher(local_address)
  if valid_ip_address?(local_address)
    options[:local_address] = local_address
  else
    raise ArgumentError.new("`be_listening` matcher requires valid IPv4 or IPv6 address")
  end
end

#optionsObject



9
10
11
# File 'lib/serverspec/type/port.rb', line 9

def options
  @options ||= {}
end

#protocol_matcher(protocol) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/serverspec/type/port.rb', line 13

def protocol_matcher(protocol)
  protocol = protocol.to_s.downcase
  if protocols.include?(protocol)
    options[:protocol] = protocol
  else
    raise ArgumentError.new("`be_listening` matcher doesn't support #{protocol}")
  end
end

#protocolsObject



5
6
7
# File 'lib/serverspec/type/port.rb', line 5

def protocols
  %w(udp tcp tcp6 udp6)
end

#valid_ip_address?(ip_address) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/serverspec/type/port.rb', line 36

def valid_ip_address?(ip_address)
  !!(ip_address =~ Resolv::IPv4::Regex) || !!(ip_address =~ Resolv::IPv6::Regex)
end