Merge pull request #47500 from dwoz/winuser

Get the current username on windows
This commit is contained in:
Daniel Wozniak 2018-05-24 12:14:06 -07:00 committed by GitHub
commit 93420950d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 21 deletions

View file

@ -12,7 +12,8 @@ from tests.support.case import ModuleCase
from tests.support.helpers import (
destructiveTest,
skip_if_binaries_missing,
skip_if_not_root
skip_if_not_root,
this_user,
)
from tests.support.paths import TMP
@ -227,12 +228,7 @@ class CMDModuleTest(ModuleCase):
cmd = '''echo 'SELECT * FROM foo WHERE bar="baz"' '''
expected_result = 'SELECT * FROM foo WHERE bar="baz"'
try:
runas = os.getlogin()
except: # pylint: disable=W0702
# On some distros (notably Gentoo) os.getlogin() fails
import pwd
runas = pwd.getpwuid(os.getuid())[0]
runas = this_user()
result = self.run_function('cmd.run_stdout', [cmd],
runas=runas).strip()

View file

@ -34,6 +34,7 @@ import types
# Import 3rd-party libs
import psutil # pylint: disable=3rd-party-module-not-gated
import salt.ext.six as six
import salt.utils
from salt.ext.six.moves import range, builtins # pylint: disable=import-error,redefined-builtin
try:
from pytestsalt.utils import get_unused_localhost_port # pylint: disable=unused-import
@ -52,6 +53,10 @@ except ImportError:
from tests.support.unit import skip, _id
from tests.support.mock import patch
from tests.support.paths import FILES, TMP
if salt.utils.is_windows():
import salt.utils.win_functions
else:
import pwd
# Import Salt libs
import salt.utils
@ -1138,7 +1143,6 @@ def skip_if_not_root(func):
func.__unittest_skip__ = True
func.__unittest_skip_why__ = 'You must be logged in as root to run this test'
else:
import salt.utils.win_functions
current_user = salt.utils.win_functions.get_current_user()
if current_user != 'SYSTEM':
if not salt.utils.win_functions.is_admin(current_user):
@ -1552,3 +1556,12 @@ def win32_kill_process_tree(pid, sig=signal.SIGTERM, include_parent=True,
gone, alive = psutil.wait_procs(children, timeout=timeout,
callback=on_terminate)
return (gone, alive)
def this_user():
'''
Get the user associated with the current process.
'''
if salt.utils.is_windows():
return salt.utils.win_functions.get_current_user()
return pwd.getpwuid(os.getuid())[0]

View file

@ -49,7 +49,6 @@
# Import Python modules
from __future__ import absolute_import, print_function
import os
import sys
import json
import shutil
import logging
@ -57,6 +56,7 @@ import multiprocessing
# Import tests support libs
import tests.support.paths as paths
import tests.support.helpers
# Import 3rd-party libs
import salt.ext.six as six
@ -103,12 +103,9 @@ try:
except ImportError:
pass
if sys.platform.startswith('win'):
import win32api # pylint: disable=import-error
RUNNING_TESTS_USER = win32api.GetUserName()
else:
import pwd
RUNNING_TESTS_USER = pwd.getpwuid(os.getuid()).pw_name
RUNNING_TESTS_USER = tests.support.helpers.this_user()
log = logging.getLogger(__name__)

View file

@ -12,7 +12,7 @@ import textwrap
import logging
import stat
try:
import pwd
import pwd # pylint: disable=unused-import
except ImportError:
pass
@ -31,6 +31,7 @@ from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import NO_MOCK, NO_MOCK_REASON
from tests.support.paths import TMP, FILES
from tests.support.helpers import this_user
# Import salt libs
import salt.utils.gitfs
@ -207,11 +208,7 @@ class GitFSTest(TestCase, LoaderModuleMockMixin):
if 'USERNAME' not in os.environ:
try:
import salt.utils
if salt.utils.is_windows():
import salt.utils.win_functions
os.environ['USERNAME'] = salt.utils.win_functions.get_current_user()
else:
os.environ['USERNAME'] = pwd.getpwuid(os.geteuid()).pw_name
os.environ['USERNAME'] = this_user()
except AttributeError:
log.error('Unable to get effective username, falling back to '
'\'root\'.')