xref: /qemu/tests/functional/qemu_test/utils.py (revision ba182a693fe15a4f6f2a04e8ecb865c2630e5a16)
1# Utilities for python-based QEMU tests
2#
3# Copyright 2024 Red Hat, Inc.
4#
5# Authors:
6#  Thomas Huth <thuth@redhat.com>
7#
8# This work is licensed under the terms of the GNU GPL, version 2 or
9# later.  See the COPYING file in the top-level directory.
10
11import os
12
13from qemu.utils import get_info_usernet_hostfwd_port
14
15
16def get_usernet_hostfwd_port(vm):
17    res = vm.cmd('human-monitor-command', command_line='info usernet')
18    return get_info_usernet_hostfwd_port(res)
19
20"""
21Round up to next power of 2
22"""
23def pow2ceil(x):
24    return 1 if x == 0 else 2**(x - 1).bit_length()
25
26def file_truncate(path, size):
27    if size != os.path.getsize(path):
28        with open(path, 'ab+') as fd:
29            fd.truncate(size)
30
31"""
32Expand file size to next power of 2
33"""
34def image_pow2ceil_expand(path):
35        size = os.path.getsize(path)
36        size_aligned = pow2ceil(size)
37        if size != size_aligned:
38            with open(path, 'ab+') as fd:
39                fd.truncate(size_aligned)
40