Debian Codegraph @

tests/test_environment.py

raw symbols SCIP
1 import pytest
2 import warnings
3
4 from labgrid import Environment
5 from labgrid.exceptions import NoConfigFoundError, InvalidConfigError
6 from labgrid.protocol import ConsoleProtocol
7 from labgrid.resource import RawSerialPort
8
9
10 class TestEnvironment:
11 def test_noconfig_instance(self):
12 with pytest.raises(NoConfigFoundError):
13 e = Environment()
14
15 def test_instance(self, tmpdir):
16 p = tmpdir.join("config.yaml")
17 p.write(
18 """
19 targets:
20 test1:
21 drivers: {}
22 test2:
23 role: foo
24 resources: {}
25 """
26 )
27 e = Environment(str(p))
28 assert (isinstance(e, Environment))
29
30 def test_get_target(self, tmpdir):
31 p = tmpdir.join("config.yaml")
32 p.write(
33 """
34 targets:
35 test1:
36 drivers: {}
37 test2:
38 role: foo
39 resources: {}
40 """
41 )
42 e = Environment(str(p))
43 assert (e.get_target("test1"))
44 assert (e.get_target("test2"))
45
46 def test_instance_invalid_yaml(self, tmpdir):
47 p = tmpdir.join("config.yaml")
48 p.write(
49 """
50 I a(m) no yaml:
51 - keks
52 cookie
53 """
54 )
55 with pytest.raises(InvalidConfigError):
56 e = Environment(str(p))
57
58 def test_env_imports_yaml(self, tmpdir):
59 import sys
60 i = tmpdir.join("myimport.py")
61 i.write(
62 """
63 class Test:
64 pass
65 """
66 )
67 p = tmpdir.join("config.yaml")
68 p.write(
69 f"""
70 targets:
71 main:
72 drivers: {{}}
73 imports:
74 - {str(i)}
75 """
76 )
77 e = Environment(str(p))
78 assert (isinstance(e, Environment))
79 assert "myimport" in sys.modules
80 import myimport
81 t = myimport.Test()
82 assert (isinstance(t, myimport.Test))
83
84 def test_create_named_resources(self, tmpdir):
85 p = tmpdir.join("config.yaml")
86 p.write(
87 """
88 targets:
89 test1:
90 resources:
91 - AndroidUSBFastboot:
92 name: "fastboot"
93 match: {}
94 - RawSerialPort:
95 port: "/dev/ttyUSB0"
96 speed: 115200
97 """
98 )
99 e = Environment(str(p))
100 t = e.get_target("test1")
101
102 def test_create_named_drivers(self, tmpdir):
103 p = tmpdir.join("config.yaml")
104 p.write(
105 """
106 targets:
107 test1:
108 resources:
109 - AndroidUSBFastboot:
110 name: "fastboot"
111 match: {}
112 - RawSerialPort:
113 name: "serial_a"
114 port: "/dev/ttyUSB0"
115 speed: 115200
116 - cls: RawSerialPort
117 name: "serial_b"
118 port: "/dev/ttyUSB0"
119 speed: 115200
120 drivers:
121 - FakeConsoleDriver:
122 name: "serial_a"
123 - FakeConsoleDriver:
124 name: "serial_b"
125 """
126 )
127 e = Environment(str(p))
128 t = e.get_target("test1")
129
130 def test_create_multi_drivers(self, tmpdir):
131 p = tmpdir.join("config.yaml")
132 p.write(
133 """
134 targets:
135 test1:
136 resources:
137 - RawSerialPort:
138 name: "serial_a"
139 port: "/dev/ttyUSB0"
140 speed: 115200
141 - RawSerialPort:
142 name: "serial_b"
143 port: "/dev/ttyUSB0"
144 speed: 115200
145 drivers:
146 - SerialDriver:
147 name: "serial_a"
148 bindings:
149 port: "serial_a"
150 - SerialDriver:
151 name: "serial_b"
152 bindings:
153 port: "serial_b"
154 """
155 )
156 e = Environment(str(p))
157 t = e.get_target("test1")
158 r_a = t.get_resource(RawSerialPort, name="serial_a")
159 r_b = t.get_resource(RawSerialPort, name="serial_b")
160 assert r_a is not r_b
161 d_a = t.get_driver(ConsoleProtocol, name="serial_a", activate=False)
162 d_b = t.get_driver(ConsoleProtocol, name="serial_b", activate=False)
163 assert d_a is not d_b
164
165 assert d_a.port is r_a
166 assert d_b.port is r_b
167
168 def test_usbserialport_warning(self, tmpdir):
169 p = tmpdir.join("config.yaml")
170 p.write(
171 """
172 targets:
173 test1:
174 resources:
175 - USBSerialPort:
176 port: /dev/ttyS0
177 drivers:
178 - SerialDriver: {}
179 """
180 )
181 e = Environment(str(p))
182 with pytest.warns(UserWarning):
183 t = e.get_target("test1")
184
185 def test_usbserialport_no_warning(self, tmpdir):
186 p = tmpdir.join("config.yaml")
187 p.write(
188 """
189 targets:
190 test1:
191 resources:
192 - USBSerialPort: {}
193 drivers:
194 - SerialDriver: {}
195 """
196 )
197 e = Environment(str(p))
198 with warnings.catch_warnings():
199 warnings.simplefilter("error")
200 t = e.get_target("test1")