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

Use more of "persistence".

Make test more flexible and controlallbe by exposing groups.
parent 7386bf0c
...@@ -8,6 +8,7 @@ import os ...@@ -8,6 +8,7 @@ import os
import pwd import pwd
USER_LIST = [] USER_LIST = []
GROUP_LIST = []
class FakeConfig: class FakeConfig:
pass pass
...@@ -28,6 +29,9 @@ class FakeCallAndRead: ...@@ -28,6 +29,9 @@ class FakeCallAndRead:
if 'useradd' in argument_list: if 'useradd' in argument_list:
global USER_LIST global USER_LIST
USER_LIST.append(argument_list[-1]) USER_LIST.append(argument_list[-1])
if 'groupadd' in argument_list:
global GROUP_LIST
GROUP_LIST.append(argument_list[-1])
self.external_command_list.append(argument_list) self.external_command_list.append(argument_list)
return 0, 'UP' return 0, 'UP'
...@@ -44,7 +48,8 @@ class LoggableWrapper: ...@@ -44,7 +48,8 @@ class LoggableWrapper:
class GrpMock: class GrpMock:
@classmethod @classmethod
def getgrnam(self, name): def getgrnam(self, name):
if name == 'testuser': global GROUP_LIST
if name in GROUP_LIST:
return True return True
raise KeyError raise KeyError
...@@ -60,11 +65,6 @@ class PwdMock: ...@@ -60,11 +65,6 @@ class PwdMock:
raise KeyError raise KeyError
class SlapformatMixin(unittest.TestCase): class SlapformatMixin(unittest.TestCase):
@classmethod
def raisingKeyError(self, *args, **kwargs):
raise KeyError
def patchPwd(self): def patchPwd(self):
self.saved_pwd = dict() self.saved_pwd = dict()
for fake in vars(PwdMock): for fake in vars(PwdMock):
...@@ -111,7 +111,9 @@ class SlapformatMixin(unittest.TestCase): ...@@ -111,7 +111,9 @@ class SlapformatMixin(unittest.TestCase):
self.partition = slapos.format.Partition('partition', '/part_path', self.partition = slapos.format.Partition('partition', '/part_path',
slapos.format.User('testuser'), [], None) slapos.format.User('testuser'), [], None)
global USER_LIST global USER_LIST
USER_LIST = ['testuser'] USER_LIST = []
global GROUP_LIST
GROUP_LIST = []
self.real_callAndRead = slapos.format.callAndRead self.real_callAndRead = slapos.format.callAndRead
self.fakeCallAndRead = FakeCallAndRead() self.fakeCallAndRead = FakeCallAndRead()
...@@ -124,8 +126,6 @@ class SlapformatMixin(unittest.TestCase): ...@@ -124,8 +126,6 @@ class SlapformatMixin(unittest.TestCase):
self.restoreOs() self.restoreOs()
self.restoreGrp() self.restoreGrp()
self.restorePwd() self.restorePwd()
global USER_LIST
USER_LIST = ['testuser']
slapos.format.callAndRead = self.real_callAndRead slapos.format.callAndRead = self.real_callAndRead
class TestComputer(SlapformatMixin): class TestComputer(SlapformatMixin):
...@@ -210,6 +210,8 @@ class TestComputer(SlapformatMixin): ...@@ -210,6 +210,8 @@ class TestComputer(SlapformatMixin):
class TestPartition(SlapformatMixin): class TestPartition(SlapformatMixin):
def test_createPath(self): def test_createPath(self):
global USER_LIST
USER_LIST = ['testuser']
self.partition.createPath() self.partition.createPath()
self.assertEqual( self.assertEqual(
[ [
...@@ -260,8 +262,8 @@ class TestUser(SlapformatMixin): ...@@ -260,8 +262,8 @@ class TestUser(SlapformatMixin):
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
def test_create_group_exists(self): def test_create_group_exists(self):
pwd.getpwnam = self.raisingKeyError global GROUP_LIST
GROUP_LIST = ['testuser']
user = slapos.format.User('testuser') user = slapos.format.User('testuser')
user.setPath('/testuser') user.setPath('/testuser')
user.create() user.create()
...@@ -274,7 +276,8 @@ class TestUser(SlapformatMixin): ...@@ -274,7 +276,8 @@ class TestUser(SlapformatMixin):
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
def test_create_user_exists_additional_groups(self): def test_create_user_exists_additional_groups(self):
grp.getgrnam = self.raisingKeyError global USER_LIST
USER_LIST = ['testuser']
user = slapos.format.User('testuser', ['additionalgroup1', user = slapos.format.User('testuser', ['additionalgroup1',
'additionalgroup2']) 'additionalgroup2'])
user.setPath('/testuser') user.setPath('/testuser')
...@@ -289,7 +292,8 @@ class TestUser(SlapformatMixin): ...@@ -289,7 +292,8 @@ class TestUser(SlapformatMixin):
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
def test_create_user_exists(self): def test_create_user_exists(self):
grp.getgrnam = self.raisingKeyError global USER_LIST
USER_LIST = ['testuser']
user = slapos.format.User('testuser') user = slapos.format.User('testuser')
user.setPath('/testuser') user.setPath('/testuser')
user.create() user.create()
...@@ -303,6 +307,10 @@ class TestUser(SlapformatMixin): ...@@ -303,6 +307,10 @@ class TestUser(SlapformatMixin):
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
def test_create_user_group_exists(self): def test_create_user_group_exists(self):
global USER_LIST
USER_LIST = ['testuser']
global GROUP_LIST
GROUP_LIST = ['testuser']
user = slapos.format.User('testuser') user = slapos.format.User('testuser')
user.setPath('/testuser') user.setPath('/testuser')
user.create() user.create()
...@@ -315,6 +323,8 @@ class TestUser(SlapformatMixin): ...@@ -315,6 +323,8 @@ class TestUser(SlapformatMixin):
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
def test_isAvailable(self): def test_isAvailable(self):
global USER_LIST
USER_LIST = ['testuser']
user = slapos.format.User('testuser') user = slapos.format.User('testuser')
self.assertTrue(user.isAvailable()) self.assertTrue(user.isAvailable())
......
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