python-dbusmock 0.37.2__tar.gz → 0.38.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {python_dbusmock-0.37.2/python_dbusmock.egg-info → python_dbusmock-0.38.1}/PKG-INFO +97 -95
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/README.md +96 -94
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/__init__.py +2 -13
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/__main__.py +1 -5
- python_dbusmock-0.38.1/dbusmock/_version.py +1 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/mockobject.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/pytest_fixtures.py +1 -5
- python_dbusmock-0.38.1/dbusmock/templates/__init__.py +3 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/bluez5-obex.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/bluez5.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/gnome_screensaver.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/gsd_rfkill.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/logind.py +2 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/low_memory_monitor.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/modemmanager.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/networkmanager.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/notification_daemon.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/ofono.py +54 -44
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/polkitd.py +6 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/power_profiles_daemon.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/systemd.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/timedated.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/upower.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/upower_power_profiles_daemon.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/urfkill.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/testcase.py +3 -7
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/pyproject.toml +4 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1/python_dbusmock.egg-info}/PKG-INFO +97 -95
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/python_dbusmock.egg-info/SOURCES.txt +1 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_api.py +3 -11
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_api_pytest.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_bluez5.py +10 -14
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_cli.py +6 -18
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_code.py +1 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_gnome_screensaver.py +4 -10
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_gsd_rfkill.py +4 -10
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_logind.py +24 -21
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_low_memory_monitor.py +4 -10
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_modemmanager.py +4 -13
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_networkmanager.py +4 -10
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_notification_daemon.py +4 -10
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_ofono.py +4 -5
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_polkitd.py +4 -10
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_power_profiles_daemon.py +4 -10
- python_dbusmock-0.38.1/tests/test_readme_examples.py +44 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_systemd.py +5 -20
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_timedated.py +4 -11
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_upower.py +4 -10
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/test_urfkill.py +4 -10
- python_dbusmock-0.37.2/dbusmock/_version.py +0 -1
- python_dbusmock-0.37.2/dbusmock/templates/__init__.py +0 -7
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/.fmf/version +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/.github/workflows/pages.yml +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/.github/workflows/release.yml +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/.github/workflows/tests.yml +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/.gitignore +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/.pre-commit-config.yaml +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/COPYING +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/NEWS +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/dbusmock/templates/SKELETON +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/distro/python-dbusmock.spec +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/doc/conf.py +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/doc/index.rst +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/packit.yaml +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/plans/all.fmf +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/python_dbusmock.egg-info/dependency_links.txt +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/python_dbusmock.egg-info/requires.txt +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/python_dbusmock.egg-info/top_level.txt +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/setup.cfg +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/__init__.py +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/conftest.py +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/main.fmf +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/run +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/run-centos +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/run-debian +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/run-fedora +0 -0
- {python_dbusmock-0.37.2 → python_dbusmock-0.38.1}/tests/run-ubuntu +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: python-dbusmock
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.38.1
|
|
4
4
|
Summary: Mock D-Bus objects
|
|
5
5
|
Author-email: Martin Pitt <martin@piware.de>
|
|
6
6
|
License: GNU LESSER GENERAL PUBLIC LICENSE
|
|
@@ -200,20 +200,20 @@ D-Bus services such as upower, systemd, logind, gnome-session or others,
|
|
|
200
200
|
and it is hard (or impossible without root privileges) to set the state
|
|
201
201
|
of the real services to what you expect in your tests.
|
|
202
202
|
|
|
203
|
-
Suppose you want to write tests for
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
Now, your test suite should not really talk to the actual system D-Bus
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
203
|
+
Suppose you want to write tests for a desktop environment's power management:
|
|
204
|
+
You want to verify that after the configured idle time the program suspends the
|
|
205
|
+
machine. So your program calls `org.freedesktop.login1.Manager.Suspend()` on
|
|
206
|
+
the system D-Bus.
|
|
207
|
+
|
|
208
|
+
Now, your test suite should not really talk to the actual system D-Bus and
|
|
209
|
+
the real systemd; a `make check` that suspends your machine will not be
|
|
210
|
+
considered very friendly by most people, and if you want to run this in
|
|
211
|
+
continuous integration test servers or package build environments, chances
|
|
212
|
+
are that your process does not have the privilege to suspend, or there is
|
|
213
|
+
no system bus or running systemd to begin with. Likewise, there is no way
|
|
214
|
+
for an user process to forcefully set the system/seat idle flag in logind,
|
|
215
|
+
so your tests cannot set up the expected test environment on the real
|
|
216
|
+
daemon.
|
|
217
217
|
|
|
218
218
|
That's where mock objects come into play: They look like the real API
|
|
219
219
|
(or at least the parts that you actually need), but they do not actually
|
|
@@ -236,11 +236,12 @@ convenience D-Bus launch API that way.
|
|
|
236
236
|
|
|
237
237
|
## Simple example using Python's unittest
|
|
238
238
|
|
|
239
|
-
Picking up the above example about mocking
|
|
240
|
-
this is how you would set up a mock
|
|
239
|
+
Picking up the above example about mocking systemd-logind's `Suspend()`
|
|
240
|
+
method, this is how you would set up a mock logind in your test case:
|
|
241
241
|
|
|
242
242
|
```python
|
|
243
243
|
import subprocess
|
|
244
|
+
import unittest
|
|
244
245
|
|
|
245
246
|
import dbus
|
|
246
247
|
|
|
@@ -254,29 +255,32 @@ class TestMyProgram(dbusmock.DBusTestCase):
|
|
|
254
255
|
cls.dbus_con = cls.get_dbus(system_bus=True)
|
|
255
256
|
|
|
256
257
|
def setUp(self):
|
|
257
|
-
self.p_mock = self.spawn_server('org.freedesktop.
|
|
258
|
-
'/org/freedesktop/
|
|
259
|
-
'org.freedesktop.
|
|
258
|
+
self.p_mock = self.spawn_server('org.freedesktop.login1',
|
|
259
|
+
'/org/freedesktop/login1',
|
|
260
|
+
'org.freedesktop.login1.Manager',
|
|
260
261
|
system_bus=True,
|
|
261
262
|
stdout=subprocess.PIPE)
|
|
263
|
+
self.addCleanup(self.p_mock.wait)
|
|
264
|
+
self.addCleanup(self.p_mock.terminate)
|
|
265
|
+
self.addCleanup(self.p_mock.stdout.close)
|
|
262
266
|
|
|
263
|
-
# Get a proxy for the
|
|
264
|
-
self.
|
|
265
|
-
'org.freedesktop.
|
|
267
|
+
# Get a proxy for the logind object's Mock interface
|
|
268
|
+
self.dbus_logind_mock = dbus.Interface(self.dbus_con.get_object(
|
|
269
|
+
'org.freedesktop.login1', '/org/freedesktop/login1'),
|
|
266
270
|
dbusmock.MOCK_IFACE)
|
|
267
271
|
|
|
268
|
-
self.
|
|
269
|
-
|
|
270
|
-
def tearDown(self):
|
|
271
|
-
self.p_mock.stdout.close()
|
|
272
|
-
self.p_mock.terminate()
|
|
273
|
-
self.p_mock.wait()
|
|
272
|
+
self.dbus_logind_mock.AddMethod('', 'Suspend', 'b', '', '')
|
|
274
273
|
|
|
275
274
|
def test_suspend_on_idle(self):
|
|
276
275
|
# run your program in a way that should trigger one suspend call
|
|
276
|
+
# represented here as direct D-Bus call
|
|
277
|
+
subprocess.check_call(
|
|
278
|
+
["busctl", "call", "org.freedesktop.login1",
|
|
279
|
+
"/org/freedesktop/login1", "org.freedesktop.login1.Manager",
|
|
280
|
+
"Suspend", "b", "false"])
|
|
277
281
|
|
|
278
282
|
# now check the log that we got one Suspend() call
|
|
279
|
-
self.assertRegex(self.p_mock.stdout.readline(), b'^[0-9.]+ Suspend$')
|
|
283
|
+
self.assertRegex(self.p_mock.stdout.readline(), b'^[0-9.]+ Suspend False$')
|
|
280
284
|
```
|
|
281
285
|
|
|
282
286
|
Let's walk through:
|
|
@@ -292,7 +296,7 @@ Let's walk through:
|
|
|
292
296
|
instead of `setUp()` is enough.
|
|
293
297
|
|
|
294
298
|
- `setUp()` spawns the mock D-Bus server process for an initial
|
|
295
|
-
`/org/freedesktop/
|
|
299
|
+
`/org/freedesktop/login1` object with an `org.freedesktop.login1`
|
|
296
300
|
D-Bus interface on the system bus. We capture its stdout to be
|
|
297
301
|
able to verify that methods were called.
|
|
298
302
|
|
|
@@ -302,18 +306,21 @@ Let's walk through:
|
|
|
302
306
|
stdout). It takes no input arguments, returns nothing, and does
|
|
303
307
|
not run any custom code.
|
|
304
308
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
309
|
+
We use `addCleanup()` to register cleanup handlers that will
|
|
310
|
+
stop our mock D-Bus server after each test. This ensures each
|
|
311
|
+
test case has a fresh and clean logind mock instance. Of course
|
|
312
|
+
you can also set up everything in `setUpClass()` if tests
|
|
308
313
|
do not interfere with each other on setting up the mock.
|
|
309
314
|
|
|
310
315
|
- `test_suspend_on_idle()` is the actual test case. It needs to run
|
|
311
|
-
your program in a way that should trigger one suspend call.
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
316
|
+
your program in a way that should trigger one suspend call. For this
|
|
317
|
+
example this is represented by doing just a direct D-Bus call using
|
|
318
|
+
`busctl`.
|
|
319
|
+
|
|
320
|
+
That `Suspend()` call is now being served by our mock instead of the real
|
|
321
|
+
logind, there will not be any actual machine suspend. Our mock process
|
|
322
|
+
will log the method call together with a time stamp; you can use the
|
|
323
|
+
latter for doing timing related tests, but we just ignore it here.
|
|
317
324
|
|
|
318
325
|
## Simple example using pytest
|
|
319
326
|
|
|
@@ -321,73 +328,68 @@ The same functionality as above but instead using the pytest fixture provided
|
|
|
321
328
|
by this package.
|
|
322
329
|
|
|
323
330
|
```python
|
|
331
|
+
# Enable dbusmock's pytest fixtures (can also go in conftest.py)
|
|
332
|
+
pytest_plugins = "dbusmock.pytest_fixtures"
|
|
333
|
+
|
|
324
334
|
import subprocess
|
|
325
335
|
|
|
326
336
|
import dbus
|
|
327
|
-
import pytest
|
|
328
337
|
|
|
329
338
|
import dbusmock
|
|
330
339
|
|
|
331
340
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
'org.freedesktop.
|
|
336
|
-
'/org/freedesktop/
|
|
337
|
-
'org.freedesktop.
|
|
338
|
-
|
|
339
|
-
stdout=subprocess.PIPE)
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
def test_suspend_on_idle(upower_mock):
|
|
356
|
-
# run your program in a way that should trigger one suspend call
|
|
357
|
-
|
|
358
|
-
# now check the log that we got one Suspend() call
|
|
359
|
-
assert upower_mock.stdout.readline() == b'^[0-9.]+ Suspend$'
|
|
341
|
+
def test_suspend_on_idle(dbusmock_system):
|
|
342
|
+
# Spawn the mock D-Bus server for logind on the system bus
|
|
343
|
+
with dbusmock.SpawnedMock.spawn_for_name(
|
|
344
|
+
'org.freedesktop.login1',
|
|
345
|
+
'/org/freedesktop/login1',
|
|
346
|
+
'org.freedesktop.login1.Manager',
|
|
347
|
+
dbusmock.BusType.SYSTEM,
|
|
348
|
+
stdout=subprocess.PIPE) as p_mock:
|
|
349
|
+
|
|
350
|
+
# Get a proxy for the logind object's Mock interface
|
|
351
|
+
obj_logind = p_mock.obj
|
|
352
|
+
obj_logind.AddMethod('', 'Suspend', 'b', '', '', interface_name=dbusmock.MOCK_IFACE)
|
|
353
|
+
|
|
354
|
+
# Run your program in a way that should trigger one suspend call
|
|
355
|
+
# represented here as direct D-Bus call
|
|
356
|
+
subprocess.check_call(
|
|
357
|
+
["busctl", "call", "org.freedesktop.login1",
|
|
358
|
+
"/org/freedesktop/login1", "org.freedesktop.login1.Manager",
|
|
359
|
+
"Suspend", "b", "false"])
|
|
360
|
+
|
|
361
|
+
# Check the log that we got one Suspend() call
|
|
362
|
+
assert b'Suspend False\n' in p_mock.stdout.readline()
|
|
360
363
|
```
|
|
361
364
|
|
|
362
365
|
Let's walk through:
|
|
363
366
|
|
|
367
|
+
- We enable dbusmock's pytest fixtures with `pytest_plugins = "dbusmock.pytest_fixtures"`.
|
|
368
|
+
This makes fixtures like `dbusmock_system` and `dbusmock_session` available to your
|
|
369
|
+
tests. In a real project, you would typically put this line in your `conftest.py`
|
|
370
|
+
instead of in each test file.
|
|
371
|
+
|
|
364
372
|
- We import the `dbusmock_system` fixture from dbusmock which provides us
|
|
365
|
-
with a system bus started for our test case
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
`/org/freedesktop/
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
your program in a way that should trigger one suspend call. Your
|
|
386
|
-
program will try to call `Suspend()`, but as that's now being
|
|
387
|
-
served by our mock instead of upower, there will not be any actual
|
|
388
|
-
machine suspend. Our mock process will log the method call
|
|
389
|
-
together with a time stamp; you can use the latter for doing
|
|
390
|
-
timing related tests, but we just ignore it here.
|
|
373
|
+
with a system bus started for our test case. Even though we don't use it
|
|
374
|
+
directly in this simple example, it ensures the test environment is set up.
|
|
375
|
+
|
|
376
|
+
- `test_suspend_on_idle()` is the actual test. It uses
|
|
377
|
+
`SpawnedMock.spawn_for_name()` to spawn the mock D-Bus server process for
|
|
378
|
+
`/org/freedesktop/login1` object with `org.freedesktop.login1.Manager`
|
|
379
|
+
interface. We capture its stdout to verify that methods were called.
|
|
380
|
+
|
|
381
|
+
We then call `org.freedesktop.DBus.Mock.AddMethod()` to add a `Suspend()`
|
|
382
|
+
method to our new logind mock object to the default D-Bus interface. This
|
|
383
|
+
will not do anything (except log its call to stdout). It takes one boolean
|
|
384
|
+
input argument, returns nothing, and does not run any custom code.
|
|
385
|
+
|
|
386
|
+
Exactly like in the unittest example above, we then run our program in a way that
|
|
387
|
+
should trigger one suspend call. For this example this is again represented
|
|
388
|
+
by doing just a direct D-Bus call using `busctl`. The log of the call gets
|
|
389
|
+
asserted.
|
|
390
|
+
|
|
391
|
+
The context manager automatically terminates the mock server process after
|
|
392
|
+
the test completes.
|
|
391
393
|
|
|
392
394
|
## Simple example from shell
|
|
393
395
|
|
|
@@ -11,20 +11,20 @@ D-Bus services such as upower, systemd, logind, gnome-session or others,
|
|
|
11
11
|
and it is hard (or impossible without root privileges) to set the state
|
|
12
12
|
of the real services to what you expect in your tests.
|
|
13
13
|
|
|
14
|
-
Suppose you want to write tests for
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
Now, your test suite should not really talk to the actual system D-Bus
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
14
|
+
Suppose you want to write tests for a desktop environment's power management:
|
|
15
|
+
You want to verify that after the configured idle time the program suspends the
|
|
16
|
+
machine. So your program calls `org.freedesktop.login1.Manager.Suspend()` on
|
|
17
|
+
the system D-Bus.
|
|
18
|
+
|
|
19
|
+
Now, your test suite should not really talk to the actual system D-Bus and
|
|
20
|
+
the real systemd; a `make check` that suspends your machine will not be
|
|
21
|
+
considered very friendly by most people, and if you want to run this in
|
|
22
|
+
continuous integration test servers or package build environments, chances
|
|
23
|
+
are that your process does not have the privilege to suspend, or there is
|
|
24
|
+
no system bus or running systemd to begin with. Likewise, there is no way
|
|
25
|
+
for an user process to forcefully set the system/seat idle flag in logind,
|
|
26
|
+
so your tests cannot set up the expected test environment on the real
|
|
27
|
+
daemon.
|
|
28
28
|
|
|
29
29
|
That's where mock objects come into play: They look like the real API
|
|
30
30
|
(or at least the parts that you actually need), but they do not actually
|
|
@@ -47,11 +47,12 @@ convenience D-Bus launch API that way.
|
|
|
47
47
|
|
|
48
48
|
## Simple example using Python's unittest
|
|
49
49
|
|
|
50
|
-
Picking up the above example about mocking
|
|
51
|
-
this is how you would set up a mock
|
|
50
|
+
Picking up the above example about mocking systemd-logind's `Suspend()`
|
|
51
|
+
method, this is how you would set up a mock logind in your test case:
|
|
52
52
|
|
|
53
53
|
```python
|
|
54
54
|
import subprocess
|
|
55
|
+
import unittest
|
|
55
56
|
|
|
56
57
|
import dbus
|
|
57
58
|
|
|
@@ -65,29 +66,32 @@ class TestMyProgram(dbusmock.DBusTestCase):
|
|
|
65
66
|
cls.dbus_con = cls.get_dbus(system_bus=True)
|
|
66
67
|
|
|
67
68
|
def setUp(self):
|
|
68
|
-
self.p_mock = self.spawn_server('org.freedesktop.
|
|
69
|
-
'/org/freedesktop/
|
|
70
|
-
'org.freedesktop.
|
|
69
|
+
self.p_mock = self.spawn_server('org.freedesktop.login1',
|
|
70
|
+
'/org/freedesktop/login1',
|
|
71
|
+
'org.freedesktop.login1.Manager',
|
|
71
72
|
system_bus=True,
|
|
72
73
|
stdout=subprocess.PIPE)
|
|
74
|
+
self.addCleanup(self.p_mock.wait)
|
|
75
|
+
self.addCleanup(self.p_mock.terminate)
|
|
76
|
+
self.addCleanup(self.p_mock.stdout.close)
|
|
73
77
|
|
|
74
|
-
# Get a proxy for the
|
|
75
|
-
self.
|
|
76
|
-
'org.freedesktop.
|
|
78
|
+
# Get a proxy for the logind object's Mock interface
|
|
79
|
+
self.dbus_logind_mock = dbus.Interface(self.dbus_con.get_object(
|
|
80
|
+
'org.freedesktop.login1', '/org/freedesktop/login1'),
|
|
77
81
|
dbusmock.MOCK_IFACE)
|
|
78
82
|
|
|
79
|
-
self.
|
|
80
|
-
|
|
81
|
-
def tearDown(self):
|
|
82
|
-
self.p_mock.stdout.close()
|
|
83
|
-
self.p_mock.terminate()
|
|
84
|
-
self.p_mock.wait()
|
|
83
|
+
self.dbus_logind_mock.AddMethod('', 'Suspend', 'b', '', '')
|
|
85
84
|
|
|
86
85
|
def test_suspend_on_idle(self):
|
|
87
86
|
# run your program in a way that should trigger one suspend call
|
|
87
|
+
# represented here as direct D-Bus call
|
|
88
|
+
subprocess.check_call(
|
|
89
|
+
["busctl", "call", "org.freedesktop.login1",
|
|
90
|
+
"/org/freedesktop/login1", "org.freedesktop.login1.Manager",
|
|
91
|
+
"Suspend", "b", "false"])
|
|
88
92
|
|
|
89
93
|
# now check the log that we got one Suspend() call
|
|
90
|
-
self.assertRegex(self.p_mock.stdout.readline(), b'^[0-9.]+ Suspend$')
|
|
94
|
+
self.assertRegex(self.p_mock.stdout.readline(), b'^[0-9.]+ Suspend False$')
|
|
91
95
|
```
|
|
92
96
|
|
|
93
97
|
Let's walk through:
|
|
@@ -103,7 +107,7 @@ Let's walk through:
|
|
|
103
107
|
instead of `setUp()` is enough.
|
|
104
108
|
|
|
105
109
|
- `setUp()` spawns the mock D-Bus server process for an initial
|
|
106
|
-
`/org/freedesktop/
|
|
110
|
+
`/org/freedesktop/login1` object with an `org.freedesktop.login1`
|
|
107
111
|
D-Bus interface on the system bus. We capture its stdout to be
|
|
108
112
|
able to verify that methods were called.
|
|
109
113
|
|
|
@@ -113,18 +117,21 @@ Let's walk through:
|
|
|
113
117
|
stdout). It takes no input arguments, returns nothing, and does
|
|
114
118
|
not run any custom code.
|
|
115
119
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
120
|
+
We use `addCleanup()` to register cleanup handlers that will
|
|
121
|
+
stop our mock D-Bus server after each test. This ensures each
|
|
122
|
+
test case has a fresh and clean logind mock instance. Of course
|
|
123
|
+
you can also set up everything in `setUpClass()` if tests
|
|
119
124
|
do not interfere with each other on setting up the mock.
|
|
120
125
|
|
|
121
126
|
- `test_suspend_on_idle()` is the actual test case. It needs to run
|
|
122
|
-
your program in a way that should trigger one suspend call.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
127
|
+
your program in a way that should trigger one suspend call. For this
|
|
128
|
+
example this is represented by doing just a direct D-Bus call using
|
|
129
|
+
`busctl`.
|
|
130
|
+
|
|
131
|
+
That `Suspend()` call is now being served by our mock instead of the real
|
|
132
|
+
logind, there will not be any actual machine suspend. Our mock process
|
|
133
|
+
will log the method call together with a time stamp; you can use the
|
|
134
|
+
latter for doing timing related tests, but we just ignore it here.
|
|
128
135
|
|
|
129
136
|
## Simple example using pytest
|
|
130
137
|
|
|
@@ -132,73 +139,68 @@ The same functionality as above but instead using the pytest fixture provided
|
|
|
132
139
|
by this package.
|
|
133
140
|
|
|
134
141
|
```python
|
|
142
|
+
# Enable dbusmock's pytest fixtures (can also go in conftest.py)
|
|
143
|
+
pytest_plugins = "dbusmock.pytest_fixtures"
|
|
144
|
+
|
|
135
145
|
import subprocess
|
|
136
146
|
|
|
137
147
|
import dbus
|
|
138
|
-
import pytest
|
|
139
148
|
|
|
140
149
|
import dbusmock
|
|
141
150
|
|
|
142
151
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
'org.freedesktop.
|
|
147
|
-
'/org/freedesktop/
|
|
148
|
-
'org.freedesktop.
|
|
149
|
-
|
|
150
|
-
stdout=subprocess.PIPE)
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
def test_suspend_on_idle(upower_mock):
|
|
167
|
-
# run your program in a way that should trigger one suspend call
|
|
168
|
-
|
|
169
|
-
# now check the log that we got one Suspend() call
|
|
170
|
-
assert upower_mock.stdout.readline() == b'^[0-9.]+ Suspend$'
|
|
152
|
+
def test_suspend_on_idle(dbusmock_system):
|
|
153
|
+
# Spawn the mock D-Bus server for logind on the system bus
|
|
154
|
+
with dbusmock.SpawnedMock.spawn_for_name(
|
|
155
|
+
'org.freedesktop.login1',
|
|
156
|
+
'/org/freedesktop/login1',
|
|
157
|
+
'org.freedesktop.login1.Manager',
|
|
158
|
+
dbusmock.BusType.SYSTEM,
|
|
159
|
+
stdout=subprocess.PIPE) as p_mock:
|
|
160
|
+
|
|
161
|
+
# Get a proxy for the logind object's Mock interface
|
|
162
|
+
obj_logind = p_mock.obj
|
|
163
|
+
obj_logind.AddMethod('', 'Suspend', 'b', '', '', interface_name=dbusmock.MOCK_IFACE)
|
|
164
|
+
|
|
165
|
+
# Run your program in a way that should trigger one suspend call
|
|
166
|
+
# represented here as direct D-Bus call
|
|
167
|
+
subprocess.check_call(
|
|
168
|
+
["busctl", "call", "org.freedesktop.login1",
|
|
169
|
+
"/org/freedesktop/login1", "org.freedesktop.login1.Manager",
|
|
170
|
+
"Suspend", "b", "false"])
|
|
171
|
+
|
|
172
|
+
# Check the log that we got one Suspend() call
|
|
173
|
+
assert b'Suspend False\n' in p_mock.stdout.readline()
|
|
171
174
|
```
|
|
172
175
|
|
|
173
176
|
Let's walk through:
|
|
174
177
|
|
|
178
|
+
- We enable dbusmock's pytest fixtures with `pytest_plugins = "dbusmock.pytest_fixtures"`.
|
|
179
|
+
This makes fixtures like `dbusmock_system` and `dbusmock_session` available to your
|
|
180
|
+
tests. In a real project, you would typically put this line in your `conftest.py`
|
|
181
|
+
instead of in each test file.
|
|
182
|
+
|
|
175
183
|
- We import the `dbusmock_system` fixture from dbusmock which provides us
|
|
176
|
-
with a system bus started for our test case
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
`/org/freedesktop/
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
your program in a way that should trigger one suspend call. Your
|
|
197
|
-
program will try to call `Suspend()`, but as that's now being
|
|
198
|
-
served by our mock instead of upower, there will not be any actual
|
|
199
|
-
machine suspend. Our mock process will log the method call
|
|
200
|
-
together with a time stamp; you can use the latter for doing
|
|
201
|
-
timing related tests, but we just ignore it here.
|
|
184
|
+
with a system bus started for our test case. Even though we don't use it
|
|
185
|
+
directly in this simple example, it ensures the test environment is set up.
|
|
186
|
+
|
|
187
|
+
- `test_suspend_on_idle()` is the actual test. It uses
|
|
188
|
+
`SpawnedMock.spawn_for_name()` to spawn the mock D-Bus server process for
|
|
189
|
+
`/org/freedesktop/login1` object with `org.freedesktop.login1.Manager`
|
|
190
|
+
interface. We capture its stdout to verify that methods were called.
|
|
191
|
+
|
|
192
|
+
We then call `org.freedesktop.DBus.Mock.AddMethod()` to add a `Suspend()`
|
|
193
|
+
method to our new logind mock object to the default D-Bus interface. This
|
|
194
|
+
will not do anything (except log its call to stdout). It takes one boolean
|
|
195
|
+
input argument, returns nothing, and does not run any custom code.
|
|
196
|
+
|
|
197
|
+
Exactly like in the unittest example above, we then run our program in a way that
|
|
198
|
+
should trigger one suspend call. For this example this is again represented
|
|
199
|
+
by doing just a direct D-Bus call using `busctl`. The log of the call gets
|
|
200
|
+
asserted.
|
|
201
|
+
|
|
202
|
+
The context manager automatically terminates the mock server process after
|
|
203
|
+
the test completes.
|
|
202
204
|
|
|
203
205
|
## Simple example from shell
|
|
204
206
|
|
|
@@ -1,22 +1,11 @@
|
|
|
1
1
|
"""Mock D-Bus objects for test suites."""
|
|
2
2
|
|
|
3
|
-
#
|
|
4
|
-
# the terms of the GNU Lesser General Public License as published by the Free
|
|
5
|
-
# Software Foundation; either version 3 of the License, or (at your option) any
|
|
6
|
-
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
|
|
7
|
-
# of the license.
|
|
8
|
-
|
|
9
|
-
__author__ = "Martin Pitt"
|
|
10
|
-
__copyright__ = """
|
|
11
|
-
(c) 2012 Canonical Ltd.
|
|
12
|
-
(c) 2017 - 2022 Martin Pitt <martin@piware.de>
|
|
13
|
-
"""
|
|
14
|
-
|
|
3
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
15
4
|
|
|
16
5
|
from dbusmock.mockobject import MOCK_IFACE, OBJECT_MANAGER_IFACE, DBusMockObject, get_object, get_objects
|
|
17
6
|
from dbusmock.testcase import BusType, DBusTestCase, PrivateDBus, SpawnedMock
|
|
18
7
|
|
|
19
|
-
try:
|
|
8
|
+
try: # noqa: RUF067
|
|
20
9
|
# created by setuptools_scm
|
|
21
10
|
from dbusmock._version import __version__
|
|
22
11
|
except ImportError:
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
"""Main entry point for running mock server."""
|
|
2
2
|
|
|
3
|
-
#
|
|
4
|
-
# the terms of the GNU Lesser General Public License as published by the Free
|
|
5
|
-
# Software Foundation; either version 3 of the License, or (at your option) any
|
|
6
|
-
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
|
|
7
|
-
# of the license.
|
|
3
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
8
4
|
|
|
9
5
|
__author__ = "Martin Pitt"
|
|
10
6
|
__copyright__ = """
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.38.1"
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
"""Mock D-Bus objects for test suites."""
|
|
2
2
|
|
|
3
|
-
#
|
|
4
|
-
# the terms of the GNU Lesser General Public License as published by the Free
|
|
5
|
-
# Software Foundation; either version 3 of the License, or (at your option) any
|
|
6
|
-
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
|
|
7
|
-
# of the license.
|
|
3
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
8
4
|
|
|
9
5
|
__author__ = "Martin Pitt"
|
|
10
6
|
__copyright__ = """
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
"""pytest fixtures for DBusMock"""
|
|
2
2
|
|
|
3
|
-
#
|
|
4
|
-
# the terms of the GNU Lesser General Public License as published by the Free
|
|
5
|
-
# Software Foundation; either version 3 of the License, or (at your option) any
|
|
6
|
-
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
|
|
7
|
-
# of the license.
|
|
3
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
8
4
|
|
|
9
5
|
__author__ = "Martin Pitt"
|
|
10
6
|
__copyright__ = "(c) 2023 Martin Pitt <martin@piware.de>"
|
|
@@ -7,11 +7,7 @@ or clients.
|
|
|
7
7
|
This supports BlueZ 5 only.
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
|
-
#
|
|
11
|
-
# the terms of the GNU Lesser General Public License as published by the Free
|
|
12
|
-
# Software Foundation; either version 3 of the License, or (at your option) any
|
|
13
|
-
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
|
|
14
|
-
# of the license.
|
|
10
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
15
11
|
|
|
16
12
|
__author__ = "Philip Withnall"
|
|
17
13
|
__copyright__ = """
|
|
@@ -7,11 +7,7 @@ devices.
|
|
|
7
7
|
This supports BlueZ 5 only.
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
|
-
#
|
|
11
|
-
# the terms of the GNU Lesser General Public License as published by the Free
|
|
12
|
-
# Software Foundation; either version 3 of the License, or (at your option) any
|
|
13
|
-
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
|
|
14
|
-
# of the license.
|
|
10
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
15
11
|
|
|
16
12
|
__author__ = "Philip Withnall"
|
|
17
13
|
__copyright__ = """
|