Example #1
0
    def test__collect_single_filepath_to_long_for_windows(self):
        """
        Should "collect" an error message which states that the target path is too long for the underlying
        platform.
        :return:
        """
        from artefact.localhost.file import FileCopy

        collector = FileCopy(parameters={}, parent=None)
        collector.source_path = self._expected_source_file_path
        expected_message = "File './testfiles/test.txt' could not be copied, because the target path length of " \
                           "'./unique' is too long for the underlying system."
        if platform.system() == "Windows":
            expected_message = "File './testfiles/test.txt' could not be copied, because the target path length of " \
                               "'.\\unique' is too long for the underlying system."

        with mock.patch('artefact.localhost.file.os.path.isfile',
                        MagicMock(return_value=True)):
            with mock.patch(
                    'artefact.localhost.file.FileCopy._ensure_target_directory',
                    MagicMock(return_value=self._expected_target_path)):
                with mock.patch(
                        'artefact.localhost.file.FileCopy._validate_target_path_length',
                        MagicMock(return_value=False)):
                    collector._collect_single(collector.source_path)
                    actual_message = collector.data[-1].collecteddata

        self.assertEqual(expected_message, actual_message)
Example #2
0
    def test__collect_single_could_not_copy_file(self):
        """
        Should ensure that unique target directory is deleted.
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected_source_file_path = f"{self._expected_unique_directory}/IDoNotExist.txt"
        collector = FileCopy(parameters={}, parent=None)
        collector.source_path = expected_source_file_path
        collector._collect_single()

        self.assertFalse(os.path.exists(self._expected_target_path))
Example #3
0
    def test__enough_space_on_target_source_does_not_exist(self):
        """
        Should return false
        :return:
        """
        from artefact.localhost.file import FileCopy

        MockedDiskUsage = namedtuple('MockedDiskUsage', ['free'])
        collector = FileCopy(parameters={}, parent=None)
        collector.source_path = './testfiles/doesnotexist.txt'
        with mock.patch('artefact.localhost.file.shutil.disk_usage',
                        MagicMock(return_value=MockedDiskUsage(free=1000000))):
            self.assertFalse(
                collector._enough_space_on_target(
                    target_path='.', source_path=collector.source_path))
Example #4
0
 def test__get_unique_directory_name_max_counter_reached(self):
     """
     Should raise an exception.
     :return:
     """
     from datetime import datetime
     from businesslogic.errors import MaxDirectoriesReachedError
     from artefact.localhost.file import FileCopy
     expected_time = datetime(2009, 3, 20, 13, 12, 2)
     expected_dir_name = 'some_file.txt_2009032013120201'
     collector = FileCopy(parameters={}, parent=None)
     collector.source_path = '~/somepath/some_file.txt'
     self.assertRaises(MaxDirectoriesReachedError,
                       collector._get_unique_directory_name, '.',
                       expected_time)
Example #5
0
    def test__get_unique_directory_name(self):
        """
        Should return directory name which is unique to target directory using
        file name, datetime stamp, and counter
        :return:
        """
        from datetime import datetime
        from artefact.localhost.file import FileCopy
        expected_time = datetime(2009, 3, 20, 13, 12, 2)
        expected_dir_name = 'some_file.txt_2009032013120201'
        collector = FileCopy(parameters={}, parent=None)
        collector.source_path = '~/somepath/some_file.txt'
        actual_dir_name = collector._get_unique_directory_name(
            'mock', expected_time)

        self.assertEqual(expected_dir_name, actual_dir_name)
Example #6
0
    def test__collect_with_two_files(self):
        """
        Should call _collect_single two times.
        :return:
        """
        from artefact.localhost.file import FileCopy

        collector = FileCopy(parameters={}, parent=None)
        expected_file_path = f"./testtree/testfile1{FileCopy._FILE_PATH_SEPERATOR}./testtree/testfile2"
        collector.source_path = expected_file_path

        expected_call_count = 2
        single_mock: MagicMock = MagicMock()
        with mock.patch('artefact.localhost.file.FileCopy._collect_single',
                        single_mock):
            collector._collect()

        self.assertEqual(expected_call_count, single_mock.call_count)
Example #7
0
    def test__collect_setter_with_abreviated_path(self):
        """
        Should expand the targert file path, if the file path provided is an abreviation, like
        for example ~/test.txt
        :return:
        """
        from pathlib import Path
        from artefact.localhost.file import FileCopy

        source_file_path = "~/IDoNotExist.txt"
        expected_file_path = f"{str(Path.home())}/IDoNotExist.txt"
        if platform.system() == "Windows":
            expected_file_path = f"{str(Path.home())}\IDoNotExist.txt"
        collector = FileCopy(parameters={}, parent=None)
        collector.source_path = source_file_path
        actual_filepath = collector._source_path

        self.assertEqual(expected_file_path, actual_filepath)
Example #8
0
    def test__collect_single(self):
        """
        Should copy the file, identified by the member 'filepath'.
        :return:
        """
        from artefact.localhost.file import FileCopy

        collector = FileCopy(parameters={}, parent=None)
        collector.source_path = self._expected_source_file_path
        with mock.patch('artefact.localhost.file.os.path.isfile',
                        MagicMock(return_value=True)):
            with mock.patch(
                    'artefact.localhost.file.FileCopy._ensure_target_directory',
                    MagicMock(return_value=self._expected_target_path)):
                # Create the target path here, because we mocked _ensure_target_directory
                if not os.path.exists(self._expected_target_path):
                    os.mkdir(self._expected_target_path)
                collector._collect_single(collector.source_path)

        self.assertTrue(os.path.exists(self._expected_target_file_path))
Example #9
0
    def test__get_unique_directory_name_timestamp_already_taken(self):
        """
        Should return a unique name with the same datetime stamp but with the
        counter incremented by one
        :return:
        """
        import os
        from datetime import datetime
        from artefact.localhost.file import FileCopy
        expected_time = datetime(2009, 3, 20, 13, 12, 2)
        expected_dir_name = 'some_file.txt_2009032013120202'
        collector = FileCopy(parameters={}, parent=None)
        collector.source_path = '~/somepath/some_file.txt'
        try:
            os.mkdir('./some_file.txt_2009032013120201')
            actual_dir_name = collector._get_unique_directory_name(
                '.', expected_time)
        finally:
            os.rmdir('./some_file.txt_2009032013120201')

        self.assertEqual(expected_dir_name, actual_dir_name)
Example #10
0
    def test__collect_single_file_does_not_exist(self):
        """
        Should store a message in data, that says that the file from which the content should be
        collected, does not exist.
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected_source_file_path = "./somedir/IDoNotExist.txt"
        expected_data = f"The file '{expected_source_file_path}' does not exist."
        collector = FileCopy(parameters={}, parent=None)
        collector.source_path = expected_source_file_path
        with mock.patch('artefact.localhost.file.os.path.isfile',
                        MagicMock(return_value=True)):
            with mock.patch(
                    'artefact.localhost.file.FileCopy._ensure_target_directory',
                    MagicMock(return_value=self._expected_target_path)):
                collector._collect_single(file_path=collector.source_path)
            actual_data = collector.data[0].collecteddata

        self.assertEqual(expected_data, actual_data)
Example #11
0
    def test__collect_single_log_data(self):
        """
        Should log inside data which file was copied and what is the target destination of the copy.
        Should collect the md5 hash of the file, and the sha1 hash of the file.
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected_data = f"Copied file '{self._expected_source_file_path}' to '{self._expected_target_file_path}'."
        collector = FileCopy(parameters={}, parent=None)
        collector.source_path = self._expected_source_file_path
        with mock.patch(
                'artefact.localhost.file.FileCopy._ensure_target_directory',
                MagicMock(return_value=self._expected_target_path)):
            # Create the target path here, because we mocked _ensure_target_directory
            if not os.path.exists(self._expected_target_path):
                os.mkdir(self._expected_target_path)
            collector._collect_single(collector.source_path)
        actual_data = collector.data[0].collecteddata

        self.assertEqual(expected_data, actual_data)