Merge branch '2018.3' into 970_two_minion_unit_tests_failing

This commit is contained in:
Gareth J. Greenaway 2018-05-22 14:41:10 -07:00 committed by GitHub
commit a5011b49ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 102 additions and 21 deletions

View file

@ -60,17 +60,18 @@ def _get_gecos(name):
Retrieve GECOS field info and return it in dictionary form
'''
gecos_field = salt.utils.stringutils.to_unicode(
pwd.getpwnam(_quote_username(name)).pw_gecos).split(',', 3)
pwd.getpwnam(_quote_username(name)).pw_gecos).split(',', 4)
if not gecos_field:
return {}
else:
# Assign empty strings for any unspecified trailing GECOS fields
while len(gecos_field) < 4:
while len(gecos_field) < 5:
gecos_field.append('')
return {'fullname': salt.utils.locales.sdecode(gecos_field[0]),
'roomnumber': salt.utils.locales.sdecode(gecos_field[1]),
'workphone': salt.utils.locales.sdecode(gecos_field[2]),
'homephone': salt.utils.locales.sdecode(gecos_field[3])}
'homephone': salt.utils.locales.sdecode(gecos_field[3]),
'other': salt.utils.locales.sdecode(gecos_field[4])}
def _build_gecos(gecos_dict):
@ -78,10 +79,11 @@ def _build_gecos(gecos_dict):
Accepts a dictionary entry containing GECOS field names and their values,
and returns a full GECOS comment string, to be used with usermod.
'''
return '{0},{1},{2},{3}'.format(gecos_dict.get('fullname', ''),
gecos_dict.get('roomnumber', ''),
gecos_dict.get('workphone', ''),
gecos_dict.get('homephone', ''))
return '{0},{1},{2},{3},{4}'.format(gecos_dict.get('fullname', ''),
gecos_dict.get('roomnumber', ''),
gecos_dict.get('workphone', ''),
gecos_dict.get('homephone', ''),
gecos_dict.get('other', ''),).rstrip(',')
def _update_gecos(name, key, value, root=None):
@ -124,6 +126,7 @@ def add(name,
roomnumber='',
workphone='',
homephone='',
other='',
createhome=True,
loginclass=None,
root=None,
@ -237,6 +240,8 @@ def add(name,
chworkphone(name, workphone)
if homephone:
chhomephone(name, homephone)
if other:
chother(name, other)
return True
@ -507,6 +512,19 @@ def chhomephone(name, homephone):
return _update_gecos(name, 'homephone', homephone)
def chother(name, other):
'''
Change the user's other GECOS attribute
CLI Example:
.. code-block:: bash
salt '*' user.chother foobar
'''
return _update_gecos(name, 'other', other)
def chloginclass(name, loginclass, root=None):
'''
Change the default login class of the user
@ -588,9 +606,9 @@ def _format_info(data):
Return user information in a pretty way
'''
# Put GECOS info into a list
gecos_field = salt.utils.stringutils.to_unicode(data.pw_gecos).split(',', 3)
# Make sure our list has at least four elements
while len(gecos_field) < 4:
gecos_field = salt.utils.stringutils.to_unicode(data.pw_gecos).split(',', 4)
# Make sure our list has at least five elements
while len(gecos_field) < 5:
gecos_field.append('')
return {'gid': data.pw_gid,
@ -603,7 +621,8 @@ def _format_info(data):
'fullname': gecos_field[0],
'roomnumber': gecos_field[1],
'workphone': gecos_field[2],
'homephone': gecos_field[3]}
'homephone': gecos_field[3],
'other': gecos_field[4]}
@salt.utils.decorators.path.which('id')

View file

@ -68,6 +68,7 @@ def _changes(name,
roomnumber='',
workphone='',
homephone='',
other='',
loginclass=None,
date=None,
mindays=0,
@ -170,24 +171,26 @@ def _changes(name,
# MacOS doesn't have full GECOS support, so check for the "ch" functions
# and ignore these parameters if these functions do not exist.
if 'user.chroomnumber' in __salt__ \
and roomnumber is not None:
if 'user.chroomnumber' in __salt__ and roomnumber is not None:
roomnumber = sdecode_if_string(roomnumber)
lusr['roomnumber'] = sdecode_if_string(lusr['roomnumber'])
if lusr['roomnumber'] != roomnumber:
change['roomnumber'] = roomnumber
if 'user.chworkphone' in __salt__ \
and workphone is not None:
if 'user.chworkphone' in __salt__ and workphone is not None:
workphone = sdecode_if_string(workphone)
lusr['workphone'] = sdecode_if_string(lusr['workphone'])
if lusr['workphone'] != workphone:
change['workphone'] = workphone
if 'user.chhomephone' in __salt__ \
and homephone is not None:
if 'user.chhomephone' in __salt__ and homephone is not None:
homephone = sdecode_if_string(homephone)
lusr['homephone'] = sdecode_if_string(lusr['homephone'])
if lusr['homephone'] != homephone:
change['homephone'] = homephone
if 'user.chother' in __salt__ and other is not None:
other = sdecode_if_string(other)
lusr['other'] = sdecode_if_string(lusr['other'])
if lusr['other'] != other:
change['other'] = other
# OpenBSD/FreeBSD login class
if __grains__['kernel'] in ('OpenBSD', 'FreeBSD'):
if loginclass:
@ -236,6 +239,7 @@ def present(name,
roomnumber=None,
workphone=None,
homephone=None,
other=None,
loginclass=None,
date=None,
mindays=None,
@ -377,7 +381,10 @@ def present(name,
homephone
The user's home phone number (not supported in MacOS)
If GECOS field contains more than 3 commas, this field will have the rest of 'em
other
The user's other attribute (not supported in MacOS)
If GECOS field contains more than 4 commas, this field will have the rest of 'em
.. versionchanged:: 2014.7.0
Shadow attribute support added.
@ -448,6 +455,8 @@ def present(name,
workphone = sdecode(workphone)
if homephone is not None:
homephone = sdecode(homephone)
if other is not None:
other = sdecode(other)
# createhome not supported on Windows or Mac
if __grains__['kernel'] in ('Darwin', 'Windows'):
@ -460,7 +469,7 @@ def present(name,
# the comma is used to separate field in GECOS, thus resulting into
# salt adding the end of fullname each time this function is called
for gecos_field in ['fullname', 'roomnumber', 'workphone']:
for gecos_field in [fullname, roomnumber, workphone]:
if isinstance(gecos_field, string_types) and ',' in gecos_field:
ret['comment'] = "Unsupported char ',' in {0}".format(gecos_field)
ret['result'] = False
@ -519,6 +528,7 @@ def present(name,
roomnumber,
workphone,
homephone,
other,
loginclass,
date,
mindays,
@ -654,6 +664,7 @@ def present(name,
roomnumber,
workphone,
homephone,
other,
loginclass,
date,
mindays,
@ -705,6 +716,7 @@ def present(name,
'roomnumber': roomnumber,
'workphone': workphone,
'homephone': homephone,
'other': other,
'createhome': createhome,
'nologinit': nologinit,
'loginclass': loginclass}

View file

@ -46,7 +46,8 @@ class UserAddTestCase(TestCase, LoaderModuleMockMixin):
'fullname': 'root',
'roomnumber': '',
'workphone': '',
'homephone': ''}
'homephone': '',
'other': ''}
@classmethod
def tearDownClass(cls):
@ -96,7 +97,8 @@ class UserAddTestCase(TestCase, LoaderModuleMockMixin):
'fullname': 'root',
'roomnumber': '',
'workphone': '',
'homephone': ''}]
'homephone': '',
'other': ''}]
with patch('salt.modules.useradd._format_info', MagicMock(return_value=self.mock_pwall)):
self.assertEqual(useradd.getent(), ret)
@ -330,6 +332,36 @@ class UserAddTestCase(TestCase, LoaderModuleMockMixin):
with patch.object(useradd, 'info', mock):
self.assertFalse(useradd.chhomephone('salt', 1))
# 'chother' function tests: 1
def test_chother(self):
'''
Test if the user's other GECOS attribute is changed
'''
mock = MagicMock(return_value=False)
with patch.object(useradd, '_get_gecos', mock):
self.assertFalse(useradd.chother('salt', 1))
mock = MagicMock(return_value={'other': 'foobar'})
with patch.object(useradd, '_get_gecos', mock):
self.assertTrue(useradd.chother('salt', 'foobar'))
mock = MagicMock(return_value={'other': 'foobar2'})
with patch.object(useradd, '_get_gecos', mock):
mock = MagicMock(return_value=None)
with patch.dict(useradd.__salt__, {'cmd.run': mock}):
mock = MagicMock(return_value={'other': 'foobar3'})
with patch.object(useradd, 'info', mock):
self.assertFalse(useradd.chother('salt', 'foobar'))
mock = MagicMock(return_value={'other': 'foobar3'})
with patch.object(useradd, '_get_gecos', mock):
mock = MagicMock(return_value=None)
with patch.dict(useradd.__salt__, {'cmd.run': mock}):
mock = MagicMock(return_value={'other': 'foobar3'})
with patch.object(useradd, 'info', mock):
self.assertFalse(useradd.chother('salt', 'foobar'))
# 'info' function tests: 1
@skipIf(HAS_PWD is False, 'The pwd module is not available')
@ -393,3 +425,21 @@ class UserAddTestCase(TestCase, LoaderModuleMockMixin):
mock = MagicMock(side_effect=[{'name': ''}, False, {'name': ''}])
with patch.object(useradd, 'info', mock):
self.assertFalse(useradd.rename('salt', 'salt'))
def test_build_gecos_field(self):
'''
Test if gecos fields are built correctly (removing trailing commas)
'''
test_gecos = {'fullname': 'Testing',
'roomnumber': 1234,
'workphone': 22222,
'homephone': 99999}
expected_gecos_fields = 'Testing,1234,22222,99999'
self.assertEqual(useradd._build_gecos(test_gecos), expected_gecos_fields)
test_gecos.pop('roomnumber')
test_gecos.pop('workphone')
expected_gecos_fields = 'Testing,,,99999'
self.assertEqual(useradd._build_gecos(test_gecos), expected_gecos_fields)
test_gecos.pop('homephone')
expected_gecos_fields = 'Testing'
self.assertEqual(useradd._build_gecos(test_gecos), expected_gecos_fields)