This commit is contained in:
Patrick J Cherry
2018-02-02 21:34:14 +00:00
parent 1b7b688f7a
commit 9c48da82cc
40 changed files with 666 additions and 858 deletions

View File

@@ -1,4 +1,4 @@
# encoding: utf-8
require 'flexnbd/fake_source'
require 'flexnbd/fake_dest'
@@ -7,20 +7,23 @@ module ProxyTests
"\xFF".b
end
def with_proxied_client( override_size = nil )
def with_proxied_client(override_size = nil)
@env.serve1 unless @server_up
@env.proxy2 unless @proxy_up
@env.nbd2.can_die(0)
client = FlexNBD::FakeSource.new(@env.ip, @env.port2, "Couldn't connect to proxy")
begin
result = client.read_hello
assert_equal "NBDMAGIC", result[:passwd]
assert_equal 'NBDMAGIC', result[:passwd]
assert_equal override_size || @env.file1.size, result[:size]
yield client
ensure
client.close rescue nil
begin
client.close
rescue StandardError
nil
end
end
end
@@ -32,11 +35,11 @@ module ProxyTests
with_proxied_client do |client|
(0..3).each do |n|
offset = n * 4096
client.write_read_request(offset, 4096, "myhandle")
client.write_read_request(offset, 4096, 'myhandle')
rsp = client.read_response
assert_equal ::FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal "myhandle", rsp[:handle]
assert_equal 'myhandle', rsp[:handle]
assert_equal 0, rsp[:error]
orig_data = @env.file1.read(offset, 4096)
@@ -45,8 +48,8 @@ module ProxyTests
assert_equal 4096, orig_data.size
assert_equal 4096, data.size
assert_equal( orig_data, data,
"Returned data does not match on request #{n+1}" )
assert_equal(orig_data, data,
"Returned data does not match on request #{n + 1}")
end
end
end
@@ -59,12 +62,12 @@ module ProxyTests
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal "myhandle", rsp[:handle]
assert_equal 'myhandle', rsp[:handle]
assert_equal 0, rsp[:error]
data = @env.file1.read(offset, 4096)
assert_equal( ( b * 4096 ), data, "Data not written correctly (offset is #{n})" )
assert_equal((b * 4096), data, "Data not written correctly (offset is #{n})")
end
end
end
@@ -78,7 +81,7 @@ module ProxyTests
sc = server.accept # just tell the supervisor we're up
sc.write_hello
[ server, sc ]
[server, sc]
end
end
@@ -89,7 +92,7 @@ module ProxyTests
server, sc1 = maker.value
# Send the read request to the proxy
client.write_read_request( 0, 4096 )
client.write_read_request(0, 4096)
# ensure we're given the read request
req1 = sc1.read_request
@@ -110,8 +113,8 @@ module ProxyTests
assert_equal req1, req2
# The reply should be proxied back to the client.
sc2.write_reply( req2[:handle] )
sc2.write_data( b * 4096 )
sc2.write_reply(req2[:handle])
sc2.write_data(b * 4096)
# Check it to make sure it's correct
rsp = Timeout.timeout(15) { client.read_response }
@@ -119,13 +122,12 @@ module ProxyTests
assert_equal 0, rsp[:error]
assert_equal req1[:handle], rsp[:handle]
data = client.read_raw( 4096 )
assert_equal( (b * 4096), data, "Wrong data returned" )
data = client.read_raw(4096)
assert_equal((b * 4096), data, 'Wrong data returned')
sc2.close
server.close
end
end
def test_write_request_retried_when_upstream_dies_partway
@@ -135,7 +137,7 @@ module ProxyTests
server, sc1 = maker.value
# Send the read request to the proxy
client.write( 0, ( b * 4096 ) )
client.write(0, (b * 4096))
# ensure we're given the read request
req1 = sc1.read_request
@@ -143,8 +145,8 @@ module ProxyTests
assert_equal ::FlexNBD::REQUEST_WRITE, req1[:type]
assert_equal 0, req1[:from]
assert_equal 4096, req1[:len]
data1 = sc1.read_data( 4096 )
assert_equal( ( b * 4096 ), data1, "Data not proxied successfully" )
data1 = sc1.read_data(4096)
assert_equal((b * 4096), data1, 'Data not proxied successfully')
# Kill the server again, now we're sure the read request has been sent once
sc1.close
@@ -156,11 +158,11 @@ module ProxyTests
# And once reconnected, it should resend an identical request.
req2 = sc2.read_request
assert_equal req1, req2
data2 = sc2.read_data( 4096 )
data2 = sc2.read_data(4096)
assert_equal data1, data2
# The reply should be proxied back to the client.
sc2.write_reply( req2[:handle] )
sc2.write_reply(req2[:handle])
# Check it to make sure it's correct
rsp = Timeout.timeout(15) { client.read_response }
@@ -174,8 +176,7 @@ module ProxyTests
end
def test_only_one_client_can_connect_to_proxy_at_a_time
with_proxied_client do |client|
with_proxied_client do |_client|
c2 = nil
assert_raises(Timeout::Error) do
Timeout.timeout(1) do
@@ -183,12 +184,13 @@ module ProxyTests
c2.read_hello
end
end
c2.close rescue nil if c2
if c2
begin
c2.close
rescue StandardError
nil
end
end
end
end
end