From 6a6ab6972250cb51caf5e0ae99ac672e2ecfb74b Mon Sep 17 00:00:00 2001 From: Daniel A Wozniak Date: Sun, 6 May 2018 16:54:55 +0000 Subject: [PATCH] Get the current username on windows --- tests/integration/modules/test_cmdmod.py | 10 +++------- tests/support/helpers.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/tests/integration/modules/test_cmdmod.py b/tests/integration/modules/test_cmdmod.py index 810ff9a5fd..f17a3d1960 100644 --- a/tests/integration/modules/test_cmdmod.py +++ b/tests/integration/modules/test_cmdmod.py @@ -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() diff --git a/tests/support/helpers.py b/tests/support/helpers.py index 6866c99730..17cbebf9d2 100644 --- a/tests/support/helpers.py +++ b/tests/support/helpers.py @@ -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 win32api +else: + import pwd # Import Salt libs import salt.utils @@ -1552,3 +1557,11 @@ 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(): + if salt.utils.is_windows(): + full_username = win32api.GetUserNameEx(win32api.NameSamCompatible) + if '\\' in full_username: + return full_username.split('\\', 1)[1] + return full_username + return pwd.getpwuid(os.getuid())[0]