From bd07779cbd3710dae23289c68b4d2e5004940201 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 9 Feb 2023 23:53:09 +0900 Subject: [PATCH 1/5] modify variable to be checked @socket_manager_server -> @socket_manager_path Signed-off-by: abetomo --- lib/fluent/test/driver/base.rb | 2 +- test/plugin_helper/test_server.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fluent/test/driver/base.rb b/lib/fluent/test/driver/base.rb index dfd56ac1a9..b53f863db4 100644 --- a/lib/fluent/test/driver/base.rb +++ b/lib/fluent/test/driver/base.rb @@ -173,7 +173,7 @@ def instance_shutdown(log: Logger.new($stdout)) if @socket_manager_server @socket_manager_server.close - if @socket_manager_server.is_a?(String) && File.exist?(@socket_manager_path) + if @socket_manager_path.is_a?(String) && File.exist?(@socket_manager_path) FileUtils.rm_f @socket_manager_path end end diff --git a/test/plugin_helper/test_server.rb b/test/plugin_helper/test_server.rb index 3dc308474b..a08734ae46 100644 --- a/test/plugin_helper/test_server.rb +++ b/test/plugin_helper/test_server.rb @@ -37,7 +37,7 @@ class Dummy < Fluent::Plugin::TestBase (@d.terminated? || @d.terminate) rescue nil @socket_manager_server.close - if @socket_manager_server.is_a?(String) && File.exist?(@socket_manager_path) + if @socket_manager_path.is_a?(String) && File.exist?(@socket_manager_path) FileUtils.rm_f @socket_manager_path end end From c806fa9046c83f87b406eef5f99fa9ed145d8945 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 9 Feb 2023 23:58:12 +0900 Subject: [PATCH 2/5] delete unnecessary files by `instance_shutdown` Files created in `/tmp` are deleted. Signed-off-by: abetomo --- test/plugin/test_in_tcp.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/plugin/test_in_tcp.rb b/test/plugin/test_in_tcp.rb index b85a919c94..69d6bc302e 100755 --- a/test/plugin/test_in_tcp.rb +++ b/test/plugin/test_in_tcp.rb @@ -226,6 +226,8 @@ def create_tcp_socket(host, port, &block) assert_equal 1, d.instance.log.logs.count { |l| l =~ /anonymous client/ } assert_equal 0, d.events.size + + d.instance_shutdown end end From a6b02da9d5d6251626b86783275f45d92f79c33c Mon Sep 17 00:00:00 2001 From: abetomo Date: Mon, 13 Feb 2023 22:07:56 +0900 Subject: [PATCH 3/5] test: set `root_dir` Backup files are generated in `/tmp` and remain there. Set `root_dir` to generate backup files in the specified directory. Signed-off-by: abetomo --- test/plugin/test_out_http.rb | 1 + test/plugin/test_output.rb | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/test/plugin/test_out_http.rb b/test/plugin/test_out_http.rb index e245f74524..b0e1a469a7 100644 --- a/test/plugin/test_out_http.rb +++ b/test/plugin/test_out_http.rb @@ -378,6 +378,7 @@ def test_basic_auth_with_invalid_auth password hello? ]) + d.instance.system_config_override(root_dir: TMP_DIR) # Backup files are generated in TMP_DIR. d.run(default_tag: 'test.http', shutdown: false) do test_events.each { |event| d.feed(event) diff --git a/test/plugin/test_output.rb b/test/plugin/test_output.rb index c25c7b1afb..a04b19d469 100644 --- a/test/plugin/test_output.rb +++ b/test/plugin/test_output.rb @@ -803,7 +803,10 @@ def waiting(seconds) end test 'output plugin will call #try_write for plugin supports delayed commit only to flush buffer chunks' do + tmp_dir = File.join(__dir__, '../tmp/test_output') + i = create_output(:delayed) + i.system_config_override(root_dir: tmp_dir) # Backup files are generated in `tmp_dir`. try_write_called = false i.register(:try_write){|chunk| try_write_called = true; commit_write(chunk.unique_id) } @@ -820,6 +823,8 @@ def waiting(seconds) assert try_write_called i.stop; i.before_shutdown; i.shutdown; i.after_shutdown; i.close; i.terminate + ensure + FileUtils.rm_rf(tmp_dir) end test '#prefer_delayed_commit (returns false) decides delayed commit is disabled if both are implemented' do @@ -849,7 +854,10 @@ def waiting(seconds) end test '#prefer_delayed_commit (returns true) decides delayed commit is enabled if both are implemented' do + tmp_dir = File.join(__dir__, '../tmp/test_output') + i = create_output(:full) + i.system_config_override(root_dir: tmp_dir) # Backup files are generated in `tmp_dir`. write_called = false try_write_called = false i.register(:write){ |chunk| write_called = true } @@ -872,6 +880,8 @@ def waiting(seconds) assert try_write_called i.stop; i.before_shutdown; i.shutdown; i.after_shutdown; i.close; i.terminate + ensure + FileUtils.rm_rf(tmp_dir) end test 'flush_interval is ignored when flush_mode is not interval' do From 233a5f9f60d9f164eda223005b4d32e445d3552a Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 14 Feb 2023 20:50:26 +0900 Subject: [PATCH 4/5] Ensure shutdown after the test `instance_shutdown` in `ensure` block. Signed-off-by: abetomo --- test/plugin/test_in_tcp.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/plugin/test_in_tcp.rb b/test/plugin/test_in_tcp.rb index 69d6bc302e..46061cb32c 100755 --- a/test/plugin/test_in_tcp.rb +++ b/test/plugin/test_in_tcp.rb @@ -226,8 +226,8 @@ def create_tcp_socket(host, port, &block) assert_equal 1, d.instance.log.logs.count { |l| l =~ /anonymous client/ } assert_equal 0, d.events.size - - d.instance_shutdown + ensure + d.instance_shutdown if d&.instance end end From 1c04889b1e036a34ec0cd94c12765a9153c54812 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 16 Feb 2023 23:41:29 +0900 Subject: [PATCH 5/5] test: terminate with sigterm instead of sigkill With SIGTERM, the shutdown process runs. `SERVERENGINE_SOCKETMANAGER_*` etc. will be removed in the shutdown process. When it cannot be terminated by SIGTERM, it is terminated by SIGKILL. Signed-off-by: abetomo --- test/command/test_fluentd.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/test/command/test_fluentd.rb b/test/command/test_fluentd.rb index 461e19ef1b..5719055521 100644 --- a/test/command/test_fluentd.rb +++ b/test/command/test_fluentd.rb @@ -71,6 +71,15 @@ def create_cmdline(conf_path, *fluentd_options) end end + def process_kill(pid) + begin + Process.kill(:TERM, pid) rescue nil + Timeout.timeout(10){ sleep 0.1 while process_exist?(pid) } + rescue Timeout::Error + Process.kill(:KILL, pid) rescue nil + end + end + def execute_command(cmdline, chdir=@tmp_dir, env = {}) null_stream = Fluent::FileWrapper.open(File::NULL, 'w') gemfile_path = File.expand_path(File.dirname(__FILE__) + "../../../Gemfile") @@ -85,12 +94,12 @@ def execute_command(cmdline, chdir=@tmp_dir, env = {}) yield pid, io # p(here: "execute command", pid: pid, worker_pids: @worker_pids) ensure - Process.kill(:KILL, pid) rescue nil + process_kill(pid) if @supervisor_pid - Process.kill(:KILL, @supervisor_pid) rescue nil + process_kill(@supervisor_pid) end @worker_pids.each do |cpid| - Process.kill(:KILL, cpid) rescue nil + process_kill(cpid) end # p(here: "execute command", pid: pid, exist: process_exist?(pid), worker_pids: @worker_pids, exists: @worker_pids.map{|i| process_exist?(i) }) Timeout.timeout(10){ sleep 0.1 while process_exist?(pid) }