2012-06-30 16:43:44 +00:00
|
|
|
'''
|
|
|
|
tests for user state
|
|
|
|
user absent
|
|
|
|
user present
|
|
|
|
user present with custom homedir
|
|
|
|
'''
|
2013-06-27 12:56:39 +00:00
|
|
|
|
|
|
|
# Import python libs
|
2012-06-30 23:51:11 +00:00
|
|
|
import os
|
2012-07-30 19:15:35 +00:00
|
|
|
import grp
|
2013-06-24 22:53:59 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2013-06-24 19:06:49 +00:00
|
|
|
from salttesting import skipIf
|
2013-09-04 19:05:41 +00:00
|
|
|
from salttesting.helpers import (
|
|
|
|
destructiveTest,
|
|
|
|
ensure_in_syspath,
|
|
|
|
requires_system_grains
|
|
|
|
)
|
2013-06-27 12:56:39 +00:00
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
import integration
|
2012-06-30 16:43:44 +00:00
|
|
|
|
|
|
|
|
2012-11-21 12:19:18 +00:00
|
|
|
class UserTest(integration.ModuleCase,
|
|
|
|
integration.SaltReturnAssertsMixIn):
|
2012-06-30 16:43:44 +00:00
|
|
|
'''
|
|
|
|
test for user absent
|
|
|
|
'''
|
|
|
|
def test_user_absent(self):
|
|
|
|
ret = self.run_state('user.absent', name='unpossible')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-06-30 16:43:44 +00:00
|
|
|
|
2012-06-30 23:51:11 +00:00
|
|
|
def test_user_if_present(self):
|
2012-07-20 06:21:01 +00:00
|
|
|
ret = self.run_state('user.present', name='nobody')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-06-30 16:43:44 +00:00
|
|
|
|
2013-03-12 07:22:52 +00:00
|
|
|
def test_user_if_present_with_gid(self):
|
|
|
|
if self.run_function('group.info', ['nobody']):
|
|
|
|
ret = self.run_state('user.present', name='nobody', gid='nobody')
|
|
|
|
elif self.run_function('group.info', ['nogroup']):
|
|
|
|
ret = self.run_state('user.present', name='nobody', gid='nogroup')
|
|
|
|
else:
|
2013-06-27 12:56:39 +00:00
|
|
|
self.skipTest(
|
|
|
|
'Neither \'nobody\' nor \'nogroup\' are valid groups'
|
|
|
|
)
|
2013-03-12 07:22:52 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
2012-09-29 22:16:08 +00:00
|
|
|
@destructiveTest
|
2013-04-30 17:32:21 +00:00
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
2012-06-30 23:51:11 +00:00
|
|
|
def test_user_not_present(self):
|
2013-03-12 06:00:30 +00:00
|
|
|
'''
|
2012-06-30 23:51:11 +00:00
|
|
|
This is a DESTRUCTIVE TEST it creates a new user on the minion.
|
2012-07-01 00:28:48 +00:00
|
|
|
And then destroys that user.
|
2012-06-30 23:51:11 +00:00
|
|
|
Assume that it will break any system you run it on.
|
2013-03-12 06:00:30 +00:00
|
|
|
'''
|
2012-07-20 06:21:01 +00:00
|
|
|
ret = self.run_state('user.present', name='salt_test')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-07-01 00:28:48 +00:00
|
|
|
ret = self.run_state('user.absent', name='salt_test')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-06-30 16:43:44 +00:00
|
|
|
|
2012-09-29 22:16:08 +00:00
|
|
|
@destructiveTest
|
2013-04-30 17:32:21 +00:00
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
2012-06-30 23:51:11 +00:00
|
|
|
def test_user_present_nondefault(self):
|
2013-03-12 06:00:30 +00:00
|
|
|
'''
|
2012-06-30 23:51:11 +00:00
|
|
|
This is a DESTRUCTIVE TEST it creates a new user on the on the minion.
|
2013-03-12 06:00:30 +00:00
|
|
|
'''
|
2012-06-30 23:51:11 +00:00
|
|
|
ret = self.run_state('user.present', name='salt_test',
|
2012-07-20 06:21:01 +00:00
|
|
|
home='/var/lib/salt_test')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isdir('/var/lib/salt_test'))
|
2012-07-01 00:28:48 +00:00
|
|
|
ret = self.run_state('user.absent', name='salt_test')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-07-01 00:28:48 +00:00
|
|
|
|
2012-09-29 22:16:08 +00:00
|
|
|
@destructiveTest
|
2013-04-30 17:32:21 +00:00
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
2013-09-04 19:05:41 +00:00
|
|
|
@requires_system_grains
|
|
|
|
def test_user_present_gid_from_name_default(self, grains=None):
|
2013-03-12 06:00:30 +00:00
|
|
|
'''
|
2012-11-25 19:20:57 +00:00
|
|
|
This is a DESTRUCTIVE TEST. It creates a new user on the on the minion.
|
2012-07-30 19:15:35 +00:00
|
|
|
This is an integration test. Not all systems will automatically create
|
|
|
|
a group of the same name as the user, but I don't have access to any.
|
|
|
|
If you run the test and it fails, please fix the code it's testing to
|
|
|
|
work on your operating system.
|
2013-03-12 06:00:30 +00:00
|
|
|
'''
|
2012-07-30 19:15:35 +00:00
|
|
|
ret = self.run_state('user.present', name='salt_test',
|
|
|
|
gid_from_name=True, home='/var/lib/salt_test')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-11-25 19:20:57 +00:00
|
|
|
|
|
|
|
ret = self.run_function('user.info', ['salt_test'])
|
2012-11-25 22:29:08 +00:00
|
|
|
self.assertReturnNonEmptySaltType(ret)
|
2012-11-25 19:20:57 +00:00
|
|
|
group_name = grp.getgrgid(ret['gid']).gr_name
|
|
|
|
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertTrue(os.path.isdir('/var/lib/salt_test'))
|
2013-09-04 18:41:20 +00:00
|
|
|
if grains['os_family'] in ('Suse',):
|
|
|
|
self.assertEqual(group_name, 'users')
|
|
|
|
else:
|
|
|
|
self.assertEqual(group_name, 'salt_test')
|
2012-11-25 19:20:57 +00:00
|
|
|
|
2012-07-30 19:15:35 +00:00
|
|
|
ret = self.run_state('user.absent', name='salt_test')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-07-30 19:15:35 +00:00
|
|
|
|
2012-09-29 22:16:08 +00:00
|
|
|
@destructiveTest
|
2013-04-30 17:32:21 +00:00
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
2012-07-30 19:15:35 +00:00
|
|
|
def test_user_present_gid_from_name(self):
|
2013-03-12 06:00:30 +00:00
|
|
|
'''
|
2012-07-30 19:15:35 +00:00
|
|
|
This is a DESTRUCTIVE TEST it creates a new user on the on the minion.
|
|
|
|
This is a unit test, NOT an integration test. We create a group of the
|
|
|
|
same name as the user beforehand, so it should all run smoothly.
|
2013-03-12 06:00:30 +00:00
|
|
|
'''
|
2012-07-30 19:15:35 +00:00
|
|
|
ret = self.run_state('group.present', name='salt_test')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-07-30 19:15:35 +00:00
|
|
|
ret = self.run_state('user.present', name='salt_test',
|
|
|
|
gid_from_name=True, home='/var/lib/salt_test')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
2012-11-25 19:20:57 +00:00
|
|
|
ret = self.run_function('user.info', ['salt_test'])
|
2012-11-25 22:29:08 +00:00
|
|
|
self.assertReturnNonEmptySaltType(ret)
|
2012-11-25 19:20:57 +00:00
|
|
|
group_name = grp.getgrgid(ret['gid']).gr_name
|
|
|
|
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertTrue(os.path.isdir('/var/lib/salt_test'))
|
|
|
|
self.assertEqual(group_name, 'salt_test')
|
2012-07-30 19:15:35 +00:00
|
|
|
ret = self.run_state('user.absent', name='salt_test')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-07-30 19:15:35 +00:00
|
|
|
ret = self.run_state('group.absent', name='salt_test')
|
2012-11-21 12:34:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-07-30 19:15:35 +00:00
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
|
2012-07-20 06:21:01 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(UserTest)
|