Skip to content

Commit

Permalink
test: add tests for transfer and watch_transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardacoppo committed Dec 9, 2024
1 parent ff58314 commit 50b7975
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/syskit/cli/log_runtime_archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
106 changes: 106 additions & 0 deletions test/cli/test_log_runtime_archive_main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -128,6 +129,104 @@ 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
<<<<<<< Updated upstream

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
=======
>>>>>>> Stashed changes
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)
Expand All @@ -149,6 +248,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?)
Expand Down

0 comments on commit 50b7975

Please sign in to comment.