From c101f33d27e22549ba6a747ddf9d125910ae6f0b Mon Sep 17 00:00:00 2001 From: eduardacoppo Date: Thu, 5 Dec 2024 18:30:29 -0300 Subject: [PATCH] test: add tests for transfer and watch_transfer --- lib/syskit/cli/log_runtime_archive.rb | 3 +- test/cli/test_log_runtime_archive_main.rb | 103 ++++++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/lib/syskit/cli/log_runtime_archive.rb b/lib/syskit/cli/log_runtime_archive.rb index f1224d93a..6ba025189 100644 --- a/lib/syskit/cli/log_runtime_archive.rb +++ b/lib/syskit/cli/log_runtime_archive.rb @@ -52,7 +52,8 @@ def process_root_folder # @param [Params] server_params the FTP server parameters: # { host, port, certificate, user, password } def process_transfer(src_dir, server_params) - host, port = server_params[:host], server_params[:port] + host = server_params[:host] + port = server_params[:port] socket = begin TCPSocket.new(host, port) rescue Errno::ECONNREFUSED => e diff --git a/test/cli/test_log_runtime_archive_main.rb b/test/cli/test_log_runtime_archive_main.rb index e30adb9d0..e2f5097f7 100644 --- a/test/cli/test_log_runtime_archive_main.rb +++ b/test/cli/test_log_runtime_archive_main.rb @@ -2,6 +2,7 @@ require "syskit/test/self" require "syskit/cli/log_runtime_archive_main" +require "syskit/roby_app/tmp_root_ca" module Syskit module CLI @@ -128,6 +129,101 @@ def call_archive(root_path, archive_path, low_limit, freed_limit) end end + describe "#watch_transfer" do + before do + @src_dir = make_tmppath + @tgt_dir = make_tmppath + host = "127.0.0.1" + ca = RobyApp::TmpRootCA.new(host) + user = "nilvo" + password = "nilvo123" + + server = spawn_server(@tgt_dir, user, password, ca) + port = server.port + + @server_params = { + host: host, port: port, certificate: "", + user: user, password: password + } + end + + it "calls transfer with the specified period" do + quit = Class.new(RuntimeError) + called = 0 + flexmock(LogRuntimeArchive) + .new_instances + .should_receive(:process_transfer) + .pass_thru do + called += 1 + raise quit if called == 3 + end + + tic = Time.now + assert_raises(quit) do + LogRuntimeArchiveMain.start( + ["watch_transfer", + @src_dir, @tgt_dir, @server_params, "--period", 0.5] + ) + end + + assert called == 3 + assert_operator(Time.now - tic, :>, 0.9) + end + + it "retries on ENOSPC" do + quit = Class.new(RuntimeError) + called = 0 + flexmock(LogRuntimeArchive) + .new_instances + .should_receive(:process_transfer) + .pass_thru do + called += 1 + raise quit if called == 3 + + raise Errno::ENOSPC + end + + tic = Time.now + assert_raises(quit) do + LogRuntimeArchiveMain.start( + ["watch_transfer", + @src_dir, @tgt_dir, @server_params, "--period", 0.5] + ) + end + assert_operator(Time.now - tic, :<, 1) + end + end + + describe "#transfer" do + before do + @src_dir = make_tmppath + @tgt_dir = make_tmppath + end + + it "raises ArgumentError if src_dir does not exist" do + e = assert_raises ArgumentError do + call_transfer("/does/not/exist", @tgt_dir, {}) + end + assert_equal "/does/not/exist does not exist, or is not a directory", + e.message + end + + it "raises ArgumentError if tgt_dir does not exist" do + e = assert_raises ArgumentError do + call_transfer(@src_dir, "/does/not/exist", {}) + end + assert_equal "/does/not/exist does not exist, or is not a directory", + e.message + end + + # Call 'transfer' function instead of 'watch' to call transfer once + def call_transfer(src_dir, tgt_dir, params) + LogRuntimeArchiveMain.start( + ["transfer", src_dir, tgt_dir, params] + ) + end + end + # Mock files sizes in bytes # @param [Array] size of files in MB def mock_files_size(sizes) @@ -149,6 +245,13 @@ def mock_available_space(total_available_disk_space) end end + def spawn_server(tgt_dir, user, password, cert) + LogRuntimeArchiveMain.start( + ["transfer_server", + tgt_dir, user, password, cert.private_certificate_path] + ) + end + def assert_deleted_files(deleted_files) if deleted_files.empty? files = @archive_dir.each_child.select(&:file?)