a bit more coverage around USB interactions (aka lots of mocks)

This commit is contained in:
Brian S. Stephan 2023-07-09 10:41:40 -05:00
parent 5bcb3dba3f
commit 2c446f595a
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
2 changed files with 59 additions and 0 deletions

View File

@ -25,6 +25,46 @@ def with_pb2s(test, *args, **kwargs):
del sys.modules['config_pb2'] del sys.modules['config_pb2']
def test_get_bootsel_endpoints():
"""Test our expected method of finding the BOOTSEL mode Pico board."""
mock_device = mock.MagicMock(name='mock_device')
mock_device.is_kernel_driver_active.return_value = False
mock_configuration = mock.MagicMock(name='mock_configuration')
mock_device.get_active_configuration.return_value = mock_configuration
mock_interface = mock.MagicMock(name='mock_interface')
with mock.patch('usb.core.find', return_value=mock_device) as mock_find:
with mock.patch('usb.util.find_descriptor', return_value=mock_interface) as mock_find_descriptor:
_, _ = pico.get_bootsel_endpoints()
mock_find.assert_called_with(idVendor=pico.PICO_VENDOR, idProduct=pico.PICO_PRODUCT)
mock_device.is_kernel_driver_active.assert_called_with(0)
mock_device.detach_kernel_driver.assert_not_called()
mock_device.get_active_configuration.assert_called_once()
assert mock_find_descriptor.call_args_list[0].args[0] == mock_configuration
assert mock_find_descriptor.call_args_list[1].args[0] == mock_interface
assert mock_find_descriptor.call_args_list[2].args[0] == mock_interface
def test_get_bootsel_endpoints_with_kernel_disconnect():
"""Test our expected method of finding the BOOTSEL mode Pico board."""
mock_device = mock.MagicMock(name='mock_device')
mock_device.is_kernel_driver_active.return_value = True
mock_configuration = mock.MagicMock(name='mock_configuration')
mock_device.get_active_configuration.return_value = mock_configuration
mock_interface = mock.MagicMock(name='mock_interface')
with mock.patch('usb.core.find', return_value=mock_device) as mock_find:
with mock.patch('usb.util.find_descriptor', return_value=mock_interface) as mock_find_descriptor:
_, _ = pico.get_bootsel_endpoints()
mock_find.assert_called_with(idVendor=pico.PICO_VENDOR, idProduct=pico.PICO_PRODUCT)
mock_device.is_kernel_driver_active.assert_called_with(0)
mock_device.detach_kernel_driver.assert_called_with(0)
mock_device.get_active_configuration.assert_called_once()
assert mock_find_descriptor.call_args_list[0].args[0] == mock_configuration
assert mock_find_descriptor.call_args_list[1].args[0] == mock_interface
assert mock_find_descriptor.call_args_list[2].args[0] == mock_interface
def test_exclusive_access(): def test_exclusive_access():
"""Test that we can get exclusive access to a BOOTSEL board.""" """Test that we can get exclusive access to a BOOTSEL board."""
end_out, end_in = mock.MagicMock(), mock.MagicMock() end_out, end_in = mock.MagicMock(), mock.MagicMock()

View File

@ -1,6 +1,7 @@
"""Unit tests for the storage module.""" """Unit tests for the storage module."""
import os import os
import sys import sys
import unittest.mock as mock
import pytest import pytest
from decorator import decorator from decorator import decorator
@ -150,3 +151,21 @@ def test_pad_config_to_storage_raises(config_binary):
"""Test that we raise an exception if the config is bigger than the storage section.""" """Test that we raise an exception if the config is bigger than the storage section."""
with pytest.raises(storage.ConfigLengthError): with pytest.raises(storage.ConfigLengthError):
_ = storage.pad_config_to_storage_size(config_binary * 5) _ = storage.pad_config_to_storage_size(config_binary * 5)
@with_pb2s
def test_get_config_from_usb(config_binary):
"""Test we attempt to read from the proper location over USB."""
mock_out = mock.MagicMock()
mock_out.device.idVendor = 0xbeef
mock_out.device.idProduct = 0xcafe
mock_out.device.bus = 1
mock_out.device.address = 2
mock_in = mock.MagicMock()
with mock.patch('gp2040ce_bintools.storage.get_bootsel_endpoints', return_value=(mock_out, mock_in)) as mock_get:
with mock.patch('gp2040ce_bintools.storage.read', return_value=config_binary) as mock_read:
config, _, _ = storage.get_config_from_usb()
mock_get.assert_called_once()
mock_read.assert_called_with(mock_out, mock_in, 0x101FE000, 8192)
assert config == storage.get_config(config_binary)