Use pwd.getpwuid() to get login

For some reasons why but os.getlogin() is broken on containers :

http://stackoverflow.com/questions/4399617/python-os-getlogin-problem

when a user is not controlling the terminal.

Let's use instead pwd.getpwuid instead of os.getlogin which would work
in any cases, and would make the function safer to run under containers.

Change-Id: I0cc62e3d13b0e89a0ea1c166ea3ac06cfcc931b3
This commit is contained in:
Chmouel Boudjnah 2014-10-14 23:19:29 +02:00
parent 51bd5fede5
commit e06b41a069
2 changed files with 8 additions and 14 deletions

View File

@ -19,6 +19,7 @@ __all__ = [
import logging
import os
import pwd
import shlex
import shutil
import subprocess
@ -44,7 +45,9 @@ class Runner(object):
if self.args.user_map:
username, uid, gid = self.args.user_map.split(':')
else:
username, uid, gid = (os.getlogin(), os.getuid(), os.getgid())
username, uid, gid = (pwd.getpwuid(os.getuid())[0],
os.getuid(),
os.getgid())
return {'username': username, 'uid': int(uid), 'gid': int(gid)}
def _get_path_mapping(self):

View File

@ -34,7 +34,7 @@ class TestRunner(base.TestCase):
@mock.patch('os.getuid', return_value=12345)
@mock.patch('os.getgid', return_value=67890)
@mock.patch('os.getlogin', return_value='toto')
@mock.patch('pwd.getpwuid', return_value=['toto'])
def test_user_mapping_default(self, os_uid, os_gid, os_username):
dr = doxrunner.Runner(argparse.Namespace(user_map=None,
path_map=None))
@ -42,28 +42,19 @@ class TestRunner(base.TestCase):
self.assertEqual(12345, dr.user_map['uid'])
self.assertEqual(67890, dr.user_map['gid'])
@mock.patch('os.getuid')
@mock.patch('os.getgid')
@mock.patch('os.getlogin')
def test_path_mapping(self, os_uid, os_gid, os_username):
def test_path_mapping(self):
dr = doxrunner.Runner(argparse.Namespace(path_map='/Users:/home',
user_map=None))
self.assertEqual('/Users', dr.path_map['local'])
self.assertEqual('/home', dr.path_map['remote'])
@mock.patch('os.getuid')
@mock.patch('os.getgid')
@mock.patch('os.getlogin')
def test_path_mapping_extra_colon(self, os_uid, os_gid, os_username):
def test_path_mapping_extra_colon(self):
dr = doxrunner.Runner(argparse.Namespace(path_map='/Users:/home:foo',
user_map=None))
self.assertEqual('/Users', dr.path_map['local'])
self.assertEqual('/home:foo', dr.path_map['remote'])
@mock.patch('os.getuid')
@mock.patch('os.getgid')
@mock.patch('os.getlogin')
def test_path_mapping_default(self, os_uid, os_gid, os_username):
def test_path_mapping_default(self):
dr = doxrunner.Runner(argparse.Namespace(path_map=None,
user_map=None))
self.assertEqual(None, dr.path_map)