Files
flexnbd-c/tests/acceptance/environment.rb

151 lines
3.0 KiB
Ruby
Raw Normal View History

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)
def initialize
@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
@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)
@fake_pid = nil
end
def blocksize=(b)
raise RuntimeError, "Unable to change blocksize after files have been opened" if @file1 or @file2
@blocksize = b
end
def prefetch_proxy!
@nbd1.prefetch_proxy = true
@nbd2.prefetch_proxy = true
end
2018-02-02 21:34:14 +00:00
def proxy1(port = @port2)
@nbd1.proxy(@ip, port)
end
2018-02-02 21:34:14 +00:00
def proxy2(port = @port1)
@nbd2.proxy(@ip, port)
end
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))
end
2018-02-02 21:34:14 +00:00
def listen2(*acl)
@nbd2.listen(@filename2, *acl)
end
def break1
@nbd1.break
end
2018-02-02 21:34:14 +00:00
def acl1(*acl)
@nbd1.acl(*acl)
end
2018-02-02 21:34:14 +00:00
def acl2(*acl)
@nbd2.acl(*acl)
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)
end
def mirror12_unchecked
2018-02-02 21:34:14 +00:00
@nbd1.mirror_unchecked(@nbd2.ip, @nbd2.port, nil, nil, 10)
end
def mirror12_unlink
2018-02-02 21:34:14 +00:00
@nbd1.mirror_unlink(@nbd2.ip, @nbd2.port, 2)
end
2018-02-02 21:34:14 +00:00
def write1(data)
@nbd1.write(0, data)
end
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)
system "truncate -s #{size} #{@filename1}"
end
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 }
end
def cleanup
if @fake_pid
begin
2018-02-02 21:34:14 +00:00
Process.waitpid2(@fake_pid)
rescue Errno::ESRCH
end
end
@nbd1.can_die(0)
@nbd1.kill
@nbd2.kill
[@filename1, @filename2].each do |f|
2018-02-02 21:34:14 +00:00
File.unlink(f) if File.exist?(f)
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
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
@fake_pid = fork do
2018-02-02 21:34:14 +00:00
exec [fake, addr, port, @nbd1.pid, sock].map(&:to_s).join(' ')
end
sleep(0.5)
end
def fake_reports_success
2018-02-02 21:34:14 +00:00
_, status = Process.waitpid2(@fake_pid)
@fake_pid = nil
status.success?
end
end # class Environment