Beispiel #1
0
    def test_pickle_file_transfer_put(self):
        test_local_file_content = \
            b"This is local file content \n which is multiline " \
            b"continuing....with other character\nanother line here \n this is last line"
        # create a test file locally
        with open(self.test_local_filepath, 'wb') as file:
            file.write(test_local_file_content)

        # put test file to remote
        put_test_task = SFTPOperator(task_id="test_sftp",
                                     ssh_hook=self.hook,
                                     local_filepath=self.test_local_filepath,
                                     remote_filepath=self.test_remote_filepath,
                                     operation=SFTPOperation.PUT,
                                     create_intermediate_dirs=True,
                                     dag=self.dag)
        self.assertIsNotNone(put_test_task)
        ti2 = TaskInstance(task=put_test_task,
                           execution_date=timezone.utcnow())
        ti2.run()

        # check the remote file content
        check_file_task = SSHOperator(task_id="test_check_file",
                                      ssh_hook=self.hook,
                                      command="cat {0}".format(
                                          self.test_remote_filepath),
                                      do_xcom_push=True,
                                      dag=self.dag)
        self.assertIsNotNone(check_file_task)
        ti3 = TaskInstance(task=check_file_task,
                           execution_date=timezone.utcnow())
        ti3.run()
        self.assertEqual(
            ti3.xcom_pull(task_ids='test_check_file',
                          key='return_value').strip(), test_local_file_content)
Beispiel #2
0
    def test_pickle_file_transfer_get(self):
        test_remote_file_content = \
            "This is remote file content \n which is also multiline " \
            "another line here \n this is last line. EOF"

        # create a test file remotely
        create_file_task = SSHOperator(task_id="test_create_file",
                                       ssh_hook=self.hook,
                                       command="echo '{0}' > {1}".format(
                                           test_remote_file_content,
                                           self.test_remote_filepath),
                                       do_xcom_push=True,
                                       dag=self.dag)
        self.assertIsNotNone(create_file_task)
        ti1 = TaskInstance(task=create_file_task,
                           execution_date=timezone.utcnow())
        ti1.run()

        # get remote file to local
        get_test_task = SFTPOperator(task_id="test_sftp",
                                     ssh_hook=self.hook,
                                     local_filepath=self.test_local_filepath,
                                     remote_filepath=self.test_remote_filepath,
                                     operation=SFTPOperation.GET,
                                     dag=self.dag)
        self.assertIsNotNone(get_test_task)
        ti2 = TaskInstance(task=get_test_task,
                           execution_date=timezone.utcnow())
        ti2.run()

        # test the received content
        content_received = None
        with open(self.test_local_filepath, 'r') as file:
            content_received = file.read()
        self.assertEqual(content_received.strip(), test_remote_file_content)
Beispiel #3
0
    def test_file_transfer_no_intermediate_dir_error_get(self):
        test_remote_file_content = \
            "This is remote file content \n which is also multiline " \
            "another line here \n this is last line. EOF"

        # create a test file remotely
        create_file_task = SSHOperator(task_id="test_create_file",
                                       ssh_hook=self.hook,
                                       command="echo '{0}' > {1}".format(
                                           test_remote_file_content,
                                           self.test_remote_filepath),
                                       do_xcom_push=True,
                                       dag=self.dag)
        self.assertIsNotNone(create_file_task)
        ti1 = TaskInstance(task=create_file_task,
                           execution_date=timezone.utcnow())
        ti1.run()

        # Try to GET test file from remote
        # This should raise an error with "No such file" as the directory
        # does not exist
        with self.assertRaises(Exception) as error:
            get_test_task = SFTPOperator(
                task_id="test_sftp",
                ssh_hook=self.hook,
                local_filepath=self.test_local_filepath_int_dir,
                remote_filepath=self.test_remote_filepath,
                operation=SFTPOperation.GET,
                dag=self.dag)
            self.assertIsNotNone(get_test_task)
            ti2 = TaskInstance(task=get_test_task,
                               execution_date=timezone.utcnow())
            ti2.run()
        self.assertIn('No such file', str(error.exception))
Beispiel #4
0
    def test_file_transfer_no_intermediate_dir_error_put(self):
        test_local_file_content = \
            b"This is local file content \n which is multiline " \
            b"continuing....with other character\nanother line here \n this is last line"
        # create a test file locally
        with open(self.test_local_filepath, 'wb') as file:
            file.write(test_local_file_content)

        # Try to put test file to remote
        # This should raise an error with "No such file" as the directory
        # does not exist
        with self.assertRaises(Exception) as error:
            put_test_task = SFTPOperator(
                task_id="test_sftp",
                ssh_hook=self.hook,
                local_filepath=self.test_local_filepath,
                remote_filepath=self.test_remote_filepath_int_dir,
                operation=SFTPOperation.PUT,
                create_intermediate_dirs=False,
                dag=self.dag)
            self.assertIsNotNone(put_test_task)
            ti2 = TaskInstance(task=put_test_task,
                               execution_date=timezone.utcnow())
            ti2.run()
        self.assertIn('No such file', str(error.exception))
Beispiel #5
0
    def test_arg_checking(self):
        # Exception should be raised if neither ssh_hook nor ssh_conn_id is provided
        with self.assertRaisesRegex(
                AirflowException,
                "Cannot operate without ssh_hook or ssh_conn_id."):
            task_0 = SFTPOperator(task_id="test_sftp",
                                  local_filepath=self.test_local_filepath,
                                  remote_filepath=self.test_remote_filepath,
                                  operation=SFTPOperation.PUT,
                                  dag=self.dag)
            task_0.execute(None)

        # if ssh_hook is invalid/not provided, use ssh_conn_id to create SSHHook
        task_1 = SFTPOperator(
            task_id="test_sftp",
            ssh_hook="string_rather_than_SSHHook",  # invalid ssh_hook
            ssh_conn_id=TEST_CONN_ID,
            local_filepath=self.test_local_filepath,
            remote_filepath=self.test_remote_filepath,
            operation=SFTPOperation.PUT,
            dag=self.dag)
        try:
            task_1.execute(None)
        except Exception:  # pylint: disable=broad-except
            pass
        self.assertEqual(task_1.ssh_hook.ssh_conn_id, TEST_CONN_ID)

        task_2 = SFTPOperator(
            task_id="test_sftp",
            ssh_conn_id=TEST_CONN_ID,  # no ssh_hook provided
            local_filepath=self.test_local_filepath,
            remote_filepath=self.test_remote_filepath,
            operation=SFTPOperation.PUT,
            dag=self.dag)
        try:
            task_2.execute(None)
        except Exception:  # pylint: disable=broad-except
            pass
        self.assertEqual(task_2.ssh_hook.ssh_conn_id, TEST_CONN_ID)

        # if both valid ssh_hook and ssh_conn_id are provided, ignore ssh_conn_id
        task_3 = SFTPOperator(task_id="test_sftp",
                              ssh_hook=self.hook,
                              ssh_conn_id=TEST_CONN_ID,
                              local_filepath=self.test_local_filepath,
                              remote_filepath=self.test_remote_filepath,
                              operation=SFTPOperation.PUT,
                              dag=self.dag)
        try:
            task_3.execute(None)
        except Exception:  # pylint: disable=broad-except
            pass
        self.assertEqual(task_3.ssh_hook.ssh_conn_id, self.hook.ssh_conn_id)