Add simple tests computing a checksum for image with all kinds of extents in raw and qcow2 formats.
The test can be extended later for other formats, format options (e..g compressed qcow2), protocols (e.g. nbd), and image with a backing chain, but I'm not sure this is really needed. To help debugging in case of failures, the output includes a json map of the test image. Signed-off-by: Nir Soffer <nsof...@redhat.com> --- tests/qemu-iotests/tests/qemu-img-checksum | 63 +++++++++++++++++++ .../tests/qemu-img-checksum.out.qcow2 | 11 ++++ .../tests/qemu-img-checksum.out.raw | 10 +++ 3 files changed, 84 insertions(+) create mode 100755 tests/qemu-iotests/tests/qemu-img-checksum create mode 100644 tests/qemu-iotests/tests/qemu-img-checksum.out.qcow2 create mode 100644 tests/qemu-iotests/tests/qemu-img-checksum.out.raw diff --git a/tests/qemu-iotests/tests/qemu-img-checksum b/tests/qemu-iotests/tests/qemu-img-checksum new file mode 100755 index 0000000000..3577a0bc41 --- /dev/null +++ b/tests/qemu-iotests/tests/qemu-img-checksum @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# group: rw auto quick +# +# Test cases for qemu-img checksum. +# +# Copyright (C) 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 +# 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, see <http://www.gnu.org/licenses/>. + +import re + +import iotests + +from iotests import ( + filter_testfiles, + qemu_img, + qemu_img_log, + qemu_io, +) + + +def checksum_available(): + out = qemu_img("--help").stdout + return re.search(r"\bchecksum .+ filename\b", out) is not None + + +if not checksum_available(): + iotests.notrun("checksum command not available") + +iotests.script_initialize( + supported_fmts=["raw", "qcow2"], + supported_cache_modes=["none", "writeback"], + supported_protocols=["file"], +) + +print("=== Create test image ===\n") + +disk = iotests.file_path('disk') +qemu_img("create", "-f", iotests.imgfmt, disk, "10m") +qemu_io("-f", iotests.imgfmt, + "-c", "write -P 0x1 0 2m", # data + "-c", "write -P 0x0 2m 2m", # data with zeroes + "-c", "write -z 4m 2m", # zero allocated + "-c", "write -z -u 6m 2m", # zero hole + # unallocated + disk) +print(filter_testfiles(disk)) +qemu_img_log("map", "--output", "json", disk) + +print("=== Compute checksum ===\n") + +qemu_img_log("checksum", "-T", iotests.cachemode, disk) diff --git a/tests/qemu-iotests/tests/qemu-img-checksum.out.qcow2 b/tests/qemu-iotests/tests/qemu-img-checksum.out.qcow2 new file mode 100644 index 0000000000..02b9616e5b --- /dev/null +++ b/tests/qemu-iotests/tests/qemu-img-checksum.out.qcow2 @@ -0,0 +1,11 @@ +=== Create test image === + +TEST_DIR/PID-disk +[{ "start": 0, "length": 4194304, "depth": 0, "present": true, "zero": false, "data": true, "offset": 327680}, +{ "start": 4194304, "length": 4194304, "depth": 0, "present": true, "zero": true, "data": false}, +{ "start": 8388608, "length": 2097152, "depth": 0, "present": false, "zero": true, "data": false}] + +=== Compute checksum === + +57cd8ef0cfad106d737f8fb0de3a0306a8a1a41db7bf7c0c36e2dfe75ee9bd26 TEST_DIR/PID-disk + diff --git a/tests/qemu-iotests/tests/qemu-img-checksum.out.raw b/tests/qemu-iotests/tests/qemu-img-checksum.out.raw new file mode 100644 index 0000000000..6294e4dace --- /dev/null +++ b/tests/qemu-iotests/tests/qemu-img-checksum.out.raw @@ -0,0 +1,10 @@ +=== Create test image === + +TEST_DIR/PID-disk +[{ "start": 0, "length": 4194304, "depth": 0, "present": true, "zero": false, "data": true, "offset": 0}, +{ "start": 4194304, "length": 6291456, "depth": 0, "present": true, "zero": true, "data": false, "offset": 4194304}] + +=== Compute checksum === + +57cd8ef0cfad106d737f8fb0de3a0306a8a1a41db7bf7c0c36e2dfe75ee9bd26 TEST_DIR/PID-disk + -- 2.38.1