Debian Codegraph @

tests/test_agent.py

raw symbols SCIP
1 import os
2 import socket
3 import subprocess
4
5 import pytest
6 from py.path import local
7
8 import labgrid.util.agentwrapper
9 from labgrid.util.agentwrapper import AgentError, AgentException, AgentWrapper
10 from labgrid.util.agent import b2s, s2b, py2s, s2py
11
12 @pytest.fixture(scope='function')
13 def subprocess_mock(mocker):
14 import subprocess
15
16 original = subprocess.Popen
17
18 agent = local(labgrid.util.agentwrapper.__file__).dirpath('agent.py')
19
20 def run(args, **kwargs):
21 assert args[0] in ['rsync', 'ssh']
22 if args[0] == 'rsync':
23 src = local(args[-2])
24 assert src == agent
25 dst = args[-1]
26 assert ':' in dst
27 dst = dst.split(':', 1)[1]
28 assert '/' not in dst
29 assert dst.startswith('.labgrid_agent')
30 return original(['true'], **kwargs)
31 elif args[0] == 'ssh':
32 assert '--' in args
33 args = args[args.index('--')+1:]
34 assert len(args) == 2
35 assert args[0] == 'python3'
36 assert args[1].startswith('.labgrid_agent')
37 # we need to use the original here to get the coverage right
38 return original(['python3', str(agent)], **kwargs)
39
40 mocker.patch('subprocess.Popen', run)
41
42 def test_create(subprocess_mock):
43 aw = AgentWrapper('localhost')
44 aw.close()
45
46 def test_call(subprocess_mock):
47 aw = AgentWrapper('localhost')
48 assert aw.call('test') == []
49 assert aw.call('test', 0) == [0]
50 assert aw.call('test', 0, 1) == [1, 0]
51 assert aw.call('test', 'foo') == ['foo']
52 assert aw.call('test', '{') == ['{']
53
54 def test_proxy(subprocess_mock):
55 aw = AgentWrapper('localhost')
56 assert aw.test() == []
57 assert aw.test( 0, 1) == [1, 0]
58
59 def test_bytes(subprocess_mock):
60 aw = AgentWrapper('localhost')
61 assert s2b(aw.test(b2s(b'\x00foo'))[0]) == b'\x00foo'
62
63 def test_pydata(subprocess_mock):
64 with AgentWrapper('localhost') as aw:
65 d = s2py(aw.test(py2s((1, 'foo', True)))[0])
66 assert isinstance(d, tuple)
67 assert d == (1, 'foo', True)
68
69 def test_exception(subprocess_mock):
70 aw = AgentWrapper('localhost')
71
72 with pytest.raises(AgentException) as excinfo:
73 aw.error('foo')
74 assert excinfo.value.args == ("ValueError('foo')",)
75
76 with pytest.raises(AgentException) as excinfo:
77 aw.error('bar')
78 assert excinfo.value.args == ("ValueError('bar')",)
79
80 def test_error(subprocess_mock):
81 aw = AgentWrapper('localhost')
82 aw.agent.stdin.write(b'\x00')
83 with pytest.raises(AgentError):
84 aw.test()
85
86 def test_module(subprocess_mock):
87 aw = AgentWrapper('localhost')
88 dummy = aw.load('dummy')
89 assert dummy.neg(1) == -1
90
91 def test_local():
92 aw = AgentWrapper(None)
93
94 assert aw.test() == []
95 assert aw.test( 0, 1) == [1, 0]
96
97 assert s2b(aw.test(b2s(b'\x00foo'))[0]) == b'\x00foo'
98
99 with pytest.raises(AgentException) as excinfo:
100 aw.error('foo')
101 assert excinfo.value.args == ("ValueError('foo')",)
102
103 with pytest.raises(AgentException) as excinfo:
104 aw.error('bar')
105 assert excinfo.value.args == ("ValueError('bar')",)
106
107 dummy = aw.load('dummy')
108 assert dummy.neg(1) == -1
109
110
111 def test_local_fdpass():
112 aw = AgentWrapper(None)
113
114 result = aw.test_fd()
115 assert isinstance(result, tuple)
116 assert result[0] == "dummy"
117 assert isinstance(result[1], int)
118
119 with os.fdopen(result[1]) as f:
120 fdpath = os.readlink(f"/proc/self/fd/{f.fileno()}")
121 assert fdpath.startswith("/memfd:test_fd")
122
123
124 @pytest.mark.parametrize('module_name', [
125 'deditec_relais8',
126 'sysfsgpio',
127 'usb_hid_relay'
128 ])
129 def test_all_modules(module_name: str) -> None:
130 aw = AgentWrapper(None)
131
132 aw.load(module_name)
133 methods = aw.list()
134 assert f'{module_name}.set' in methods
135 assert f'{module_name}.get' in methods
136
137 def test_import_modules():
138 import labgrid.util.agents
139 import labgrid.util.agents.dummy
140 from labgrid.util.agents import deditec_relais8, sysfsgpio