Metadata-Version: 2.1
Name: python-dbusmock
Version: 0.31.1
Summary: Mock D-Bus objects
Home-page: https://github.com/martinpitt/python-dbusmock
Download-URL: https://pypi.python.org/pypi/python-dbusmock/
Author: Martin Pitt
Author-email: Martin Pitt <martin@piware.de>
License: GNU LESSER GENERAL PUBLIC LICENSE
                               Version 3, 29 June 2007
        
         Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
         Everyone is permitted to copy and distribute verbatim copies
         of this license document, but changing it is not allowed.
        
        
          This version of the GNU Lesser General Public License incorporates
        the terms and conditions of version 3 of the GNU General Public
        License, supplemented by the additional permissions listed below.
        
          0. Additional Definitions.
        
          As used herein, "this License" refers to version 3 of the GNU Lesser
        General Public License, and the "GNU GPL" refers to version 3 of the GNU
        General Public License.
        
          "The Library" refers to a covered work governed by this License,
        other than an Application or a Combined Work as defined below.
        
          An "Application" is any work that makes use of an interface provided
        by the Library, but which is not otherwise based on the Library.
        Defining a subclass of a class defined by the Library is deemed a mode
        of using an interface provided by the Library.
        
          A "Combined Work" is a work produced by combining or linking an
        Application with the Library.  The particular version of the Library
        with which the Combined Work was made is also called the "Linked
        Version".
        
          The "Minimal Corresponding Source" for a Combined Work means the
        Corresponding Source for the Combined Work, excluding any source code
        for portions of the Combined Work that, considered in isolation, are
        based on the Application, and not on the Linked Version.
        
          The "Corresponding Application Code" for a Combined Work means the
        object code and/or source code for the Application, including any data
        and utility programs needed for reproducing the Combined Work from the
        Application, but excluding the System Libraries of the Combined Work.
        
          1. Exception to Section 3 of the GNU GPL.
        
          You may convey a covered work under sections 3 and 4 of this License
        without being bound by section 3 of the GNU GPL.
        
          2. Conveying Modified Versions.
        
          If you modify a copy of the Library, and, in your modifications, a
        facility refers to a function or data to be supplied by an Application
        that uses the facility (other than as an argument passed when the
        facility is invoked), then you may convey a copy of the modified
        version:
        
           a) under this License, provided that you make a good faith effort to
           ensure that, in the event an Application does not supply the
           function or data, the facility still operates, and performs
           whatever part of its purpose remains meaningful, or
        
           b) under the GNU GPL, with none of the additional permissions of
           this License applicable to that copy.
        
          3. Object Code Incorporating Material from Library Header Files.
        
          The object code form of an Application may incorporate material from
        a header file that is part of the Library.  You may convey such object
        code under terms of your choice, provided that, if the incorporated
        material is not limited to numerical parameters, data structure
        layouts and accessors, or small macros, inline functions and templates
        (ten or fewer lines in length), you do both of the following:
        
           a) Give prominent notice with each copy of the object code that the
           Library is used in it and that the Library and its use are
           covered by this License.
        
           b) Accompany the object code with a copy of the GNU GPL and this license
           document.
        
          4. Combined Works.
        
          You may convey a Combined Work under terms of your choice that,
        taken together, effectively do not restrict modification of the
        portions of the Library contained in the Combined Work and reverse
        engineering for debugging such modifications, if you also do each of
        the following:
        
           a) Give prominent notice with each copy of the Combined Work that
           the Library is used in it and that the Library and its use are
           covered by this License.
        
           b) Accompany the Combined Work with a copy of the GNU GPL and this license
           document.
        
           c) For a Combined Work that displays copyright notices during
           execution, include the copyright notice for the Library among
           these notices, as well as a reference directing the user to the
           copies of the GNU GPL and this license document.
        
           d) Do one of the following:
        
               0) Convey the Minimal Corresponding Source under the terms of this
               License, and the Corresponding Application Code in a form
               suitable for, and under terms that permit, the user to
               recombine or relink the Application with a modified version of
               the Linked Version to produce a modified Combined Work, in the
               manner specified by section 6 of the GNU GPL for conveying
               Corresponding Source.
        
               1) Use a suitable shared library mechanism for linking with the
               Library.  A suitable mechanism is one that (a) uses at run time
               a copy of the Library already present on the user's computer
               system, and (b) will operate properly with a modified version
               of the Library that is interface-compatible with the Linked
               Version.
        
           e) Provide Installation Information, but only if you would otherwise
           be required to provide such information under section 6 of the
           GNU GPL, and only to the extent that such information is
           necessary to install and execute a modified version of the
           Combined Work produced by recombining or relinking the
           Application with a modified version of the Linked Version. (If
           you use option 4d0, the Installation Information must accompany
           the Minimal Corresponding Source and Corresponding Application
           Code. If you use option 4d1, you must provide the Installation
           Information in the manner specified by section 6 of the GNU GPL
           for conveying Corresponding Source.)
        
          5. Combined Libraries.
        
          You may place library facilities that are a work based on the
        Library side by side in a single library together with other library
        facilities that are not Applications and are not covered by this
        License, and convey such a combined library under terms of your
        choice, if you do both of the following:
        
           a) Accompany the combined library with a copy of the same work based
           on the Library, uncombined with any other library facilities,
           conveyed under the terms of this License.
        
           b) Give prominent notice with the combined library that part of it
           is a work based on the Library, and explaining where to find the
           accompanying uncombined form of the same work.
        
          6. Revised Versions of the GNU Lesser General Public License.
        
          The Free Software Foundation may publish revised and/or new versions
        of the GNU Lesser General Public License from time to time. Such new
        versions will be similar in spirit to the present version, but may
        differ in detail to address new problems or concerns.
        
          Each version is given a distinguishing version number. If the
        Library as you received it specifies that a certain numbered version
        of the GNU Lesser General Public License "or any later version"
        applies to it, you have the option of following the terms and
        conditions either of that published version or of any later version
        published by the Free Software Foundation. If the Library as you
        received it does not specify a version number of the GNU Lesser
        General Public License, you may choose any version of the GNU Lesser
        General Public License ever published by the Free Software Foundation.
        
          If the Library as you received it specifies that a proxy can decide
        whether future versions of the GNU Lesser General Public License shall
        apply, that proxy's public statement of acceptance of any version is
        permanent authorization for you to choose that version for the
        Library.
        
Project-URL: homepage, https://github.com/martinpitt/python-dbusmock
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
Classifier: Programming Language :: Python :: 3
Classifier: Development Status :: 6 - Mature
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: POSIX :: BSD
Classifier: Operating System :: Unix
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Testing
Classifier: Topic :: Software Development :: Testing :: Mocking
Classifier: Topic :: Software Development :: Testing :: Unit
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: COPYING
Requires-Dist: dbus-python

![CI status](https://github.com/martinpitt/python-dbusmock/actions/workflows/tests.yml/badge.svg)

python-dbusmock
===============

## Purpose

With this program/Python library you can easily create mock objects on
D-Bus. This is useful for writing tests for software which talks to
D-Bus services such as upower, systemd, logind, gnome-session or others,
and it is hard (or impossible without root privileges) to set the state
of the real services to what you expect in your tests.

Suppose you want to write tests for gnome-settings-daemon's power
plugin, or another program that talks to upower. You want to verify that
after the configured idle time the program suspends the machine. So your
program calls `org.freedesktop.UPower.Suspend()` on the system D-Bus.

Now, your test suite should not really talk to the actual system D-Bus
and the real upower; a `make check` that suspends your machine will not
be considered very friendly by most people, and if you want to run this
in continuous integration test servers or package build environments,
chances are that your process does not have the privilege to suspend, or
there is no system bus or upower to begin with. Likewise, there is no
way for an user process to forcefully set the system/seat idle flag in
logind, so your tests cannot set up the expected test environment on the
real daemon.

That's where mock objects come into play: They look like the real API
(or at least the parts that you actually need), but they do not actually
do anything (or only some action that you specify yourself). You can
configure their state, behaviour and responses as you like in your test,
without making any assumptions about the real system status.

When using a local system/session bus, you can do unit or integration
testing without needing root privileges or disturbing a running system.
The Python API offers some convenience functions like
`start_session_bus()` and `start_system_bus()` for this, in a
`DBusTestCase` class (subclass of the standard `unittest.TestCase`) or
alternatively as a `@pytest.fixture`.

You can use this with any programming language, as you can run the
mocker as a normal program. The actual setup of the mock (adding
objects, methods, properties, and signals) all happen via D-Bus methods
on the `org.freedesktop.DBus.Mock` interface. You just don't have the
convenience D-Bus launch API that way.

## Simple example using Python's unittest

Picking up the above example about mocking upower's `Suspend()` method,
this is how you would set up a mock upower in your test case:

```python
import subprocess

import dbus

import dbusmock


class TestMyProgram(dbusmock.DBusTestCase):
    @classmethod
    def setUpClass(cls):
        cls.start_system_bus()
        cls.dbus_con = cls.get_dbus(system_bus=True)

    def setUp(self):
        self.p_mock = self.spawn_server('org.freedesktop.UPower',
                                        '/org/freedesktop/UPower',
                                        'org.freedesktop.UPower',
                                        system_bus=True,
                                        stdout=subprocess.PIPE)

        # Get a proxy for the UPower object's Mock interface
        self.dbus_upower_mock = dbus.Interface(self.dbus_con.get_object(
            'org.freedesktop.UPower', '/org/freedesktop/UPower'),
            dbusmock.MOCK_IFACE)

        self.dbus_upower_mock.AddMethod('', 'Suspend', '', '', '')

    def tearDown(self):
        self.p_mock.stdout.close()
        self.p_mock.terminate()
        self.p_mock.wait()

    def test_suspend_on_idle(self):
        # run your program in a way that should trigger one suspend call

        # now check the log that we got one Suspend() call
        self.assertRegex(self.p_mock.stdout.readline(), b'^[0-9.]+ Suspend$')
```

Let's walk through:

 -   We derive our tests from `dbusmock.DBusTestCase` instead of
     `unittest.TestCase` directly, to make use of the convenience API
     to start a local system bus.

 -   `setUpClass()` starts a local system bus, and makes a connection
     to it available to all methods as `dbus_con`. `True` means that we
     connect to the system bus, not the session bus. We can use the
     same bus for all tests, so doing this once in `setUpClass()`
     instead of `setUp()` is enough.

 -   `setUp()` spawns the mock D-Bus server process for an initial
     `/org/freedesktop/UPower` object with an `org.freedesktop.UPower`
     D-Bus interface on the system bus. We capture its stdout to be
     able to verify that methods were called.

     We then call `org.freedesktop.DBus.Mock.AddMethod()` to add a
     `Suspend()` method to our new object to the default D-Bus
     interface. This will not do anything (except log its call to
     stdout). It takes no input arguments, returns nothing, and does
     not run any custom code.

 -   `tearDown()` stops our mock D-Bus server again. We do this so that
     each test case has a fresh and clean upower instance, but of
     course you can also set up everything in `setUpClass()` if tests
     do not interfere with each other on setting up the mock.

 -   `test_suspend_on_idle()` is the actual test case. It needs to run
     your program in a way that should trigger one suspend call. Your
     program will try to call `Suspend()`, but as that's now being
     served by our mock instead of upower, there will not be any actual
     machine suspend. Our mock process will log the method call
     together with a time stamp; you can use the latter for doing
     timing related tests, but we just ignore it here.

## Simple example using pytest

The same functionality as above but instead using the pytest fixture provided
by this package.

```python
import subprocess

import dbus
import pytest

import dbusmock


@pytest.fixture
def upower_mock(dbusmock_system):
    p_mock = dbusmock_system.spawn_server(
        'org.freedesktop.UPower',
        '/org/freedesktop/UPower',
        'org.freedesktop.UPower',
        system_bus=True,
        stdout=subprocess.PIPE)

    # Get a proxy for the UPower object's Mock interface
    dbus_upower_mock = dbus.Interface(dbusmock_system.get_dbus(True).get_object(
        'org.freedesktop.UPower',
        '/org/freedesktop/UPower'
    ), dbusmock.MOCK_IFACE)
    dbus_upower_mock.AddMethod('', 'Suspend', '', '', '')

    yield p_mock

    p_mock.stdout.close()
    p_mock.terminate()
    p_mock.wait()


def test_suspend_on_idle(upower_mock):
    # run your program in a way that should trigger one suspend call

    # now check the log that we got one Suspend() call
    assert upower_mock.stdout.readline() == b'^[0-9.]+ Suspend$'
```

Let's walk through:

- We import the `dbusmock_system` fixture from dbusmock which provides us
  with a system bus started for our test case wherever the
  `dbusmock_system` argument is used by a test case and/or a pytest
  fixture.

- The `upower_mock` fixture spawns the mock D-Bus server process for an initial
  `/org/freedesktop/UPower` object with an `org.freedesktop.UPower`
  D-Bus interface on the system bus. We capture its stdout to be
  able to verify that methods were called.

  We then call `org.freedesktop.DBus.Mock.AddMethod()` to add a
  `Suspend()` method to our new object to the default D-Bus
  interface. This will not do anything (except log its call to
  stdout). It takes no input arguments, returns nothing, and does
  not run any custom code.

  This mock server process is yielded to the test function that uses
  the `upower_mock` fixture - once the test is complete the process is
  terminated again.

- `test_suspend_on_idle()` is the actual test case. It needs to run
  your program in a way that should trigger one suspend call. Your
  program will try to call `Suspend()`, but as that's now being
  served by our mock instead of upower, there will not be any actual
  machine suspend. Our mock process will log the method call
  together with a time stamp; you can use the latter for doing
  timing related tests, but we just ignore it here.

## Simple example from shell

We use the actual session bus for this example. You can use
`dbus-run-session` to start a private one as well if you want, but that
is not part of the actual mocking.

So let's start a mock at the D-Bus name `com.example.Foo` with an
initial "main" object on path /, with the main D-Bus interface
`com.example.Foo.Manager`:

    python3 -m dbusmock com.example.Foo / com.example.Foo.Manager

On another terminal, let's first see what it does:

    gdbus introspect --session -d com.example.Foo -o /

You'll see that it supports the standard D-Bus `Introspectable` and
`Properties` interfaces, as well as the `org.freedesktop.DBus.Mock`
interface for controlling the mock, but no "real" functionality yet.
So let's add a method:

    gdbus call --session -d com.example.Foo -o / -m org.freedesktop.DBus.Mock.AddMethod '' Ping '' '' ''

Now you can see the new method in `introspect`, and call it:

    gdbus call --session -d com.example.Foo -o / -m com.example.Foo.Manager.Ping

The mock process in the other terminal will log the method call with a
time stamp, and you'll see something like `1348832614.970 Ping`.

Now add another method with two int arguments and a return value and
call it:

    gdbus call --session -d com.example.Foo -o / -m org.freedesktop.DBus.Mock.AddMethod \
        '' Add 'ii' 'i' 'ret = args[0] + args[1]'
    gdbus call --session -d com.example.Foo -o / -m com.example.Foo.Manager.Add 2 3

This will print `(5,)` as expected (remember that the return value is
always a tuple), and again the mock process will log the Add method
call.

You can do the same operations in e. g. d-feet or any other D-Bus
language binding.

## Interactive debugging

It's possible to use dbus-mock to run interactive sessions using something like:

    python3 -m dbusmock com.example.Foo / com.example.Foo.Manager -e $SHELL

Where a shell session with the defined mocks is set and others can be added.

Or more complex ones such as:

    python3 -m dbusmock --session -t upower -e \
      python3 -m dbusmock com.example.Foo / com.example.Foo.Manager -e \
        gdbus introspect --session -d com.example.Foo -o /

## Logging

Usually you want to verify which methods have been called on the mock
with which arguments. There are three ways to do that:

 -   By default, the mock process writes the call log to stdout.
 -   You can call the mock process with the `-l`/`--logfile` argument,
     or specify a log file object in the `spawn_server()` method if you
     are using Python.
 -   You can use the `GetCalls()`, `GetMethodCalls()` and
     `ClearCalls()` methods on the `org.freedesktop.DBus.Mock` D-Bus
     interface to get an array of tuples describing the calls.

## Templates

Some D-Bus services are commonly used in test suites, such as UPower or
NetworkManager. python-dbusmock provides "templates" which set up the
common structure of these services (their main objects, properties, and
methods) so that you do not need to carry around this common code, and
only need to set up the particular properties and specific D-Bus objects
that you need. These templates can be parameterized for common
customizations, and they can provide additional convenience methods on
the `org.freedesktop.DBus.Mock` interface to provide more abstract
functionality like "add a battery".

For example, for starting a server with the `upower` template in
Python you can run

    (self.p_mock, self.obj_upower) = self.spawn_server_template(
        'upower', {'OnBattery': True}, stdout=subprocess.PIPE)

or load a template into an already running server with the
`AddTemplate()` method; this is particularly useful if you are not using
Python:

    python3 -m dbusmock --system org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower

    gdbus call --system -d org.freedesktop.UPower -o /org/freedesktop/UPower -m org.freedesktop.DBus.Mock.AddTemplate 'upower' '{"OnBattery": <true>}'

This creates all expected properties such as `DaemonVersion`, and
changes the default for one of them (`OnBattery`) through the (optional)
parameters dict.

If you do not need to specify parameters, you can do this in a simpler
way with

    python3 -m dbusmock --template upower

The template does not create any devices by default. You can add some
with the template's convenience methods like

    ac_path = self.dbusmock.AddAC('mock_AC', 'Mock AC')
    bt_path = self.dbusmock.AddChargingBattery('mock_BAT', 'Mock Battery', 30.0, 1200)

or calling `AddObject()` yourself with the desired properties, of
course.

Templates commonly implement some non-trivial functionality with actual Python
methods and the standard [dbus-python](https://dbus.freedesktop.org/doc/dbus-python/)
[`@dbus.service.method`](https://dbus.freedesktop.org/doc/dbus-python/dbus.service.html#dbus.service.method)
decorator.

To build your own template, you can copy
[dbusmock/templates/SKELETON](./dbusmock/templates/SKELETON) to your
new template file name and replace `CHANGEME` with the actual code/values.
Look at [dbusmock/templates/upower.py](./dbusmock/templates/upower.py) for
a real-life implementation.

A template can be loaded from these locations:

 * Provide a path to its `.py` file. This is intended for running tests out of
   git/build trees with very project specific or unstable templates.

 * From [`$XDG_DATA_DIRS/python-dbusmock/templates/`*name*`.py`](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html).
   This is intended for shipping reusable templates in distribution development
   packages. Load them by module name.

 * python-dbusmock [ships a set of widely applicable templates](./dbusmock/templates/)
   which are collaboratively maintained, like the `upower` one in the example
   above. Load them by module name.

## More Examples

Have a look at the test suite for two real-live use cases:

 -   `tests/test_upower.py` simulates upowerd, in a more complete way
     than in above example and using the `upower` template. It verifies
     that `upower --dump` is convinced that it's talking to upower.
 -   `tests/test_api.py` runs a mock on the session bus and exercises
     all available functionality, such as adding additional objects,
     properties, multiple methods, input arguments, return values, code
     in methods, sending signals, raising exceptions, and introspection.

## Documentation

The `dbusmock` module has extensive documentation built in, which you
can read with e. g. `pydoc3 dbusmock` or online at
https://martinpitt.github.io/python-dbusmock/

`pydoc3 dbusmock.DBusMockObject` shows the D-Bus API of the mock object,
i. e. methods like `AddObject()`, `AddMethod()` etc. which are used to
set up your mock object.

`pydoc3 dbusmock.DBusTestCase` shows the convenience Python API for
writing test cases with local private session/system buses and launching
the server.

`pydoc3 dbusmock.templates` shows all available templates.

`pydoc3 dbusmock.templates.NAME` shows the documentation and available
parameters for the `NAME` template.

`python3 -m dbusmock --help` shows the arguments and options for running
the mock server as a program.

## Development

python-dbusmock is hosted on https://github.com/martinpitt/python-dbusmock

Run the unit tests with `python3 -m unittest` or `pytest`.

In CI, the unit tests run in containers. You can run them locally with e.g.

    tests/run registry.fedoraproject.org/fedora:latest

Check the [unit-tests GitHub workflow](.github/workflows/tests.yml) for the
operating systems/container images on which python-dbusmock is tested and
supported.

To debug failures interactively, run

    DEBUG=1 tests/run [image]

which will sleep on failures. You can then attach to the running container
image with e.g. `podman exec -itl bash`. The `/source` directory is mounted from the
host, i.e. edit files in your normal git checkout outside of the container, and
re-run all tests in the container shell like above. You can also run a specific
test:

    python3 -m unittest tests.test_api.TestAPI.test_onearg_ret
