fluentd 1.7.4-x64-mingw32 → 1.8.0-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of fluentd might be problematic. Click here for more details.

Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -1
  3. data/.travis.yml +4 -0
  4. data/CHANGELOG.md +70 -0
  5. data/MAINTAINERS.md +1 -0
  6. data/example/out_forward_sd.conf +17 -0
  7. data/example/sd.yaml +8 -0
  8. data/fluentd.gemspec +1 -1
  9. data/lib/fluent/agent.rb +3 -1
  10. data/lib/fluent/command/cat.rb +1 -2
  11. data/lib/fluent/command/fluentd.rb +16 -8
  12. data/lib/fluent/compat/call_super_mixin.rb +9 -0
  13. data/lib/fluent/compat/exec_util.rb +1 -1
  14. data/lib/fluent/config/configure_proxy.rb +4 -4
  15. data/lib/fluent/config/element.rb +28 -15
  16. data/lib/fluent/config/error.rb +6 -0
  17. data/lib/fluent/config/literal_parser.rb +24 -2
  18. data/lib/fluent/config/section.rb +43 -6
  19. data/lib/fluent/config/types.rb +98 -26
  20. data/lib/fluent/configurable.rb +2 -2
  21. data/lib/fluent/counter/base_socket.rb +2 -4
  22. data/lib/fluent/engine.rb +41 -122
  23. data/lib/fluent/event.rb +5 -7
  24. data/lib/fluent/fluent_log_event_router.rb +141 -0
  25. data/lib/fluent/msgpack_factory.rb +19 -2
  26. data/lib/fluent/plugin.rb +10 -1
  27. data/lib/fluent/plugin/base.rb +2 -2
  28. data/lib/fluent/plugin/buf_file.rb +11 -7
  29. data/lib/fluent/plugin/buf_file_single.rb +8 -5
  30. data/lib/fluent/plugin/buffer/chunk.rb +1 -1
  31. data/lib/fluent/plugin/buffer/file_chunk.rb +4 -6
  32. data/lib/fluent/plugin/buffer/file_single_chunk.rb +3 -5
  33. data/lib/fluent/plugin/formatter_csv.rb +23 -1
  34. data/lib/fluent/plugin/formatter_stdout.rb +1 -1
  35. data/lib/fluent/plugin/in_forward.rb +1 -1
  36. data/lib/fluent/plugin/in_monitor_agent.rb +4 -2
  37. data/lib/fluent/plugin/in_tail.rb +6 -0
  38. data/lib/fluent/plugin/in_unix.rb +1 -1
  39. data/lib/fluent/plugin/out_forward.rb +77 -28
  40. data/lib/fluent/plugin/out_forward/ack_handler.rb +1 -1
  41. data/lib/fluent/plugin/out_forward/load_balancer.rb +5 -2
  42. data/lib/fluent/plugin/out_stream.rb +1 -1
  43. data/lib/fluent/plugin/output.rb +11 -3
  44. data/lib/fluent/plugin/parser.rb +1 -0
  45. data/lib/fluent/plugin/sd_file.rb +155 -0
  46. data/lib/fluent/plugin/sd_static.rb +58 -0
  47. data/lib/fluent/plugin/service_discovery.rb +80 -0
  48. data/lib/fluent/plugin_helper.rb +1 -0
  49. data/lib/fluent/plugin_helper/child_process.rb +3 -3
  50. data/lib/fluent/plugin_helper/compat_parameters.rb +11 -1
  51. data/lib/fluent/plugin_helper/extract.rb +1 -1
  52. data/lib/fluent/plugin_helper/inject.rb +1 -1
  53. data/lib/fluent/plugin_helper/record_accessor.rb +10 -19
  54. data/lib/fluent/plugin_helper/server.rb +8 -4
  55. data/lib/fluent/plugin_helper/service_discovery.rb +80 -0
  56. data/lib/fluent/plugin_helper/service_discovery/manager.rb +132 -0
  57. data/lib/fluent/plugin_helper/service_discovery/round_robin_balancer.rb +43 -0
  58. data/lib/fluent/plugin_id.rb +7 -0
  59. data/lib/fluent/root_agent.rb +7 -9
  60. data/lib/fluent/supervisor.rb +192 -211
  61. data/lib/fluent/system_config.rb +26 -52
  62. data/lib/fluent/test/driver/base_owned.rb +15 -2
  63. data/lib/fluent/time.rb +8 -6
  64. data/lib/fluent/version.rb +1 -1
  65. data/test/command/test_fluentd.rb +12 -7
  66. data/test/config/test_configurable.rb +154 -0
  67. data/test/config/test_element.rb +18 -0
  68. data/test/config/test_literal_parser.rb +4 -0
  69. data/test/config/test_system_config.rb +48 -91
  70. data/test/config/test_types.rb +293 -120
  71. data/test/counter/test_client.rb +8 -4
  72. data/test/plugin/data/sd_file/config +11 -0
  73. data/test/plugin/data/sd_file/config.json +17 -0
  74. data/test/plugin/data/sd_file/config.yaml +11 -0
  75. data/test/plugin/data/sd_file/config.yml +11 -0
  76. data/test/plugin/data/sd_file/invalid_config.yml +7 -0
  77. data/test/plugin/out_forward/test_handshake_protocol.rb +2 -2
  78. data/test/plugin/out_forward/test_load_balancer.rb +1 -1
  79. data/test/plugin/out_forward/test_socket_cache.rb +2 -2
  80. data/test/plugin/test_buf_file.rb +40 -0
  81. data/test/plugin/test_buf_file_single.rb +32 -0
  82. data/test/plugin/test_buffer_file_chunk.rb +0 -11
  83. data/test/plugin/test_buffer_file_single_chunk.rb +0 -10
  84. data/test/plugin/test_formatter_csv.rb +9 -0
  85. data/test/plugin/test_in_forward.rb +9 -9
  86. data/test/plugin/test_in_monitor_agent.rb +37 -10
  87. data/test/plugin/test_in_unix.rb +5 -5
  88. data/test/plugin/test_out_forward.rb +45 -1
  89. data/test/plugin/test_out_stdout.rb +36 -1
  90. data/test/plugin/test_out_stream.rb +3 -3
  91. data/test/plugin/test_output.rb +25 -1
  92. data/test/plugin/test_sd_file.rb +211 -0
  93. data/test/plugin_helper/service_discovery/test_manager.rb +93 -0
  94. data/test/plugin_helper/service_discovery/test_round_robin_balancer.rb +21 -0
  95. data/test/plugin_helper/test_server.rb +13 -0
  96. data/test/plugin_helper/test_service_discovery.rb +72 -0
  97. data/test/test_event.rb +15 -15
  98. data/test/test_fluent_log_event_router.rb +99 -0
  99. data/test/test_logger_initializer.rb +26 -0
  100. data/test/test_supervisor.rb +30 -59
  101. metadata +43 -6
@@ -39,7 +39,7 @@ class ForwardOutputTest < Test::Unit::TestCase
39
39
 
40
40
  def create_driver(conf=CONFIG)
41
41
  Fluent::Test::Driver::Output.new(Fluent::Plugin::ForwardOutput) {
42
- attr_reader :sent_chunk_ids, :ack_handler
42
+ attr_reader :sent_chunk_ids, :ack_handler, :discovery_manager
43
43
 
44
44
  def initialize
45
45
  super
@@ -260,6 +260,30 @@ EOL
260
260
  end
261
261
  end
262
262
 
263
+ test 'server is an abbreviation of static type of service_discovery' do
264
+ @d = d = create_driver(%[
265
+ <server>
266
+ host 127.0.0.1
267
+ port 1234
268
+ </server>
269
+
270
+ <service_discovery>
271
+ @type static
272
+
273
+ <service>
274
+ host 127.0.0.1
275
+ port 1235
276
+ </service>
277
+ </service_discovery>
278
+ ])
279
+
280
+ assert_equal 2, d.instance.discovery_manager.services.size
281
+ assert_equal '127.0.0.1', d.instance.discovery_manager.services[0].host
282
+ assert_equal 1234, d.instance.discovery_manager.services[0].port
283
+ assert_equal '127.0.0.1', d.instance.discovery_manager.services[1].host
284
+ assert_equal 1235, d.instance.discovery_manager.services[1].port
285
+ end
286
+
263
287
  test 'compress_default_value' do
264
288
  @d = d = create_driver
265
289
  assert_equal :text, d.instance.compress
@@ -945,6 +969,26 @@ EOL
945
969
  end
946
970
  end
947
971
 
972
+ test 'when out_forward has @id' do
973
+ # cancel https://github.com/fluent/fluentd/blob/077508ac817b7637307434d0c978d7cdc3d1c534/lib/fluent/plugin_id.rb#L43-L53
974
+ # it always return true in test
975
+ mock.proxy(Fluent::Plugin).new_sd(:static, anything) { |v|
976
+ stub(v).plugin_id_for_test? { false }
977
+ }.once
978
+
979
+ output = Fluent::Test::Driver::Output.new(Fluent::Plugin::ForwardOutput) {
980
+ def plugin_id_for_test?
981
+ false
982
+ end
983
+ }
984
+
985
+ assert_nothing_raised do
986
+ output.configure(CONFIG + %[
987
+ @id unique_out_forward
988
+ ])
989
+ end
990
+ end
991
+
948
992
  sub_test_case 'verify_connection_at_startup' do
949
993
  test 'nodes are not available' do
950
994
  @d = d = create_driver(CONFIG + %[
@@ -37,6 +37,25 @@ class StdoutOutputTest < Test::Unit::TestCase
37
37
  end
38
38
  end
39
39
 
40
+ test 'configure with time_format' do
41
+ d = create_driver(CONFIG + <<-CONF)
42
+ <format>
43
+ @type stdout
44
+ time_format %Y-%m-%dT%H:%M:%S.%L%z
45
+ </format>
46
+ CONF
47
+
48
+ time = event_time
49
+ out = capture_log do
50
+ d.run(default_tag: 'test') do
51
+ d.feed(time, {'test' => 'test'})
52
+ end
53
+ end
54
+
55
+ t = Time.at(time).localtime.strftime("%Y-%m-%dT%H:%M:%S.%L%z")
56
+ assert_equal "#{t} test: {\"test\":\"test\"}\n", out
57
+ end
58
+
40
59
  test 'emit with default configuration' do
41
60
  d = create_driver
42
61
  time = event_time()
@@ -157,6 +176,23 @@ class StdoutOutputTest < Test::Unit::TestCase
157
176
  end
158
177
  end
159
178
 
179
+ data(
180
+ 'utc and !localtime' => "utc true\nlocaltime false",
181
+ '!utc and localtime' => "utc false\nlocaltime true")
182
+ test 'success when configure with localtime and utc' do |c|
183
+ assert_nothing_raised do
184
+ create_driver(CONFIG + c)
185
+ end
186
+ end
187
+
188
+ data('utc and localtime' => "utc true\nlocaltime true",
189
+ '!utc and !localtime' => "utc false\nlocaltime false")
190
+ test 'raise an error when configure with localtime and utc' do |c|
191
+ assert_raise(Fluent::ConfigError.new('both of utc and localtime are specified, use only one of them')) do
192
+ create_driver(CONFIG + c)
193
+ end
194
+ end
195
+
160
196
  # Capture the log output of the block given
161
197
  def capture_log(&block)
162
198
  tmp = $log
@@ -167,4 +203,3 @@ class StdoutOutputTest < Test::Unit::TestCase
167
203
  $log = tmp
168
204
  end
169
205
  end
170
-
@@ -31,10 +31,10 @@ module StreamOutputTest
31
31
  d.emit({"a"=>2}, time)
32
32
 
33
33
  expect = ["test",
34
- Fluent::Engine.msgpack_factory.packer.write([time,{"a"=>1}]).to_s +
35
- Fluent::Engine.msgpack_factory.packer.write([time,{"a"=>2}]).to_s
34
+ Fluent::MessagePackFactory.msgpack_packer.write([time,{"a"=>1}]).to_s +
35
+ Fluent::MessagePackFactory.msgpack_packer.write([time,{"a"=>2}]).to_s
36
36
  ]
37
- expect = Fluent::Engine.msgpack_factory.packer.write(expect).to_s
37
+ expect = Fluent::MessagePackFactory.msgpack_packer.write(expect).to_s
38
38
 
39
39
  result = d.run
40
40
  assert_equal(expect, result)
@@ -378,6 +378,30 @@ class OutputTest < Test::Unit::TestCase
378
378
  assert { logs.any? { |log| log.include?("${chunk_id} is not allowed in this plugin") } }
379
379
  end
380
380
 
381
+ test '#extract_placeholders logs warn message with not replaced key' do
382
+ @i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')]))
383
+ tmpl = "/mypath/${key1}/test"
384
+ t = event_time('2016-04-11 20:30:00 +0900')
385
+ v = { key1: "value1" }
386
+ m = create_metadata(timekey: t, tag: 'fluentd.test.output', variables: v)
387
+ @i.extract_placeholders(tmpl, m)
388
+ logs = @i.log.out.logs
389
+
390
+ assert { logs.any? { |log| log.include?("chunk key placeholder 'key1' not replaced. template:#{tmpl}") } }
391
+ end
392
+
393
+ test '#extract_placeholders logs warn message with not replaced key if variables exist and chunk_key is not empty' do
394
+ @i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'key1')]))
395
+ tmpl = "/mypath/${key1}/${key2}/test"
396
+ t = event_time('2016-04-11 20:30:00 +0900')
397
+ v = { key1: "value1" }
398
+ m = create_metadata(timekey: t, tag: 'fluentd.test.output', variables: v)
399
+ @i.extract_placeholders(tmpl, m)
400
+ logs = @i.log.out.logs
401
+
402
+ assert { logs.any? { |log| log.include?("chunk key placeholder 'key2' not replaced. template:#{tmpl}") } }
403
+ end
404
+
381
405
  sub_test_case '#placeholder_validators' do
382
406
  test 'returns validators for time, tag and keys when a template has placeholders even if plugin is not configured with these keys' do
383
407
  @i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')]))
@@ -870,7 +894,7 @@ class OutputTest < Test::Unit::TestCase
870
894
 
871
895
  test 'raises an error if timekey is less than equal 0' do
872
896
  i = create_output(:delayed)
873
- assert_raise Fluent::ConfigError.new('timekey should be greater than 0. current timekey: 0.0') do
897
+ assert_raise Fluent::ConfigError.new("<buffer ...> argument includes 'time', but timekey is not configured") do
874
898
  i.configure(config_element('ROOT','',{},[config_element('buffer', 'time', { "timekey" => nil })]))
875
899
  end
876
900
 
@@ -0,0 +1,211 @@
1
+ require_relative '../helper'
2
+ require 'fluent/plugin/sd_file'
3
+ require 'fileutils'
4
+ require 'json'
5
+
6
+ class FileServiceDiscoveryTest < ::Test::Unit::TestCase
7
+ setup do
8
+ @dir = File.expand_path('data/sd_file', __dir__)
9
+ FileUtils.mkdir_p(File.join(@dir, 'tmp'))
10
+ end
11
+
12
+ teardown do
13
+ FileUtils.rm_r(File.join(@dir, 'tmp'))
14
+ end
15
+
16
+ sub_test_case 'configure' do
17
+ test 'load yml' do
18
+ sdf = Fluent::Plugin::FileServiceDiscovery.new
19
+ sdf.configure(config_element('service_discovery', '', { 'path' => File.join(@dir, 'config.yml') }))
20
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:file, '127.0.0.1', 24224, 'test1', 1, false, 'user1', 'pass1', 'key1'), sdf.services[0]
21
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:file, '127.0.0.1', 24225, nil, 1), sdf.services[1]
22
+ end
23
+
24
+ test 'load yaml' do
25
+ sdf = Fluent::Plugin::FileServiceDiscovery.new
26
+ sdf.configure(config_element('service_discovery', '', { 'path' => File.join(@dir, 'config.yaml') }))
27
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:file, '127.0.0.1', 24224, 'test1', 1, false, 'user1', 'pass1', 'key1'), sdf.services[0]
28
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:file, '127.0.0.1', 24225, nil, 1), sdf.services[1]
29
+ end
30
+
31
+ test 'load json' do
32
+ sdf = Fluent::Plugin::FileServiceDiscovery.new
33
+ sdf.configure(config_element('service_discovery', '', { 'path' => File.join(@dir, 'config.json') }))
34
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:file, '127.0.0.1', 24224, 'test1', 1, false, 'user1', 'pass1', 'key1'), sdf.services[0]
35
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:file, '127.0.0.1', 24225, nil, 1), sdf.services[1]
36
+ end
37
+
38
+ test 'regard as yaml if ext is not givened' do
39
+ sdf = Fluent::Plugin::FileServiceDiscovery.new
40
+ sdf.configure(config_element('service_discovery', '', { 'path' => File.join(@dir, 'config') }))
41
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:file, '127.0.0.1', 24224, 'test1', 1, false, 'user1', 'pass1', 'key1'), sdf.services[0]
42
+ assert_equal Fluent::Plugin::ServiceDiscovery::Service.new(:file, '127.0.0.1', 24225, nil, 1), sdf.services[1]
43
+ end
44
+
45
+ test 'raise an error if config has error' do
46
+ sdf = Fluent::Plugin::FileServiceDiscovery.new
47
+ e = assert_raise Fluent::ConfigError do
48
+ sdf.configure(config_element('service_discovery', '', { 'path' => File.join(@dir, 'invalid_config.yaml') }))
49
+ end
50
+ assert_match(/path=/, e.message)
51
+ end
52
+
53
+ test 'raise an error if config file does not exist' do
54
+ sdf = Fluent::Plugin::FileServiceDiscovery.new
55
+ e = assert_raise Fluent::ConfigError do
56
+ sdf.configure(config_element('service_discovery', '', { 'path' => File.join(@dir, 'invalid_not_found.json') }))
57
+ end
58
+ assert_match(/not found/, e.message)
59
+ end
60
+ end
61
+
62
+ sub_test_case '#start' do
63
+ module TestStatEventHelperWrapper
64
+ # easy to control statsevent
65
+ def event_loop_attach(watcher)
66
+ unless watcher.is_a?(Fluent::Plugin::FileServiceDiscovery::StatWatcher)
67
+ super
68
+ return
69
+ end
70
+
71
+ @test_stat_event_helper_wrapper_watchers ||= []
72
+ @test_stat_event_helper_wrapper_watchers << watcher
73
+
74
+ @test_stat_event_helper_wrapper_context = Fiber.new do
75
+ loop do
76
+ @test_stat_event_helper_wrapper_watchers.each do |w|
77
+ w.on_change('old', 'new')
78
+ end
79
+
80
+ if Fiber.yield == :finish
81
+ break
82
+ end
83
+ end
84
+ end
85
+ resume
86
+ end
87
+
88
+ def resume
89
+ @test_stat_event_helper_wrapper_context.resume(:resume)
90
+ end
91
+
92
+ def shutdown
93
+ super
94
+
95
+ if @test_stat_event_helper_wrapper_context
96
+ @test_stat_event_helper_wrapper_context.resume(:finish)
97
+ end
98
+ end
99
+ end
100
+
101
+ def create_tmp_config(path, body)
102
+ File.write(File.join(@dir, 'tmp', path), body)
103
+ end
104
+
105
+ setup do
106
+ sdf = Fluent::Plugin::FileServiceDiscovery.new
107
+ @sd_file = sdf
108
+ end
109
+
110
+ teardown do
111
+ if @sd_file
112
+ @sd_file.stop unless @sd_file.stopped?
113
+ @sd_file.before_shutdown unless @sd_file.before_shutdown?
114
+ @sd_file.shutdown unless @sd_file.shutdown?
115
+ @sd_file.after_shutdown unless @sd_file.after_shutdown?
116
+ @sd_file.close unless @sd_file.closed?
117
+ @sd_file.terminate unless @sd_file.terminated?
118
+ end
119
+ end
120
+
121
+ test 'Skip if file is not updated' do
122
+ @sd_file.extend(TestStatEventHelperWrapper)
123
+
124
+ create_tmp_config('config.json', JSON.generate([{ port: 1233, host: '127.0.0.1' }]))
125
+ @sd_file.configure(config_element('service_discovery', '', { 'path' => File.join(@dir, 'config.yml') }))
126
+ queue = []
127
+ mock.proxy(@sd_file).refresh_file(queue).twice
128
+
129
+ @sd_file.start(queue)
130
+ assert_empty queue
131
+
132
+ @sd_file.resume
133
+ assert_empty queue
134
+ end
135
+
136
+ test 'Skip if file is invalid contents' do
137
+ @sd_file.extend(TestStatEventHelperWrapper)
138
+
139
+ create_tmp_config('config.json', JSON.generate([{ port: 1233, host: '127.0.0.1' }]))
140
+ @sd_file.configure(config_element('service_discovery', '', { 'path' => File.join(@dir, 'config.yml') }))
141
+
142
+ queue = []
143
+ @sd_file.start(queue)
144
+
145
+ mock.proxy(@sd_file).refresh_file(queue).once
146
+ create_tmp_config('test.json', 'invalid contents')
147
+ @sd_file.resume
148
+
149
+ assert_empty queue
150
+ end
151
+
152
+ test 'if service is updated, service_in and service_out event happen' do
153
+ @sd_file.extend(TestStatEventHelperWrapper)
154
+
155
+ create_tmp_config('test.json', JSON.generate([{ port: 1233, host: '127.0.0.1' }]))
156
+ @sd_file.configure(config_element('service_discovery', '', { 'path' => File.join(@dir, 'tmp/test.json') }))
157
+
158
+ queue = []
159
+ @sd_file.start(queue)
160
+ create_tmp_config('test.json', JSON.generate([{ port: 1234, host: '127.0.0.1' }]))
161
+ @sd_file.resume
162
+
163
+ assert_equal 2, queue.size
164
+ join = queue.shift
165
+ drain = queue.shift
166
+ assert_equal Fluent::Plugin::ServiceDiscovery::SERVICE_IN, join.type
167
+ assert_equal 1234, join.service.port
168
+ assert_equal '127.0.0.1', join.service.host
169
+
170
+ assert_equal Fluent::Plugin::ServiceDiscovery::SERVICE_OUT, drain.type
171
+ assert_equal 1233, drain.service.port
172
+ assert_equal '127.0.0.1', drain.service.host
173
+ end
174
+
175
+ test 'if service is deleted, service_out event happens' do
176
+ @sd_file.extend(TestStatEventHelperWrapper)
177
+
178
+ create_tmp_config('test.json', JSON.generate([{ port: 1233, host: '127.0.0.1' }, { port: 1234, host: '127.0.0.2' }]))
179
+ @sd_file.configure(config_element('service_discovery', '', { 'path' => File.join(@dir, 'tmp/test.json') }))
180
+
181
+ queue = []
182
+ @sd_file.start(queue)
183
+ create_tmp_config('test.json', JSON.generate([{ port: 1233, host: '127.0.0.1' }]))
184
+ @sd_file.resume
185
+
186
+ assert_equal 1, queue.size
187
+ drain = queue.shift
188
+ assert_equal Fluent::Plugin::ServiceDiscovery::SERVICE_OUT, drain.type
189
+ assert_equal 1234, drain.service.port
190
+ assert_equal '127.0.0.2', drain.service.host
191
+ end
192
+
193
+ test 'if new service is added, service_in event happens' do
194
+ @sd_file.extend(TestStatEventHelperWrapper)
195
+
196
+ create_tmp_config('test.json', JSON.generate([{ port: 1233, host: '127.0.0.1' }]))
197
+ @sd_file.configure(config_element('service_discovery', '', { 'path' => File.join(@dir, 'tmp/test.json') }))
198
+
199
+ queue = []
200
+ @sd_file.start(queue)
201
+ create_tmp_config('test.json', JSON.generate([{ port: 1233, host: '127.0.0.1' }, { port: 1234, host: '127.0.0.2' }]))
202
+ @sd_file.resume
203
+
204
+ assert_equal 1, queue.size
205
+ join = queue.shift
206
+ assert_equal Fluent::Plugin::ServiceDiscovery::SERVICE_IN, join.type
207
+ assert_equal 1234, join.service.port
208
+ assert_equal '127.0.0.2', join.service.host
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,93 @@
1
+ require_relative '../../helper'
2
+ require 'fluent/plugin_helper/service_discovery/manager'
3
+
4
+ class TestServiceDiscoveryManager < ::Test::Unit::TestCase
5
+ setup do
6
+ @sd_file_dir = File.expand_path('../../plugin/data/sd_file', __dir__)
7
+ end
8
+
9
+ class TestSdPlugin < Fluent::Plugin::ServiceDiscovery
10
+ Fluent::Plugin.register_sd('test_sd', self)
11
+
12
+ def initialize
13
+ super
14
+ end
15
+
16
+ def service_in(host, port)
17
+ s = Fluent::Plugin::ServiceDiscovery::Service.new(:sd_test, host, port)
18
+ @queue << Fluent::Plugin::ServiceDiscovery.service_in_msg(s)
19
+ end
20
+
21
+ def service_out(host, port)
22
+ s = Fluent::Plugin::ServiceDiscovery::Service.new(:sd_test, host, port)
23
+ @queue << Fluent::Plugin::ServiceDiscovery.service_out_msg(s)
24
+ end
25
+
26
+ def start(queue)
27
+ @queue = queue
28
+
29
+ super
30
+ end
31
+ end
32
+
33
+ sub_test_case '#configure' do
34
+ test 'build sd plugins and services' do
35
+ sdm = Fluent::PluginHelper::ServiceDiscovery::Manager.new(log: $log)
36
+ sdm.configure(
37
+ [
38
+ { type: :file, conf: config_element('service_discovery', '', { 'path' => File.join(@sd_file_dir, 'config.yml') }) },
39
+ { type: :static, conf: config_element('root', '', {}, [config_element('service', '', { 'host' => '127.0.0.2', 'port' => '5432' })]) },
40
+ ],
41
+ )
42
+
43
+ assert_equal 3, sdm.services.size
44
+ assert_equal 24224, sdm.services[0].port
45
+ assert_equal '127.0.0.1', sdm.services[0].host
46
+
47
+ assert_equal 24225, sdm.services[1].port
48
+ assert_equal '127.0.0.1', sdm.services[1].host
49
+
50
+ assert_equal 5432, sdm.services[2].port
51
+ assert_equal '127.0.0.2', sdm.services[2].host
52
+
53
+ assert_false sdm.static_config?
54
+ end
55
+
56
+ test 'no need to timer if only static' do
57
+ sdm = Fluent::PluginHelper::ServiceDiscovery::Manager.new(log: $log)
58
+ sdm.configure(
59
+ [{ type: :static, conf: config_element('root', '', {}, [config_element('service', '', { 'host' => '127.0.0.2', 'port' => '5432' })]) }]
60
+ )
61
+
62
+ assert_equal 1, sdm.services.size
63
+ assert_equal 5432, sdm.services[0].port
64
+ assert_equal '127.0.0.2', sdm.services[0].host
65
+
66
+ assert_true sdm.static_config?
67
+ end
68
+ end
69
+
70
+ sub_test_case '#run_once' do
71
+ test 'if new service added and deleted' do
72
+ sdm = Fluent::PluginHelper::ServiceDiscovery::Manager.new(log: $log)
73
+ t = TestSdPlugin.new
74
+ mock(Fluent::Plugin).new_sd(:sd_test, anything) { t }
75
+ sdm.configure([{ type: :sd_test, conf: config_element('service_discovery', '', {})}])
76
+ sdm.start
77
+
78
+ assert_equal 0, sdm.services.size
79
+
80
+ t.service_in('127.0.0.1', '1234')
81
+
82
+ sdm.run_once
83
+ assert_equal 1, sdm.services.size
84
+ assert_equal '127.0.0.1', sdm.services[0].host
85
+ assert_equal '1234', sdm.services[0].port
86
+
87
+ t.service_out('127.0.0.1', '1234')
88
+
89
+ sdm.run_once
90
+ assert_equal 0, sdm.services.size
91
+ end
92
+ end
93
+ end