salt/tests/integration/modules/mac_user.py

139 lines
4.0 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
'''
# Import Python Libs
import os
import random
import string
# Import Salt Testing Libs
from salttesting import skipIf
from salttesting.helpers import (
destructiveTest,
ensure_in_syspath,
requires_system_grains
)
ensure_in_syspath('../../')
# Import Salt Libs
import integration
from salt.exceptions import CommandExecutionError
2014-03-13 20:29:58 +00:00
def __random_string(size=6):
'''
Generates a random username
'''
return 'RS-' + ''.join(
random.choice(string.ascii_uppercase + string.digits)
for x in range(size)
)
# Create user strings for tests
2014-03-13 19:51:58 +00:00
ADD_USER = __random_string()
DEL_USER = __random_string()
2014-03-17 15:02:16 +00:00
CHANGE_USER = __random_string()
class MacUserModuleTest(integration.ModuleCase):
'''
Integration tests for the mac_user module
'''
2014-03-11 21:26:16 +00:00
def setUp(self):
'''
Sets up test requirements
'''
super(MacUserModuleTest, self).setUp()
2014-03-11 21:26:16 +00:00
os_grain = self.run_function('grains.item', ['kernel'])
2014-03-12 03:45:41 +00:00
if os_grain['kernel'] not in 'Darwin':
self.skipTest(
'Test not applicable to \'{kernel}\' kernel'.format(
**os_grain
)
)
2014-03-17 15:02:16 +00:00
# @destructiveTest
@skipIf(os.geteuid() != 0, 'You must be logged in as root to run this test')
@requires_system_grains
def test_mac_user_add(self, grains=None):
'''
Tests the add function
'''
try:
2014-03-13 19:51:58 +00:00
self.run_function('user.add', [ADD_USER])
user_info = self.run_function('user.info', [ADD_USER])
self.assertEqual(ADD_USER, user_info['name'])
except CommandExecutionError:
2014-03-13 19:51:58 +00:00
self.run_function('user.delete', [ADD_USER])
raise
2014-03-17 15:02:16 +00:00
# @destructiveTest
@skipIf(os.geteuid() != 0, 'You must be logged in as root to run this test')
@requires_system_grains
def test_mac_user_delete(self, grains=None):
'''
Tests the delete function
'''
# Create a user to delete - If unsuccessful, skip the test
2014-03-13 19:51:58 +00:00
if self.run_function('user.add', [DEL_USER]) is not True:
2014-03-17 15:02:16 +00:00
self.run_function('user.delete', [DEL_USER])
self.skipTest('Failed to create a user to delete')
try:
# Now try to delete the added user
2014-03-13 19:51:58 +00:00
ret = self.run_function('user.delete', [DEL_USER])
self.assertTrue(ret)
except CommandExecutionError:
raise
2014-03-17 15:02:16 +00:00
# @destructiveTest
@skipIf(os.geteuid() != 0, 'You must be logged in as root to run this test')
@requires_system_grains
def test_mac_user_changes(self, grains=None):
'''
Tests mac_user functions that change user properties
'''
# Create a user to manipulate - if unsuccessful, skip the test
if self.run_function('user.add', [CHANGE_USER]) is not True:
self.run_function('user.delete', [CHANGE_USER])
self.skipTest('Failed to create a user')
try:
user_info = self.run_function('user.info', [CHANGE_USER])
# Test chudi
self.run_function('user.chuid', [CHANGE_USER, 4376])
new_info = self.run_function('user.info', [CHANGE_USER])
self.assertEqual(new_info['uid'], user_info['uid'])
except AssertionError:
self.run_function('user.delete', [CHANGE_USER])
# @destructiveTest
2014-03-11 21:26:16 +00:00
@skipIf(os.geteuid() != 0, 'You must be logged in as root to run this test')
@requires_system_grains
def tearDown(self, grains=None):
'''
Clean up after tests
'''
2014-03-17 15:02:16 +00:00
# Delete ADD_USER
2014-03-13 20:29:58 +00:00
add_info = self.run_function('user.info', [ADD_USER])
if add_info:
2014-03-13 19:51:58 +00:00
self.run_function('user.delete', [ADD_USER])
2014-03-17 15:02:16 +00:00
# Delete DEL_USER if something failed
2014-03-13 19:51:58 +00:00
del_info = self.run_function('user.info', [DEL_USER])
if del_info:
2014-03-13 19:51:58 +00:00
self.run_function('user.delete', [DEL_USER])
if __name__ == '__main__':
from integration import run_tests
run_tests(MacUserModuleTest)