2013-11-27 11:19:24 +00:00
# -*- coding: utf-8 -*-
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
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
# Import Python libs
2014-11-21 19:05:13 +00:00
from __future__ import absolute_import
2012-06-30 23:51:11 +00:00
import os
2016-05-05 14:06:07 +00:00
import sys
2016-04-29 16:40:54 +00:00
from random import randint
2012-07-30 19:15:35 +00:00
import grp
2013-06-24 22:53:59 +00:00
# Import Salt Testing libs
2017-04-03 16:04:09 +00:00
from tests . support . case import ModuleCase
2017-02-27 13:58:07 +00:00
from tests . support . unit import skipIf
2017-04-04 17:57:27 +00:00
from tests . support . helpers import destructiveTest , requires_system_grains , skip_if_not_root
2017-04-02 16:09:47 +00:00
from tests . support . mixins import SaltReturnAssertsMixin
2013-06-27 12:56:39 +00:00
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
# Import Salt libs
import salt . utils . platform
2012-06-30 16:43:44 +00:00
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
if salt . utils . platform . is_darwin ( ) :
2016-04-29 16:40:54 +00:00
USER = ' macuser '
GROUP = ' macuser '
GID = randint ( 400 , 500 )
NOGROUPGID = randint ( 400 , 500 )
else :
USER = ' nobody '
GROUP = ' nobody '
GID = ' nobody '
NOGROUPGID = ' nogroup '
2012-06-30 16:43:44 +00:00
2017-03-15 22:16:34 +00:00
@destructiveTest
2017-04-04 17:57:27 +00:00
@skip_if_not_root
2017-04-03 16:04:09 +00:00
class UserTest ( ModuleCase , SaltReturnAssertsMixin ) :
2012-06-30 16:43:44 +00:00
'''
test for user absent
'''
2017-03-15 22:16:34 +00:00
user_name = ' salt_test '
user_home = ' /var/lib/salt_test '
2016-04-29 16:40:54 +00:00
def setUp ( self ) :
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
if salt . utils . platform . is_darwin ( ) :
2016-04-29 16:40:54 +00:00
#on mac we need to add user, because there is
#no creationtime for nobody user.
add_user = self . run_function ( ' user.add ' , [ USER ] , gid = GID )
2012-06-30 16:43:44 +00:00
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 ) :
2016-04-29 16:40:54 +00:00
ret = self . run_state ( ' user.present ' , name = USER )
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 ) :
2016-04-29 16:40:54 +00:00
if self . run_function ( ' group.info ' , [ USER ] ) :
ret = self . run_state ( ' user.present ' , name = USER , gid = GID )
2013-03-12 07:22:52 +00:00
elif self . run_function ( ' group.info ' , [ ' nogroup ' ] ) :
2016-04-29 16:40:54 +00:00
ret = self . run_state ( ' user.present ' , name = USER , gid = NOGROUPGID )
2013-03-12 07:22:52 +00:00
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-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
'''
2017-03-15 22:16:34 +00:00
ret = self . run_state ( ' user.present ' , name = self . user_name )
2012-11-21 12:34:26 +00:00
self . assertSaltTrueReturn ( ret )
2012-06-30 16:43:44 +00:00
2015-04-22 08:23:29 +00:00
def test_user_present_when_home_dir_does_not_18843 ( self ) :
'''
This is a DESTRUCTIVE TEST it creates a new user on the minion .
And then destroys that user .
Assume that it will break any system you run it on .
'''
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
if salt . utils . platform . is_darwin ( ) :
2017-03-15 22:16:34 +00:00
HOMEDIR = ' /Users/home_of_ ' + self . user_name
2016-04-26 21:32:11 +00:00
else :
2017-03-15 22:16:34 +00:00
HOMEDIR = ' /home/home_of_ ' + self . user_name
ret = self . run_state ( ' user.present ' , name = self . user_name ,
2015-04-22 08:23:29 +00:00
home = HOMEDIR )
self . assertSaltTrueReturn ( ret )
self . run_function ( ' file.absent ' , name = HOMEDIR )
2017-03-15 22:16:34 +00:00
ret = self . run_state ( ' user.present ' , name = self . user_name ,
2015-04-22 08:23:29 +00:00
home = HOMEDIR )
self . assertSaltTrueReturn ( ret )
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
'''
2017-03-15 22:16:34 +00:00
ret = self . run_state ( ' user.present ' , name = self . user_name ,
home = self . user_home )
2012-11-21 12:34:26 +00:00
self . assertSaltTrueReturn ( ret )
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
if not salt . utils . platform . is_darwin ( ) :
2017-06-06 15:02:19 +00:00
self . assertTrue ( os . path . isdir ( self . user_home ) )
2012-07-01 00:28:48 +00:00
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
'''
2016-03-03 06:10:34 +00:00
# MacOS users' primary group defaults to staff (20), not the name of
# user
gid_from_name = False if grains [ ' os_family ' ] == ' MacOS ' else True
2017-03-15 22:16:34 +00:00
ret = self . run_state ( ' user.present ' , name = self . user_name ,
gid_from_name = gid_from_name , home = self . user_home )
2012-11-21 12:34:26 +00:00
self . assertSaltTrueReturn ( ret )
2012-11-25 19:20:57 +00:00
2017-03-15 22:16:34 +00:00
ret = self . run_function ( ' user.info ' , [ self . user_name ] )
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
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
if not salt . utils . platform . is_darwin ( ) :
2017-06-06 15:20:18 +00:00
self . assertTrue ( os . path . isdir ( self . user_home ) )
2016-09-01 22:28:13 +00:00
if grains [ ' os_family ' ] in ( ' Suse ' , ) :
2013-09-04 18:41:20 +00:00
self . assertEqual ( group_name , ' users ' )
2016-03-03 06:10:34 +00:00
elif grains [ ' os_family ' ] == ' MacOS ' :
self . assertEqual ( group_name , ' staff ' )
2013-09-04 18:41:20 +00:00
else :
2017-03-15 22:16:34 +00:00
self . assertEqual ( group_name , self . user_name )
2012-11-25 19:20:57 +00:00
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
'''
2017-03-15 22:16:34 +00:00
ret = self . run_state ( ' group.present ' , name = self . user_name )
2012-11-21 12:34:26 +00:00
self . assertSaltTrueReturn ( ret )
2017-03-15 22:16:34 +00:00
ret = self . run_state ( ' user.present ' , name = self . user_name ,
gid_from_name = True , home = self . user_home )
2012-11-21 12:34:26 +00:00
self . assertSaltTrueReturn ( ret )
2017-03-15 22:16:34 +00:00
ret = self . run_function ( ' user.info ' , [ self . user_name ] )
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
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
if not salt . utils . platform . is_darwin ( ) :
2017-06-06 15:20:18 +00:00
self . assertTrue ( os . path . isdir ( self . user_home ) )
2017-03-15 22:16:34 +00:00
self . assertEqual ( group_name , self . user_name )
ret = self . run_state ( ' user.absent ' , name = self . user_name )
2012-11-21 12:34:26 +00:00
self . assertSaltTrueReturn ( ret )
2017-03-15 22:16:34 +00:00
ret = self . run_state ( ' group.absent ' , name = self . user_name )
2012-11-21 12:34:26 +00:00
self . assertSaltTrueReturn ( ret )
2016-04-18 14:56:40 +00:00
2016-05-05 14:06:07 +00:00
@skipIf ( sys . getfilesystemencoding ( ) . startswith ( ' ANSI ' ) , ' A system encoding which supports Unicode characters must be set. Current setting is: {0} . Try setting $LANG= \' en_US.UTF-8 \' ' . format ( sys . getfilesystemencoding ( ) ) )
2016-04-18 14:56:40 +00:00
def test_user_present_unicode ( self ) :
'''
This is a DESTRUCTIVE TEST it creates a new user on the on the minion .
It ensures that unicode GECOS data will be properly handled , without
any encoding - related failures .
'''
ret = self . run_state (
2017-03-15 22:16:34 +00:00
' user.present ' ,
name = self . user_name ,
fullname = u ' Sålt Test ' ,
roomnumber = u ' ①②③ ' ,
workphone = u ' ١٢٣٤ ' ,
homephone = u ' ६७८ '
2016-04-18 14:56:40 +00:00
)
self . assertSaltTrueReturn ( ret )
# Ensure updating a user also works
ret = self . run_state (
2017-03-15 22:16:34 +00:00
' user.present ' ,
name = self . user_name ,
fullname = u ' Sølt Test ' ,
roomnumber = u ' ①③② ' ,
workphone = u ' ٣٤١٢ ' ,
homephone = u ' ६८७ '
2016-04-18 14:56:40 +00:00
)
self . assertSaltTrueReturn ( ret )
2012-07-30 19:15:35 +00:00
2013-12-06 22:16:38 +00:00
def test_user_present_gecos ( self ) :
'''
This is a DESTRUCTIVE TEST it creates a new user on the on the minion .
It ensures that numeric GECOS data will be properly coerced to strings ,
otherwise the state will fail because the GECOS fields are written as
strings ( and show up in the user . info output as such ) . Thus the
comparison will fail , since ' 12345 ' != 12345.
'''
ret = self . run_state (
2017-03-15 22:16:34 +00:00
' user.present ' ,
name = self . user_name ,
fullname = 12345 ,
roomnumber = 123 ,
workphone = 1234567890 ,
homephone = 1234567890
2013-12-06 22:16:38 +00:00
)
self . assertSaltTrueReturn ( ret )
2015-08-07 07:25:21 +00:00
def test_user_present_gecos_none_fields ( self ) :
'''
This is a DESTRUCTIVE TEST it creates a new user on the on the minion .
It ensures that if no GECOS data is supplied , the fields will be coerced
into empty strings as opposed to the string " None " .
'''
ret = self . run_state (
2017-03-15 22:16:34 +00:00
' user.present ' ,
name = self . user_name ,
fullname = None ,
roomnumber = None ,
workphone = None ,
homephone = None
2015-08-07 07:25:21 +00:00
)
self . assertSaltTrueReturn ( ret )
2017-03-15 22:16:34 +00:00
ret = self . run_function ( ' user.info ' , [ self . user_name ] )
2015-08-07 07:25:21 +00:00
self . assertReturnNonEmptySaltType ( ret )
self . assertEqual ( ' ' , ret [ ' fullname ' ] )
2016-03-03 02:56:09 +00:00
# MacOS does not supply the following GECOS fields
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
if not salt . utils . platform . is_darwin ( ) :
2016-03-03 02:56:09 +00:00
self . assertEqual ( ' ' , ret [ ' roomnumber ' ] )
self . assertEqual ( ' ' , ret [ ' workphone ' ] )
self . assertEqual ( ' ' , ret [ ' homephone ' ] )
2015-08-07 07:25:21 +00:00
2016-04-29 16:40:54 +00:00
def tearDown ( self ) :
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
if salt . utils . platform . is_darwin ( ) :
2016-04-29 16:40:54 +00:00
check_user = self . run_function ( ' user.list_users ' )
if USER in check_user :
del_user = self . run_function ( ' user.delete ' , [ USER ] , remove = True )
2017-03-15 22:16:34 +00:00
self . assertSaltTrueReturn (
self . run_state ( ' user.absent ' , name = self . user_name )
)