Ejemplo n.º 1
0
def test_stat(sftpserver):
    '''test stat'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            dirname = 'pub'
            rslt = sftp.stat(dirname)
            assert rslt.st_size >= 0
Ejemplo n.º 2
0
def test_walktree_cbmock(sftpserver):
    '''test the walktree function, with mocked callbacks (standalone functions)
    '''
    file_cb = Mock(return_value=None)
    dir_cb = Mock(return_value=None)
    unk_cb = Mock(return_value=None)

    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            sftp.walktree('.',
                          fcallback=file_cb,
                          dcallback=dir_cb,
                          ucallback=unk_cb)
            # check calls to the file callback
            file_cb.assert_called_with('./read.me')
            thecall = call('./pub/foo2/bar1/bar1.txt')
            assert thecall in file_cb.mock_calls
            assert file_cb.call_count > 3
            # check calls to the directory callback
            assert [call('./pub'),
                    call('./pub/foo1'),
                    call('./pub/foo2'),
                    call('./pub/foo2/bar1')] == dir_cb.mock_calls
            # check calls to the unknown callback
            assert [] == unk_cb.mock_calls
Ejemplo n.º 3
0
def test_walktree_cbmock(sftpserver):
    '''test the walktree function, with mocked callbacks (standalone functions)
    '''
    file_cb = Mock(return_value=None)
    dir_cb = Mock(return_value=None)
    unk_cb = Mock(return_value=None)

    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            sftp.walktree('.',
                          fcallback=file_cb,
                          dcallback=dir_cb,
                          ucallback=unk_cb)
            # check calls to the file callback
            file_cb.assert_called_with('./read.me')
            thecall = call('./pub/foo2/bar1/bar1.txt')
            assert thecall in file_cb.mock_calls
            assert file_cb.call_count > 3
            # check calls to the directory callback
            assert [
                call('./pub'),
                call('./pub/foo1'),
                call('./pub/foo2'),
                call('./pub/foo2/bar1')
            ] == dir_cb.mock_calls
            # check calls to the unknown callback
            assert [] == unk_cb.mock_calls
Ejemplo n.º 4
0
def test_get_r_pwd(sftpserver):
    '''test the get_r for remotepath is pwd '/pub/foo2' '''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            localpath = mkdtemp()
            sftp.get_r('pub/foo2', localpath)

            checks = [
                ([
                    '',
                ], [
                    'pub',
                ]),
                ([
                    '',
                    'pub',
                ], [
                    'foo2',
                ]),
                (['', 'pub', 'foo2'], ['bar1', 'foo2.txt']),
                (['', 'pub', 'foo2', 'bar1'], [
                    'bar1.txt',
                ]),
            ]
            for pth, fls in checks:
                assert sorted(os.listdir(os.path.join(localpath, *pth))) == fls

            # cleanup local
            shutil.rmtree(localpath)
Ejemplo n.º 5
0
def test_username_specified(sftpserver):
    '''test specifying username as parameter'''
    with sftpserver.serve_content(VFS):
        params = conn(sftpserver)
        params['username'] = '******'
        with pysftp.Connection(**params) as sftp:
            assert sftp._username == params['username']
Ejemplo n.º 6
0
def test_getcwd_after_chdir(sftpserver):
    '''test getcwd after a chdir operation'''
    with sftpserver.serve_content(VFS):
        cnn = conn(sftpserver)
        cnn['default_path'] = None
        with pysftp.Connection(**cnn) as sftp:
            sftp.chdir('/home/test/pub/foo1')
            assert sftp.getcwd() == '/home/test/pub/foo1'
Ejemplo n.º 7
0
def test_cd_path(sftpserver):
    '''test .cd with a path'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            home = sftp.pwd
            with sftp.cd('pub'):
                assert sftp.pwd == '/home/test/pub'
            assert home == sftp.pwd
Ejemplo n.º 8
0
def test_get_glob_fails(sftpserver):
    '''try and use get a file with a pattern - Fails'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.cwd('pub/foo1')
            with tempfile_containing('') as fname:
                with pytest.raises(IOError):
                    psftp.get('*', fname)
Ejemplo n.º 9
0
def test_getcwd_none(sftpserver):
    '''test .getcwd as the first operation - need pristine connection
    and no default_path arg'''
    with sftpserver.serve_content(VFS):
        cnn = conn(sftpserver)
        cnn['default_path'] = None
        with pysftp.Connection(**cnn) as sftp:
            assert sftp.getcwd() is None
Ejemplo n.º 10
0
def test_get(sftpserver):
    '''download a file'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.cwd('pub/foo1')
            with tempfile_containing('') as fname:
                psftp.get('foo1.txt', fname)
                assert open(fname, 'rb').read() == b'content of foo1.txt'
Ejemplo n.º 11
0
def test_get(sftpserver):
    '''download a file'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.cwd('pub/foo1')
            with tempfile_containing('') as fname:
                psftp.get('foo1.txt', fname)
                assert open(fname, 'rb').read() == b'content of foo1.txt'
Ejemplo n.º 12
0
def test_open_read_with(sftpserver):
    '''test the open function in a with statment'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.chdir('pub')
            with psftp.open('make.txt') as rfile:
                contents = rfile.read()
            assert contents == b'content of make.txt'
Ejemplo n.º 13
0
def test_log_cnopts_explicit_false(sftpserver):
    '''test .logfile returns false when CnOpts.log is set to false'''
    copts = conn(sftpserver)
    cnopts = pysftp.CnOpts()
    copts['cnopts'] = cnopts
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**copts) as sftp:
            assert sftp.logfile is False
Ejemplo n.º 14
0
def test_isfile(sftpserver):
    '''test .isfile() functionality'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            rfile = 'pub/make.txt'
            rdir = 'pub'
            assert sftp.isfile(rfile)
            assert sftp.isfile(rdir) is False
Ejemplo n.º 15
0
def test_get_glob_fails(sftpserver):
    '''try and use get a file with a pattern - Fails'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.cwd('pub/foo1')
            with tempfile_containing('') as fname:
                with pytest.raises(IOError):
                    psftp.get('*', fname)
Ejemplo n.º 16
0
def test_cd_none(sftpserver):
    '''test .cd with None'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            home = sftp.pwd
            with sftp.cd():
                sftp.chdir('pub')
                assert sftp.pwd == '/home/test/pub'
            assert home == sftp.pwd
Ejemplo n.º 17
0
def test_put_bad_local(sftpserver):
    '''try to put a non-existing file to a read-only server'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            with tempfile_containing('should fail') as fname:
                pass
            # tempfile has been removed
            with pytest.raises(OSError):
                sftp.put(fname)
Ejemplo n.º 18
0
def test_normalize(sftpserver):
    '''test the normalize function'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            assert sftp.normalize('make.txt') == '/home/test/make.txt'
            assert sftp.normalize('.') == '/home/test'
            assert sftp.normalize('pub') == '/home/test/pub'
            sftp.chdir('pub')
            assert sftp.normalize('.') == '/home/test/pub'
Ejemplo n.º 19
0
def test_issue_xx_sftpserver_plugin(sftpserver):
    '''an example showing how to use the sftpserver plugin in a test'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            home = sftp.pwd
            with sftp.cd():
                sftp.chdir('pub')
                assert sftp.pwd == '/home/test/pub'
            assert home == sftp.pwd
Ejemplo n.º 20
0
def test_get_bad_remote(sftpserver):
    '''download a file'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.cwd('pub/foo1')
            with tempfile_containing('') as fname:
                with pytest.raises(IOError):
                    psftp.get('readme-not-there.txt', fname)
                assert open(fname, 'rb').read()[0:7] != b'Welcome'
Ejemplo n.º 21
0
def test_exists(sftpserver):
    '''test .exists() fuctionality'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            rfile = 'pub/foo2/bar1/bar1.txt'
            rbad = 'pub/foo2/bar1/peek-a-boo.txt'
            assert sftp.exists(rfile)
            assert sftp.exists(rbad) is False
            assert sftp.exists('pub')
Ejemplo n.º 22
0
def test_depr_log_param(sftpserver):
    '''test deprecation warning for Connection log parameter'''
    warnings.simplefilter('always')
    copts = conn(sftpserver)
    copts['log'] = True
    with sftpserver.serve_content(VFS):
        with pytest.warns(DeprecationWarning):
            with pysftp.Connection(**copts) as sftp:
                sftp.listdir()
Ejemplo n.º 23
0
def test_put_bad_local(sftpserver):
    '''try to put a non-existing file to a read-only server'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            with tempfile_containing('should fail') as fname:
                pass
            # tempfile has been removed
            with pytest.raises(OSError):
                sftp.put(fname)
Ejemplo n.º 24
0
def test_open_read(sftpserver):
    '''test the open function'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.chdir('pub')
            rfile = psftp.open('make.txt')
            contents = rfile.read()
            rfile.close()
            assert contents == b'content of make.txt'
Ejemplo n.º 25
0
def test_cd_bad_path(sftpserver):
    '''test .cd with a bad path'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            home = sftp.pwd
            with pytest.raises(IOError):
                with sftp.cd('not-there'):
                    pass
            assert home == sftp.pwd
Ejemplo n.º 26
0
def test_get_bad_remote(sftpserver):
    '''download a file'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.cwd('pub/foo1')
            with tempfile_containing('') as fname:
                with pytest.raises(IOError):
                    psftp.get('readme-not-there.txt', fname)
                assert open(fname, 'rb').read()[0:7] != b'Welcome'
Ejemplo n.º 27
0
def test_getfo_callback(sftpserver):
    '''test getfo callback'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            flo = BytesIO()
            cback = Mock(return_value=None)
            sftp.chdir('pub')
            sftp.getfo('make.txt', flo, callback=cback)

            assert cback.call_count >= 2
Ejemplo n.º 28
0
def test_remote_server_key(sftpserver):
    '''test .remote_server_key property'''
    with sftpserver.serve_content(VFS):
        this_conn = conn(sftpserver)
        this_conn['cnopts'].hostkeys = None  # turn-off hostkey verification
        with pysftp.Connection(**this_conn) as sftp:
            rsk = sftp.remote_server_key
            hks = HostKeys()
            hks.add(hostname=sftpserver.host, keytype=rsk.get_name(), key=rsk)
            hks.save('sftpserver.pub')
Ejemplo n.º 29
0
def test_get_preserve_mtime(sftpserver):
    '''test that m_time is preserved from local to remote, when get'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.cwd('pub/foo1')
            rfile = 'foo1.txt'
            with tempfile_containing('') as localfile:
                r_stat = psftp.stat(rfile)
                psftp.get(rfile, localfile, preserve_mtime=True)
                assert r_stat.st_mtime == os.stat(localfile).st_mtime
Ejemplo n.º 30
0
def test_pwd(sftpserver):
    '''test the pwd property'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            sftp.chdir('pub/foo2')
            assert sftp.pwd == '/home/test/pub/foo2'
            sftp.chdir('bar1')
            assert sftp.pwd == '/home/test/pub/foo2/bar1'
            sftp.chdir('../../foo1')
            assert sftp.pwd == '/home/test/pub/foo1'
Ejemplo n.º 31
0
def test_mkdir(sftpserver):
    '''test mkdir'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            dirname = 'test-dir'
            assert dirname not in sftp.listdir()
            sftp.mkdir(dirname)
            assert dirname in sftp.listdir()
            # clean up
            sftp.rmdir(dirname)
Ejemplo n.º 32
0
def test_getfo_callback(sftpserver):
    '''test getfo callback'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            flo = BytesIO()
            cback = Mock(return_value=None)
            sftp.chdir('pub')
            sftp.getfo('make.txt', flo, callback=cback)

            assert cback.call_count
Ejemplo n.º 33
0
def test_getfo_flo(sftpserver):
    '''test getfo to a file-like object'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            flo = BytesIO()
            sftp.chdir('pub')
            num_bytes = sftp.getfo('make.txt', flo)

            assert flo.getvalue() == b'content of make.txt'
            assert num_bytes == len(flo.getvalue())
Ejemplo n.º 34
0
def test_get_preserve_mtime(sftpserver):
    '''test that m_time is preserved from local to remote, when get'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.cwd('pub/foo1')
            rfile = 'foo1.txt'
            with tempfile_containing('') as localfile:
                r_stat = psftp.stat(rfile)
                psftp.get(rfile, localfile, preserve_mtime=True)
                assert r_stat.st_mtime == os.stat(localfile).st_mtime
Ejemplo n.º 35
0
def test_getfo_flo(sftpserver):
    '''test getfo to a file-like object'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            flo = BytesIO()
            sftp.chdir('pub')
            num_bytes = sftp.getfo('make.txt', flo)

            assert flo.getvalue() == b'content of make.txt'
            assert num_bytes == len(flo.getvalue())
Ejemplo n.º 36
0
def test_no_username_raises_err(sftpserver):
    '''test No username raises CredentialException.'''
    hold_logname = os.environ.get('LOGNAME')
    del os.environ['LOGNAME']
    with sftpserver.serve_content(VFS):
        params = conn(sftpserver)
        del params['username']
        with pytest.raises(pysftp.CredentialException):
            pysftp.Connection(**params)
    if hold_logname is not None:
        os.environ['LOGNAME'] = hold_logname
Ejemplo n.º 37
0
def test_cd_nested(sftpserver):
    '''test nested cd's'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            home = sftp.pwd
            with sftp.cd('pub'):
                assert sftp.pwd == '/home/test/pub'
                with sftp.cd('foo1'):
                    assert sftp.pwd == '/home/test/pub/foo1'
                assert sftp.pwd == '/home/test/pub'
            assert home == sftp.pwd
Ejemplo n.º 38
0
def test_log_param_user_file(sftpserver):
    '''test .logfile returns temp filename when log param is set to True'''
    copts = conn(sftpserver)
    copts['log'] = os.path.expanduser('~/my-logfile.txt')
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**copts) as sftp:
            assert sftp.logfile == copts['log']
            assert os.path.exists(sftp.logfile)
            logfile = sftp.logfile
        # cleanup
        os.unlink(logfile)
Ejemplo n.º 39
0
def test_username_from_environ(sftpserver):
    '''test reading username from $LOGNAME environment variable.'''
    username = '******'
    hold_logname = os.environ.get('LOGNAME')
    os.environ['LOGNAME'] = username
    with sftpserver.serve_content(VFS):
        params = conn(sftpserver)
        del params['username']
        with pysftp.Connection(**params) as sftp:
            if hold_logname is not None:
                os.environ['LOGNAME'] = hold_logname
            assert sftp._username == username
Ejemplo n.º 40
0
def test_log_param_true(sftpserver):
    '''test .logfile returns temp filename when log param is set to True'''
    copts = conn(sftpserver)
    copts['log'] = True
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**copts) as sftp:
            assert os.path.exists(sftp.logfile)
            # and we are not writing to a file named 'True'
            assert sftp.logfile != copts['log']
            logfile = sftp.logfile
        # cleanup
        os.unlink(logfile)
Ejemplo n.º 41
0
def test_get_callback(sftpserver):
    '''test .get callback'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.cwd('pub/foo1')
            cback = Mock(return_value=None)
            with tempfile_containing('') as fname:
                result = psftp.get('foo1.txt', fname, callback=cback)
                assert open(fname, 'rb').read() == b'content of foo1.txt'
            # verify callback was called more than once - usually a min of 2
            assert cback.call_count >= 2
            # unlike .put() nothing is returned from the operation
            assert result is None
Ejemplo n.º 42
0
def test_get_d(sftpserver):
    '''test the get_d for remotepath is pwd '.' '''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            sftp.cwd('pub')
            localpath = mkdtemp()
            sftp.get_d('.', localpath)

            checks = [(['', ], ['make.txt', ]), ]
            for pth, fls in checks:
                assert sorted(os.listdir(os.path.join(localpath, *pth))) == fls

            # cleanup local
            shutil.rmtree(localpath)
Ejemplo n.º 43
0
def test_listdir_attr(sftpserver):
    '''test listdir'''
    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as psftp:
            psftp.cwd('pub')
            attrs = psftp.listdir_attr()
            assert len(attrs) == 3
            # test they are in filename order
            assert attrs[0].filename == 'foo1'
            assert attrs[1].filename == 'foo2'
            assert attrs[2].filename == 'make.txt'
            # test that longname is there
            for attr in attrs:
                assert attr.longname is not None