Commit 6c493099 authored by Łukasz Nowak's avatar Łukasz Nowak

Mock required classes in test.

parent 7d480307
...@@ -2,6 +2,11 @@ import logging ...@@ -2,6 +2,11 @@ import logging
import slapos.format import slapos.format
import unittest import unittest
# for mocking
import grp
import os
import pwd
class FakeConfig: class FakeConfig:
pass pass
...@@ -18,18 +23,76 @@ def fakeCallAndRead(argument_list, raise_on_error=True): ...@@ -18,18 +23,76 @@ def fakeCallAndRead(argument_list, raise_on_error=True):
global call_and_read_list global call_and_read_list
call_and_read_list.append(argument_list) call_and_read_list.append(argument_list)
def raising_KeyError(name): class LoggableWrapper:
raise KeyError def __init__(self, logger, name):
self.__logger = logger
self.__name = name
def __call__(self, *args, **kwargs):
arg_list = [repr(x) for x in args] + [
'%s=%r' % (x, y) for x, y in kwargs.iteritems()]
self.__logger.debug('%s(%s)' % (self.__name, ', '.join(arg_list)))
def returning_True(name): class GrpMock:
@classmethod
def getgrnam(self, name):
if name == 'testuser':
return True return True
class FakeClass: raise KeyError
pass
class PwdMock:
@classmethod
def getpwnam(self, name):
if name == 'testuser':
class result:
pw_uid = 0
pw_gid = 0
return result
raise KeyError
class SlapformatMixin(unittest.TestCase): class SlapformatMixin(unittest.TestCase):
@classmethod
def raisingKeyError(self, *args, **kwargs):
raise KeyError
def patchPwd(self):
self.saved_pwd = dict()
for fake in vars(PwdMock):
self.saved_pwd[fake] = getattr(pwd, fake, None)
setattr(pwd, fake, getattr(PwdMock, fake))
def restorePwd(self):
for name, original_value in self.saved_pwd.items():
setattr(pwd, name, original_value)
del self.saved_pwd
def patchGrp(self):
self.saved_grp = dict()
for fake in vars(GrpMock):
self.saved_grp[fake] = getattr(grp, fake, None)
setattr(grp, fake, getattr(GrpMock, fake))
def restoreGrp(self):
for name, original_value in self.saved_grp.items():
setattr(grp, name, original_value)
del self.saved_grp
def patchOs(self, logger):
self.saved_os = dict()
for fake in ['mkdir', 'chown', 'chmod', 'makedirs']:
self.saved_os[fake] = getattr(os, fake, None)
f = LoggableWrapper(logger, fake)
setattr(os, fake, f)
def restoreOs(self):
for name, original_value in self.saved_os.items():
setattr(os, name, original_value)
del self.saved_os
def setUp(self): def setUp(self):
config = FakeConfig() config = FakeConfig()
config.dry_run = True config.dry_run = False
config.verbose = True config.verbose = True
logger = logging.getLogger('testcatch') logger = logging.getLogger('testcatch')
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
...@@ -37,14 +100,19 @@ class SlapformatMixin(unittest.TestCase): ...@@ -37,14 +100,19 @@ class SlapformatMixin(unittest.TestCase):
logger.addHandler(self.test_result) logger.addHandler(self.test_result)
config.logger = logger config.logger = logger
self.partition = slapos.format.Partition('partition', '/part_path', self.partition = slapos.format.Partition('partition', '/part_path',
slapos.format.User('root'), [], None) slapos.format.User('testuser'), [], None)
self.partition._os = slapos.format.OS(config)
global call_and_read_list global call_and_read_list
call_and_read_list = [] call_and_read_list = []
self.real_callAndRead = slapos.format.callAndRead self.real_callAndRead = slapos.format.callAndRead
slapos.format.callAndRead = fakeCallAndRead slapos.format.callAndRead = fakeCallAndRead
self.patchOs(logger)
self.patchGrp()
self.patchPwd()
def tearDown(self): def tearDown(self):
self.restoreOs()
self.restoreGrp()
self.restorePwd()
global call_and_read_list global call_and_read_list
call_and_read_list = [] call_and_read_list = []
slapos.format.callAndRead = self.real_callAndRead slapos.format.callAndRead = self.real_callAndRead
...@@ -85,48 +153,39 @@ class TestPartition(SlapformatMixin): ...@@ -85,48 +153,39 @@ class TestPartition(SlapformatMixin):
class TestUser(SlapformatMixin): class TestUser(SlapformatMixin):
def test_create(self): def test_create(self):
global call_and_read_list global call_and_read_list
user = slapos.format.User('testuser') user = slapos.format.User('doesnotexistsyet')
user._getpwnam = raising_KeyError user.setPath('/doesnotexistsyet')
grp = FakeClass()
grp.getgrnam = raising_KeyError
user._grp = grp
user.setPath('/testuser')
user.create() user.create()
self.assertEqual( self.assertEqual(
[ [
['groupadd', 'testuser'], ['groupadd', 'doesnotexistsyet'],
['useradd', '-d', '/testuser', '-g', 'testuser', '-s', '/bin/false', ['useradd', '-d', '/doesnotexistsyet', '-g', 'doesnotexistsyet', '-s',
'testuser'] '/bin/false', 'doesnotexistsyet']
], ],
call_and_read_list) call_and_read_list)
def test_create_additional_groups(self): def test_create_additional_groups(self):
global call_and_read_list global call_and_read_list
user = slapos.format.User('testuser', ['additionalgroup1', user = slapos.format.User('doesnotexistsyet', ['additionalgroup1',
'additionalgroup2']) 'additionalgroup2'])
user._getpwnam = raising_KeyError user.setPath('/doesnotexistsyet')
grp = FakeClass()
grp.getgrnam = raising_KeyError
user._grp = grp
user.setPath('/testuser')
user.create() user.create()
self.assertEqual( self.assertEqual(
[ [
['groupadd', 'testuser'], ['groupadd', 'doesnotexistsyet'],
['useradd', '-d', '/testuser', '-g', 'testuser', '-s', '/bin/false', ['useradd', '-d', '/doesnotexistsyet', '-g', 'doesnotexistsyet', '-s',
'-G', 'additionalgroup1,additionalgroup2', 'testuser'] '/bin/false', '-G', 'additionalgroup1,additionalgroup2',
'doesnotexistsyet']
], ],
call_and_read_list) call_and_read_list)
def test_create_group_exists(self): def test_create_group_exists(self):
pwd.getpwnam = self.raisingKeyError
global call_and_read_list global call_and_read_list
user = slapos.format.User('testuser') user = slapos.format.User('testuser')
user._getpwnam = raising_KeyError
grp = FakeClass()
grp.getgrnam = returning_True
user._grp = grp
user.setPath('/testuser') user.setPath('/testuser')
user.create() user.create()
...@@ -138,13 +197,10 @@ class TestUser(SlapformatMixin): ...@@ -138,13 +197,10 @@ class TestUser(SlapformatMixin):
call_and_read_list) call_and_read_list)
def test_create_user_exists_additional_groups(self): def test_create_user_exists_additional_groups(self):
grp.getgrnam = self.raisingKeyError
global call_and_read_list global call_and_read_list
user = slapos.format.User('testuser', ['additionalgroup1', user = slapos.format.User('testuser', ['additionalgroup1',
'additionalgroup2']) 'additionalgroup2'])
user._getpwnam = returning_True
grp = FakeClass()
grp.getgrnam = raising_KeyError
user._grp = grp
user.setPath('/testuser') user.setPath('/testuser')
user.create() user.create()
...@@ -157,12 +213,9 @@ class TestUser(SlapformatMixin): ...@@ -157,12 +213,9 @@ class TestUser(SlapformatMixin):
call_and_read_list) call_and_read_list)
def test_create_user_exists(self): def test_create_user_exists(self):
grp.getgrnam = self.raisingKeyError
global call_and_read_list global call_and_read_list
user = slapos.format.User('testuser') user = slapos.format.User('testuser')
user._getpwnam = returning_True
grp = FakeClass()
grp.getgrnam = raising_KeyError
user._grp = grp
user.setPath('/testuser') user.setPath('/testuser')
user.create() user.create()
...@@ -177,10 +230,6 @@ class TestUser(SlapformatMixin): ...@@ -177,10 +230,6 @@ class TestUser(SlapformatMixin):
def test_create_user_group_exists(self): def test_create_user_group_exists(self):
global call_and_read_list global call_and_read_list
user = slapos.format.User('testuser') user = slapos.format.User('testuser')
user._getpwnam = returning_True
grp = FakeClass()
grp.getgrnam = returning_True
user._grp = grp
user.setPath('/testuser') user.setPath('/testuser')
user.create() user.create()
...@@ -193,18 +242,10 @@ class TestUser(SlapformatMixin): ...@@ -193,18 +242,10 @@ class TestUser(SlapformatMixin):
def test_isAvailable(self): def test_isAvailable(self):
user = slapos.format.User('testuser') user = slapos.format.User('testuser')
user._getpwnam = returning_True
grp = FakeClass()
grp.getgrnam = returning_True
user._grp = grp
self.assertTrue(user.isAvailable()) self.assertTrue(user.isAvailable())
def test_isAvailable_notAvailable(self): def test_isAvailable_notAvailable(self):
user = slapos.format.User('testuser') user = slapos.format.User('doesnotexistsyet')
user._getpwnam = raising_KeyError
grp = FakeClass()
grp.getgrnam = raising_KeyError
user._grp = grp
self.assertFalse(user.isAvailable()) self.assertFalse(user.isAvailable())
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment