Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: mock user euid to be able to run tests as root #4519

Merged
merged 1 commit into from
May 9, 2022

Conversation

amurzeau
Copy link
Contributor

@amurzeau amurzeau commented May 9, 2022

When running test as root, there is an additional log done to warn the
user that it is running streamlink as root. This causes some logging
tests to fail.

For these logging test, ensure we run with a mocked user euid when not
testing for the root euid.

I know that running as root is unusual, but it can happen when packaging for distributions.

@amurzeau amurzeau force-pushed the fix-running-tests-as-root branch from 4425d87 to 3b3ab88 Compare May 9, 2022 18:14
Copy link
Member

@bastimeyer bastimeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I've already seen your patch in your debian source package repo yesterday...

Could you please update your branch to the following diff?
This fixes the syntax errors with the missing new= keyword in the patch calls. The signature of the _TestCLIMainLogging.subject method should also not get changed, and in the streamlink_cli.main module, the hasattr argument should be equal to the method that's actually called. The missing else branch should also get ignored in the code coverage.

diff --git a/src/streamlink_cli/main.py b/src/streamlink_cli/main.py
index bb49b502..85b4da54 100644
--- a/src/streamlink_cli/main.py
+++ b/src/streamlink_cli/main.py
@@ -893,7 +893,7 @@ def setup_plugin_options(session: Streamlink, plugin: Type[Plugin]):
 
 
 def log_root_warning():
-    if hasattr(os, "getuid"):
+    if hasattr(os, "geteuid"):  # pragma: no branch
         if os.geteuid() == 0:
             log.info("streamlink is running as root! Be careful!")
 
diff --git a/tests/test_cli_main.py b/tests/test_cli_main.py
index d72945f8..e77de698 100644
--- a/tests/test_cli_main.py
+++ b/tests/test_cli_main.py
@@ -572,7 +572,8 @@ class _TestCLIMainLogging(unittest.TestCase):
         class StopTest(Exception):
             pass
 
-        with patch("streamlink_cli.main.streamlink", session), \
+        with patch("streamlink_cli.main.os.geteuid", create=True, new=Mock(return_value=kwargs.get("euid", 1000))), \
+             patch("streamlink_cli.main.streamlink", session), \
              patch("streamlink_cli.main.setup_signals", side_effect=StopTest), \
              patch("streamlink_cli.main.CONFIG_FILES", []), \
              patch("streamlink_cli.main.setup_streamlink"), \
@@ -694,9 +695,8 @@ class TestCLIMainLoggingStreams(_TestCLIMainLogging):
 class TestCLIMainLoggingInfos(_TestCLIMainLogging):
     @unittest.skipIf(is_win32, "test only applicable on a POSIX OS")
     @patch("streamlink_cli.main.log")
-    @patch("streamlink_cli.main.os.geteuid", Mock(return_value=0))
     def test_log_root_warning(self, mock_log):
-        self.subject(["streamlink"])
+        self.subject(["streamlink"], euid=0)
         self.assertEqual(mock_log.info.mock_calls, [call("streamlink is running as root! Be careful!")])
 
     @patch("streamlink_cli.main.log")
@@ -902,7 +902,8 @@ class TestCLIMainPrint(unittest.TestCase):
              patch.object(Streamlink, "resolve_url_no_redirect") as mock_resolve_url_no_redirect:
             session = Streamlink()
             session.load_plugins(os.path.join(os.path.dirname(__file__), "plugin"))
-            with patch("streamlink_cli.main.streamlink", session), \
+            with patch("streamlink_cli.main.os.geteuid", create=True, new=Mock(return_value=1000)), \
+                 patch("streamlink_cli.main.streamlink", session), \
                  patch("streamlink_cli.main.CONFIG_FILES", []), \
                  patch("streamlink_cli.main.setup_streamlink"), \
                  patch("streamlink_cli.main.setup_plugins"), \

@amurzeau
Copy link
Contributor Author

amurzeau commented May 9, 2022

Thanks ! I was not sure how to handle the Windows part.

When running test as root, there is an additional log done to warn the
user that it is running streamlink as root. This causes some logging
tests to fail.

For these logging test, ensure we run with a mocked user euid when not
testing for the root euid.
@amurzeau amurzeau force-pushed the fix-running-tests-as-root branch from 2c485c0 to 41b851a Compare May 9, 2022 21:01
@bastimeyer bastimeyer merged commit 081595c into streamlink:master May 9, 2022
@bastimeyer
Copy link
Member

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants