Skip to content

Commit

Permalink
Changes in module testing
Browse files Browse the repository at this point in the history
- Tests fixed to support python 3.x
- Added support for tox.ini
  • Loading branch information
imankulov committed Jan 4, 2013
1 parent 038a535 commit 88620d4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.pyc
.*.swp
/*egg-info
/MANIFEST
/.tox
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include tests.py
include README.rst
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
)
1 change: 1 addition & 0 deletions ssh_config.test
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Host *
PasswordAuthentication no
BatchMode yes
56 changes: 32 additions & 24 deletions tests.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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
Expand All @@ -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'
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tox]
envlist = py26, py27, py33

[testenv]
deps =
pytest
commands = py.test tests.py {posargs}

0 comments on commit 88620d4

Please sign in to comment.