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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
# 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
nbdkit = os.getenv("NBDKIT", "nbdkit")
script = "%s/../tests/opt-info.sh" % os.getenv("srcdir", ".")
def must_fail(f, *args, **kwds):
try:
f(*args, **kwds)
assert False
except nbd.Error:
pass
h = nbd.NBD()
h.set_opt_mode(True)
h.connect_command([nbdkit, "-s", "--exit-with-parent", "-v", "sh", script])
h.add_meta_context(nbd.CONTEXT_BASE_ALLOCATION)
# No size, flags, or meta-contexts yet
must_fail(h.get_size)
must_fail(h.is_read_only)
must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION)
# info with no prior name gets info on ""
h.opt_info()
assert h.get_size() == 0
assert h.is_read_only() is True
assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is True
# changing export wipes out prior info
h.set_export_name("b")
must_fail(h.get_size)
must_fail(h.is_read_only)
must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION)
# info on something not present fails
h.set_export_name("a")
must_fail(h.opt_info)
# info for a different export, with automatic meta_context disabled
h.set_export_name("b")
h.set_request_meta_context(False)
h.opt_info()
# idempotent name change is no-op
h.set_export_name("b")
assert h.get_size() == 1
assert h.is_read_only() is False
must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION)
h.set_request_meta_context(True)
# go on something not present
h.set_export_name("a")
must_fail(h.opt_go)
must_fail(h.get_size)
must_fail(h.is_read_only)
must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION)
# go on a valid export
h.set_export_name("good")
h.opt_go()
assert h.get_size() == 4
assert h.is_read_only() is True
assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is True
# now info is no longer valid, but does not wipe data
must_fail(h.set_export_name, "a")
assert h.get_export_name() == "good"
must_fail(h.opt_info)
assert h.get_size() == 4
assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is True
h.shutdown()
# Another connection. This time, check that SET_META triggered by opt_info
# persists through nbd_opt_go with set_request_meta_context disabled.
h = nbd.NBD()
h.set_opt_mode(True)
h.connect_command([nbdkit, "-s", "--exit-with-parent", "-v", "sh", script])
h.add_meta_context("x-unexpected:bogus")
must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION)
h.opt_info()
assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is False
h.set_request_meta_context(False)
# Adding to the request list now won't matter
h.add_meta_context(nbd.CONTEXT_BASE_ALLOCATION)
h.opt_go()
assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is False
h.shutdown()
|