Fixes for test_sftp

This commit is contained in:
Scott Maxwell 2013-10-31 17:20:27 -07:00
parent 8e1a7ef4d8
commit 0677ea76cd
1 changed files with 38 additions and 34 deletions

View File

@ -31,6 +31,7 @@ import warnings
import sys import sys
import threading import threading
import unittest import unittest
from tempfile import mkstemp
import paramiko import paramiko
from paramiko.common import * from paramiko.common import *
@ -234,7 +235,7 @@ class SFTPTest (unittest.TestCase):
sftp.rename(FOLDER + '/first.txt', FOLDER + '/second.txt') sftp.rename(FOLDER + '/first.txt', FOLDER + '/second.txt')
try: try:
f = sftp.open(FOLDER + '/first.txt', 'r') f = sftp.open(FOLDER + '/first.txt', 'r')
self.assert_(False, 'no exception on reading nonexistent file') self.assertTrue(False, 'no exception on reading nonexistent file')
except IOError: except IOError:
pass pass
f = sftp.open(FOLDER + '/second.txt', 'r') f = sftp.open(FOLDER + '/second.txt', 'r')
@ -264,7 +265,7 @@ class SFTPTest (unittest.TestCase):
try: try:
f = sftp.open(FOLDER + '/subfolder/test') f = sftp.open(FOLDER + '/subfolder/test')
# shouldn't be able to create that file # shouldn't be able to create that file
self.assert_(False, 'no exception at dummy file creation') self.assertTrue(False, 'no exception at dummy file creation')
except IOError: except IOError:
pass pass
@ -285,10 +286,10 @@ class SFTPTest (unittest.TestCase):
x = sftp.listdir(FOLDER) x = sftp.listdir(FOLDER)
self.assertEqual(len(x), 3) self.assertEqual(len(x), 3)
self.assert_('duck.txt' in x) self.assertTrue('duck.txt' in x)
self.assert_('fish.txt' in x) self.assertTrue('fish.txt' in x)
self.assert_('tertiary.py' in x) self.assertTrue('tertiary.py' in x)
self.assert_('random' not in x) self.assertTrue('random' not in x)
finally: finally:
sftp.remove(FOLDER + '/duck.txt') sftp.remove(FOLDER + '/duck.txt')
sftp.remove(FOLDER + '/fish.txt') sftp.remove(FOLDER + '/fish.txt')
@ -454,7 +455,7 @@ class SFTPTest (unittest.TestCase):
self.assertEqual(sftp.stat(FOLDER + '/link.txt').st_size, 9) self.assertEqual(sftp.stat(FOLDER + '/link.txt').st_size, 9)
# the sftp server may be hiding extra path members from us, so the # the sftp server may be hiding extra path members from us, so the
# length may be longer than we expect: # length may be longer than we expect:
self.assert_(sftp.lstat(FOLDER + '/link2.txt').st_size >= len(abs_path)) self.assertTrue(sftp.lstat(FOLDER + '/link2.txt').st_size >= len(abs_path))
self.assertEqual(sftp.stat(FOLDER + '/link2.txt').st_size, 9) self.assertEqual(sftp.stat(FOLDER + '/link2.txt').st_size, 9)
self.assertEqual(sftp.stat(FOLDER + '/original.txt').st_size, 9) self.assertEqual(sftp.stat(FOLDER + '/original.txt').st_size, 9)
finally: finally:
@ -499,10 +500,10 @@ class SFTPTest (unittest.TestCase):
error. error.
""" """
pwd = sftp.normalize('.') pwd = sftp.normalize('.')
self.assert_(len(pwd) > 0) self.assertTrue(len(pwd) > 0)
f = sftp.normalize('./' + FOLDER) f = sftp.normalize('./' + FOLDER)
self.assert_(len(f) > 0) self.assertTrue(len(f) > 0)
self.assertEquals(os.path.join(pwd, FOLDER), f) self.assertEqual(os.path.join(pwd, FOLDER), f)
def test_F_mkdir(self): def test_F_mkdir(self):
""" """
@ -511,19 +512,19 @@ class SFTPTest (unittest.TestCase):
try: try:
sftp.mkdir(FOLDER + '/subfolder') sftp.mkdir(FOLDER + '/subfolder')
except: except:
self.assert_(False, 'exception creating subfolder') self.assertTrue(False, 'exception creating subfolder')
try: try:
sftp.mkdir(FOLDER + '/subfolder') sftp.mkdir(FOLDER + '/subfolder')
self.assert_(False, 'no exception overwriting subfolder') self.assertTrue(False, 'no exception overwriting subfolder')
except IOError: except IOError:
pass pass
try: try:
sftp.rmdir(FOLDER + '/subfolder') sftp.rmdir(FOLDER + '/subfolder')
except: except:
self.assert_(False, 'exception removing subfolder') self.assertTrue(False, 'exception removing subfolder')
try: try:
sftp.rmdir(FOLDER + '/subfolder') sftp.rmdir(FOLDER + '/subfolder')
self.assert_(False, 'no exception removing nonexistent subfolder') self.assertTrue(False, 'no exception removing nonexistent subfolder')
except IOError: except IOError:
pass pass
@ -538,17 +539,17 @@ class SFTPTest (unittest.TestCase):
sftp.mkdir(FOLDER + '/alpha') sftp.mkdir(FOLDER + '/alpha')
sftp.chdir(FOLDER + '/alpha') sftp.chdir(FOLDER + '/alpha')
sftp.mkdir('beta') sftp.mkdir('beta')
self.assertEquals(root + FOLDER + '/alpha', sftp.getcwd()) self.assertEqual(root + FOLDER + '/alpha', sftp.getcwd())
self.assertEquals(['beta'], sftp.listdir('.')) self.assertEqual(['beta'], sftp.listdir('.'))
sftp.chdir('beta') sftp.chdir('beta')
f = sftp.open('fish', 'w') f = sftp.open('fish', 'w')
f.write('hello\n') f.write('hello\n')
f.close() f.close()
sftp.chdir('..') sftp.chdir('..')
self.assertEquals(['fish'], sftp.listdir('beta')) self.assertEqual(['fish'], sftp.listdir('beta'))
sftp.chdir('..') sftp.chdir('..')
self.assertEquals(['fish'], sftp.listdir('alpha/beta')) self.assertEqual(['fish'], sftp.listdir('alpha/beta'))
finally: finally:
sftp.chdir(root) sftp.chdir(root)
try: try:
@ -570,8 +571,9 @@ class SFTPTest (unittest.TestCase):
""" """
warnings.filterwarnings('ignore', 'tempnam.*') warnings.filterwarnings('ignore', 'tempnam.*')
localname = os.tempnam() fd, localname = mkstemp()
text = 'All I wanted was a plastic bunny rabbit.\n' os.close(fd)
text = b('All I wanted was a plastic bunny rabbit.\n')
f = open(localname, 'wb') f = open(localname, 'wb')
f.write(text) f.write(text)
f.close() f.close()
@ -581,19 +583,20 @@ class SFTPTest (unittest.TestCase):
sftp.put(localname, FOLDER + '/bunny.txt', progress_callback) sftp.put(localname, FOLDER + '/bunny.txt', progress_callback)
f = sftp.open(FOLDER + '/bunny.txt', 'r') f = sftp.open(FOLDER + '/bunny.txt', 'r')
self.assertEquals(text, f.read(128)) self.assertEqual(text, f.read(128))
f.close() f.close()
self.assertEquals((41, 41), saved_progress[-1]) self.assertEqual((41, 41), saved_progress[-1])
os.unlink(localname) os.unlink(localname)
localname = os.tempnam() fd, localname = mkstemp()
os.close(fd)
saved_progress = [] saved_progress = []
sftp.get(FOLDER + '/bunny.txt', localname, progress_callback) sftp.get(FOLDER + '/bunny.txt', localname, progress_callback)
f = open(localname, 'rb') f = open(localname, 'rb')
self.assertEquals(text, f.read(128)) self.assertEqual(text, f.read(128))
f.close() f.close()
self.assertEquals((41, 41), saved_progress[-1]) self.assertEqual((41, 41), saved_progress[-1])
os.unlink(localname) os.unlink(localname)
sftp.unlink(FOLDER + '/bunny.txt') sftp.unlink(FOLDER + '/bunny.txt')
@ -611,11 +614,11 @@ class SFTPTest (unittest.TestCase):
try: try:
f = sftp.open(FOLDER + '/kitty.txt', 'r') f = sftp.open(FOLDER + '/kitty.txt', 'r')
sum = f.check('sha1') sum = f.check('sha1')
self.assertEquals('91059CFC6615941378D413CB5ADAF4C5EB293402', hexlify(sum).upper()) self.assertEqual('91059CFC6615941378D413CB5ADAF4C5EB293402', hexlify(sum).upper())
sum = f.check('md5', 0, 512) sum = f.check('md5', 0, 512)
self.assertEquals('93DE4788FCA28D471516963A1FE3856A', hexlify(sum).upper()) self.assertEqual('93DE4788FCA28D471516963A1FE3856A', hexlify(sum).upper())
sum = f.check('md5', 0, 0, 510) sum = f.check('md5', 0, 0, 510)
self.assertEquals('EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6', self.assertEqual('EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6',
hexlify(sum).upper()) hexlify(sum).upper())
f.close() f.close()
finally: finally:
@ -650,7 +653,7 @@ class SFTPTest (unittest.TestCase):
sftp.open(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65', 'r') sftp.open(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65', 'r')
except Exception: except Exception:
e = sys.exc_info()[1] e = sys.exc_info()[1]
self.fail('exception ' + e) self.fail('exception ' + str(e))
sftp.unlink(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65') sftp.unlink(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65')
def test_L_utf8_chdir(self): def test_L_utf8_chdir(self):
@ -689,8 +692,9 @@ class SFTPTest (unittest.TestCase):
""" """
warnings.filterwarnings('ignore', 'tempnam.*') warnings.filterwarnings('ignore', 'tempnam.*')
localname = os.tempnam() fd, localname = mkstemp()
text = 'All I wanted was a plastic bunny rabbit.\n' os.close(fd)
text = b('All I wanted was a plastic bunny rabbit.\n')
f = open(localname, 'wb') f = open(localname, 'wb')
f.write(text) f.write(text)
f.close() f.close()
@ -699,12 +703,12 @@ class SFTPTest (unittest.TestCase):
saved_progress.append((x, y)) saved_progress.append((x, y))
res = sftp.put(localname, FOLDER + '/bunny.txt', progress_callback, False) res = sftp.put(localname, FOLDER + '/bunny.txt', progress_callback, False)
self.assertEquals(SFTPAttributes().attr, res.attr) self.assertEqual(SFTPAttributes().attr, res.attr)
f = sftp.open(FOLDER + '/bunny.txt', 'r') f = sftp.open(FOLDER + '/bunny.txt', 'r')
self.assertEquals(text, f.read(128)) self.assertEqual(text, f.read(128))
f.close() f.close()
self.assertEquals((41, 41), saved_progress[-1]) self.assertEqual((41, 41), saved_progress[-1])
os.unlink(localname) os.unlink(localname)
sftp.unlink(FOLDER + '/bunny.txt') sftp.unlink(FOLDER + '/bunny.txt')