Skip to content

Commit

Permalink
Merge pull request #436 from Aigeruth/feature/414-be_listening_local_…
Browse files Browse the repository at this point in the history
…address_support

Add local address matching support to be_listening.
  • Loading branch information
mizzy committed Jul 5, 2014
2 parents a5d3efa + b16dfd9 commit d687ce7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
8 changes: 6 additions & 2 deletions lib/serverspec/matchers/be_listening.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
RSpec::Matchers.define :be_listening do
match do |port|
port.listening?(@with)
port.listening? @with, @local_address
end

chain :with do |with|
@with = with
@with = with
end

chain :on do |local_address|
@local_address = local_address
end
end
40 changes: 32 additions & 8 deletions lib/serverspec/type/port.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
require 'resolv'

module Serverspec
module Type
class Port < Base
def listening?(protocol)
if protocol
protocol = protocol.to_s.downcase
unless ["udp", "tcp", "tcp6", "udp6"].include?(protocol)
raise ArgumentError.new("`be_listening` matcher doesn't support #{protocol}")
end
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

@runner.check_listening_with_protocol(@name, protocol)
def local_address_matcher(local_address)
if valid_ip_address?(local_address)
options[:local_address] = local_address
else
@runner.check_listening(@name)
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_listening(@name, options)
end

def valid_ip_address?(ip_address)
!!(ip_address =~ Resolv::IPv4::Regex) || !!(ip_address =~ Resolv::IPv6::Regex)
end
end
end
end
42 changes: 24 additions & 18 deletions spec/type/port_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@

include Specinfra::Helper::RedHat

describe port(80) do
it { should be_listening }
end
describe Serverspec::Type::Port do
describe port(80) do
it { should be_listening }

describe port('invalid') do
it { should_not be_listening }
end
it('protocol: tcp') { should be_listening.with('tcp') }

describe port(80) do
it { should be_listening.with("tcp") }
end
it 'invalid protocol raises error' do
expect {
should be_listening.with('not implemented')
}.to raise_error(ArgumentError, %r/\A`be_listening` matcher doesn\'t support/)
end

describe port(123) do
it { should be_listening.with("udp") }
end
it('on: 127.0.0.1') do
should be_listening.on('127.0.0.1')
end

it 'invalid local address raises error' do
expect{ should be_listening.on('') }.to raise_error(ArgumentError)
end
end

describe port('invalid') do
it { should_not be_listening }
end

describe port(80) do
it {
expect {
should be_listening.with('not implemented')
}.to raise_error(ArgumentError, %r/\A`be_listening` matcher doesn\'t support/)
}
describe port(123) do
it { should be_listening.with("udp") }
end
end

0 comments on commit d687ce7

Please sign in to comment.