Skip to content

Commit

Permalink
Merge pull request #4675 from Watson1978/test-bind
Browse files Browse the repository at this point in the history
tests: fix unused_port
  • Loading branch information
ashie authored Oct 21, 2024
2 parents a2b935a + 0634bd5 commit d1a58fd
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 82 deletions.
2 changes: 1 addition & 1 deletion test/command/test_cat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def setup
@primary = create_primary
metadata = @primary.buffer.new_metadata
@chunk = create_chunk(@primary, metadata, @es)
@port = unused_port
@port = unused_port(protocol: :tcp)
end

def teardown
Expand Down
2 changes: 1 addition & 1 deletion test/command/test_fluentd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ def multi_workers_ready?; true; end
end
end

sub_test_case 'sahred socket options' do
sub_test_case 'shared socket options' do
test 'enable shared socket by default' do
conf = ""
conf_path = create_conf_file('empty.conf', conf)
Expand Down
34 changes: 27 additions & 7 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,31 @@ class Test::Unit::AssertionFailedError < StandardError

include Fluent::Test::Helpers

def unused_port(num = 1, protocol: :tcp, bind: "0.0.0.0")
def unused_port(num = 1, protocol:, bind: "0.0.0.0")
case protocol
when :tcp
when :tcp, :tls
unused_port_tcp(num)
when :udp
unused_port_udp(num, bind: bind)
when :all
unused_port_tcp_udp(num)
else
raise ArgumentError, "unknown protocol: #{protocol}"
end
end

def unused_port_tcp_udp(num = 1)
raise "not support num > 1" if num > 1

# The default maximum number of file descriptors in macOS is 256.
# It might need to set num to a smaller value than that.
tcp_ports = unused_port_tcp(200)
port = unused_port_udp(1, port_list: tcp_ports)
raise "can't find unused port" unless port

port
end

def unused_port_tcp(num = 1)
ports = []
sockets = []
Expand All @@ -90,7 +104,7 @@ def unused_port_tcp(num = 1)
sockets << s
ports << s.addr[1]
end
sockets.each{|s| s.close }
sockets.each(&:close)
if num == 1
return ports.first
else
Expand All @@ -100,21 +114,27 @@ def unused_port_tcp(num = 1)

PORT_RANGE_AVAILABLE = (1024...65535)

def unused_port_udp(num = 1, bind: "0.0.0.0")
def unused_port_udp(num = 1, port_list: [], bind: "0.0.0.0")
family = IPAddr.new(IPSocket.getaddress(bind)).ipv4? ? ::Socket::AF_INET : ::Socket::AF_INET6
ports = []
sockets = []
while ports.size < num
port = rand(PORT_RANGE_AVAILABLE)

use_random_port = port_list.empty?
i = 0
loop do
port = use_random_port ? rand(PORT_RANGE_AVAILABLE) : port_list[i]
u = UDPSocket.new(family)
if (u.bind(bind, port) rescue nil)
ports << port
sockets << u
else
u.close
end
i += 1
break if ports.size >= num
break if !use_random_port && i >= port_list.size
end
sockets.each{|s| s.close }
sockets.each(&:close)
if num == 1
return ports.first
else
Expand Down
3 changes: 2 additions & 1 deletion test/plugin/test_in_forward.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def setup
Fluent::Test.setup
@responses = [] # for testing responses after sending data
@d = nil
@port = unused_port
# forward plugin uses TCP and UDP sockets on the same port number
@port = unused_port(protocol: :all)
end

def teardown
Expand Down
2 changes: 1 addition & 1 deletion test/plugin/test_in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def shutdown

def setup
Fluent::Test.setup
@port = unused_port
@port = unused_port(protocol: :tcp)
end

def teardown
Expand Down
12 changes: 6 additions & 6 deletions test/plugin/test_in_monitor_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def test_enable_input_metrics(with_config)
end

test "emit" do
port = unused_port
port = unused_port(protocol: :tcp)
d = create_driver("
@type monitor_agent
bind '127.0.0.1'
Expand Down Expand Up @@ -451,7 +451,7 @@ def get(uri, header = {})

sub_test_case "servlets" do
setup do
@port = unused_port
@port = unused_port(protocol: :tcp)
# check @type and type in one configuration
conf = <<-EOC
<source>
Expand Down Expand Up @@ -759,7 +759,7 @@ def write(chunk)
end

setup do
@port = unused_port
@port = unused_port(protocol: :tcp)
# check @type and type in one configuration
conf = <<-EOC
<source>
Expand Down Expand Up @@ -840,7 +840,7 @@ def write(chunk)

sub_test_case "check the port number of http server" do
test "on single worker environment" do
port = unused_port
port = unused_port(protocol: :tcp)
d = create_driver("
@type monitor_agent
bind '127.0.0.1'
Expand All @@ -851,7 +851,7 @@ def write(chunk)
end

test "worker_id = 2 on multi worker environment" do
port = unused_port
port = unused_port(protocol: :tcp)
Fluent::SystemConfig.overwrite_system_config('workers' => 4) do
d = Fluent::Test::Driver::Input.new(Fluent::Plugin::MonitorAgentInput)
d.instance.instance_eval{ @_fluentd_worker_id = 2 }
Expand Down Expand Up @@ -905,7 +905,7 @@ def filter(tag, time, record)
end

test "plugins have a variable named buffer does not throws NoMethodError" do
port = unused_port
port = unused_port(protocol: :tcp)
d = create_driver("
@type monitor_agent
bind '127.0.0.1'
Expand Down
43 changes: 25 additions & 18 deletions test/plugin/test_in_syslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
class SyslogInputTest < Test::Unit::TestCase
def setup
Fluent::Test.setup
@port = unused_port
@port = unused_port(protocol: :udp)
end

def teardown
@port = nil
end

def ipv4_config
def ipv4_config(port = @port)
%[
port #{@port}
port #{port}
bind 127.0.0.1
tag syslog
]
end

def ipv6_config
def ipv6_config(port = @port)
%[
port #{@port}
port #{port}
bind ::1
tag syslog
]
Expand Down Expand Up @@ -69,7 +69,8 @@ def test_configure_resolve_hostname(param)
'Use transport and protocol' => ["protocol_type udp\n<transport tcp>\n </transport>", :udp, :tcp])
def test_configure_protocol(param)
conf, proto_type, transport_proto_type = *param
d = create_driver([ipv4_config, conf].join("\n"))
port = unused_port(protocol: proto_type ? proto_type : transport_proto_type)
d = create_driver([ipv4_config(port), conf].join("\n"))

assert_equal(d.instance.protocol_type, proto_type)
assert_equal(d.instance.transport_config.protocol, transport_proto_type)
Expand Down Expand Up @@ -158,12 +159,13 @@ def test_msg_size_udp_for_large_msg
end

def test_msg_size_with_tcp
d = create_driver([ipv4_config, "<transport tcp> \n</transport>"].join("\n"))
port = unused_port(protocol: :tcp)
d = create_driver([ipv4_config(port), "<transport tcp> \n</transport>"].join("\n"))
tests = create_test_case

d.run(expect_emits: 2) do
tests.each {|test|
TCPSocket.open('127.0.0.1', @port) do |s|
TCPSocket.open('127.0.0.1', port) do |s|
s.send(test['msg'], 0)
end
}
Expand All @@ -189,11 +191,12 @@ def test_emit_rfc5452
end

def test_msg_size_with_same_tcp_connection
d = create_driver([ipv4_config, "<transport tcp> \n</transport>"].join("\n"))
port = unused_port(protocol: :tcp)
d = create_driver([ipv4_config(port), "<transport tcp> \n</transport>"].join("\n"))
tests = create_test_case

d.run(expect_emits: 2) do
TCPSocket.open('127.0.0.1', @port) do |s|
TCPSocket.open('127.0.0.1', port) do |s|
tests.each {|test|
s.send(test['msg'], 0)
}
Expand Down Expand Up @@ -347,12 +350,13 @@ def compare_test_result(events, tests, options = {})

sub_test_case 'octet counting frame' do
def test_msg_size_with_tcp
d = create_driver([ipv4_config, "<transport tcp> \n</transport>", 'frame_type octet_count'].join("\n"))
port = unused_port(protocol: :tcp)
d = create_driver([ipv4_config(port), "<transport tcp> \n</transport>", 'frame_type octet_count'].join("\n"))
tests = create_test_case

d.run(expect_emits: 2) do
tests.each {|test|
TCPSocket.open('127.0.0.1', @port) do |s|
TCPSocket.open('127.0.0.1', port) do |s|
s.send(test['msg'], 0)
end
}
Expand All @@ -363,11 +367,12 @@ def test_msg_size_with_tcp
end

def test_msg_size_with_same_tcp_connection
d = create_driver([ipv4_config, "<transport tcp> \n</transport>", 'frame_type octet_count'].join("\n"))
port = unused_port(protocol: :tcp)
d = create_driver([ipv4_config(port), "<transport tcp> \n</transport>", 'frame_type octet_count'].join("\n"))
tests = create_test_case

d.run(expect_emits: 2) do
TCPSocket.open('127.0.0.1', @port) do |s|
TCPSocket.open('127.0.0.1', port) do |s|
tests.each {|test|
s.send(test['msg'], 0)
}
Expand Down Expand Up @@ -469,7 +474,8 @@ def test_emit_unmatched_lines_with_address
end

def test_send_keepalive_packet_is_disabled_by_default
d = create_driver(ipv4_config + %[
port = unused_port(protocol: :tcp)
d = create_driver(ipv4_config(port) + %[
<transport tcp>
</transport>
protocol tcp
Expand All @@ -479,19 +485,20 @@ def test_send_keepalive_packet_is_disabled_by_default

def test_send_keepalive_packet_can_be_enabled
addr = "127.0.0.1"
d = create_driver(ipv4_config + %[
port = unused_port(protocol: :tcp)
d = create_driver(ipv4_config(port) + %[
<transport tcp>
</transport>
send_keepalive_packet true
])
assert_true d.instance.send_keepalive_packet
mock.proxy(d.instance).server_create_connection(
:in_syslog_tcp_server, @port,
:in_syslog_tcp_server, port,
bind: addr,
resolve_name: nil,
send_keepalive_packet: true)
d.run do
TCPSocket.open(addr, @port)
TCPSocket.open(addr, port)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/plugin/test_in_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class TcpInputTest < Test::Unit::TestCase
def setup
Fluent::Test.setup
@port = unused_port
@port = unused_port(protocol: :tcp)
end

def teardown
Expand Down
2 changes: 1 addition & 1 deletion test/plugin/test_in_udp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class UdpInputTest < Test::Unit::TestCase
def setup
Fluent::Test.setup
@port = unused_port
@port = unused_port(protocol: :udp)
end

def teardown
Expand Down
3 changes: 2 additions & 1 deletion test/plugin/test_out_forward.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def setup
FileUtils.rm_rf(TMP_DIR)
FileUtils.mkdir_p(TMP_DIR)
@d = nil
@target_port = unused_port
# forward plugin uses TCP and UDP sockets on the same port number
@target_port = unused_port(protocol: :all)
end

def teardown
Expand Down
2 changes: 1 addition & 1 deletion test/plugin/test_out_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class TcpOutputTest < Test::Unit::TestCase

def setup
super
@port = unused_port
@port = unused_port(protocol: :tcp)
end

def teardown
Expand Down
2 changes: 1 addition & 1 deletion test/plugin_helper/test_http_server_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class HttpHelperTest < Test::Unit::TestCase
CERT_CA_DIR = File.expand_path(File.dirname(__FILE__) + '/data/cert/with_ca')

def setup
@port = unused_port
@port = unused_port(protocol: :tcp)
end

def teardown
Expand Down
Loading

0 comments on commit d1a58fd

Please sign in to comment.