Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluetooth unit testing by "python-dbusmock" package #139

Open
posterzh opened this issue Nov 22, 2021 · 0 comments
Open

Bluetooth unit testing by "python-dbusmock" package #139

posterzh opened this issue Nov 22, 2021 · 0 comments

Comments

@posterzh
Copy link
Contributor

posterzh commented Nov 22, 2021

The current unit test cases for Bluetooth are written by using unittest library's MagicMock object.
https://github.com/NebraLtd/hm-config/blob/master/tests/gatewayconfig/bluetooth/test_bluetooth_connection_advertisement.py#L263-L265

For example, to write unit test cases for DiagnosticsCharacteristics(https://github.com/NebraLtd/hm-config/blob/master/tests/gatewayconfig/bluetooth/test_diagnostics_characteristic.py#L20), dbus objects for bluetooth profile, server, helim miner and also need to simulate the pairing behavior of bluetooth device.

Rather than merely patching individual dbus objects blindly, we can make the mocked BLE device, mocked WiFi, mocked Helium Api dbus objects using https://github.com/martinpitt/python-dbusmock.

Pros

  • The mocked BLE device will implement the API like the real device, or at least the parts that we actually need.
  • As the testing doesn't depend on the real dbus objects, testing environment will be consistent across local, CI(Github Action). For ex, once we make a mocked LTE dbus device, it will work on a machine without LTE device, CI environment in a same way.
  • Once we made a BLE device template, it can be used across the test cases of all the Characteristics.

Cons

  • This will bring up extra complexity in the infrastructure of unit testing.

Example:

import dbus
import dbusmock

class TestBlueZ5(dbusmock.DBusTestCase):
    '''Test mocking bluetoothd'''

    @classmethod
    def setUpClass(cls):
        cls.start_system_bus()
        cls.dbus_con = cls.get_dbus(True)
        (cls.p_mock, cls.obj_bluez) = cls.spawn_server_template(
            'bluez5', {}, stdout=subprocess.PIPE)

    def setUp(self):
        self.obj_bluez.Reset()
        self.dbusmock = dbus.Interface(self.obj_bluez, dbusmock.MOCK_IFACE)
        self.dbusmock_bluez = dbus.Interface(self.obj_bluez, 'org.bluez.Mock')

    def test_no_adapters(self):
        # Check for adapters.
        out = _run_bluetoothctl('list')
        for line in out:
            self.assertFalse(line.startswith('Controller '))

    def test_one_adapter(self):
        # Chosen parameters.
        adapter_name = 'hci0'
        system_name = 'my-computer'

        # Add an adapter
        path = self.dbusmock_bluez.AddAdapter(adapter_name, system_name)
        self.assertEqual(path, '/org/bluez/' + adapter_name)

        adapter = self.dbus_con.get_object('org.bluez', path)
        address = adapter.Get('org.bluez.Adapter1', 'Address')

        # Check for the adapter.
        out = _run_bluetoothctl('list')
        self.assertIn('Controller ' + address + ' ' + system_name + ' [default]', out)

        out = _run_bluetoothctl('show ' + address)
        self.assertIn('Controller ' + address, out)
        self.assertIn('Name: ' + system_name, out)
        self.assertIn('Alias: ' + system_name, out)
        self.assertIn('Powered: yes', out)
        self.assertIn('Discoverable: yes', out)
        self.assertIn('Pairable: yes', out)
        self.assertIn('Discovering: yes', out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants