@@ -107,69 +107,216 @@ def on_sync(self):
107
107
def _process_side_effect (target = None , args = None ):
108
108
return MockThread (target = target , args = args )
109
109
110
- class BlackBoxHostTestTestCase ( fake_filesystem_unittest . TestCase ):
110
+ class MockTestEnvironment ( object ):
111
111
112
- def setUp (self ):
113
- self .setUpPyfakefs ()
112
+ def __init__ (self , test_case , platform_info , image_path ):
113
+ self ._test_case = test_case
114
+ self ._platform_info = platform_info
115
+ self ._image_path = image_path
116
+ self ._patch_definitions = []
117
+ self .patches = {}
114
118
115
- def tearDown (self ):
116
- pass
117
-
118
- def test_host_test_has_setup_teardown_attribute (self ):
119
- platform_info = {
120
- "platform_name" : "K64F" ,
121
- "target_id" : "0240000031754e45000c0018948500156461000097969900" ,
122
- "mount_point" : os .path .normpath ("/mnt/DAPLINK" ),
123
- "serial_port" : os .path .normpath ("/dev/ttyACM0" ),
124
- }
125
- image_path = os .path .normpath (
126
- "BUILD/tests/K64F/GCC_ARM/TESTS/network/interface/interface.bin"
127
- )
128
- self .fs .create_file (image_path )
129
- self .fs .create_dir (platform_info ['mount_point' ])
130
119
args = (
131
120
'mbedhtrun -m {} -p {}:9600 -f '
132
121
'"{}" -e "TESTS/host_tests" -d {} -c default '
133
122
'-t {} -r default '
134
123
'-C 4 --sync 5 -P 60'
135
124
).format (
136
- platform_info ['platform_name' ],
137
- platform_info ['serial_port' ],
138
- image_path ,
139
- platform_info ['mount_point' ],
140
- platform_info ['target_id' ]
125
+ self . _platform_info ['platform_name' ],
126
+ self . _platform_info ['serial_port' ],
127
+ self . _image_path ,
128
+ self . _platform_info ['mount_point' ],
129
+ self . _platform_info ['target_id' ]
141
130
).split ()
131
+ self .patch ('sys.argv' , new = args )
132
+
133
+ # Mock detect
134
+ detect_mock = MagicMock ()
135
+ detect_mock .return_value .list_mbeds .return_value = [
136
+ self ._platform_info
137
+ ]
138
+ self .patch ('mbed_os_tools.detect.create' , new = detect_mock )
139
+
140
+ # Mock process
141
+ self .patch (
142
+ 'mbed_os_tools.test.host_tests_runner.host_test_default.Process' ,
143
+ new = MagicMock (side_effect = _process_side_effect )
144
+ )
145
+ self .patch (
146
+ 'mbed_os_tools.test.host_tests_plugins.host_test_plugins.call' ,
147
+ new = MagicMock (return_value = 0 )
148
+ )
149
+
150
+ # Mock serial
151
+ mock_serial = MockSerial ()
152
+ mock_device = MockMbedDevice (mock_serial )
153
+ self .patch (
154
+ 'mbed_os_tools.test.host_tests_conn_proxy.conn_primitive_serial.Serial' ,
155
+ new = MagicMock (return_value = mock_serial )
156
+ )
157
+
158
+
159
+ def patch (self , path , ** kwargs ):
160
+ self ._patch_definitions .append ((path , patch (path , ** kwargs )))
161
+
162
+ def __enter__ (self ):
163
+ self ._test_case .fs .create_file (self ._image_path )
164
+ self ._test_case .fs .create_dir (self ._platform_info ['mount_point' ])
165
+
166
+ for path , patcher in self ._patch_definitions :
167
+ self .patches [path ] = patcher .start ()
168
+
169
+ def __exit__ (self , type , value , traceback ):
170
+ for _ , patcher in self ._patch_definitions :
171
+ patcher .stop ()
172
+
173
+ class MockTestEnvironmentPosix (MockTestEnvironment ):
174
+
175
+ def __init__ (self , test_case , platform_info , image_path ):
176
+ super ().__init__ (test_case , platform_info , image_path )
177
+
178
+ self .patch ('os.name' , new = 'posix' )
179
+
180
+ def __exit__ (self , type , value , traceback ):
181
+ super ().__exit__ (type , value , traceback )
182
+
183
+ # Assert for proper image copy
184
+ mocked_call = self .patches [
185
+ 'mbed_os_tools.test.host_tests_plugins.host_test_plugins.call'
186
+ ]
187
+
188
+ first_call_args = mocked_call .call_args_list [0 ][0 ][0 ]
189
+ self ._test_case .assertEqual (first_call_args [0 ], "cp" )
190
+ self ._test_case .assertEqual (first_call_args [1 ], self ._image_path )
191
+ self ._test_case .assertTrue (first_call_args [2 ].startswith (self ._platform_info ["mount_point" ]))
192
+ self ._test_case .assertTrue (first_call_args [2 ].endswith (os .path .splitext (self ._image_path )[1 ]))
193
+
194
+
195
+ class MockTestEnvironmentLinux (MockTestEnvironmentPosix ):
196
+
197
+ def __init__ (self , test_case , platform_info , image_path ):
198
+ super ().__init__ (test_case , platform_info , image_path )
199
+
200
+ self .patch (
201
+ 'os.uname' ,
202
+ new = MagicMock (return_value = ('Linux' ,)),
203
+ create = True
204
+ )
205
+
206
+ def __exit__ (self , type , value , traceback ):
207
+ super ().__exit__ (type , value , traceback )
208
+
209
+ # Assert for proper image copy
210
+ mocked_call = self .patches [
211
+ 'mbed_os_tools.test.host_tests_plugins.host_test_plugins.call'
212
+ ]
213
+
214
+ second_call_args = mocked_call .call_args_list [1 ][0 ][0 ]
215
+ destination_path = os .path .normpath (
216
+ os .path .join (
217
+ self ._platform_info ["mount_point" ],
218
+ os .path .basename (self ._image_path )
219
+ )
220
+ )
221
+
222
+ self ._test_case .assertEqual (
223
+ second_call_args ,
224
+ ["sync" , "-f" , destination_path ]
225
+ )
226
+
227
+ # Ensure only two subprocesses were started
228
+ self ._test_case .assertEqual (len (mocked_call .call_args_list ), 2 )
229
+
230
+ class MockTestEnvironmentDarwin (MockTestEnvironmentPosix ):
231
+
232
+ def __init__ (self , test_case , platform_info , image_path ):
233
+ super ().__init__ (test_case , platform_info , image_path )
234
+
235
+ self .patch (
236
+ 'os.uname' ,
237
+ new = MagicMock (return_value = ('Darwin' ,)),
238
+ create = True
239
+ )
240
+
241
+ def __exit__ (self , type , value , traceback ):
242
+ super ().__exit__ (type , value , traceback )
243
+
244
+ # Assert for proper image copy
245
+ mocked_call = self .patches [
246
+ 'mbed_os_tools.test.host_tests_plugins.host_test_plugins.call'
247
+ ]
248
+
249
+ second_call_args = mocked_call .call_args_list [1 ][0 ][0 ]
250
+ self ._test_case .assertEqual (second_call_args , ["sync" ])
251
+
252
+ # Ensure only two subprocesses were started
253
+ self ._test_case .assertEqual (len (mocked_call .call_args_list ), 2 )
254
+
255
+ class MockTestEnvironmentWindows (MockTestEnvironment ):
256
+
257
+ def __init__ (self , test_case , platform_info , image_path ):
258
+ super ().__init__ (test_case , platform_info , image_path )
259
+
260
+ self .patch ('os.name' , new = 'nt' )
261
+
262
+ def __exit__ (self , type , value , traceback ):
263
+ super ().__exit__ (type , value , traceback )
264
+
265
+ # Assert for proper image copy
266
+ mocked_call = self .patches [
267
+ 'mbed_os_tools.test.host_tests_plugins.host_test_plugins.call'
268
+ ]
269
+
270
+ first_call_args = mocked_call .call_args_list [0 ][0 ][0 ]
271
+ self ._test_case .assertEqual (first_call_args [0 ], "copy" )
272
+ self ._test_case .assertEqual (first_call_args [1 ], self ._image_path )
273
+ self ._test_case .assertTrue (first_call_args [2 ].startswith (self ._platform_info ["mount_point" ]))
274
+ self ._test_case .assertTrue (first_call_args [2 ].endswith (os .path .splitext (self ._image_path )[1 ]))
275
+
276
+ # Ensure only one subprocess was started
277
+ self ._test_case .assertEqual (len (mocked_call .call_args_list ), 1 )
142
278
143
- with patch ('sys.argv' , new = args ) as _argv ,\
144
- patch ('os.name' , new = 'posix' ),\
145
- patch ('os.uname' , return_value = ('Linux' ,), create = True ),\
146
- patch ('mbed_os_tools.detect.create' ) as _detect ,\
147
- patch ('mbed_os_tools.test.host_tests_plugins.host_test_plugins.call' ) as _call ,\
148
- patch ('mbed_os_tools.test.host_tests_runner.host_test_default.Process' ) as _process ,\
149
- patch ('mbed_os_tools.test.host_tests_conn_proxy.conn_primitive_serial.Serial' ) as _serial :
150
- _process .side_effect = _process_side_effect
151
- _detect .return_value .list_mbeds .return_value = [
152
- platform_info
153
- ]
154
- _call .return_value = 0
155
-
156
- mock_serial = MockSerial ()
157
- mock_device = MockMbedDevice (mock_serial )
158
- _serial .return_value = mock_serial
279
+ mock_platform_info = {
280
+ "platform_name" : "K64F" ,
281
+ "target_id" : "0240000031754e45000c0018948500156461000097969900" ,
282
+ "mount_point" : os .path .normpath ("/mnt/DAPLINK" ),
283
+ "serial_port" : os .path .normpath ("/dev/ttyACM0" ),
284
+ }
285
+ mock_image_path = os .path .normpath (
286
+ "BUILD/tests/K64F/GCC_ARM/TESTS/network/interface/interface.bin"
287
+ )
159
288
289
+ class BlackBoxHostTestTestCase (fake_filesystem_unittest .TestCase ):
290
+
291
+ def setUp (self ):
292
+ self .setUpPyfakefs ()
293
+
294
+ def tearDown (self ):
295
+ pass
296
+
297
+ def test_host_test_linux (self ):
298
+ with MockTestEnvironmentLinux (self , mock_platform_info , mock_image_path ) as _env :
160
299
test_selector = DefaultTestSelector (init_host_test_cli_params ())
161
300
result = test_selector .execute ()
162
301
test_selector .finish ()
163
302
164
- matched_calls = [
165
- ca for ca in _call .call_args_list if ca [0 ][0 ][0 ] == "cp"
166
- ]
167
- cmd_args = matched_calls [0 ][0 ][0 ]
168
- self .assertEqual (cmd_args [1 ], image_path )
169
- self .assertTrue (cmd_args [2 ].startswith (platform_info ["mount_point" ]))
170
- self .assertTrue (cmd_args [2 ].endswith (os .path .splitext (image_path )[1 ]))
303
+ self .assertEqual (result , 0 )
304
+
305
+ def test_host_test_darwin (self ):
306
+ with MockTestEnvironmentDarwin (self , mock_platform_info , mock_image_path ) as _env :
307
+ test_selector = DefaultTestSelector (init_host_test_cli_params ())
308
+ result = test_selector .execute ()
309
+ test_selector .finish ()
310
+
311
+ self .assertEqual (result , 0 )
312
+
313
+ def test_host_test_windows (self ):
314
+ with MockTestEnvironmentWindows (self , mock_platform_info , mock_image_path ) as _env :
315
+ test_selector = DefaultTestSelector (init_host_test_cli_params ())
316
+ result = test_selector .execute ()
317
+ test_selector .finish ()
171
318
172
- self .assertEqual (result , 0 )
319
+ self .assertEqual (result , 0 )
173
320
174
321
if __name__ == '__main__' :
175
322
unittest .main ()
0 commit comments