From a7eedffe20bd6e480c9e2c65e68daef1beb2af05 Mon Sep 17 00:00:00 2001 From: Mattias Nissler <122288598+mnissler-rivos@users.noreply.github.com> Date: Thu, 31 Aug 2023 11:38:55 +0200 Subject: Introduce client object in python tests (#772) Thus far, the client end of the socket is the only piece of client state tracked in tests, for which a global `socket` variable has been used. In preparation to add more state, replace the `socket` global with a `client` global object that groups all client state. Signed-off-by: Mattias Nissler Reviewed-by: John Levon Reviewed-by: Thanos Makatos --- test/py/test_device_get_region_info.py | 49 +++++++++++++++++----------------- 1 file changed, 25 insertions(+), 24 deletions(-) (limited to 'test/py/test_device_get_region_info.py') diff --git a/test/py/test_device_get_region_info.py b/test/py/test_device_get_region_info.py index 62d6740..f847cb4 100644 --- a/test/py/test_device_get_region_info.py +++ b/test/py/test_device_get_region_info.py @@ -32,7 +32,7 @@ import errno import tempfile ctx = None -sock = None +client = None argsz = len(vfio_region_info()) migr_region_size = 2 << PAGE_SHIFT @@ -40,7 +40,7 @@ migr_mmap_areas = [(PAGE_SIZE, PAGE_SIZE)] def test_device_get_region_info_setup(): - global ctx, sock + global ctx, client ctx = vfu_create_ctx(flags=LIBVFIO_USER_FLAG_ATTACH_NB) assert ctx is not None @@ -89,14 +89,14 @@ def test_device_get_region_info_setup(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) def test_device_get_region_info_short_write(): payload = struct.pack("II", 0, 0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, expect=errno.EINVAL) @@ -106,7 +106,7 @@ def test_device_get_region_info_bad_argsz(): index=VFU_PCI_DEV_BAR1_REGION_IDX, cap_offset=0, size=0, offset=0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, expect=errno.EINVAL) @@ -116,7 +116,7 @@ def test_device_get_region_info_bad_index(): index=VFU_PCI_DEV_NUM_REGIONS, cap_offset=0, size=0, offset=0) - msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, + msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload, expect=errno.EINVAL) @@ -126,7 +126,7 @@ def test_device_get_region_info_larger_argsz(): index=VFU_PCI_DEV_BAR1_REGION_IDX, cap_offset=0, size=0, offset=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) assert len(result) == argsz @@ -142,13 +142,13 @@ def test_device_get_region_info_larger_argsz(): def test_device_get_region_info_small_argsz_caps(): - global sock + global client payload = vfio_region_info(argsz=argsz, flags=0, index=VFU_PCI_DEV_BAR2_REGION_IDX, cap_offset=0, size=0, offset=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) info, _ = vfio_region_info.pop_from_buffer(result) @@ -167,20 +167,21 @@ def test_device_get_region_info_small_argsz_caps(): assert info.offset == 0x8000 # skip reading the SCM_RIGHTS - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_device_get_region_info_caps(): - global sock + global client - sock = connect_client(ctx) + client = connect_client(ctx) payload = vfio_region_info(argsz=80, flags=0, index=VFU_PCI_DEV_BAR2_REGION_IDX, cap_offset=0, size=0, offset=0) payload = bytes(payload) + b'\0' * (80 - 32) - fds, result = msg_fds(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + fds, result = msg_fds(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, + payload) info, result = vfio_region_info.pop_from_buffer(result) cap, result = vfio_region_info_cap_sparse_mmap.pop_from_buffer(result) @@ -203,20 +204,20 @@ def test_device_get_region_info_caps(): assert area2.size == 0x2000 assert len(fds) == 1 - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_device_get_region_info_migr(): - global sock + global client - sock = connect_client(ctx) + client = connect_client(ctx) payload = vfio_region_info(argsz=80, flags=0, index=VFU_PCI_DEV_MIGR_REGION_IDX, cap_offset=0, size=0, offset=0) payload = bytes(payload) + b'\0' * (80 - 32) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) info, result = vfio_region_info.pop_from_buffer(result) mcap, result = vfio_region_info_cap_type.pop_from_buffer(result) @@ -241,7 +242,7 @@ def test_device_get_region_info_migr(): assert area.size == migr_mmap_areas[0][1] # skip reading the SCM_RIGHTS - disconnect_client(ctx, sock) + client.disconnect(ctx) def test_device_get_region_info_cleanup(): @@ -260,13 +261,13 @@ def test_device_get_pci_config_space_info_implicit_pci_init(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) payload = vfio_region_info(argsz=argsz + 8, flags=0, index=VFU_PCI_DEV_CFG_REGION_IDX, cap_offset=0, size=0, offset=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) assert len(result) == argsz @@ -281,7 +282,7 @@ def test_device_get_pci_config_space_info_implicit_pci_init(): assert info.size == PCI_CFG_SPACE_EXP_SIZE assert info.offset == 0 - disconnect_client(ctx, sock) + client.disconnect(ctx) vfu_destroy_ctx(ctx) @@ -296,13 +297,13 @@ def test_device_get_pci_config_space_info_implicit_no_pci_init(): ret = vfu_realize_ctx(ctx) assert ret == 0 - sock = connect_client(ctx) + client = connect_client(ctx) payload = vfio_region_info(argsz=argsz + 8, flags=0, index=VFU_PCI_DEV_CFG_REGION_IDX, cap_offset=0, size=0, offset=0) - result = msg(ctx, sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) + result = msg(ctx, client.sock, VFIO_USER_DEVICE_GET_REGION_INFO, payload) assert len(result) == argsz @@ -315,7 +316,7 @@ def test_device_get_pci_config_space_info_implicit_no_pci_init(): assert info.size == PCI_CFG_SPACE_SIZE assert info.offset == 0 - disconnect_client(ctx, sock) + client.disconnect(ctx) vfu_destroy_ctx(ctx) -- cgit v1.1