2013-02-15 16:51:28 +00:00
|
|
|
require 'test/unit'
|
|
|
|
require 'environment'
|
|
|
|
require 'flexnbd/fake_source'
|
2018-02-08 22:28:34 +00:00
|
|
|
require 'ld_preload'
|
2013-02-15 16:51:28 +00:00
|
|
|
|
|
|
|
class TestServeMode < Test::Unit::TestCase
|
2018-02-08 22:28:34 +00:00
|
|
|
include LdPreload
|
|
|
|
|
2013-02-15 16:51:28 +00:00
|
|
|
def setup
|
|
|
|
super
|
2016-10-05 11:54:09 +01:00
|
|
|
@b = "\xFF".b
|
2013-02-15 16:51:28 +00:00
|
|
|
@env = Environment.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
@env.cleanup
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def connect_to_server
|
2018-02-06 10:24:54 +00:00
|
|
|
@env.writefile1('0')
|
|
|
|
@env.serve1
|
2018-02-02 21:34:14 +00:00
|
|
|
client = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
|
2013-02-15 16:51:28 +00:00
|
|
|
begin
|
|
|
|
result = client.read_hello
|
2018-02-02 21:27:12 +00:00
|
|
|
assert_equal 'NBDMAGIC', result[:passwd]
|
|
|
|
assert_equal 0x00420281861253, result[:magic]
|
2013-02-15 16:51:28 +00:00
|
|
|
assert_equal @env.file1.size, result[:size]
|
2018-02-03 20:10:47 +00:00
|
|
|
# See src/common/nbdtypes.h for the various flags. At the moment we
|
|
|
|
# support HAS_FLAGS (1), SEND_FLUSH (4), SEND_FUA (8)
|
|
|
|
assert_equal (1 | 4 | 8), result[:flags]
|
2018-02-02 21:27:12 +00:00
|
|
|
assert_equal "\x0" * 124, result[:reserved]
|
2013-02-15 16:51:28 +00:00
|
|
|
yield client
|
|
|
|
ensure
|
2018-02-02 21:34:14 +00:00
|
|
|
begin
|
|
|
|
client.close
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
|
|
|
end
|
2013-02-15 16:51:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bad_request_magic_receives_error_response
|
|
|
|
connect_to_server do |client|
|
|
|
|
# replace REQUEST_MAGIC with all 0s to make it look bad
|
2018-02-02 21:34:14 +00:00
|
|
|
client.send_request(0, 'myhandle', 0, 0, "\x00\x00\x00\x00")
|
2013-02-15 16:51:28 +00:00
|
|
|
rsp = client.read_response
|
|
|
|
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
2018-02-02 21:34:14 +00:00
|
|
|
assert_equal 'myhandle', rsp[:handle]
|
2013-02-15 16:51:28 +00:00
|
|
|
assert rsp[:error] != 0, "Server sent success reply back: #{rsp[:error]}"
|
|
|
|
|
|
|
|
# The client should be disconnected now
|
2018-02-02 21:34:14 +00:00
|
|
|
assert client.disconnected?, 'Server not disconnected'
|
2013-02-15 16:51:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-13 15:17:15 +01:00
|
|
|
def test_long_write_on_top_of_short_write_is_respected
|
|
|
|
connect_to_server do |client|
|
|
|
|
# Start with a file of all-zeroes.
|
2018-02-02 21:34:14 +00:00
|
|
|
client.write(0, "\x00" * @env.file1.size)
|
2013-09-13 15:17:15 +01:00
|
|
|
rsp = client.read_response
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
|
|
|
assert_equal 0, rsp[:error]
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
client.write(0, @b)
|
2013-09-13 15:17:15 +01:00
|
|
|
rsp = client.read_response
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
|
|
|
assert_equal 0, rsp[:error]
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
client.write(0, @b * 2)
|
2013-09-13 15:17:15 +01:00
|
|
|
rsp = client.read_response
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
|
|
|
assert_equal 0, rsp[:error]
|
|
|
|
end
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
assert_equal @b * 2, @env.file1.read(0, 2)
|
2013-09-13 15:17:15 +01:00
|
|
|
end
|
|
|
|
|
2013-02-15 16:51:28 +00:00
|
|
|
def test_read_request_out_of_bounds_receives_error_response
|
|
|
|
connect_to_server do |client|
|
2018-02-02 21:34:14 +00:00
|
|
|
client.write_read_request(@env.file1.size, 4096)
|
2013-02-15 16:51:28 +00:00
|
|
|
rsp = client.read_response
|
|
|
|
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
2018-02-02 21:34:14 +00:00
|
|
|
assert_equal 'myhandle', rsp[:handle]
|
2018-02-08 13:19:51 +00:00
|
|
|
# NBD protocol suggests ENOSPC (28) is returned
|
|
|
|
assert_equal 28, rsp[:error], 'Server sent incorrect response'
|
2013-02-15 16:51:28 +00:00
|
|
|
|
|
|
|
# Ensure we're not disconnected by sending a request. We don't care about
|
|
|
|
# whether the reply is good or not, here.
|
2018-02-02 21:34:14 +00:00
|
|
|
client.write_read_request(0, 4096)
|
2013-02-15 16:51:28 +00:00
|
|
|
rsp = client.read_response
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_write_request_out_of_bounds_receives_error_response
|
|
|
|
connect_to_server do |client|
|
2018-02-02 21:34:14 +00:00
|
|
|
client.write(@env.file1.size, "\x00" * 4096)
|
2013-02-15 16:51:28 +00:00
|
|
|
rsp = client.read_response
|
|
|
|
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
2018-02-02 21:34:14 +00:00
|
|
|
assert_equal 'myhandle', rsp[:handle]
|
2018-02-08 13:19:51 +00:00
|
|
|
# NBD protocol suggests ENOSPC (28) is returned
|
|
|
|
assert_equal 28, rsp[:error], 'Server sent incorrect response'
|
|
|
|
|
|
|
|
# Ensure we're not disconnected by sending a request. We don't care about
|
|
|
|
# whether the reply is good or not, here.
|
|
|
|
client.write(0, "\x00" * @env.file1.size)
|
|
|
|
rsp = client.read_response
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unknown_command_receives_error_response
|
|
|
|
connect_to_server do |client|
|
|
|
|
client.send_request(123)
|
|
|
|
rsp = client.read_response
|
|
|
|
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
|
|
|
assert_equal 'myhandle', rsp[:handle]
|
|
|
|
# NBD protocol suggests EINVAL (22) is returned
|
|
|
|
assert_equal 22, rsp[:error], 'Server sent incorrect response'
|
2013-02-15 16:51:28 +00:00
|
|
|
|
|
|
|
# Ensure we're not disconnected by sending a request. We don't care about
|
|
|
|
# whether the reply is good or not, here.
|
2018-02-02 21:34:14 +00:00
|
|
|
client.write(0, "\x00" * @env.file1.size)
|
2013-02-15 16:51:28 +00:00
|
|
|
rsp = client.read_response
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
|
|
|
end
|
|
|
|
end
|
2018-02-03 20:10:47 +00:00
|
|
|
|
|
|
|
def test_flush_is_accepted
|
2018-02-08 22:28:34 +00:00
|
|
|
with_ld_preload('msync_logger') do
|
|
|
|
connect_to_server do |client|
|
|
|
|
client.flush
|
|
|
|
rsp = client.read_response
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
|
|
|
assert_equal 0, rsp[:error]
|
|
|
|
end
|
2018-02-08 23:07:17 +00:00
|
|
|
op = parse_ld_preload_logs('msync_logger')
|
2018-02-08 22:28:34 +00:00
|
|
|
assert_equal 1, op.count, 'Only one msync expected'
|
|
|
|
assert_equal @env.blocksize, op.first[2], 'msync length wrong'
|
|
|
|
assert_equal 6, op.first[3], 'msync called with incorrect flags'
|
2018-02-03 20:10:47 +00:00
|
|
|
end
|
|
|
|
end
|
2018-02-03 20:29:15 +00:00
|
|
|
|
|
|
|
def test_write_with_fua_is_accepted
|
2018-02-08 22:28:34 +00:00
|
|
|
with_ld_preload('msync_logger') do
|
|
|
|
page_size = Integer(`getconf PAGESIZE`)
|
|
|
|
|
|
|
|
@env.blocksize = page_size * 10
|
|
|
|
connect_to_server do |client|
|
|
|
|
# Write somewhere in the third page
|
|
|
|
pos = page_size * 3 + 100
|
|
|
|
client.write_with_fua(pos, "\x00" * 33)
|
|
|
|
rsp = client.read_response
|
|
|
|
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
|
|
|
assert_equal 0, rsp[:error]
|
|
|
|
end
|
2018-02-08 23:07:17 +00:00
|
|
|
op = parse_ld_preload_logs('msync_logger')
|
2018-02-07 21:45:20 +00:00
|
|
|
|
2018-02-08 22:28:34 +00:00
|
|
|
assert_equal 1, op.count, 'Only one msync expected'
|
2018-02-07 21:45:20 +00:00
|
|
|
|
2018-02-08 22:28:34 +00:00
|
|
|
# Should be 100 + 33, as we've started writing 100 bytes into a page, for
|
|
|
|
# 33 bytes
|
|
|
|
assert_equal 133, op.first[2], 'msync length wrong'
|
|
|
|
assert_equal 6, op.first[3], 'msync called with incorrect flags'
|
|
|
|
end
|
2018-02-03 20:29:15 +00:00
|
|
|
end
|
2018-02-07 21:45:20 +00:00
|
|
|
|
2018-02-08 16:31:28 +00:00
|
|
|
def test_odd_size_discs_are_truncated_to_nearest_512
|
|
|
|
# This should get rounded down to 1024
|
|
|
|
@env.blocksize = 1024 + 511
|
|
|
|
@env.writefile1('0')
|
|
|
|
@env.serve1
|
|
|
|
client = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
|
|
|
|
begin
|
|
|
|
result = client.read_hello
|
|
|
|
assert_equal 'NBDMAGIC', result[:passwd]
|
|
|
|
assert_equal 0x00420281861253, result[:magic]
|
|
|
|
assert_equal 1024, result[:size]
|
|
|
|
client.close
|
|
|
|
end
|
|
|
|
end
|
2018-02-08 23:07:17 +00:00
|
|
|
|
|
|
|
def test_server_sets_tcpkeepalive
|
|
|
|
with_ld_preload('setsockopt_logger') do
|
|
|
|
connect_to_server(&:close)
|
2018-02-09 10:26:08 +00:00
|
|
|
op = read_ld_preload_log('setsockopt_logger')
|
|
|
|
assert_func_call(op,
|
|
|
|
['setsockopt', '\d+',
|
|
|
|
Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1, 0],
|
|
|
|
'TCP Keepalive not successfully set')
|
|
|
|
assert_func_call(op,
|
|
|
|
['setsockopt', '\d+',
|
|
|
|
Socket::SOL_TCP, Socket::TCP_KEEPIDLE, 30, 0],
|
|
|
|
'TCP Keepalive idle timeout not set to 30s')
|
|
|
|
assert_func_call(op,
|
|
|
|
['setsockopt', '\d+',
|
|
|
|
Socket::SOL_TCP, Socket::TCP_KEEPINTVL, 10, 0],
|
|
|
|
'TCP keepalive retry time not set to 10s')
|
|
|
|
assert_func_call(op,
|
|
|
|
['setsockopt', '\d+',
|
|
|
|
Socket::SOL_TCP, Socket::TCP_KEEPCNT, 3, 0],
|
|
|
|
'TCP keepalive count not set to 3')
|
2018-02-08 23:07:17 +00:00
|
|
|
end
|
|
|
|
end
|
2018-02-16 12:58:03 +00:00
|
|
|
|
|
|
|
def test_status_returns_correct_client_count
|
2018-02-16 13:46:31 +00:00
|
|
|
@env.writefile1('0')
|
|
|
|
@env.serve1
|
|
|
|
assert_equal('0', @env.status1['num_clients'])
|
|
|
|
client = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
|
|
|
|
assert_equal('1', @env.status1['num_clients'])
|
|
|
|
client2 = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
|
|
|
|
assert_equal('2', @env.status1['num_clients'])
|
|
|
|
client2.close
|
|
|
|
client.close
|
|
|
|
assert_equal('0', @env.status1['num_clients'])
|
2018-02-16 12:58:03 +00:00
|
|
|
end
|
2013-02-15 16:51:28 +00:00
|
|
|
end
|