Moved acceptance tests into tests/acceptance
This commit is contained in:
36
tests/acceptance/flexnbd/constants.rb
Normal file
36
tests/acceptance/flexnbd/constants.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
# encoding: utf-8
|
||||
|
||||
module FlexNBD
|
||||
|
||||
# eeevil is his one and only name...
|
||||
def self.read_constants
|
||||
parents = []
|
||||
current = File.expand_path(".")
|
||||
while current != "/"
|
||||
parents << current
|
||||
current = File.expand_path( File.join( current, ".." ) )
|
||||
end
|
||||
|
||||
source_root = parents.find do |dirname|
|
||||
File.directory?( File.join( dirname, "src" ) )
|
||||
end
|
||||
|
||||
fail "No source root!" unless source_root
|
||||
|
||||
headers = Dir[File.join( source_root, "src", "*.h" ) ]
|
||||
|
||||
headers.each do |header_filename|
|
||||
txt_lines = File.readlines( header_filename )
|
||||
txt_lines.each do |line|
|
||||
if line =~ /^#\s*define\s+([A-Z0-9_]+)\s+(\d+)\s*$/
|
||||
const_set($1, $2.to_i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
read_constants()
|
||||
end # module FlexNBD
|
||||
|
||||
|
127
tests/acceptance/flexnbd/fake_dest.rb
Normal file
127
tests/acceptance/flexnbd/fake_dest.rb
Normal file
@@ -0,0 +1,127 @@
|
||||
# encoding: utf-8
|
||||
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
|
||||
require 'flexnbd/constants'
|
||||
|
||||
module FlexNBD
|
||||
class FakeDest
|
||||
|
||||
class Client
|
||||
def initialize( sock )
|
||||
@sock = sock
|
||||
end
|
||||
|
||||
|
||||
def write_hello( opts = {} )
|
||||
self.class.write_hello( @sock, opts )
|
||||
end
|
||||
|
||||
def read_request()
|
||||
self.class.read_request( @sock )
|
||||
end
|
||||
|
||||
def write_error( handle )
|
||||
self.class.write_error( @sock, handle )
|
||||
end
|
||||
|
||||
def close
|
||||
@sock.close
|
||||
end
|
||||
|
||||
|
||||
def self.write_hello( client_sock, opts={} )
|
||||
client_sock.write( "NBDMAGIC" )
|
||||
|
||||
if opts[:magic] == :wrong
|
||||
client_sock.write( "\x00\x00\x42\x02\x81\x86\x12\x52" )
|
||||
else
|
||||
client_sock.write( "\x00\x00\x42\x02\x81\x86\x12\x53" )
|
||||
end
|
||||
|
||||
if opts[:size] == :wrong
|
||||
8.times do client_sock.write rand(256).chr end
|
||||
else
|
||||
client_sock.write( "\x00\x00\x00\x00\x00\x00\x10\x00" )
|
||||
end
|
||||
|
||||
client_sock.write( "\x00" * 128 )
|
||||
end
|
||||
|
||||
|
||||
|
||||
def self.read_request( client_sock )
|
||||
req = client_sock.read(28)
|
||||
|
||||
magic_s = req[0 ... 4 ]
|
||||
type_s = req[4 ... 8 ]
|
||||
handle_s = req[8 ... 16]
|
||||
from_s = req[16 ... 24]
|
||||
len_s = req[24 ... 28]
|
||||
|
||||
{
|
||||
:magic => magic_s,
|
||||
:type => type_s.unpack("N").first,
|
||||
:handle => handle_s,
|
||||
:from => parse_be64( from_s ),
|
||||
:len => len_s.unpack( "N").first
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
def self.parse_be64(str)
|
||||
raise "String is the wrong length: 8 bytes expected (#{str.length} received)" unless
|
||||
str.length == 8
|
||||
|
||||
top, bottom = str.unpack("NN")
|
||||
(top << 32) + bottom
|
||||
end
|
||||
|
||||
|
||||
def self.write_error( client_sock, handle )
|
||||
client_sock.write( "\x67\x44\x66\x98")
|
||||
client_sock.write( "\x00\x00\x00\x01")
|
||||
client_sock.write( handle )
|
||||
end
|
||||
|
||||
|
||||
end # class Client
|
||||
|
||||
|
||||
def initialize( addr, port )
|
||||
@sock = self.class.serve( addr, port )
|
||||
end
|
||||
|
||||
def accept( err_msg = "Timed out waiting for a connection", timeout = 2)
|
||||
Client.new( self.class.accept( @sock, err_msg, timeout ) )
|
||||
end
|
||||
|
||||
def close
|
||||
@sock.close
|
||||
end
|
||||
|
||||
|
||||
def self.serve( addr, port )
|
||||
TCPServer.new( addr, port )
|
||||
end
|
||||
|
||||
|
||||
def self.accept( sock, err_msg, timeout )
|
||||
client_sock = nil
|
||||
|
||||
begin
|
||||
Timeout.timeout(timeout) do
|
||||
client_sock = sock.accept
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts err_msg
|
||||
exit 1
|
||||
end
|
||||
|
||||
client_sock
|
||||
end
|
||||
|
||||
|
||||
end # module FakeDest
|
||||
end # module FlexNBD
|
65
tests/acceptance/flexnbd/fake_source.rb
Normal file
65
tests/acceptance/flexnbd/fake_source.rb
Normal file
@@ -0,0 +1,65 @@
|
||||
# encoding: utf-8
|
||||
|
||||
require 'socket'
|
||||
require "timeout"
|
||||
require 'flexnbd/constants'
|
||||
|
||||
module FlexNBD
|
||||
module FakeSource
|
||||
|
||||
def connect( addr, port, err_msg, source_addr=nil, source_port=0 )
|
||||
timing_out( 2, err_msg ) do
|
||||
TCPSocket.new( addr, port, source_addr, source_port )
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def read_hello( client_sock )
|
||||
timing_out( FlexNBD::MS_HELLO_TIME_SECS,
|
||||
"Timed out waiting for hello." ) do
|
||||
fail "No hello." unless (hello = client_sock.read( 152 )) &&
|
||||
hello.length==152
|
||||
hello
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def write_write_request( client_sock, from, len, handle )
|
||||
fail "Bad handle" unless handle.length == 8
|
||||
|
||||
client_sock.write( "\x25\x60\x95\x13" )
|
||||
client_sock.write( "\x00\x00\x00\x01" )
|
||||
client_sock.write( handle )
|
||||
client_sock.write( "\x0"*4 )
|
||||
client_sock.write( [from].pack( 'N' ) )
|
||||
client_sock.write( [len ].pack( 'N' ) )
|
||||
end
|
||||
|
||||
|
||||
def read_response( client_sock )
|
||||
magic = client_sock.read(4)
|
||||
error_s = client_sock.read(4)
|
||||
handle = client_sock.read(8)
|
||||
|
||||
{
|
||||
:magic => magic,
|
||||
:error => error_s.unpack("N"),
|
||||
:handle => handle
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
def timing_out( time, msg )
|
||||
begin
|
||||
Timeout.timeout( time ) do
|
||||
yield
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts msg
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end # module FakeSource
|
||||
end # module FlexNBD
|
Reference in New Issue
Block a user