Skip to content

Commit 0e6259f

Browse files
committed
Adding black box test
1 parent d3e9ebc commit 0e6259f

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

test/test/host_test_black_box.py

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#!/usr/bin/env python
2+
# Copyright (c) 2018, Arm Limited and affiliates.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import unittest
18+
import os
19+
import re
20+
21+
import threading
22+
from builtins import super
23+
from mbed_os_tools.test import init_host_test_cli_params
24+
from mbed_os_tools.test.host_tests_runner.host_test_default import DefaultTestSelector
25+
from mock import patch, MagicMock
26+
from pyfakefs import fake_filesystem_unittest
27+
28+
29+
class MockThread(threading.Thread):
30+
def __init__(self, target=None, args=None):
31+
super().__init__(target=target, args=args)
32+
print('Mock Thread constr')
33+
self._terminates = 0
34+
self.exitcode = 0 # TODO maybe this needs to be setable? Mock sys.exit
35+
36+
def terminate(self):
37+
self._terminates += 1
38+
39+
class MockSerial(object):
40+
def __init__(self, *args, **kwargs):
41+
super().__init__(*args, **kwargs)
42+
self._args = args
43+
self._kwargs = kwargs
44+
self._open = True
45+
self._rx_counter = 0
46+
self._tx_buffer = ""
47+
self._rx_buffer = ""
48+
self._upstream_write_cb = None
49+
50+
def read(self, count):
51+
contents = self._rx_buffer[self._rx_counter:count]
52+
self._rx_counter += len(contents)
53+
return contents
54+
55+
def write(self, data):
56+
self._tx_buffer += data
57+
if self._upstream_write_cb:
58+
# TODO this may not work...
59+
self._upstream_write_cb(data)
60+
61+
def close(self):
62+
self._open = False
63+
64+
def downstream_write(self, data):
65+
self._rx_buffer += data
66+
67+
def on_upstream_write(self, func):
68+
self._upstream_write_cb = func
69+
70+
kv_regex = re.compile(r"\{\{([\w\d_-]+);([^\}]+)\}\}")
71+
72+
class MockMbedDevice(object):
73+
def __init__(self, serial):
74+
self._synced = False
75+
self._kvs = []
76+
self._serial = serial
77+
self._serial.on_upstream_write(self.on_write)
78+
79+
def handle_kv(self, key, value):
80+
if not self._synced:
81+
if key == "__sync":
82+
self._synced = True
83+
self.send_kv(key, value)
84+
self.on_sync()
85+
else:
86+
pass
87+
88+
def send_kv(self, key, value):
89+
self._serial.downstream_write("{{{{{};{}}}}}\r\n".format(key, value))
90+
91+
def on_write(self, data):
92+
kvs = kv_regex.findall(data)
93+
94+
for key, value in kvs:
95+
self.handle_kv(key, value)
96+
self._kvs.append((key, value))
97+
98+
def on_sync(self):
99+
self._serial.downstream_write(
100+
"{{__timeout;15}}\r\n"
101+
"{{__host_test_name;default_auto}}\r\n"
102+
"{{end;success}}\n"
103+
"{{__exit;0}}\r\n"
104+
)
105+
106+
107+
def _process_side_effect(target=None, args=None):
108+
return MockThread(target=target, args=args)
109+
110+
class BlackBoxHostTestTestCase(fake_filesystem_unittest.TestCase):
111+
112+
def setUp(self):
113+
self.setUpPyfakefs()
114+
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+
args = (
131+
'mbedhtrun -m {} -p {}:9600 -f '
132+
'"{}" -e "TESTS/host_tests" -d {} -c default '
133+
'-t {} -r default '
134+
'-C 4 --sync 5 -P 60'
135+
).format(
136+
platform_info['platform_name'],
137+
platform_info['serial_port'],
138+
image_path,
139+
platform_info['mount_point'],
140+
platform_info['target_id']
141+
).split()
142+
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
159+
160+
test_selector = DefaultTestSelector(init_host_test_cli_params())
161+
result = test_selector.execute()
162+
test_selector.finish()
163+
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]))
171+
172+
self.assertEqual(result, 0)
173+
174+
if __name__ == '__main__':
175+
unittest.main()

0 commit comments

Comments
 (0)