connect failure scenarios
This commit is contained in:
47
tests/fakes/dest/close_after_hello.rb
Executable file
47
tests/fakes/dest/close_after_hello.rb
Executable file
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Wait for a sender connection, send a correct hello, then disconnect.
|
||||
# Simulate a server which crashes after sending the hello. We then
|
||||
# reopen the server socket to check that the sender retries: since the
|
||||
# command-line has gone away, and can't feed an error back to the
|
||||
# user, we have to keep trying.
|
||||
|
||||
addr, port = *ARGV
|
||||
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
|
||||
sock = TCPServer.new( addr, port )
|
||||
client_sock = nil
|
||||
|
||||
begin
|
||||
Timeout.timeout(2) do
|
||||
client_sock = sock.accept
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out waiting for a connection"
|
||||
exit 1
|
||||
end
|
||||
|
||||
|
||||
client_sock.write( "NBDMAGIC" )
|
||||
client_sock.write( "\x00\x00\x42\x02\x81\x86\x12\x53" )
|
||||
client_sock.write( "\x00\x00\x00\x00\x00\x00\x10\x00" )
|
||||
client_sock.write( "\x00" * 128 )
|
||||
|
||||
client_sock.close
|
||||
|
||||
new_sock = nil
|
||||
begin
|
||||
Timeout.timeout(60) do
|
||||
new_sock = sock.accept
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out waiting for a reconnection"
|
||||
exit 1
|
||||
end
|
||||
|
||||
new_sock.close
|
||||
sock.close
|
||||
|
||||
exit 0
|
34
tests/fakes/dest/hang_after_connect.rb
Executable file
34
tests/fakes/dest/hang_after_connect.rb
Executable file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Will open a server, accept a single connection, then sleep for 5
|
||||
# seconds. After that time, the client should have disconnected,
|
||||
# which we can can't effectively check.
|
||||
#
|
||||
# This allows the test runner to check that the command-line sees the
|
||||
# right error message after the timeout time.
|
||||
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
require 'flexnbd/constants'
|
||||
|
||||
addr, port = *ARGV
|
||||
|
||||
serve_sock = TCPServer.new( addr, port )
|
||||
client_sock = nil
|
||||
|
||||
begin
|
||||
# A failure here means a more serious issue with flexnbd
|
||||
Timeout.timeout( 2 ) do
|
||||
client_sock = serve_sock.accept
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Client didn't make connection"
|
||||
exit 1
|
||||
end
|
||||
|
||||
# Sleep for one second past the timeout (a bit of slop in case ruby
|
||||
# doesn't launch things quickly)
|
||||
sleep(FlexNBD::MS_HELLO_TIME_SECS + 1)
|
||||
|
||||
client_sock.close if client_sock
|
||||
serve_sock.close
|
46
tests/fakes/dest/hello_wrong_magic.rb
Executable file
46
tests/fakes/dest/hello_wrong_magic.rb
Executable file
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Simulate a destination which sends the wrong magic.
|
||||
# We expect the sender to disconnect and reconnect.
|
||||
|
||||
addr, port = *ARGV
|
||||
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
require 'flexnbd/constants'
|
||||
|
||||
sock = TCPServer.new( addr, port )
|
||||
client_sock = nil
|
||||
|
||||
begin
|
||||
Timeout.timeout(2) do
|
||||
client_sock = sock.accept
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out waiting for a connection"
|
||||
exit 1
|
||||
end
|
||||
|
||||
|
||||
t = Thread.new do
|
||||
begin
|
||||
Timeout.timeout(FlexNBD::MS_RETRY_DELAY_SECS + 1) do
|
||||
client_sock2 = sock.accept
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out waiting for a reconnection"
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
|
||||
client_sock.write( "NBDMAGIC" )
|
||||
# We're off in the last byte, should be \x53
|
||||
client_sock.write( "\x00\x00\x42\x02\x81\x86\x12\x52" )
|
||||
# 4096 is the right size; this is defined in nbd_scenarios
|
||||
client_sock.write( "\x00\x00\x00\x00\x00\x00\x10\x00" )
|
||||
client_sock.write( "\x00" * 128 )
|
||||
|
||||
|
||||
t.join
|
||||
|
||||
exit 0
|
44
tests/fakes/dest/hello_wrong_size.rb
Executable file
44
tests/fakes/dest/hello_wrong_size.rb
Executable file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Simulate a server which has a disc of the wrong size attached: send
|
||||
# a valid NBD hello with a random size, then check that we have see an
|
||||
# EOF on read.
|
||||
|
||||
addr, port = *ARGV
|
||||
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
require 'flexnbd/constants'
|
||||
|
||||
sock = TCPServer.new( addr, port )
|
||||
client_sock = nil
|
||||
|
||||
begin
|
||||
Timeout.timeout(2) do
|
||||
client_sock = sock.accept
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out waiting for a connection"
|
||||
exit 1
|
||||
end
|
||||
|
||||
t = Thread.new do
|
||||
begin
|
||||
Timeout.timeout(FlexNBD::MS_RETRY_DELAY_SECS + 1) do
|
||||
client_sock2 = sock.accept
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out waiting for a reconnection"
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
|
||||
client_sock.write( "NBDMAGIC" )
|
||||
client_sock.write( "\x00\x00\x42\x02\x81\x86\x12\x53" )
|
||||
8.times do client_sock.write rand(256).chr end
|
||||
client_sock.write( "\x00" * 128 )
|
||||
|
||||
|
||||
t.join
|
||||
|
||||
exit 0
|
22
tests/fakes/dest/reject_acl.rb
Executable file
22
tests/fakes/dest/reject_acl.rb
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Accept a connection, then immediately close it. This simulates an ACL rejection.
|
||||
|
||||
addr, port = *ARGV
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
|
||||
serve_sock = TCPServer.open( addr, port )
|
||||
|
||||
begin
|
||||
Timeout.timeout( 2 ) do
|
||||
serve_sock.accept.close
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out waiting for a connection"
|
||||
exit 1
|
||||
end
|
||||
|
||||
serve_sock.close
|
||||
|
||||
exit(0)
|
28
tests/fakes/source/close_after_connect.rb
Executable file
28
tests/fakes/source/close_after_connect.rb
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Connects to the destination server, then immediately disconnects,
|
||||
# simulating a source crash.
|
||||
#
|
||||
# It then connects again, to check that the destination is still
|
||||
# listening.
|
||||
|
||||
addr, port = *ARGV
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
|
||||
begin
|
||||
Timeout.timeout( 2 ) do
|
||||
sock = TCPSocket.open( addr, port.to_i )
|
||||
sock.close
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Failed to connect"
|
||||
exit 1
|
||||
end
|
||||
|
||||
Timeout.timeout( 2 ) do
|
||||
sock = TCPSocket.open( addr, port.to_i )
|
||||
sock.close
|
||||
end
|
||||
|
||||
exit 0
|
49
tests/fakes/source/close_after_hello.rb
Executable file
49
tests/fakes/source/close_after_hello.rb
Executable file
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Connect, read the hello, then immediately disconnect. This
|
||||
# simulates a sender which dislikes something in the hello message - a
|
||||
# wrong size, for instance.
|
||||
|
||||
# After the disconnect, we reconnect to be sure that the destination
|
||||
# is still alive.
|
||||
|
||||
|
||||
require 'socket'
|
||||
require "timeout"
|
||||
require 'flexnbd/constants'
|
||||
|
||||
addr, port = *ARGV
|
||||
|
||||
client_sock = nil
|
||||
begin
|
||||
Timeout.timeout(2) do
|
||||
client_sock = TCPSocket.open( addr, port )
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out connecting."
|
||||
exit 1
|
||||
end
|
||||
|
||||
begin
|
||||
Timeout.timeout( FlexNBD::MS_HELLO_TIME_SECS ) do
|
||||
fail "No hello." unless (hello = client_sock.read( 152 )) &&
|
||||
hello.length==152
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out waiting for hello."
|
||||
exit 1
|
||||
end
|
||||
|
||||
client_sock.close
|
||||
|
||||
sleep(0.2)
|
||||
begin
|
||||
Timeout.timeout(2) do
|
||||
client_sock = TCPSocket.open( addr, port )
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out reconnecting."
|
||||
exit 1
|
||||
end
|
||||
|
||||
exit(0)
|
69
tests/fakes/source/hang_after_hello.rb
Executable file
69
tests/fakes/source/hang_after_hello.rb
Executable file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Simulate the hello message going astray, or the source hanging after
|
||||
# receiving it.
|
||||
#
|
||||
# We then connect again, to confirm that the destination is still
|
||||
# listening for an incoming migration.
|
||||
|
||||
addr, port = *ARGV
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
|
||||
require "flexnbd/constants"
|
||||
|
||||
|
||||
client_sock=nil
|
||||
begin
|
||||
Timeout.timeout(2) do
|
||||
client_sock = TCPSocket.open( addr, port )
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out connecting"
|
||||
exit 1
|
||||
end
|
||||
|
||||
begin
|
||||
Timeout.timeout(FlexNBD::MS_HELLO_TIME_SECS) do
|
||||
client_sock.read(152)
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out reading hello"
|
||||
exit 1
|
||||
end
|
||||
|
||||
# Now we do two things:
|
||||
|
||||
# - In the parent process, we sleep for CLIENT_MAX_WAIT_SECS+5, which
|
||||
# will make the destination give up and close the connection.
|
||||
# - In the child process, we sleep for CLIENT_MAX_WAIT_SECS+1, which
|
||||
# should be able to reconnect despite the parent process not having
|
||||
# closed its end yet.
|
||||
|
||||
kidpid = fork do
|
||||
client_sock.close
|
||||
new_sock = nil
|
||||
sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 1 )
|
||||
begin
|
||||
Timeout.timeout( 2 ) do
|
||||
new_sock = TCPSocket.open( addr, port )
|
||||
end
|
||||
Timeout.timeout( FlexNBD::MS_HELLO_TIME_SECS ) do
|
||||
fail "No hello." unless (hello = new_sock.read( 152 )) &&
|
||||
hello.length==152
|
||||
end
|
||||
new_sock.close
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out reconnecting"
|
||||
exit 1
|
||||
end
|
||||
exit 0
|
||||
end
|
||||
|
||||
# Sleep for longer than the child, to give the flexnbd process a bit
|
||||
# of slop
|
||||
sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 3 )
|
||||
client_sock.close
|
||||
|
||||
_,status = Process.waitpid2( kidpid )
|
||||
exit status.exitstatus
|
Reference in New Issue
Block a user