def test_put(lsftp): '''run test on localhost''' contents = 'now is the time\nfor all good...' with tempfile_containing(contents=contents) as fname: base_fname = os.path.split(fname)[1] if base_fname in lsftp.listdir(): lsftp.remove(base_fname) assert base_fname not in lsftp.listdir() lsftp.put(fname) assert base_fname in lsftp.listdir() with tempfile_containing('') as tfile: lsftp.get(base_fname, tfile) assert open(tfile).read() == contents # clean up lsftp.remove(base_fname)
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'
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)
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)
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'
def test_lexists(lsftp): '''test .lexists() functionality''' with tempfile_containing(contents='yup') as fname: base_fname = os.path.split(fname)[1] lsftp.put(fname) # rfile = '/home/test/readme.txt' rbad = '/home/test/peek-a-boo.txt' assert lsftp.lexists(fname) lsftp.remove(base_fname) assert lsftp.lexists(rbad) is False
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
def test_chown_none(lsftp): '''call .chown with no gid or uid specified''' with tempfile_containing('contents') as fname: base_fname = base_fname = os.path.split(fname)[1] org_attrs = lsftp.put(fname) lsftp.chown(base_fname) new_attrs = lsftp.stat(base_fname) lsftp.remove(base_fname) assert new_attrs.st_gid == org_attrs.st_gid assert new_attrs.st_uid == org_attrs.st_uid # confirm no change to uid
def test_chown_gid(lsftp): '''test changing just the gid''' with tempfile_containing('contents') as fname: base_fname = base_fname = os.path.split(fname)[1] org_attrs = lsftp.put(fname) gid = org_attrs.st_gid # - 1 lsftp.chown(base_fname, gid=gid) new_attrs = lsftp.stat(base_fname) lsftp.remove(base_fname) assert new_attrs.st_gid == gid assert new_attrs.st_uid == org_attrs.st_uid # confirm no change to uid
def test_symlink(lsftp): '''test symlink creation''' rdest = '/home/test/honey-boo-boo' with tempfile_containing(contents=8192 * '*') as fname: lsftp.put(fname) lsftp.symlink(fname, rdest) rslt = lsftp.lstat(rdest) is_link = S_ISLNK(rslt.st_mode) lsftp.remove(rdest) lsftp.remove(os.path.split(fname)[1]) assert is_link
def test_put_callback(lsftp): '''test the callback feature of put''' cback = Mock(return_value=None) with tempfile_containing(contents=4096 * '*') as fname: base_fname = os.path.split(fname)[1] lsftp.chdir('/home/test') lsftp.put(fname, callback=cback) # clean up lsftp.remove(base_fname) # verify callback was called more than once - usually a min of 2 assert cback.call_count >= 2
def test_put_callback(lsftp): '''test the callback feature of put''' cback = Mock(return_value=None) with tempfile_containing(contents=4096*'*') as fname: base_fname = os.path.split(fname)[1] lsftp.chdir('/home/test') lsftp.put(fname, callback=cback) # clean up lsftp.remove(base_fname) # verify callback was called more than once - usually a min of 2 assert cback.call_count >= 2
def test_remove(lsftp): '''test the remove method''' with tempfile_containing('*' * 8192) as fname: base_fname = os.path.split(fname)[1] lsftp.chdir('/home/test') lsftp.put(fname) is_there = base_fname in lsftp.listdir() lsftp.remove(base_fname) not_there = base_fname not in lsftp.listdir() assert is_there assert not_there
def test_unlink(lsftp): '''test the unlink function''' with tempfile_containing('*' * 8192) as fname: base_fname = os.path.split(fname)[1] lsftp.chdir('/home/test') lsftp.put(fname) is_there = base_fname in lsftp.listdir() lsftp.unlink(base_fname) not_there = base_fname not in lsftp.listdir() assert is_there assert not_there
def test_chmod_simple(lsftp): '''test basic chmod with octal mode represented by an int''' new_mode = 744 # user=rwx g=r o=r with tempfile_containing('') as fname: base_fname = os.path.split(fname)[1] org_attrs = lsftp.put(fname) lsftp.chmod(base_fname, new_mode) new_attrs = lsftp.stat(base_fname) lsftp.remove(base_fname) # that the new mod 744 is as we wanted assert pysftp.st_mode_to_int(new_attrs.st_mode) == new_mode # that we actually changed something assert new_attrs.st_mode != org_attrs.st_mode
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
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 assert cback.call_count # unlike .put() nothing is returned from the operation assert result is None
def test_put_confirm(lsftp): '''test the confirm feature of put''' with tempfile_containing(contents=8192*'*') as fname: base_fname = os.path.split(fname)[1] lsftp.chdir('/home/test') result = lsftp.put(fname) # clean up lsftp.remove(base_fname) # verify that an SFTPAttribute like os.stat was returned assert result.st_size == 8192 assert result.st_uid is not None assert result.st_gid is not None assert result.st_atime assert result.st_mtime
def test_rename(lsftp): '''test rename on remote''' contents = 'now is the time\nfor all good...' with tempfile_containing(contents=contents) as fname: base_fname = os.path.split(fname)[1] if base_fname in lsftp.listdir(): lsftp.remove(base_fname) assert base_fname not in lsftp.listdir() lsftp.put(fname) lsftp.rename(base_fname, 'bob') rdirs = lsftp.listdir() assert 'bob' in rdirs assert base_fname not in rdirs lsftp.remove('bob')
def test_put_confirm(lsftp): '''test the confirm feature of put''' with tempfile_containing(contents=8192 * '*') as fname: base_fname = os.path.split(fname)[1] lsftp.chdir('/home/test') result = lsftp.put(fname) # clean up lsftp.remove(base_fname) # verify that an SFTPAttribute like os.stat was returned assert result.st_size == 8192 assert result.st_uid is not None assert result.st_gid is not None assert result.st_atime assert result.st_mtime
def test_put_preserve_mtime(lsftp): '''test that m_time is preserved from local to remote, when put''' with tempfile_containing(contents=STARS8192) as fname: base_fname = os.path.split(fname)[1] base = os.stat(fname) # with pysftp.Connection(**SFTP_LOCAL) as sftp: result1 = lsftp.put(fname, preserve_mtime=True) sleep(2) result2 = lsftp.put(fname, preserve_mtime=True) # clean up lsftp.remove(base_fname) # see if times are modified # assert base.st_atime == result1.st_atime assert int(base.st_mtime) == result1.st_mtime # assert result1.st_atime == result2.st_atime assert int(result1.st_mtime) == result2.st_mtime