Skip to content

Commit 351ce15

Browse files
committed
port number
1 parent 71a2cfc commit 351ce15

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

test/fluent/plugin/test_in_otlp.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77

88
class Fluent::Plugin::OtlpInputTest < Test::Unit::TestCase
99
def config
10-
<<~CONFIG
10+
<<~"CONFIG"
1111
tag otlp.test
1212
<http>
1313
bind 127.0.0.1
14-
port 14318
14+
port #{@port}
1515
</http>
1616
CONFIG
1717
end
1818

1919
def setup
2020
Fluent::Test.setup
2121

22+
@port = unused_tcp_port
23+
2224
# Enable mock_process_clock for freezing Fluent::EventTime
2325
Timecop.mock_process_clock = true
2426
Timecop.freeze(Time.parse("2025-01-01 00:00:00 UTC"))
@@ -38,7 +40,7 @@ def test_configure
3840
d = create_driver
3941
assert_equal "otlp.test", d.instance.tag
4042
assert_equal "127.0.0.1", d.instance.http_config.bind
41-
assert_equal 14318, d.instance.http_config.port
43+
assert_equal @port, d.instance.http_config.port
4244
end
4345

4446
data("metrics" => {
@@ -163,7 +165,7 @@ def config
163165
tag otlp.test
164166
<http>
165167
bind 127.0.0.1
166-
port 14318
168+
port #{@port}
167169
</http>
168170
<transport tls>
169171
ca_path "#{File.expand_path(File.dirname(__FILE__) + '/../resources/certs/ca.crt')}"
@@ -205,7 +207,7 @@ def compress(data)
205207

206208
def post_https_json(path, json, headers: {})
207209
headers = headers.merge({ "Content-Type" => "application/json" })
208-
post(path, json, endpoint: "https://127.0.0.1:14318", headers: headers, options: https_option)
210+
post(path, json, endpoint: "https://127.0.0.1:#{@port}", headers: headers, options: https_option)
209211
end
210212

211213
def post_json(path, json, headers: {}, options: {})
@@ -215,15 +217,15 @@ def post_json(path, json, headers: {}, options: {})
215217

216218
def post_https_protobuf(path, binary, headers: {}, options: {})
217219
headers = headers.merge({ "Content-Type" => "application/x-protobuf" })
218-
post(path, binary, endpoint: "https://127.0.0.1:14318", headers: headers, options: options)
220+
post(path, binary, endpoint: "https://127.0.0.1:#{@port}", headers: headers, options: options)
219221
end
220222

221223
def post_protobuf(path, binary, headers: {}, options: {})
222224
headers = headers.merge({ "Content-Type" => "application/x-protobuf" })
223225
post(path, binary, headers: headers, options: options)
224226
end
225227

226-
def post(path, body, endpoint: "http://127.0.0.1:14318", headers: {}, options: {})
228+
def post(path, body, endpoint: "http://127.0.0.1:#{@port}", headers: {}, options: {})
227229
connection = Excon.new("#{endpoint}#{path}", body: body, headers: headers, **options)
228230
connection.post
229231
end

test/fluent/plugin/test_out_otlp.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ class Fluent::Plugin::OtlpOutputTest < Test::Unit::TestCase
1414
DEFAULT_LOGGER = ::WEBrick::Log.new($stdout, ::WEBrick::BasicLog::FATAL)
1515

1616
def config
17-
<<~CONFIG
17+
<<~"CONFIG"
1818
<http>
19-
endpoint "http://127.0.0.1:24318"
19+
endpoint "http://127.0.0.1:#{@port}"
2020
</http>
2121
CONFIG
2222
end
2323

2424
def server_config
25-
config = { BindAddress: "127.0.0.1", Port: "24318" }
25+
config = { BindAddress: "127.0.0.1", Port: @port }
2626
# Suppress webrick logs
2727
config[:Logger] = DEFAULT_LOGGER
2828
config[:AccessLog] = []
@@ -63,6 +63,8 @@ def run_http_server
6363
def setup
6464
Fluent::Test.setup
6565

66+
@port = unused_tcp_port
67+
6668
@@server_request = nil
6769
@@server_response_code = 200
6870
@@http_server_thread = Thread.new do
@@ -71,8 +73,6 @@ def setup
7173
end
7274

7375
def teardown
74-
p "!" * 80
75-
p @@http_server_thread
7676
@@http_server_thread.kill
7777
@@http_server_thread = nil
7878
end
@@ -83,7 +83,7 @@ def create_driver(conf = config)
8383

8484
def test_configure
8585
d = create_driver
86-
assert_equal "http://127.0.0.1:24318", d.instance.http_config.endpoint
86+
assert_equal "http://127.0.0.1:#{@port}", d.instance.http_config.endpoint
8787
end
8888

8989
def test_send_logs
@@ -133,7 +133,7 @@ def test_send_compressed_message
133133

134134
d = create_driver(%[
135135
<http>
136-
endpoint "http://127.0.0.1:24318"
136+
endpoint "http://127.0.0.1:#{@port}"
137137
compress gzip
138138
</http>
139139
])
@@ -157,7 +157,7 @@ def test_unrecoverable_error
157157
d.feed(event)
158158
end
159159

160-
assert_match(%r{got unrecoverable error response from 'http://127.0.0.1:24318/v1/logs', response code is 500},
160+
assert_match(%r{got unrecoverable error response from 'http://127.0.0.1:#{@port}/v1/logs', response code is 500},
161161
d.instance.log.out.logs.join)
162162

163163
d.instance_shutdown
@@ -169,15 +169,15 @@ def test_error_with_disabled_unrecoverable
169169

170170
d = create_driver(%[
171171
<http>
172-
endpoint "http://127.0.0.1:24318"
172+
endpoint "http://127.0.0.1:#{@port}"
173173
error_response_as_unrecoverable false
174174
</http>
175175
])
176176
d.run(default_tag: "otlp.test", shutdown: false) do
177177
d.feed(event)
178178
end
179179

180-
assert_match(%r{got error response from 'http://127.0.0.1:24318/v1/logs', response code is 500},
180+
assert_match(%r{got error response from 'http://127.0.0.1:#{@port}/v1/logs', response code is 500},
181181
d.instance.log.out.logs.join)
182182

183183
d.instance_shutdown
@@ -192,7 +192,7 @@ def test_write_with_retryable_response
192192

193193
d = create_driver(%[
194194
<http>
195-
endpoint "http://127.0.0.1:24318"
195+
endpoint "http://127.0.0.1:#{@port}"
196196
retryable_response_codes [503]
197197
</http>
198198
])
@@ -211,7 +211,7 @@ def test_write_with_retryable_response
211211
sub_test_case "HTTPS" do
212212
def server_config
213213
config = super
214-
config[:Port] = "24318"
214+
config[:Port] = "#{@port}"
215215
# WEBrick supports self-generated self-signed certificate
216216
config[:SSLEnable] = true
217217
config[:SSLCertName] = [["CN", WEBrick::Utils.getservername]]
@@ -221,7 +221,7 @@ def server_config
221221
def config
222222
<<~"CONFIG"
223223
<http>
224-
endpoint "https://127.0.0.1:24318"
224+
endpoint "https://127.0.0.1:#{@port}"
225225
</http>
226226
<transport tls>
227227
cert_path "#{File.expand_path(File.dirname(__FILE__) + '/../resources/certs/ca.crt')}"

test/helper.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ module ProtocolBuffers
3333
INVALID = "invalid"
3434
end
3535
end
36+
37+
def unused_tcp_port(num = 1)
38+
ports = []
39+
sockets = []
40+
num.times do
41+
s = TCPServer.open(0)
42+
sockets << s
43+
ports << s.addr[1]
44+
end
45+
sockets.each(&:close)
46+
if num == 1
47+
return ports.first
48+
else
49+
return *ports
50+
end
51+
end

0 commit comments

Comments
 (0)