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__ensure_target_directory(self):
        """
        Should return path of unique target directory
        :return:
        """
        import os.path
        from artefact.localhost.file import FileCopy

        expected_target_dir = "./test_target"
        expected_unique_dir_name = "me_unique"
        expected_return_value = './test_target/me_unique'
        if platform.system() == "Windows":
            expected_return_value = './test_target\\me_unique'
        collector = FileCopy(parameters={}, parent=None)
        collector._destination_directory = expected_target_dir
        try:
            with mock.patch(
                    'artefact.localhost.file.FileCopy._get_unique_directory_name',
                    MagicMock(return_value=expected_unique_dir_name)):
                acutal_return_value = collector._ensure_target_directory()
                self.assertEqual(expected_return_value, acutal_return_value)
        finally:
            if os.path.exists(
                    os.path.join(expected_target_dir,
                                 expected_unique_dir_name)):
                os.rmdir(
                    os.path.join(expected_target_dir,
                                 expected_unique_dir_name))
            if os.path.exists(expected_target_dir):
                os.rmdir(expected_target_dir)
Example #3
0
    def test__ensure_target_directory_unique_inside_target_dir(self):
        """
        Should create directory with unique name inside target directory.
        :return:
        """
        import os.path
        from artefact.localhost.file import FileCopy

        expected_target_dir = "./test_target"
        expected_unique_dir_name = "me_unique"
        collector = FileCopy(parameters={}, parent=None)
        collector._destination_directory = expected_target_dir
        try:
            with mock.patch(
                    'artefact.localhost.file.FileCopy._get_unique_directory_name',
                    MagicMock(return_value=expected_unique_dir_name)):
                collector._ensure_target_directory()
            self.assertTrue(
                os.path.exists(
                    os.path.join(expected_target_dir,
                                 expected_unique_dir_name)))
        finally:
            if os.path.exists(
                    os.path.join(expected_target_dir,
                                 expected_unique_dir_name)):
                os.rmdir(
                    os.path.join(expected_target_dir,
                                 expected_unique_dir_name))
            if os.path.exists(expected_target_dir):
                os.rmdir(expected_target_dir)
Example #4
0
    def test_destination_directory_does_not_exist(self):
        """
        Should raise an execption.
        :return:
        """
        from artefact.localhost.file import FileCopy

        file_ = FileCopy(parameters={}, parent=None)
        with self.assertRaises(FileNotFoundError):
            file_.destination_directory = "./doesNotExist/"
Example #5
0
    def test_destination_directory_is_file(self):
        """
        Should raise an exception.
        :return:
        """
        from artefact.localhost.file import FileCopy
        from businesslogic.errors import CollectorParameterError

        file_ = FileCopy(parameters={}, parent=None)
        with self.assertRaises(CollectorParameterError):
            file_.destination_directory = "./testfiles/test.txt"
Example #6
0
    def test__expand_path_with_wildcard_in_parent_node(self):
        """
        Should raise an excecption.
        :return:
        """
        from artefact.localhost.file import FileCopy
        from businesslogic.errors import TreePathError

        tree = FileCopy(parent=None, parameters={})
        with self.assertRaises(TreePathError):
            tree._expand_path("/not_*_allowed/*")
Example #7
0
    def test__expand_path_with_wildcard_in_folder_name_in_leaf(self):
        """
        Should return list with all subdirectories which do match with wildcard text
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected = ['./testfiles', './testtree']
        tree = FileCopy(parameters={}, parent=None)
        actual = tree._expand_path('./test*')

        self.assertEqual(expected, actual)
Example #8
0
    def test__expand_path_with_no_wildcard(self):
        """
        Should return the input value in a list.
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected = ['.']
        tree = FileCopy(parameters={}, parent=None)
        actual = tree._expand_path('.')

        self.assertEqual(expected, actual)
Example #9
0
    def test__collect_with_file(self):
        """
        Should call _collect_single
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected_source = "./testtree/testfile1"
        collector = FileCopy(parameters={}, parent=None)
        with mock.patch.object(collector, '_collect_single') as mock_:
            collector._source_path = expected_source
            collector._collect()
            mock_.assert_called_once()
Example #10
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 #11
0
    def test_destination_directory_exists(self):
        """
        Should set the property _destination_directory
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected = './copydestination/'
        file_ = FileCopy(parameters={}, parent=None)
        file_.destination_directory = expected
        actual = file_._destination_directory

        self.assertEqual(expected, actual)
Example #12
0
    def test__supportedplatform_on_linux(self):
        """
        Should suport "Linux"
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected_support = True
        with mock.patch('artefact.localhost.file.platform.system',
                        MagicMock(return_value='Linux')):
            collector = FileCopy(parameters={}, parent={})
            actual_support = collector.is_platform_supported()

        self.assertEqual(expected_support, actual_support)
Example #13
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 #14
0
    def test__expand_path_with_wildcard_in_leaf(self):
        """
        Should add a path for every directory in the leaf fitting the wildcard to _tree_path.
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected = [
            './instructions', './testfiles', './copydestination', './support',
            './testtree'
        ]
        tree = FileCopy(parameters={}, parent=None)
        actual = tree._expand_path(f"./{tree._WILDCARD}")

        self.assertEqual(expected, actual)
Example #15
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 #16
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 #17
0
    def test__collect_single_filepath_is_a_directory(self):
        """
        Should "collect" an error message which states that the "filepath" provided is actually a directory.
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected_file = "/iamnotafile"
        expected_message = f"The provided filepath '{expected_file}' is not a file."
        collector = FileCopy(parameters={'filepath': expected_file},
                             parent=None)
        collector._collect_single(file_path=expected_file)

        actual_message = collector.data[0].collecteddata

        self.assertEqual(expected_message, actual_message)
Example #18
0
    def test__validate_target_path_length_platform_is_not_windows(self):
        """
        Should return True
        :return:
        """
        from artefact.localhost.file import FileCopy

        collector = FileCopy(parent=None, parameters={})

        expected_target_path = f"/{'d' * 300}/file.txt"
        expected_result = True
        with mock.patch('artefact.localhost.file.platform.system',
                        MagicMock(return_value="Darwin")):
            actual_result = collector._validate_target_path_length(
                expected_target_path)

        self.assertEqual(expected_result, actual_result)
Example #19
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 #20
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 #21
0
    def test___init___targert_directory(self):
        """
        By default _target_directory should be set to '.'
        :return:
        """
        from artefact.localhost.file import FileCopy
        expected_target_dir = '.'
        collector = FileCopy(parameters={}, parent=None)

        self.assertEqual(expected_target_dir, collector._destination_directory)
Example #22
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 #23
0
    def test_destination_directory_default(self):
        """
        By default, it should return '.'.
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected = '.'
        file_ = FileCopy(parameters={}, parent=None)
        actual = file_.destination_directory

        self.assertEqual(expected, actual)
Example #24
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 #25
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)
Example #26
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 #27
0
    def test__collect_getter_with_newline_at_the_end(self):
        """
        Should sanitize the file path. There must not be any new line, carriage return, etc. at
        the end. Otherwise things like os.path.exists do not work.
        :return:
        """
        from artefact.localhost.file import FileCopy

        expected_file_path = "./somepath"

        with mock.patch('artefact.localhost.file.os.path.exists',
                        MagicMock(return_value=True)):
            collector = FileCopy(parameters={'source_path': './somepath\r\n'},
                                 parent=None)

        actual_file_path = collector.source_path

        self.assertEqual(expected_file_path, actual_file_path)