File: mock.py

package info (click to toggle)
python-varlink 31.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 372 kB
  • sloc: python: 2,462; sh: 177; makefile: 31
file content (322 lines) | stat: -rw-r--r-- 10,465 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import datetime
import inspect
import os
import subprocess
import sys
import textwrap
import time
import uuid

import varlink


if sys.version_info[0] == 2:
    raise ImportError("The mock module isn't compatible with python 2")


def cast_type(typeof):
    cast = {'str': 'string'}
    typeof = str(typeof).replace("<class '", "").replace("'>", "")
    return cast.get(typeof, typeof)


def get_ignored():
    ignore = dir(MockedService)
    return ignore


def get_interface_attributs(interface, ignored):
    attributs = {"callables": [], "others": []}
    for attr in dir(interface):
        if attr in ignored:
            continue
        attribut = getattr(interface, attr)
        if callable(attribut):
            attributs["callables"].append(attr)
        else:
            attributs["others"].append(attr)
    return attributs


def generate_callable_interface(interface, attr):
    attribut = getattr(interface, attr)
    signature = inspect.signature(attribut)
    params = signature.parameters.values()
    sign = []
    for param in params:
        if param.name == "self":
            continue
        typeof = param.annotation
        sign.append("{}: {}".format(param.name, cast_type(typeof)))
    returned = signature.return_annotation
    if returned:
        returned = cast_type(returned)
        doc = attribut.__doc__
        if not doc:
            raise ValueError(
                "docstring format must be:"
                "return name: type")
        doc = doc.replace("return ", "")
        if ":" in doc:
            returned = doc
        else:
            returned = "{}: {}".format(doc, returned)
    else:
        returned = ""
    return "method {name}({signature}) -> ({returned})".format(
        name=attr,
        signature=",".join(sign),
        returned=returned
    )


class MockedServiceProcess():
    address = None
    vendor = None
    product = None
    version = None
    url = None
    interface = None
    interface_file = None
    interface_name = None
    interface_content = None
    service_to_mock = None

    def run(self):
        mocked_service = varlink.Service(
            vendor=self.vendor,
            product=self.product,
            version=self.version,
            url=self.url)
        instanciated_service = self.service_to_mock()
        mocked_service._set_interface(
            self.interface_file,
            instanciated_service)

        class ServiceRequestHandler(varlink.RequestHandler):
            service = mocked_service

        self.varlink_server = varlink.ThreadingServer(
            self.address, ServiceRequestHandler)
        self.varlink_server.serve_forever()


def service_generator(service, info, filename="mockedservice.py"):
    with open(filename, "w+") as pyfp:
        pyfp.write(textwrap.dedent("""\
        '''
            Generated by varlink mocking system

            {datetime}
            Only for testing purpose and unit testing
        '''
        """.format(datetime=datetime.datetime.now())))
        pyfp.write("import varlink\n\n")
        pyfp.write(inspect.getsource(service))
        pyfp.write("\n\n")
        pyfp.write(inspect.getsource(MockedServiceProcess))
        pyfp.write("\n\n")
        pyfp.write("if __name__ == '__main__':\n")
        pyfp.write("    msp = MockedServiceProcess()\n")
        for key, value in info.items():
            surround = "'"
            if value["type"] == "raw":
                surround = ""
            pyfp.write("    msp.{key} = {surround}{value}{surround}\n".format(
                key=key, value=value["value"], surround=surround))
        pyfp.write("    msp.run()\n")


def 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``.
    """
    def decorator(func):
        def wrapper(*args, **kwargs):
            with MockedService(fake_service, fake_types, name=name,
                               address=address):
                try:
                    func(*args, **kwargs)
                except BrokenPipeError:
                    # manage fake service stoping
                    pass
            return
        return wrapper
    return decorator


class MockedService():

    def __init__(self, service, types, address='unix:@test', name=None,
                 vendor='varlink', product='mock', version=1,
                 url='http://localhost'):
        if not name:
            module = service.__module__
            try:
                self.name = os.path.splitext(module)[1].replace('.', '')
            except IndexError:
                self.name = module
        else:
            self.name = name
        self.identifier = str(uuid.uuid4())
        self.interface_description = None
        self.service = service
        self.types = types
        self.address = address
        self.vendor = vendor
        self.product = product
        self.version = version
        self.url = url
        self.service_info = {
            "address": {'type': 'inherited', 'value': address},
            "vendor": {'type': 'inherited', 'value': vendor},
            "product": {'type': 'inherited', 'value': product},
            "version": {'type': 'raw', 'value': version},
            "url": {'type': 'inherited', 'value': url},
            "interface_name": {'type': 'inherited', 'value': self.name},
            "interface_file": {
                'type': 'inherited',
                'value': self.get_interface_file_path()},
            "service_to_mock": {'type': 'raw', 'value': service.__name__},
        }
        self.generate_interface()

    def generate_interface(self):
        ignore = get_ignored()
        self.interface_description = ["interface {}".format(self.name)]
        if self.types:
            for line in self.types.split("\n"):
                self.interface_description.append(line)
        attributs = get_interface_attributs(self.service, ignore)
        for attr in attributs["callables"]:
            self.interface_description.append(generate_callable_interface(
                self.service, attr))

    def get_interface_file_path(self):
        return "/tmp/{}".format(self.name)

    def generate_interface_file(self):
        tfp = open(self.get_interface_file_path(), "w+")
        tfp.write("\n".join(self.interface_description))
        tfp.close()

    def delete_interface_files(self):
        os.remove(self.get_interface_file_path())
        os.remove(self.mocked_service_file)

    def service_start(self):
        self.service_pid = subprocess.Popen(
            [sys.executable, self.mocked_service_file],
            env = { "PYTHONPATH": ':'.join(sys.path) }
        )
        time.sleep(2)

    def service_stop(self):
        self.service_pid.kill()
        self.service_pid.communicate()

    def __enter__(self):
        self.mocked_service_file = "/tmp/{}".format(self.identifier)
        service_generator(
            self.service, self.service_info,
            filename=self.mocked_service_file)
        self.generate_interface_file()
        self.service_start()
        return self

    def __exit__(self, type, value, traceback):
        self.service_stop()
        self.delete_interface_files()