File: test_callbacks.py

package info (click to toggle)
pyroute2 0.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,700 kB
  • sloc: python: 50,245; makefile: 280; javascript: 183; ansic: 81; sh: 44; awk: 17
file content (45 lines) | stat: -rw-r--r-- 1,366 bytes parent folder | download | duplicates (3)
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
from pr2test.marks import require_root

pytestmark = [require_root()]


def callback(msg, cb_context):
    cb_context['counter'] += 1


def test_callbacks_positive(context):
    ifname = context.new_ifname
    cb_context = {'counter': 0}
    interface = context.ndb.interfaces.create(
        ifname=ifname, kind='dummy'
    ).commit()

    context.ipr.register_callback(
        callback,
        lambda x: x.get('index', None) == interface['index'],
        (cb_context,),
    )
    context.ipr.link('set', index=interface['index'], state='up')
    context.ipr.link('get', index=interface['index'])
    counter = cb_context['counter']
    assert counter > 0
    context.ipr.unregister_callback(callback)
    context.ipr.link('set', index=interface['index'], state='down')
    context.ipr.link('get', index=interface['index'])
    assert cb_context['counter'] == counter


def test_callbacks_negative(context):
    ifname = context.new_ifname
    cb_context = {'counter': 0}
    interface = context.ndb.interfaces.create(
        ifname=ifname, kind='dummy'
    ).commit()

    context.ipr.register_callback(
        callback, lambda x: x.get('index', None) == -1, (cb_context,)
    )
    context.ipr.link('set', index=interface['index'], state='up')
    context.ipr.link('get', index=interface['index'])
    counter = cb_context['counter']
    assert counter == 0