1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
# libnbd Python bindings
# Copyright Red Hat
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import nbd
import os
from contextlib import contextmanager
nbdkit = os.getenv("NBDKIT", "nbdkit")
# Require new-enough nbdkit
if os.system(nbdkit + " sh --dump-plugin | grep -q has_list_exports=1"):
print("skipping: " + nbdkit + " too old for this test")
exit(0)
script = "%s/../tests/opt-list.sh" % os.getenv("srcdir", ".")
exports = []
@contextmanager
def conn(mode):
global exports
exports = []
h = nbd.NBD()
try:
h.set_opt_mode(True)
h.connect_command([nbdkit, "-s", "--exit-with-parent", "-v", "sh",
script, "mode=%d" % mode])
yield h
finally:
h.opt_abort()
h = None
def f(user_data, name, desc):
global exports
assert user_data == 42
assert desc == ""
exports.append(name)
# First pass: server fails NBD_OPT_LIST
with conn(0) as h:
try:
h.opt_list(lambda *args: f(42, *args))
assert False
except nbd.Error:
pass
assert exports == []
# Second pass: server advertises 'a' and 'b'
with conn(1) as h:
assert h.opt_list(lambda *args: f(42, *args)) == 2
assert exports == ["a", "b"]
# Third pass: server advertises empty list
with conn(2) as h:
assert h.opt_list(lambda *args: f(42, *args)) == 0
assert exports == []
# Final pass: server advertises 'a'
with conn(3) as h:
assert h.opt_list(lambda *args: f(42, *args)) == 1
assert exports == ["a"]
|