sapling/eden/integration/userinfo_test.py
Zhengchao Liu 55986887a8 test: skip userinfo test if it's run as root
Summary: This test can fail for not having `USERNAME` environment variable when the test is run as root. Let's just skip the test when this happens because it doesn't make sense to drop priv as root.

Reviewed By: xavierd

Differential Revision: D30868518

fbshipit-source-id: 14ff6db218b1477f5905f2df3ad075a5ca186117
2021-09-10 16:42:15 -07:00

33 lines
1.1 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2.
import os
import subprocess
import unittest
from .lib import edenclient, testcase
from .lib.find_executables import FindExe
class UserInfoTest(testcase.IntegrationTestCase):
@unittest.skipIf(not edenclient.can_run_sudo(), "unable to run sudo")
def test_drop_privs(self):
try:
expected_user = os.environ["USER"]
except KeyError:
self.skipTest("No USER environment variable available")
return
if expected_user == "root":
self.skipTest("Is root user")
return
cmd = ["/usr/bin/sudo", FindExe.DROP_PRIVS, "/usr/bin/env"]
out = subprocess.check_output(cmd)
lines = out.splitlines()
self.assertIn(f"USER={expected_user}".encode("utf-8"), lines)
self.assertIn(f"USERNAME={expected_user}".encode("utf-8"), lines)
self.assertIn(f"LOGNAME={expected_user}".encode("utf-8"), lines)