Esempio n. 1
0
    def test_run_fail_file(self, mock_stdout):
        mbeds = mbed_lstools.create()
        targets = mbeds.list_mbeds()
        mount_point = None
        target_to_test = None
        fail_txt_path = os.path.join('test', 'failing.txt')
        for target in targets:
            if target['platform_name'] == 'K64F':
                if 'target_id' in target and 'mount_point' in target:
                    target_to_test = target
                    mount_point = target['mount_point']
                    break

        with open(fail_txt_path, 'w') as new_file:
            new_file.write("0000000000000000000000000000000000")

        with self.assertRaises(FlashError) as cm:
            flasher = FlasherMbed()
            flasher.flash(source=fail_txt_path,
                          target=target_to_test,
                          method='simple',
                          no_reset=False)

        if platform.system() == 'Windows':
            os.system('del %s' % os.path.join(mount_point, 'failing.txt'))
            os.system('del %s' % os.path.join(os.getcwd(), fail_txt_path))
        else:
            os.system('rm %s' % os.path.join(mount_point, 'failing.txt'))
            os.system('rm %s' % os.path.join(os.getcwd(), fail_txt_path))

        self.assertEqual(cm.exception.return_code,
                         EXIT_CODE_FILE_STILL_PRESENT)
Esempio n. 2
0
 def test_run_fail_file(self, mock_stdout):
     mbeds = mbed_lstools.create()
     targets = mbeds.list_mbeds()
     mount_point = None
     target_to_test = None
     fail_txt_path = os.path.join('test', 'failing.txt')
     for target in targets:
         if target['platform_name'] == 'K64F':
             if 'target_id' in target and 'mount_point' in target:
                 target_to_test = target
                 mount_point = target['mount_point']
                 break
     if target_to_test:
         flasher = FlasherMbed()
         flasher.FLASHING_VERIFICATION_TIMEOUT = 2
         with open(fail_txt_path, 'w') as new_file:
             new_file.write("0000000000000000000000000000000000")
         ret = flasher.flash(source=fail_txt_path,
                             target=target_to_test,
                             method='simple',
                             no_reset=False)
         if platform.system() == 'Windows':
             os.system('del %s' % os.path.join(mount_point, 'failing.txt'))
             os.system('del %s' % os.path.join(os.getcwd(), fail_txt_path))
         else:
             os.system('rm %s' % os.path.join(mount_point, 'failing.txt'))
             os.system('rm %s' % os.path.join(os.getcwd(), fail_txt_path))
         self.assertEqual(ret, -15)
     if mock_stdout:
         pass
Esempio n. 3
0
 def test_copy_empty_file(self, mock_os_fsync):
     flasher = FlasherMbed()
     with open("empty_file", 'a'):
         os.utime("empty_file", None)
     flasher.copy_file("empty_file", "target")
     os.remove("empty_file")
     os.remove("target")
     mock_os_fsync.assert_called_once()
Esempio n. 4
0
    def test_copy_empty_file_linux(self, mock_copy_file):
        flasher = FlasherMbed()
        with open("empty_file", 'a'):
            pass

        flasher.copy_file("empty_file", "target")
        os.remove("empty_file")
        mock_copy_file.assert_called_once_with(b"", "target")
Esempio n. 5
0
    def test_copy_file_flush_is_called(self, mock_os_fsync, mock_sha1,
                                       mock_open):
        flasher = FlasherMbed()
        flasher.copy_file("source_file", "destination")

        file_handle = mock_open.return_value.__enter__.return_value

        self.assertEqual(1, file_handle.flush.call_count)
        file_handle.flush.assert_called_once()
Esempio n. 6
0
 def test_copy_empty_file_windows(self, mock_system):
     flasher = FlasherMbed()
     file_path = os.path.join(os.getcwd(), "empty_file")
     with open(file_path, 'a'):
         os.utime(file_path, None)
     flasher.copy_file(file_path, "target")
     os.remove(file_path)
     should_be = ["cmd", "/c", "copy", file_path, "target"]
     mock_system.assert_called_once_with(should_be)
Esempio n. 7
0
    def test_retries_on_os_error(self, mock_copy_file, mock_reset_board,
                                 mock_refresh_target, mock_sleep):
        target = {"target_id": "a", "mount_point": ""}
        mock_copy_file.side_effect = OSError("")
        mock_reset_board.return_value = True
        mock_refresh_target.return_value = target
        flasher = FlasherMbed()

        with self.assertRaises(FlashError) as cm:
            flasher.flash(source="",
                          target=target,
                          method="simple",
                          no_reset=False)

        self.assertEqual(cm.exception.return_code, EXIT_CODE_OS_ERROR)
        self.assertEqual(mock_copy_file.call_count, 5)
Esempio n. 8
0
    def test_copy_file_write_success(self, mock_os_fsync):
        flasher = FlasherMbed()
        with open("source_file", 'wb') as source_file:
            source_file.write(b"test data")

        flasher.copy_file("source_file", "destination")

        with open("destination", "rb") as destination:
            result = destination.read()

        # remove file before assert to clean environment
        os.remove("source_file")
        os.remove("destination")

        # make sure file.write() really worked
        self.assertEqual(result, b"test data", "copy file failed")
        mock_os_fsync.assert_called_once()
Esempio n. 9
0
    def test_verify_flash_success_ok(self, mock_isfile):
        mock_isfile.return_value = False

        new_target = {"mount_point": ""}
        return_value = FlasherMbed().verify_flash_success(
            new_target=new_target, target={}, tail="")

        self.assertEqual(return_value, 0)
Esempio n. 10
0
        def check(reason, code):
            mock_read_file.return_value = reason
            with self.assertRaises(FlashError) as cm:
                FlasherMbed().verify_flash_success(target=target, file_path="")

            exception = cm.exception
            self.assertEqual(exception.return_code, code)
            self.assertEqual(exception.message, reason)
Esempio n. 11
0
    def test_does_not_retry_on_daplink_user_error(self, mock_copy_file,
                                                  mock_reset_board,
                                                  mock_refresh_target):
        target = {"target_id": "a", "mount_point": ""}
        mock_copy_file.side_effect = FlashError(
            message="", return_code=EXIT_CODE_DAPLINK_USER_ERROR)
        mock_reset_board.return_value = True
        mock_refresh_target.return_value = target
        flasher = FlasherMbed()

        with self.assertRaises(FlashError) as cm:
            flasher.flash(source="",
                          target=target,
                          method="simple",
                          no_reset=False)

        self.assertEqual(cm.exception.return_code,
                         EXIT_CODE_DAPLINK_USER_ERROR)
        self.assertEqual(mock_copy_file.call_count, 1)
Esempio n. 12
0
    def test_refresh_target_returns_target(self, mock_mbed_lstools_create):
        class MockLS(object):
            def __init__(self):
                pass

            def list_mbeds(self, filter_function=None):
                return [{"target_id": "test_id"}]

        mock_mbed_lstools_create.return_value = MockLS()

        target = {"target_id": "test_id"}
        self.assertEqual(target,
                         FlasherMbed.refresh_target(target["target_id"]))
Esempio n. 13
0
    def test_refresh_target_returns_empty_list_when_no_devices(
            self, mock_mbed_lstools_create):
        class MockLS(object):
            def __init__(self):
                pass

            def list_mbeds(self, filter_function=None):
                return []

        mock_mbed_lstools_create.return_value = MockLS()

        FlasherMbed.REFRESH_TARGET_SLEEP = 0.01
        target = {"target_id": "test_id"}
        self.assertEqual(None, FlasherMbed.refresh_target(target["target_id"]))
Esempio n. 14
0
    def test_verify_flash_success_fail_no_reason(self, mock_isfile, mock_read_file):
        def isfile_function(path):
            if "FAIL" in path:
                return True
            return False

        mock_isfile.side_effect = isfile_function
        mock_read_file.return_value = ""

        new_target = {"mount_point": ""}
        target = {"target_id": ""}
        return_value = FlasherMbed().verify_flash_success(
            new_target=new_target, target=target, tail="")

        self.assertEqual(return_value, -4)
Esempio n. 15
0
    def test_verify_flash_success_fail_no_reason(self, mock_isfile,
                                                 mock_read_file):
        def isfile_function(path):
            if "FAIL" in path:
                return True
            return False

        mock_isfile.side_effect = isfile_function
        mock_read_file.return_value = ""

        target = {"target_id": "", "mount_point": ""}
        with self.assertRaises(FlashError) as cm:
            FlasherMbed().verify_flash_success(target=target, file_path="")

        exception = cm.exception
        self.assertEqual(exception.return_code, EXIT_CODE_FLASH_FAILED)
Esempio n. 16
0
    def test_verify_flash_success_new_style(self, mock_isfile, mock_read_file):
        def isfile_function(path):
            if "FAIL" in path:
                return True
            return False

        mock_isfile.side_effect = isfile_function
        mock_read_file.return_value = """
error: File sent out of order by PC. Target might not be programmed correctly.
error type: transient, user
        """

        target = {"target_id": "", "mount_point": ""}
        with self.assertRaises(FlashError) as cm:
            FlasherMbed().verify_flash_success(target=target, file_path="")

        self.assertEqual(cm.exception.return_code,
                         EXIT_CODE_DAPLINK_TRANSIENT_ERROR)
Esempio n. 17
0
    def test_verify_flash_success_multiple_hits(self, mock_isfile,
                                                mock_read_file):
        def isfile_function(path):
            if "FAIL" in path:
                return True
            return False

        mock_isfile.side_effect = isfile_function
        mock_read_file.return_value = """
error: File sent out of order by PC. Target might not be programmed correctly.
An error occurred during the transfer.
error type: transient, user
        """

        target = {"target_id": "", "mount_point": ""}
        with self.assertRaises(FlashError) as cm:
            FlasherMbed().verify_flash_success(target=target, file_path="")

        self.assertEqual(cm.exception.return_code, EXIT_CODE_FLASH_FAILED)
Esempio n. 18
0
    def test_flash_with_target_filename(self, copy_file, mock_refresh_target,
                                        mock_wait_for_file_disappear):
        flasher = FlasherMbed()
        flasher.return_value = True
        copy_file.return_value = True
        target = {"target_id": "a", "mount_point": ""}
        mock_refresh_target.return_value = target
        flasher.verify_flash_success = mock.MagicMock()
        flasher.verify_flash_success.return_value = EXIT_CODE_SUCCESS
        mock_wait_for_file_disappear.return_value = {
            "target_id": "a",
            "mount_point": ""
        }
        flasher.flash(source=__file__,
                      target=target,
                      method='simple',
                      no_reset=True,
                      target_filename="test.ext")

        target_filename = os.path.abspath(
            os.path.join(os.path.dirname(__file__), "../..", "test.ext"))
        copy_file.assert_called_once_with(__file__, target_filename)
Esempio n. 19
0
 def check(reason, code):
     mock_read_file.return_value = reason
     return_value = FlasherMbed().verify_flash_success(
         new_target=new_target, target=target, tail="")
     self.assertEqual(return_value, code)
Esempio n. 20
0
 def test_copy_file_with_spaces(self, mock_system):
     flasher = FlasherMbed()
     flasher.copy_file(__file__, "tar get")
     should_be = 'copy "%s" "tar get"' % __file__
     mock_system.assert_called_once_with(should_be)
Esempio n. 21
0
 def test_copy_file_unable_to_read(self):
     flasher = FlasherMbed()
     with self.assertRaises(FlashError):
         flasher.copy_file("not-existing-file", "target")
Esempio n. 22
0
 def test_copy_file_with_spaces(self, mock_check_call):
     flasher = FlasherMbed()
     flasher.copy_file(__file__, "tar get")
     should_be = ["cmd", "/c", "copy", __file__, "tar get"]
     mock_check_call.assert_called_with(should_be)
Esempio n. 23
0
    def test_verify_flash_success_ok(self, mock_isfile):
        mock_isfile.return_value = False

        return_value = FlasherMbed().verify_flash_success(
            target={"mount_point": ""}, file_path="")
        self.assertEqual(return_value, EXIT_CODE_SUCCESS)
Esempio n. 24
0
 def test_copy_file_with_spaces(self, mock_os_fsync):
     flasher = FlasherMbed()
     flasher.copy_file(__file__, "tar get")
     os.remove("tar get")
     mock_os_fsync.assert_called_once()
Esempio n. 25
0
    def _erase_board_simple(self, target, no_reset):
        """
        :param target: target to which perform the erase
        :param no_reset: erase with/without reset
        :return: exit code
        """
        if 'mount_point' not in target:
            raise EraseError(message="mount point missing from target",
                             return_code=EXIT_CODE_MOUNT_POINT_MISSING)
        if 'serial_port' not in target:
            raise EraseError(message="serial port missing from target",
                             return_code=EXIT_CODE_SERIAL_PORT_MISSING)

        automation_activated = False
        daplink_version = 0
        if not isfile(join(target["mount_point"], 'DETAILS.TXT')):
            raise EraseError(message="No DETAILS.TXT found",
                             return_code=EXIT_CODE_IMPLEMENTATION_MISSING)

        self.logger.debug(join(target["mount_point"], 'DETAILS.TXT'))
        with open(join(target["mount_point"], 'DETAILS.TXT'),
                  'rb') as new_file:
            for line in new_file:
                if line.find(b"Automation allowed: 1") != -1:
                    automation_activated = True
                if line.find(b"Interface Version") != -1:
                    try:
                        if six.PY2:
                            daplink_version = int(line.split(' ')[-1])
                        else:
                            daplink_version = int(
                                line.decode('utf-8').split(' ')[-1])
                    except (IndexError, ValueError):
                        raise EraseError(
                            message=
                            "Failed to parse DAPLINK version from DETAILS.TXT",
                            return_code=EXIT_CODE_IMPLEMENTATION_MISSING)

        if not automation_activated:
            msg = "Selected device does not have automation activated in DAPLINK"
            raise EraseError(message=msg,
                             return_code=EXIT_CODE_IMPLEMENTATION_MISSING)

        if daplink_version < ERASE_DAPLINK_SUPPORT_VERSION:
            msg = "Selected device has Daplink version {}," \
                  "erasing supported from version {} onwards".\
                format(daplink_version, ERASE_DAPLINK_SUPPORT_VERSION)
            raise EraseError(message=msg,
                             return_code=EXIT_CODE_IMPLEMENTATION_MISSING)

        with open(join(target["mount_point"], 'ERASE.ACT'), 'wb'):
            pass

        auto_thread = Thread(target=self.wait_to_disappear,
                             args=(target["mount_point"], ))
        auto_thread.start()
        while auto_thread.is_alive():
            auto_thread.join(0.5)

        target = FlasherMbed.refresh_target(target["target_id"])
        if not target:
            raise EraseError(message="target id is missing",
                             return_code=EXIT_CODE_TARGET_ID_MISSING)

        auto_thread = Thread(target=self.runner,
                             args=(target["mount_point"], 'ERASE.ACT'))
        auto_thread.start()
        while auto_thread.is_alive():
            auto_thread.join(0.5)

        if not no_reset:
            success = self.reset_board(target["serial_port"])
            if success != 0:
                raise EraseError(message="erase failed", return_code=success)

        self.logger.info("erase %s completed", target['target_id'])
        return EXIT_CODE_SUCCESS