-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #436 from Aigeruth/feature/414-be_listening_local_…
…address_support Add local address matching support to be_listening.
- Loading branch information
Showing
3 changed files
with
62 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters