File: port.rb

package info (click to toggle)
ruby-serverspec 2.42.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 980 kB
  • sloc: ruby: 4,835; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 1,043 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'resolv'

module Serverspec::Type
  class Port < Base
    def protocols
      %w(udp tcp tcp6 udp6)
    end

    def options
      @options ||= {}
    end

    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

    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

    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

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