|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +mbed SDK |
| 4 | +Copyright (c) 2011-2015 ARM Limited |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +""" |
| 18 | + |
| 19 | +import unittest |
| 20 | +import os |
| 21 | +import copy |
| 22 | +from mock import patch, mock_open, DEFAULT |
| 23 | + |
| 24 | +from mbed_lstools.lstools_base import MbedLsToolsBase |
| 25 | + |
| 26 | +TEST_DATA_PATH = 'test_data' |
| 27 | + |
| 28 | +class DummyLsTools(MbedLsToolsBase): |
| 29 | + return_value = [] |
| 30 | + def find_candidates(self): |
| 31 | + return self.return_value |
| 32 | + |
| 33 | +try: |
| 34 | + basestring |
| 35 | +except NameError: |
| 36 | + # Python 3 |
| 37 | + basestring = str |
| 38 | + |
| 39 | + |
| 40 | +def get_case_insensitive_path(path, file_name): |
| 41 | + for entry in os.listdir(path): |
| 42 | + if entry.lower() == file_name.lower(): |
| 43 | + return os.path.join(path, entry) |
| 44 | + |
| 45 | + raise Exception('No matching file for %s found in $s' % (file_name, path)) |
| 46 | + |
| 47 | + |
| 48 | +class PlatformDetectionTestCase(unittest.TestCase): |
| 49 | + """ Basic test cases checking trivial asserts |
| 50 | + """ |
| 51 | + |
| 52 | + def setUp(self): |
| 53 | + self.base = DummyLsTools() |
| 54 | + |
| 55 | + def tearDown(self): |
| 56 | + pass |
| 57 | + |
| 58 | + def run_test(self, test_data_case, candidate_data, expected_data): |
| 59 | + # Add necessary candidate data |
| 60 | + candidate_data['mount_point'] = 'dummy_mount_point' |
| 61 | + |
| 62 | + # Find the test data in the test_data folder |
| 63 | + test_script_path = os.path.dirname(os.path.abspath(__file__)) |
| 64 | + test_data_path = os.path.join(test_script_path, TEST_DATA_PATH) |
| 65 | + test_data_cases = os.listdir(test_data_path) |
| 66 | + self.assertTrue(test_data_case in test_data_cases, 'Expected %s to be present in %s folder' % (test_data_case, test_data_path)) |
| 67 | + test_data_case_path = os.path.join(test_data_path, test_data_case) |
| 68 | + |
| 69 | + # NOTE a limitation of this mocked test is that it only allows mocking of one directory level. |
| 70 | + # This is enough at the moment because all firmwares seem to use a flat file structure. |
| 71 | + # If this changes in the future, this mocking framework can be extended to support this. |
| 72 | + |
| 73 | + test_data_case_file_names = os.listdir(test_data_case_path) |
| 74 | + mocked_open_file_paths = [os.path.join(candidate_data['mount_point'], file_name ) for file_name in test_data_case_file_names] |
| 75 | + |
| 76 | + # Setup all the mocks |
| 77 | + self.base.return_value = [candidate_data] |
| 78 | + |
| 79 | + def do_open(path, mode='r'): |
| 80 | + file_name = os.path.basename(path) |
| 81 | + try: |
| 82 | + with open(get_case_insensitive_path(test_data_case_path, file_name), 'r') as test_data_file: |
| 83 | + test_data_file_data = test_data_file.read() |
| 84 | + except OSError: |
| 85 | + raise OSError("(mocked open) No such file or directory: '%s'" % (path)) |
| 86 | + |
| 87 | + file_object = mock_open(read_data=test_data_file_data).return_value |
| 88 | + file_object.__iter__.return_value = test_data_file_data.splitlines(True) |
| 89 | + return file_object |
| 90 | + |
| 91 | + with patch("mbed_lstools.lstools_base.MbedLsToolsBase.mount_point_ready") as _mpr,\ |
| 92 | + patch('mbed_lstools.lstools_base.open', do_open) as _,\ |
| 93 | + patch('os.listdir') as _listdir: |
| 94 | + _mpr.return_value = True |
| 95 | + _listdir.return_value = test_data_case_file_names |
| 96 | + results = self.base.list_mbeds(read_details_txt=True) |
| 97 | + |
| 98 | + # There should only ever be one result |
| 99 | + self.assertEqual(len(results), 1) |
| 100 | + actual = results[0] |
| 101 | + |
| 102 | + expected_keys = list(expected_data.keys()) |
| 103 | + actual_keys = list(actual.keys()) |
| 104 | + differing_map = {} |
| 105 | + for key in expected_keys: |
| 106 | + actual_value = actual.get(key) |
| 107 | + if actual_value != expected_data[key]: |
| 108 | + differing_map[key] = (actual_value, expected_data[key]) |
| 109 | + |
| 110 | + |
| 111 | + if differing_map: |
| 112 | + differing_string = '' |
| 113 | + for differing_key in sorted(list(differing_map.keys())): |
| 114 | + actual, expected = differing_map[differing_key] |
| 115 | + differing_string += ' "%s": "%s" (expected "%s")\n' % (differing_key, actual, expected) |
| 116 | + |
| 117 | + assert_string = 'Expected data mismatch:\n\n{\n%s}' % (differing_string) |
| 118 | + self.assertTrue(False, assert_string) |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + def test_efm32pg_stk3401_jlink(self): |
| 123 | + self.run_test('efm32pg_stk3401_jlink', { |
| 124 | + 'target_id_usb_id': u'000440074453', |
| 125 | + 'vendor_id': '1366', |
| 126 | + 'product_id': '1015' |
| 127 | + }, { |
| 128 | + 'platform_name': 'EFM32PG_STK3401', |
| 129 | + 'device_type': 'jlink', |
| 130 | + 'target_id': '2035022D000122D5D475113A', |
| 131 | + 'target_id_usb_id': '000440074453', |
| 132 | + 'target_id_mbed_htm': '2035022D000122D5D475113A' |
| 133 | + }) |
| 134 | + |
| 135 | + def test_lpc1768(self): |
| 136 | + self.run_test('lpc1768', { |
| 137 | + 'target_id_usb_id': u'101000000000000000000002F7F20DF3', |
| 138 | + 'vendor_id': '0d28', |
| 139 | + 'product_id': '0204' |
| 140 | + }, { |
| 141 | + 'platform_name': 'LPC1768', |
| 142 | + 'device_type': 'daplink', |
| 143 | + 'target_id': '101000000000000000000002F7F20DF3d51e6be5ac41795761dc44148e3b7000', |
| 144 | + 'target_id_usb_id': '101000000000000000000002F7F20DF3', |
| 145 | + 'target_id_mbed_htm': '101000000000000000000002F7F20DF3d51e6be5ac41795761dc44148e3b7000' |
| 146 | + }) |
| 147 | + |
| 148 | + def test_nucleo_f411re_stlink(self): |
| 149 | + self.run_test('nucleo_f411re_stlink', { |
| 150 | + 'target_id_usb_id': u'0671FF554856805087112815', |
| 151 | + 'vendor_id': '0483', |
| 152 | + 'product_id': '374b' |
| 153 | + }, { |
| 154 | + 'platform_name': 'NUCLEO_F411RE', |
| 155 | + 'device_type': 'stlink', |
| 156 | + 'target_id': '07400221076061193824F764', |
| 157 | + 'target_id_usb_id': '0671FF554856805087112815', |
| 158 | + 'target_id_mbed_htm': '07400221076061193824F764' |
| 159 | + }) |
| 160 | + |
| 161 | + def test_nrf51_microbit(self): |
| 162 | + self.run_test('nrf51_microbit', { |
| 163 | + 'target_id_usb_id': u'9900007031324e45000f9019000000340000000097969901', |
| 164 | + 'vendor_id': '0d28', |
| 165 | + 'product_id': '0204' |
| 166 | + }, { |
| 167 | + 'platform_name': 'NRF51_MICROBIT', |
| 168 | + 'device_type': 'daplink', |
| 169 | + 'target_id': '9900007031324e45000f9019000000340000000097969901', |
| 170 | + 'target_id_usb_id': '9900007031324e45000f9019000000340000000097969901' |
| 171 | + }) |
| 172 | + |
| 173 | + def test_k64f_daplink(self): |
| 174 | + self.run_test('k64f_daplink', { |
| 175 | + 'target_id_usb_id': u'0240000032044e45000a700a997b00356781000097969900', |
| 176 | + 'vendor_id': '0d28', |
| 177 | + 'product_id': '0204' |
| 178 | + }, { |
| 179 | + 'platform_name': 'K64F', |
| 180 | + 'device_type': 'daplink', |
| 181 | + 'target_id': '0240000032044e45000a700a997b00356781000097969900', |
| 182 | + 'target_id_usb_id': '0240000032044e45000a700a997b00356781000097969900', |
| 183 | + 'target_id_mbed_htm': '0240000032044e45000a700a997b00356781000097969900' |
| 184 | + }) |
| 185 | + |
| 186 | + def test_nrf52_dk_daplink(self): |
| 187 | + self.run_test('nrf52_dk_daplink', { |
| 188 | + 'target_id_usb_id': u'110100004420312043574641323032203233303397969903', |
| 189 | + 'vendor_id': '0d28', |
| 190 | + 'product_id': '0204' |
| 191 | + }, { |
| 192 | + 'platform_name': 'NRF52_DK', |
| 193 | + 'device_type': 'daplink', |
| 194 | + 'target_id': '110100004420312043574641323032203233303397969903', |
| 195 | + 'target_id_usb_id': '110100004420312043574641323032203233303397969903', |
| 196 | + 'target_id_mbed_htm': '110100004420312043574641323032203233303397969903' |
| 197 | + }) |
| 198 | + |
| 199 | + def test_nrf52_dk_jlink(self): |
| 200 | + self.run_test('nrf52_dk_jlink', { |
| 201 | + 'target_id_usb_id': u'000682546728', |
| 202 | + 'vendor_id': '1366', |
| 203 | + 'product_id': '1015' |
| 204 | + }, { |
| 205 | + 'platform_name': 'NRF52_DK', |
| 206 | + 'device_type': 'jlink', |
| 207 | + 'target_id': '000682546728', |
| 208 | + 'target_id_usb_id': '000682546728' |
| 209 | + }) |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +if __name__ == '__main__': |
| 214 | + unittest.main() |
0 commit comments