Get the current username on windows

This commit is contained in:
Daniel A Wozniak 2018-05-06 16:54:55 +00:00 committed by Daniel A. Wozniak
parent f45a96ba1d
commit 6a6ab69722
No known key found for this signature in database
GPG Key ID: 166B9D2C06C82D61
2 changed files with 16 additions and 7 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 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]