Example #1
0
def test_credential_validation():
    with pytest.raises(ValueError):
        SFTP(host=None, username=None, password=None, rsa_private_key_file=None)

    with pytest.raises(ValueError):
        SFTP(host=None, username='******', password='******', rsa_private_key_file='/path/to/key/file')

    with pytest.raises(ValueError):
        SFTP(host='host', username=None, password='******', rsa_private_key_file='/path/to/key/file')

    with pytest.raises(ValueError):
        SFTP(host='host', username='******', password=None, rsa_private_key_file=None)
Example #2
0
def live_sftp(simple_table, simple_csv_path,
              simple_compressed_csv_path):  # noqa: F811
    # Generate a live SFTP connection based on these env vars
    host = os.environ['SFTP_HOST']
    username = os.environ['SFTP_USERNAME']
    password = None
    rsa_private_key_file = os.environ['SFTP_RSA_PRIVATE_KEY_FILE']

    sftp = SFTP(host,
                username,
                password,
                rsa_private_key_file=rsa_private_key_file)

    # Add a test directory and test files

    sftp.make_directory(REMOTE_DIR)
    sftp.put_file(simple_csv_path, REMOTE_CSV_PATH)
    sftp.put_file(simple_compressed_csv_path, REMOTE_COMPRESSED_CSV_PATH)

    yield sftp

    # Cleanup after test
    sftp.remove_file(REMOTE_CSV_PATH)
    sftp.remove_file(REMOTE_COMPRESSED_CSV_PATH)
    sftp.remove_directory(REMOTE_DIR)
Example #3
0
    def setUp(self):

        self.ts = TargetSmartAutomation()
        self.job_name = 'a-test-job'
        self.sftp = SFTP(self.ts.sftp_host, os.environ['TS_SFTP_USERNAME'],
                         os.environ['TS_SFTP_PASSWORD'], self.ts.sftp_port)
        self.test_xml = 'test/test_ts/job_config.xml'
Example #4
0
    def to_sftp_csv(self,
                    remote_path,
                    host,
                    username,
                    password,
                    port=22,
                    encoding=None,
                    compression=None,
                    errors='strict',
                    write_header=True,
                    rsa_private_key_file=None,
                    **csvargs):
        """
        Writes the table to a CSV file on a remote SFTP server

        `Args:`
            remote_path: str
                The remote path of the file. If it ends in '.gz', the file will be compressed.
            host: str
                The remote host
            username: str
                The username to access the SFTP server
            password: str
                The password to access the SFTP server
            port: int
                The port number of the SFTP server
            encoding: str
                The CSV encoding type for `csv.writer()
                <https://docs.python.org/2/library/csv.html#csv.writer/>`_
            errors: str
                Raise an Error if encountered
            write_header: boolean
                Include header in output
            rsa_private_key_file str
                Absolute path to a private RSA key used
                to authenticate stfp connection
            \**csvargs: kwargs
                ``csv_writer`` optional arguments
        """  # noqa: W605

        from parsons import SFTP

        sftp = SFTP(host, username, password, port, rsa_private_key_file)

        compression = files.compression_type_for_path(remote_path)

        local_path = self.to_csv(temp_file_compression=compression,
                                 encoding=encoding,
                                 errors=errors,
                                 write_header=write_header,
                                 **csvargs)
        sftp.put_file(local_path, remote_path)
Example #5
0
def live_sftp(simple_table, simple_csv_path, simple_compressed_csv_path):
    # Generate a live SFTP connection based on these env vars
    host = os.environ['SFTP_HOST']
    username = os.environ['SFTP_USERNAME']
    password = os.environ['SFTP_PASSWORD']

    sftp = SFTP(host, username, password)

    # Add a test directory and test files
    sftp.make_directory(REMOTE_DIR)
    sftp.put_file(simple_csv_path, REMOTE_CSV_PATH)
    sftp.put_file(simple_compressed_csv_path, REMOTE_COMPRESSED_CSV_PATH)

    yield sftp

    # Cleanup after test
    sftp.remove_file(REMOTE_CSV_PATH)
    sftp.remove_file(REMOTE_COMPRESSED_CSV_PATH)
    sftp.remove_directory(REMOTE_DIR)
Example #6
0
def test_credential_validation():
    with pytest.raises(ValueError):
        SFTP(host=None, username=None, password=None)

    with pytest.raises(ValueError):
        SFTP(host=None, username='******', password='******')
Example #7
0
def generate_live_sftp_connection():
    host = os.environ['SFTP_HOST']
    username = os.environ['SFTP_USERNAME']
    password = os.environ['SFTP_PASSWORD']
    return SFTP(host, username, password)