varlink(1)

varlink1

Varlink documentation an implementation of the varlink protocol see https://www.varlink.org for more information about the varlink protocol and interface definition files. for server implementations use the varlink.server class. for client implementations use the varlink.client class. for installation and examples, see the git repository https://github.com/varlink/python. or the source code of varlink.tests.test_orgexamplemore

Examples

Server and Client example of varlink for python

From the main git repository directory run:

$ PYTHONPATH=$(pwd) python3 ./varlink/tests/test_orgexamplemore.py

or:

$ PYTHONPATH=$(pwd) python3 ./varlink/tests/test_orgexamplemore.py --varlink="unix:@test" &
Listening on @test
[1] 6434
$ PYTHONPATH=$(pwd) python3 ./varlink/tests/test_orgexamplemore.py --client --varlink="unix:@test"
[...]
exception varlink.tests.test_orgexamplemore.ActionFailed(reason)
class varlink.tests.test_orgexamplemore.ServiceRequestHandler(request, client_address, server)
class varlink.tests.test_orgexamplemore.TestService(methodName='runTest')

Testing

varlink.mock.mockedservice(fake_service=None, fake_types=None, address='unix:@test', name=None, vendor='varlink', product='mock', version=1, url='http://localhost')

Varlink mocking service

To mock a fake service and merely test your varlink client against.

The mocking feature is for testing purpose, it's allow you to test your varlink client against a fake service which will returned self handed result defined in your object who will be mocked.

Example:

>>> import unittest
>>> from varlink import mock
>>> import varlink
>>>
>>>
>>> types = '''
>>> type MyPersonalType (
>>>     foo: string,
>>>     bar: string,
>>> )
>>> '''
>>>
>>>
>>> class Service():
>>>
>>>     def Test1(self, param1: int) -> dict:
>>>         '''
>>>         return test: MyPersonalType
>>>         '''
>>>         return {
>>>             "test": {
>>>                 "foo": "bim",
>>>                 "bar": "boom"
>>>             }
>>>         }
>>>
>>>     def Test2(self, param1: str) -> dict:
>>>         '''
>>>         return (test: string)
>>>         '''
>>>         return {"test": param1}
>>>
>>>     def Test3(self, param1: int) -> dict:
>>>         '''
>>>         return (test: int, boom: string, foo: string, bar: 42)
>>>         '''
>>>         return {
>>>             "test": param1 * 2,
>>>             "boom": "foo",
>>>             "foo": "bar",
>>>             "bar": 42,
>>>         }
>>>
>>>
>>> class TestMyClientWithMockedService(unittest.TestCase):
>>>
>>>     @mock.mockedservice(
>>>         fake_service=Service,
>>>         fake_types=types,
>>>         name='org.service.com',
>>>         address='unix:@foo'
>>>     )
>>>     def test_my_client_against_a_mock(self):
>>>         with varlink.Client("unix:@foo") as client:
>>>             connection = client.open('org.service.com')
>>>             self.assertEqual(
>>>                 connection.Test1(param1=1)["test"]["bar"], "boom")
>>>             self.assertEqual(
>>>                    connection.Test2(param1="foo")["test"], "foo")
>>>             self.assertEqual(
>>>                    connection.Test3(param1=6)["test"], 12)
>>>             self.assertEqual(
>>>                    connection.Test3(param1=6)["bar"], 42)

First you need to define a sample class that will be passed to your decorator mock.mockedservice and then a service will be initialized and launched automatically, and after that you just need to connect your client to him and to establish your connection then now you can call your methods and it will give you the expected result.

You can also mock some types too, to help you to mock more complex service and interfaces like podman by example.

You can define the return type by using the method docstring like the method Test1 in our previous example.

The mocking module is only compatible with python 3 or higher version of python because this module require annotation to generate interface description.

If you try to use it with python 2.x it will raise an ImportError.

Indices and tables

•

Index

•

Module Index

•

Search Page