1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
require 'time'
require 'helper'
module SSHKit
class TestCoordinator < UnitTest
def setup
super
@output = String.new
SSHKit.config.output_verbosity = :debug
SSHKit.config.output = SSHKit::Formatter::SimpleText.new(@output)
SSHKit.config.backend = SSHKit::Backend::Printer
end
def echo_time
lambda do |_host|
execute "echo #{Time.now.to_f}"
end
end
def test_the_connection_manager_handles_empty_argument
Coordinator.new([]).each do
raise "This should not be executed"
end
end
def test_connection_manager_handles_a_single_argument
h = Host.new('1.example.com')
Host.expects(:new).with('1.example.com').once().returns(h)
Coordinator.new '1.example.com'
end
def test_connection_manager_resolves_hosts
h = Host.new('n.example.com')
Host.expects(:new).times(3).returns(h)
Coordinator.new %w{1.example.com 2.example.com 3.example.com}
end
def test_the_connection_manager_yields_the_host_to_each_connection_instance
Coordinator.new(%w{1.example.com}).each do |host|
execute "echo #{host.hostname}"
end
assert_equal "Command: echo 1.example.com\n", actual_output_commands.last
end
def test_the_connection_manager_can_run_things_in_sequence
Coordinator.new(%w{1.example.com 2.example.com}).each in: :sequence, &echo_time
assert_equal 2, actual_execution_times.length
assert_at_least_1_sec_apart(actual_execution_times.first, actual_execution_times.last)
end
class MyRunner < SSHKit::Runner::Parallel
def execute
threads = hosts.map do |host|
Thread.new(host) do |h|
b = backend(h, &block)
b.run
b.warn "custom runner out"
end
end
threads.each(&:join)
end
end
def test_the_connection_manager_can_run_things_in_sequence_with_wait
start = Time.now
Coordinator.new(%w{1.example.com 2.example.com}).each in: :sequence, wait: 10, &echo_time
stop = Time.now
assert_operator(stop - start, :>=, 10.0)
end
private
def assert_at_least_1_sec_apart(first_time, last_time)
assert_operator(last_time - first_time, :>, 1.0)
end
def assert_at_least_5_sec_apart(first_time, last_time)
assert_operator(last_time - first_time, :>, 5.0)
end
def actual_execution_times
actual_output_commands.map { |line| line.split(' ').last.to_f }
end
def actual_output_commands
@output.lines.select { |line| line.start_with?('Command:') }
end
end
end
|