Skip to content

Commit

Permalink
Enable coverage and improve test coverage (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-bednar committed Apr 20, 2016
1 parent 4c8895e commit 3f86429
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 9 deletions.
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest>=2.8.7
pytest-cov
4 changes: 4 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def execute(self, bufsize=-1, timeout=None):
yield six.StringIO(), six.StringIO(out), six.StringIO(err)
self._rc = rc

def __init__(self, user, address):
super(FakeExecutor, self).__init__(user)
self.address = address

def session(self, timeout=None):
return FakeExecutor.Session(self, timeout)

Expand Down
15 changes: 15 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf8 -*-
import pytest
import netaddr
from rrmngmnt import common


def test_fqdn2ip_positive():
ip = common.fqdn2ip('github.org')
assert netaddr.valid_ipv4(ip)


def test_fqdn2ip_negative():
with pytest.raises(Exception) as ex_info:
common.fqdn2ip('github.or')
assert 'github.or' in str(ex_info.value)
73 changes: 73 additions & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# -*- coding: utf8 -*-
import pytest

from rrmngmnt import Host, User
from rrmngmnt.db import Database
from .common import FakeExecutor


host_executor = Host.executor


def teardown_module():
Host.executor = host_executor


def fake_cmd_data(cmd_to_data, files):
def executor(self, user=None, pkey=False):
e = FakeExecutor(user, self.ip)
e.cmd_to_data = cmd_to_data.copy()
e.files_content = files
return e
Host.executor = executor


class TestDb(object):
data = {
'which systemctl': (0, "", ""),
'systemctl list-unit-files | grep -o ^[^.][^.]*.service | '
'cut -d. -f1 | sort | uniq': (0, "postgresql\n", "",),
'systemctl restart postgresql.service': (0, '', ''),
'export PGPASSWORD=db_pass; psql -d db_name -U db_user '
'-h localhost -R __RECORD_SEPARATOR__ -t -A -c '
'"SELECT key, value FROM dist"': (
0,
"key1|value1__RECORD_SEPARATOR__key2|value2",
"",
),
'export PGPASSWORD=db_pass; psql -d db_name -U db_user -h localhost '
'-R __RECORD_SEPARATOR__ -t -A -c "SELECT * FROM table ERROR"': (
1, "", "Syntax Error"
),
}
files = {}

db_name = "db_name"
db_user = "db_user"
db_pass = "db_pass"

@classmethod
def setup_class(cls):
fake_cmd_data(cls.data, cls.files)

def get_db(self, ip='1.1.1.1'):
return Database(
Host(ip),
self.db_name,
User(self.db_user, self.db_pass),
)

def test_restart(self):
db = self.get_db()
db.restart()

def test_psql(self):
db = self.get_db()
res = db.psql("SELECT %s, %s FROM %s", "key", "value", "dist")
assert res == [['key1', 'value1'], ['key2', 'value2']]

def test_negative(self):
db = self.get_db()
with pytest.raises(Exception) as ex_info:
db.psql("SELECT * FROM table ERROR")
assert "Syntax Error" in str(ex_info.value)
37 changes: 32 additions & 5 deletions tests/test_filesystem.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf8 -*-
import pytest

from rrmngmnt import Host
from rrmngmnt import Host, User
from rrmngmnt import errors
from .common import FakeExecutor

Expand All @@ -14,8 +14,8 @@ def teardown_module():


def fake_cmd_data(cmd_to_data, files):
def executor(self, user=None, pkey=False):
e = FakeExecutor(user)
def executor(self, user=User('fakeuser', 'password'), pkey=False):
e = FakeExecutor(user, self.ip)
e.cmd_to_data = cmd_to_data.copy()
e.files_content = files
return e
Expand All @@ -40,7 +40,14 @@ class TestFilesystem(object):
'mkdir /dir/to/remove': (0, '', ''),
'chown root:root /dir/to/remove': (0, '', ''),
'chmod 600 /dir/to/remove': (0, '', ''),
'chmod 600 /tmp/nofile': (1, '', ''),
'chmod 600 /tmp/nofile': (
1, '',
'chmod: cannot access ‘/tmp/nofile’: No such file or directory',
),
'touch /path/to/file': (0, '', ''),
'touch /path/to/nopermission': (1, '', ''),
'ls -A1 /path/to/empty': (0, '\n', ''),
'ls -A1 /path/to/two': (0, 'first\nsecond\n', ''),
}
files = {}

Expand Down Expand Up @@ -101,5 +108,25 @@ def test_chmod_positive(self):
self.get_host().fs.chmod('/dir/to/remove', '600')

def test_chmod_negative(self):
with pytest.raises(errors.CommandExecutionFailure):
with pytest.raises(errors.CommandExecutionFailure) as ex_info:
self.get_host().fs.chmod('/tmp/nofile', '600')
assert "No such file or directory" in str(ex_info.value)

def test_touch_positive(self):
assert self.get_host().fs.touch('/path/to/file', '')

def test_touch_negative(self):
assert not self.get_host().fs.touch('/path/to/nopermission', '')

def test_touch_wrong_params(self):
with pytest.raises(Exception) as ex_info:
self.get_host().fs.touch('/path/to', 'somefile')
assert "touch /path/to" in str(ex_info.value)

def test_listdir_empty(self):
assert self.get_host().fs.listdir('/path/to/empty') == []

def test_listdir_two(self):
assert self.get_host().fs.listdir('/path/to/two') == [
'first', 'second',
]
2 changes: 1 addition & 1 deletion tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def teardown_module():

def fake_cmd_data(cmd_to_data, files):
def executor(self, user=None, pkey=False):
e = FakeExecutor(user)
e = FakeExecutor(user, self.ip)
e.cmd_to_data = cmd_to_data.copy()
e.files = files
return e
Expand Down
2 changes: 1 addition & 1 deletion tests/test_power_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def teardown_module():

def fake_cmd_data(cmd_to_data):
def executor(self, user=None, pkey=False):
e = FakeExecutor(user)
e = FakeExecutor(user, self.ip)
e.cmd_to_data = cmd_to_data.copy()
return e
Host.executor = executor
Expand Down
2 changes: 1 addition & 1 deletion tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def teardown_module():

def fake_cmd_data(cmd_to_data):
def executor(self, user=None, pkey=False):
e = FakeExecutor(user)
e = FakeExecutor(user, self.ip)
e.cmd_to_data = cmd_to_data.copy()
return e
Host.executor = executor
Expand Down
18 changes: 18 additions & 0 deletions tests/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf8 -*-
from rrmngmnt import User, InternalDomain, ADUser, Domain


def test_user():
assert "user" == User("user", 'pass').full_name


def test_indernal_domain():
assert "user@internal" == ADUser(
"user", "pass", InternalDomain(),
).full_name


def test_domain():
assert "[email protected]" == ADUser(
"user", "pass", Domain("example.com"),
).full_name
7 changes: 6 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ envlist = py26,py27,py34,pep8
[testenv]
deps = -r{toxinidir}/test-requirements.txt
-r{toxinidir}/requirements.txt
commands=py.test --basetemp={envtmpdir} {posargs} tests
commands=py.test \
--basetemp={envtmpdir} \
--cov rrmngmnt \
--cov-report term \
--cov-report html \
{posargs} tests
[testenv:pep8]
deps=flake8
commands=flake8 rrmngmnt tests

0 comments on commit 3f86429

Please sign in to comment.