From 88620d46ef7998a21ac2250bc685744c17b8054c Mon Sep 17 00:00:00 2001 From: Roman Imankulov Date: Fri, 4 Jan 2013 14:43:33 +0300 Subject: [PATCH] Changes in module testing - Tests fixed to support python 3.x - Added support for tox.ini --- .gitignore | 2 ++ MANIFEST.in | 2 ++ setup.py | 4 +++- ssh_config.test | 1 + tests.py | 56 ++++++++++++++++++++++++++++--------------------- tox.ini | 7 +++++++ 6 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 MANIFEST.in create mode 100644 tox.ini diff --git a/.gitignore b/.gitignore index 3ffbb60..96122a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.pyc .*.swp /*egg-info +/MANIFEST +/.tox diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..7e106dc --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include tests.py +include README.rst diff --git a/setup.py b/setup.py index 1c1d446..a2c7127 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,9 @@ def read(fname): 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', - 'Programming Language :: Python', 'Topic :: Software Development :: Libraries :: Python Modules', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.3', ), ) diff --git a/ssh_config.test b/ssh_config.test index 3f2e593..13bb3e8 100644 --- a/ssh_config.test +++ b/ssh_config.test @@ -1,2 +1,3 @@ Host * PasswordAuthentication no + BatchMode yes diff --git a/tests.py b/tests.py index 1e774d6..b27d02d 100644 --- a/tests.py +++ b/tests.py @@ -1,27 +1,35 @@ # -*- coding: utf-8 -*- +import io import os +import pytest from openssh_wrapper import * -from nose.tools import * + test_file = os.path.join(os.path.dirname(__file__), 'tests.py') + +def eq_(arg1, arg2): + assert arg1 == arg2 + + class TestSSHCommandNames(object): - def setUp(self): + def setup_method(self, meth): self.c = SSHConnection('localhost', login='root', configfile='ssh_config.test') def test_ssh_command(self): eq_(self.c.ssh_command('/bin/bash', False), - ['/usr/bin/ssh', '-l', 'root', '-F', 'ssh_config.test', 'localhost', '/bin/bash']) + b_list(['/usr/bin/ssh', '-l', 'root', '-F', 'ssh_config.test', 'localhost', '/bin/bash'])) def test_scp_command(self): eq_(self.c.scp_command(('/tmp/1.txt', ), target='/tmp/2.txt'), - ['/usr/bin/scp', '-q', '-r', '-F', 'ssh_config.test', '/tmp/1.txt', 'root@localhost:/tmp/2.txt']) + b_list(['/usr/bin/scp', '-q', '-r', '-F', 'ssh_config.test', '/tmp/1.txt', 'root@localhost:/tmp/2.txt'])) def test_scp_multiple_files(self): eq_(self.c.scp_command(('/tmp/1.txt', '2.txt'), target='/home/username/'), - ['/usr/bin/scp', '-q', '-r', '-F', 'ssh_config.test', '/tmp/1.txt', '2.txt', 'root@localhost:/home/username/']) + b_list(['/usr/bin/scp', '-q', '-r', '-F', 'ssh_config.test', '/tmp/1.txt', '2.txt', + 'root@localhost:/home/username/'])) def test_scp_targets(self): targets = self.c.get_scp_targets(['foo.txt', 'bar.txt'], '/etc') @@ -31,46 +39,46 @@ def test_scp_targets(self): def test_simple_command(self): result = self.c.run('whoami') - eq_(result.stdout, 'root') - eq_(result.stderr, '') + eq_(result.stdout, b('root')) + eq_(result.stderr, b('')) eq_(result.returncode, 0) def test_python_command(self): result = self.c.run('print "Hello world"', interpreter='/usr/bin/python') - eq_(result.stdout, 'Hello world') - eq_(result.stderr, '') + eq_(result.stdout, b('Hello world')) + eq_(result.stderr, b('')) eq_(result.returncode, 0) -@raises(SSHError) # ssh connect timeout def test_timeout(): c = SSHConnection('example.com', login='root', timeout=1) - c.run('whoami') + with pytest.raises(SSHError): # ssh connect timeout + c.run('whoami') -@raises(SSHError) # Permission denied (publickey) def test_permission_denied(): c = SSHConnection('localhost', login='www-data', configfile='ssh_config.test') - c.run('whoami') + with pytest.raises(SSHError): # Permission denied (publickey) + c.run('whoami') class TestSCP(object): - def setUp(self): + def setup_method(self, meth): self.c = SSHConnection('localhost', login='root') self.c.run('rm -f /tmp/*.py /tmp/test*.txt') def test_scp(self): self.c.scp((test_file, ), target='/tmp') - ok_(os.path.isfile('/tmp/tests.py')) + assert os.path.isfile('/tmp/tests.py') - @raises(SSHError) def test_scp_to_nonexistent_dir(self): - self.c.scp((test_file, ), target='/abc/def/') + with pytest.raises(SSHError): + self.c.scp((test_file, ), target='/abc/def/') def test_mode(self): self.c.scp((test_file, ), target='/tmp', mode='0666') - mode = os.stat('/tmp/tests.py').st_mode & 0777 - eq_(mode, 0666) + mode = os.stat('/tmp/tests.py').st_mode & 0o777 + eq_(mode, 0o666) def test_owner(self): import pwd, grp @@ -82,13 +90,13 @@ def test_owner(self): eq_(stat.st_gid, gid) def test_file_descriptors(self): - from StringIO import StringIO # name is set explicitly as target - fd1 = StringIO('test') + fd1 = io.BytesIO(b('test')) self.c.scp((fd1, ), target='/tmp/test1.txt', mode='0644') - eq_(open('/tmp/test1.txt').read(), 'test') + assert io.open('/tmp/test1.txt', 'rt').read() == 'test' + # name is set explicitly in the name option - fd2 = StringIO('test') + fd2 = io.BytesIO(b('test')) fd2.name = 'test2.txt' self.c.scp((fd2, ), target='/tmp', mode='0644') - eq_(open('/tmp/test2.txt').read(), 'test') + assert io.open('/tmp/test2.txt', 'rt').read() == 'test' diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..382ba77 --- /dev/null +++ b/tox.ini @@ -0,0 +1,7 @@ +[tox] +envlist = py26, py27, py33 + +[testenv] +deps = + pytest +commands = py.test tests.py {posargs}