python-dbusmock 0.37.2__tar.gz → 0.38.0__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.
Files changed (77) hide show
  1. {python_dbusmock-0.37.2/python_dbusmock.egg-info → python_dbusmock-0.38.0}/PKG-INFO +97 -95
  2. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/README.md +96 -94
  3. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/__init__.py +2 -13
  4. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/__main__.py +1 -5
  5. python_dbusmock-0.38.0/dbusmock/_version.py +1 -0
  6. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/mockobject.py +1 -5
  7. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/pytest_fixtures.py +1 -5
  8. python_dbusmock-0.38.0/dbusmock/templates/__init__.py +3 -0
  9. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/bluez5-obex.py +1 -5
  10. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/bluez5.py +1 -5
  11. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/gnome_screensaver.py +1 -5
  12. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/gsd_rfkill.py +1 -5
  13. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/logind.py +2 -5
  14. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/low_memory_monitor.py +1 -5
  15. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/modemmanager.py +1 -5
  16. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/networkmanager.py +1 -5
  17. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/notification_daemon.py +1 -5
  18. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/ofono.py +54 -44
  19. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/polkitd.py +6 -5
  20. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/power_profiles_daemon.py +1 -5
  21. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/systemd.py +1 -5
  22. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/timedated.py +1 -5
  23. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/upower.py +1 -5
  24. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/upower_power_profiles_daemon.py +1 -5
  25. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/urfkill.py +1 -5
  26. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/testcase.py +3 -7
  27. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/pyproject.toml +4 -0
  28. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0/python_dbusmock.egg-info}/PKG-INFO +97 -95
  29. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/python_dbusmock.egg-info/SOURCES.txt +1 -0
  30. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_api.py +3 -11
  31. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_api_pytest.py +1 -5
  32. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_bluez5.py +10 -14
  33. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_cli.py +6 -18
  34. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_code.py +1 -5
  35. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_gnome_screensaver.py +4 -10
  36. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_gsd_rfkill.py +4 -10
  37. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_logind.py +24 -21
  38. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_low_memory_monitor.py +4 -10
  39. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_modemmanager.py +4 -13
  40. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_networkmanager.py +4 -10
  41. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_notification_daemon.py +4 -10
  42. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_ofono.py +4 -5
  43. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_polkitd.py +4 -10
  44. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_power_profiles_daemon.py +4 -10
  45. python_dbusmock-0.38.0/tests/test_readme_examples.py +42 -0
  46. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_systemd.py +5 -20
  47. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_timedated.py +4 -11
  48. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_upower.py +4 -10
  49. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/test_urfkill.py +4 -10
  50. python_dbusmock-0.37.2/dbusmock/_version.py +0 -1
  51. python_dbusmock-0.37.2/dbusmock/templates/__init__.py +0 -7
  52. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/.fmf/version +0 -0
  53. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/.github/workflows/pages.yml +0 -0
  54. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/.github/workflows/release.yml +0 -0
  55. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/.github/workflows/tests.yml +0 -0
  56. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/.gitignore +0 -0
  57. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/.pre-commit-config.yaml +0 -0
  58. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/COPYING +0 -0
  59. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/NEWS +0 -0
  60. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/dbusmock/templates/SKELETON +0 -0
  61. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/distro/python-dbusmock.spec +0 -0
  62. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/doc/conf.py +0 -0
  63. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/doc/index.rst +0 -0
  64. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/packit.yaml +0 -0
  65. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/plans/all.fmf +0 -0
  66. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/python_dbusmock.egg-info/dependency_links.txt +0 -0
  67. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/python_dbusmock.egg-info/requires.txt +0 -0
  68. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/python_dbusmock.egg-info/top_level.txt +0 -0
  69. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/setup.cfg +0 -0
  70. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/__init__.py +0 -0
  71. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/conftest.py +0 -0
  72. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/main.fmf +0 -0
  73. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/run +0 -0
  74. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/run-centos +0 -0
  75. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/run-debian +0 -0
  76. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/run-fedora +0 -0
  77. {python_dbusmock-0.37.2 → python_dbusmock-0.38.0}/tests/run-ubuntu +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-dbusmock
3
- Version: 0.37.2
3
+ Version: 0.38.0
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 gnome-settings-daemon's power
204
- plugin, or another program that talks to upower. You want to verify that
205
- after the configured idle time the program suspends the machine. So your
206
- program calls `org.freedesktop.UPower.Suspend()` on the system D-Bus.
207
-
208
- Now, your test suite should not really talk to the actual system D-Bus
209
- and the real upower; a `make check` that suspends your machine will not
210
- be considered very friendly by most people, and if you want to run this
211
- in continuous integration test servers or package build environments,
212
- chances are that your process does not have the privilege to suspend, or
213
- there is no system bus or upower to begin with. Likewise, there is no
214
- way for an user process to forcefully set the system/seat idle flag in
215
- logind, so your tests cannot set up the expected test environment on the
216
- real daemon.
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 upower's `Suspend()` method,
240
- this is how you would set up a mock upower in your test case:
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.UPower',
258
- '/org/freedesktop/UPower',
259
- 'org.freedesktop.UPower',
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 UPower object's Mock interface
264
- self.dbus_upower_mock = dbus.Interface(self.dbus_con.get_object(
265
- 'org.freedesktop.UPower', '/org/freedesktop/UPower'),
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.dbus_upower_mock.AddMethod('', 'Suspend', '', '', '')
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/UPower` object with an `org.freedesktop.UPower`
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
- - `tearDown()` stops our mock D-Bus server again. We do this so that
306
- each test case has a fresh and clean upower instance, but of
307
- course you can also set up everything in `setUpClass()` if tests
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. Your
312
- program will try to call `Suspend()`, but as that's now being
313
- served by our mock instead of upower, there will not be any actual
314
- machine suspend. Our mock process will log the method call
315
- together with a time stamp; you can use the latter for doing
316
- timing related tests, but we just ignore it here.
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
- @pytest.fixture
333
- def upower_mock(dbusmock_system):
334
- p_mock = dbusmock_system.spawn_server(
335
- 'org.freedesktop.UPower',
336
- '/org/freedesktop/UPower',
337
- 'org.freedesktop.UPower',
338
- system_bus=True,
339
- stdout=subprocess.PIPE)
340
-
341
- # Get a proxy for the UPower object's Mock interface
342
- dbus_upower_mock = dbus.Interface(dbusmock_system.get_dbus(True).get_object(
343
- 'org.freedesktop.UPower',
344
- '/org/freedesktop/UPower'
345
- ), dbusmock.MOCK_IFACE)
346
- dbus_upower_mock.AddMethod('', 'Suspend', '', '', '')
347
-
348
- yield p_mock
349
-
350
- p_mock.stdout.close()
351
- p_mock.terminate()
352
- p_mock.wait()
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 wherever the
366
- `dbusmock_system` argument is used by a test case and/or a pytest
367
- fixture.
368
-
369
- - The `upower_mock` fixture spawns the mock D-Bus server process for an initial
370
- `/org/freedesktop/UPower` object with an `org.freedesktop.UPower`
371
- D-Bus interface on the system bus. We capture its stdout to be
372
- able to verify that methods were called.
373
-
374
- We then call `org.freedesktop.DBus.Mock.AddMethod()` to add a
375
- `Suspend()` method to our new object to the default D-Bus
376
- interface. This will not do anything (except log its call to
377
- stdout). It takes no input arguments, returns nothing, and does
378
- not run any custom code.
379
-
380
- This mock server process is yielded to the test function that uses
381
- the `upower_mock` fixture - once the test is complete the process is
382
- terminated again.
383
-
384
- - `test_suspend_on_idle()` is the actual test case. It needs to run
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 gnome-settings-daemon's power
15
- plugin, or another program that talks to upower. You want to verify that
16
- after the configured idle time the program suspends the machine. So your
17
- program calls `org.freedesktop.UPower.Suspend()` on the system D-Bus.
18
-
19
- Now, your test suite should not really talk to the actual system D-Bus
20
- and the real upower; a `make check` that suspends your machine will not
21
- be considered very friendly by most people, and if you want to run this
22
- in continuous integration test servers or package build environments,
23
- chances are that your process does not have the privilege to suspend, or
24
- there is no system bus or upower to begin with. Likewise, there is no
25
- way for an user process to forcefully set the system/seat idle flag in
26
- logind, so your tests cannot set up the expected test environment on the
27
- real daemon.
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 upower's `Suspend()` method,
51
- this is how you would set up a mock upower in your test case:
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.UPower',
69
- '/org/freedesktop/UPower',
70
- 'org.freedesktop.UPower',
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 UPower object's Mock interface
75
- self.dbus_upower_mock = dbus.Interface(self.dbus_con.get_object(
76
- 'org.freedesktop.UPower', '/org/freedesktop/UPower'),
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.dbus_upower_mock.AddMethod('', 'Suspend', '', '', '')
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/UPower` object with an `org.freedesktop.UPower`
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
- - `tearDown()` stops our mock D-Bus server again. We do this so that
117
- each test case has a fresh and clean upower instance, but of
118
- course you can also set up everything in `setUpClass()` if tests
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. Your
123
- program will try to call `Suspend()`, but as that's now being
124
- served by our mock instead of upower, there will not be any actual
125
- machine suspend. Our mock process will log the method call
126
- together with a time stamp; you can use the latter for doing
127
- timing related tests, but we just ignore it here.
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
- @pytest.fixture
144
- def upower_mock(dbusmock_system):
145
- p_mock = dbusmock_system.spawn_server(
146
- 'org.freedesktop.UPower',
147
- '/org/freedesktop/UPower',
148
- 'org.freedesktop.UPower',
149
- system_bus=True,
150
- stdout=subprocess.PIPE)
151
-
152
- # Get a proxy for the UPower object's Mock interface
153
- dbus_upower_mock = dbus.Interface(dbusmock_system.get_dbus(True).get_object(
154
- 'org.freedesktop.UPower',
155
- '/org/freedesktop/UPower'
156
- ), dbusmock.MOCK_IFACE)
157
- dbus_upower_mock.AddMethod('', 'Suspend', '', '', '')
158
-
159
- yield p_mock
160
-
161
- p_mock.stdout.close()
162
- p_mock.terminate()
163
- p_mock.wait()
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 wherever the
177
- `dbusmock_system` argument is used by a test case and/or a pytest
178
- fixture.
179
-
180
- - The `upower_mock` fixture spawns the mock D-Bus server process for an initial
181
- `/org/freedesktop/UPower` object with an `org.freedesktop.UPower`
182
- D-Bus interface on the system bus. We capture its stdout to be
183
- able to verify that methods were called.
184
-
185
- We then call `org.freedesktop.DBus.Mock.AddMethod()` to add a
186
- `Suspend()` method to our new object to the default D-Bus
187
- interface. This will not do anything (except log its call to
188
- stdout). It takes no input arguments, returns nothing, and does
189
- not run any custom code.
190
-
191
- This mock server process is yielded to the test function that uses
192
- the `upower_mock` fixture - once the test is complete the process is
193
- terminated again.
194
-
195
- - `test_suspend_on_idle()` is the actual test case. It needs to run
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
- # This program is free software; you can redistribute it and/or modify it under
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
- # This program is free software; you can redistribute it and/or modify it under
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.0"
@@ -1,10 +1,6 @@
1
1
  """Mock D-Bus objects for test suites."""
2
2
 
3
- # This program is free software; you can redistribute it and/or modify it under
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
- # This program is free software; you can redistribute it and/or modify it under
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>"
@@ -0,0 +1,3 @@
1
+ """Mock templates for common D-Bus services"""
2
+
3
+ # SPDX-License-Identifier: LGPL-3.0-or-later
@@ -7,11 +7,7 @@ or clients.
7
7
  This supports BlueZ 5 only.
8
8
  """
9
9
 
10
- # This program is free software; you can redistribute it and/or modify it under
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
- # This program is free software; you can redistribute it and/or modify it under
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__ = """