Sha256: 9a821d869a62c41e31820d6c90f02e216ef0bce62b0b33e5dd141f5adbc3a1b2

Contents?: true

Size: 1.09 KB

Versions: 11

Compression:

Stored size: 1.09 KB

Contents

require 'resolv'

module Serverspec
  module 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
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
serverspec-2.0.0.beta19 lib/serverspec/type/port.rb
serverspec-2.0.0.beta18 lib/serverspec/type/port.rb
serverspec-2.0.0.beta17 lib/serverspec/type/port.rb
serverspec-2.0.0.beta16 lib/serverspec/type/port.rb
serverspec-2.0.0.beta15 lib/serverspec/type/port.rb
serverspec-2.0.0.beta14 lib/serverspec/type/port.rb
serverspec-2.0.0.beta13 lib/serverspec/type/port.rb
serverspec-2.0.0.beta12 lib/serverspec/type/port.rb
serverspec-2.0.0.beta11 lib/serverspec/type/port.rb
serverspec-2.0.0.beta10 lib/serverspec/type/port.rb
serverspec-2.0.0.beta9 lib/serverspec/type/port.rb