File: 585-aio-buffer-share.py

package info (click to toggle)
libnbd 1.22.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,636 kB
  • sloc: ansic: 53,855; ml: 12,311; sh: 8,499; python: 4,595; makefile: 2,902; perl: 165; cpp: 24
file content (47 lines) | stat: -rw-r--r-- 1,621 bytes parent folder | download | duplicates (2)
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
# 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

# This tests various nbd.Buffer methods.  We can do this entirely
# without using the NBD protocol.

import nbd

# Use of to/from_bytearray always creates copies
ba = bytearray(512)
buf = nbd.Buffer.from_bytearray(ba)
ba.append(1)
assert len(ba) == 513
assert len(buf) == 512
assert buf.is_zero() is True
assert buf.to_bytearray() is not ba

# Use of to/from_buffer shares the same buffer
buf = nbd.Buffer.from_buffer(ba)
assert buf.is_zero() is False
assert len(buf) == 513
ba.pop()
assert buf.is_zero() is True
assert len(buf) == 512
assert buf.to_buffer() is ba

# Even though nbd.Buffer(n) start uninitialized, we sanitize before exporting.
# This test cheats and examines the private member ._init
buf = nbd.Buffer(512)
assert buf.is_zero() is True
assert hasattr(buf, '_init') is False
assert buf.to_buffer() == bytearray(512)
assert hasattr(buf, '_init') is True