[Libguestfs] [libnbd PATCH v2 3/8] python: Enhance tests of nbd.Buffer

Richard W.M. Jones rjones at redhat.com
Tue Jun 7 13:14:36 UTC 2022


On Mon, Jun 06, 2022 at 09:08:28PM -0500, Eric Blake wrote:
> Add some more coverage of existing behavior, so we can better track
> that we are not introducing unintended changes in upcoming patches.
> ---
>  python/t/500-aio-pread.py  | 83 +++++++++++++++++++++++++++++++++++---
>  python/t/510-aio-pwrite.py |  6 +++
>  2 files changed, 84 insertions(+), 5 deletions(-)
> 
> diff --git a/python/t/500-aio-pread.py b/python/t/500-aio-pread.py
> index fc1abad..c236163 100644
> --- a/python/t/500-aio-pread.py
> +++ b/python/t/500-aio-pread.py
> @@ -1,5 +1,5 @@
>  # libnbd Python bindings
> -# Copyright (C) 2010-2019 Red Hat Inc.
> +# Copyright (C) 2010-2022 Red Hat Inc.
>  #
>  # 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
> @@ -19,17 +19,25 @@ import nbd
> 
>  h = nbd.NBD()
>  h.connect_command(["nbdkit", "-s", "--exit-with-parent", "-v",
> -                   "pattern", "size=512"])
> +                   "pattern", "size=1024"])
>  buf = nbd.Buffer(512)
>  cookie = h.aio_pread(buf, 0)
>  while not h.aio_command_completed(cookie):
>      h.poll(-1)
> 
> -buf = buf.to_bytearray()
> +buf1 = buf.to_bytearray()
> 
> -print("%r" % buf)
> +# Prove that .to_bytearray() defaults to copying, even if buf is reused
> 
> -assert buf == (b'\x00\x00\x00\x00\x00\x00\x00\x00'
> +cookie = h.aio_pread(buf, 512)
> +while not h.aio_command_completed(cookie):
> +    h.poll(-1)
> +
> +buf2 = buf.to_bytearray()
> +
> +print("%r" % buf1)
> +
> +assert buf1 == (b'\x00\x00\x00\x00\x00\x00\x00\x00'
>                 + b'\x00\x00\x00\x00\x00\x00\x00\x08'
>                 + b'\x00\x00\x00\x00\x00\x00\x00\x10'
>                 + b'\x00\x00\x00\x00\x00\x00\x00\x18'
> @@ -93,3 +101,68 @@ assert buf == (b'\x00\x00\x00\x00\x00\x00\x00\x00'
>                 + b'\x00\x00\x00\x00\x00\x00\x01\xe8'
>                 + b'\x00\x00\x00\x00\x00\x00\x01\xf0'
>                 + b'\x00\x00\x00\x00\x00\x00\x01\xf8')
> +
> +assert buf2 == (b'\x00\x00\x00\x00\x00\x00\x02\x00'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\x08'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\x10'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\x18'
> +               + b'\x00\x00\x00\x00\x00\x00\x02 '
> +               + b'\x00\x00\x00\x00\x00\x00\x02('
> +               + b'\x00\x00\x00\x00\x00\x00\x020'
> +               + b'\x00\x00\x00\x00\x00\x00\x028'
> +               + b'\x00\x00\x00\x00\x00\x00\x02@'
> +               + b'\x00\x00\x00\x00\x00\x00\x02H'
> +               + b'\x00\x00\x00\x00\x00\x00\x02P'
> +               + b'\x00\x00\x00\x00\x00\x00\x02X'
> +               + b'\x00\x00\x00\x00\x00\x00\x02`'
> +               + b'\x00\x00\x00\x00\x00\x00\x02h'
> +               + b'\x00\x00\x00\x00\x00\x00\x02p'
> +               + b'\x00\x00\x00\x00\x00\x00\x02x'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\x80'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\x88'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\x90'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\x98'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xa0'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xa8'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xb0'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xb8'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xc0'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xc8'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xd0'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xd8'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xe0'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xe8'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xf0'
> +               + b'\x00\x00\x00\x00\x00\x00\x02\xf8'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\x00'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\x08'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\x10'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\x18'
> +               + b'\x00\x00\x00\x00\x00\x00\x03 '
> +               + b'\x00\x00\x00\x00\x00\x00\x03('
> +               + b'\x00\x00\x00\x00\x00\x00\x030'
> +               + b'\x00\x00\x00\x00\x00\x00\x038'
> +               + b'\x00\x00\x00\x00\x00\x00\x03@'
> +               + b'\x00\x00\x00\x00\x00\x00\x03H'
> +               + b'\x00\x00\x00\x00\x00\x00\x03P'
> +               + b'\x00\x00\x00\x00\x00\x00\x03X'
> +               + b'\x00\x00\x00\x00\x00\x00\x03`'
> +               + b'\x00\x00\x00\x00\x00\x00\x03h'
> +               + b'\x00\x00\x00\x00\x00\x00\x03p'
> +               + b'\x00\x00\x00\x00\x00\x00\x03x'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\x80'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\x88'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\x90'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\x98'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xa0'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xa8'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xb0'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xb8'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xc0'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xc8'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xd0'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xd8'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xe0'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xe8'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xf0'
> +               + b'\x00\x00\x00\x00\x00\x00\x03\xf8')
> diff --git a/python/t/510-aio-pwrite.py b/python/t/510-aio-pwrite.py
> index 89599fc..d09e249 100644
> --- a/python/t/510-aio-pwrite.py
> +++ b/python/t/510-aio-pwrite.py
> @@ -45,6 +45,12 @@ while not h.aio_command_completed(cookie):
> 
>  assert buf == buf2.to_bytearray()
> 
> +# Check that .from_bytearray() defaults to copying
> +buf[511] = 0x55
> +assert buf != buf1.to_bytearray()
> +buf[511] = 0xAA
> +assert buf == buf1.to_bytearray()
> +
>  with open(datafile, "rb") as f:
>      content = f.read()
> 

Reviewed-by: Richard W.M. Jones <rjones at redhat.com>

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW


More information about the Libguestfs mailing list