2012-07-03 13:33:52 +01:00
|
|
|
require 'flexnbd'
|
|
|
|
require 'file_writer'
|
|
|
|
|
|
|
|
class Environment
|
2018-02-02 21:34:14 +00:00
|
|
|
attr_reader(:blocksize, :filename1, :filename2, :ip,
|
|
|
|
:port1, :port2, :nbd1, :nbd2, :file1, :file2)
|
2012-07-03 13:33:52 +01:00
|
|
|
|
|
|
|
def initialize
|
2018-02-06 09:55:32 +00:00
|
|
|
@blocksize = 1024
|
2018-02-02 21:34:14 +00:00
|
|
|
@filename1 = "/tmp/.flexnbd.test.#{$PROCESS_ID}.#{Time.now.to_i}.1"
|
|
|
|
@filename2 = "/tmp/.flexnbd.test.#{$PROCESS_ID}.#{Time.now.to_i}.2"
|
|
|
|
@ip = '127.0.0.1'
|
|
|
|
@available_ports = [*40_000..41_000] - listening_ports
|
2012-07-03 13:33:52 +01:00
|
|
|
@port1 = @available_ports.shift
|
|
|
|
@port2 = @available_ports.shift
|
2018-02-02 21:34:14 +00:00
|
|
|
@nbd1 = FlexNBD::FlexNBD.new('../../build/flexnbd', @ip, @port1)
|
|
|
|
@nbd2 = FlexNBD::FlexNBD.new('../../build/flexnbd', @ip, @port2)
|
2012-07-03 13:33:52 +01:00
|
|
|
|
|
|
|
@fake_pid = nil
|
|
|
|
end
|
|
|
|
|
2018-02-06 09:55:32 +00:00
|
|
|
def blocksize=(b)
|
|
|
|
raise RuntimeError, "Unable to change blocksize after files have been opened" if @file1 or @file2
|
|
|
|
@blocksize = b
|
|
|
|
end
|
|
|
|
|
2014-02-27 14:21:36 +00:00
|
|
|
def prefetch_proxy!
|
|
|
|
@nbd1.prefetch_proxy = true
|
|
|
|
@nbd2.prefetch_proxy = true
|
|
|
|
end
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
def proxy1(port = @port2)
|
2013-02-15 16:52:16 +00:00
|
|
|
@nbd1.proxy(@ip, port)
|
|
|
|
end
|
2018-02-02 21:34:14 +00:00
|
|
|
|
|
|
|
def proxy2(port = @port1)
|
2013-02-15 16:52:16 +00:00
|
|
|
@nbd2.proxy(@ip, port)
|
|
|
|
end
|
|
|
|
|
2012-07-03 13:33:52 +01:00
|
|
|
def serve1(*acl)
|
|
|
|
@nbd1.serve(@filename1, *acl)
|
|
|
|
end
|
|
|
|
|
|
|
|
def serve2(*acl)
|
|
|
|
@nbd2.serve(@filename2, *acl)
|
|
|
|
end
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
def listen1(*acl)
|
|
|
|
@nbd1.listen(@filename1, *(acl.empty? ? @acl1 : acl))
|
2012-07-03 13:33:52 +01:00
|
|
|
end
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
def listen2(*acl)
|
|
|
|
@nbd2.listen(@filename2, *acl)
|
2012-07-03 13:33:52 +01:00
|
|
|
end
|
|
|
|
|
2012-07-17 16:30:49 +01:00
|
|
|
def break1
|
|
|
|
@nbd1.break
|
|
|
|
end
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
def acl1(*acl)
|
|
|
|
@nbd1.acl(*acl)
|
2012-07-03 13:33:52 +01:00
|
|
|
end
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
def acl2(*acl)
|
|
|
|
@nbd2.acl(*acl)
|
2012-07-03 13:33:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def status1
|
|
|
|
@nbd1.status.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def status2
|
|
|
|
@nbd2.status.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def mirror12
|
2018-02-02 21:34:14 +00:00
|
|
|
@nbd1.mirror(@nbd2.ip, @nbd2.port)
|
2012-07-03 13:33:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def mirror12_unchecked
|
2018-02-02 21:34:14 +00:00
|
|
|
@nbd1.mirror_unchecked(@nbd2.ip, @nbd2.port, nil, nil, 10)
|
2012-07-03 13:33:52 +01:00
|
|
|
end
|
|
|
|
|
2012-07-23 13:39:27 +01:00
|
|
|
def mirror12_unlink
|
2018-02-02 21:34:14 +00:00
|
|
|
@nbd1.mirror_unlink(@nbd2.ip, @nbd2.port, 2)
|
2012-07-23 13:39:27 +01:00
|
|
|
end
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
def write1(data)
|
|
|
|
@nbd1.write(0, data)
|
2012-11-20 17:24:19 +00:00
|
|
|
end
|
|
|
|
|
2012-07-03 13:33:52 +01:00
|
|
|
def writefile1(data)
|
|
|
|
@file1 = FileWriter.new(@filename1, @blocksize).write(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def writefile2(data)
|
|
|
|
@file2 = FileWriter.new(@filename2, @blocksize).write(data)
|
|
|
|
end
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
def truncate1(size)
|
2012-07-12 18:01:10 +01:00
|
|
|
system "truncate -s #{size} #{@filename1}"
|
|
|
|
end
|
|
|
|
|
2012-07-03 13:33:52 +01:00
|
|
|
def listening_ports
|
2018-02-02 21:34:14 +00:00
|
|
|
`netstat -ltn`
|
|
|
|
.split("\n")
|
|
|
|
.map { |x| x.split(/\s+/) }[2..-1]
|
|
|
|
.map { |l| l[3].split(':')[-1].to_i }
|
2012-07-03 13:33:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup
|
|
|
|
if @fake_pid
|
|
|
|
begin
|
2018-02-02 21:34:14 +00:00
|
|
|
Process.waitpid2(@fake_pid)
|
2012-07-03 13:33:52 +01:00
|
|
|
rescue Errno::ESRCH
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-12 14:14:46 +01:00
|
|
|
@nbd1.can_die(0)
|
2012-07-03 13:33:52 +01:00
|
|
|
@nbd1.kill
|
|
|
|
@nbd2.kill
|
|
|
|
|
|
|
|
[@filename1, @filename2].each do |f|
|
2018-02-02 21:34:14 +00:00
|
|
|
File.unlink(f) if File.exist?(f)
|
2012-07-03 13:33:52 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-02 21:34:14 +00:00
|
|
|
def run_fake(name, addr, port, sock = nil)
|
|
|
|
fakedir = File.join(File.dirname(__FILE__), 'fakes')
|
|
|
|
fakeglob = File.join(fakedir, name) + '*'
|
|
|
|
fake = Dir[fakeglob].sort.find do |fn|
|
|
|
|
File.executable?(fn)
|
|
|
|
end
|
2012-07-03 13:33:52 +01:00
|
|
|
|
2012-10-04 14:41:55 +01:00
|
|
|
raise "no fake executable at #{fakeglob}" unless fake
|
2018-02-02 21:34:14 +00:00
|
|
|
raise 'no addr' unless addr
|
|
|
|
raise 'no port' unless port
|
2012-07-12 14:14:46 +01:00
|
|
|
|
2012-07-03 13:33:52 +01:00
|
|
|
@fake_pid = fork do
|
2018-02-02 21:34:14 +00:00
|
|
|
exec [fake, addr, port, @nbd1.pid, sock].map(&:to_s).join(' ')
|
2012-07-03 13:33:52 +01:00
|
|
|
end
|
|
|
|
sleep(0.5)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fake_reports_success
|
2018-02-02 21:34:14 +00:00
|
|
|
_, status = Process.waitpid2(@fake_pid)
|
2012-07-03 13:33:52 +01:00
|
|
|
@fake_pid = nil
|
|
|
|
status.success?
|
|
|
|
end
|
|
|
|
end # class Environment
|