From 2091b7ee86fabc5600d2afeb10fca73f9685ed51 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Thu, 20 Jul 2023 11:36:30 -0400 Subject: [PATCH] skip permission-related tests if the environment is not suitable posix superuser can do anything on the filesystem --- src/allmydata/test/cli/test_grid_manager.py | 3 ++- src/allmydata/test/test_client.py | 25 +++++++------------- src/allmydata/test/test_node.py | 26 ++++++--------------- 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/src/allmydata/test/cli/test_grid_manager.py b/src/allmydata/test/cli/test_grid_manager.py index 2ed738544..970315af7 100644 --- a/src/allmydata/test/cli/test_grid_manager.py +++ b/src/allmydata/test/cli/test_grid_manager.py @@ -222,7 +222,8 @@ class GridManagerCommandLine(TestCase): result.output, ) - @skipIf(not platform.isLinux(), "I only know how permissions work on linux") + @skipIf(platform.isWindows(), "We don't know how to set permissions on Windows.") + @skipIf(os.getuid() == 0, "cannot test as superuser which all of the permissions") def test_sign_bad_perms(self): """ Error reported if we can't create certificate file diff --git a/src/allmydata/test/test_client.py b/src/allmydata/test/test_client.py index 04d8e6160..848f98a13 100644 --- a/src/allmydata/test/test_client.py +++ b/src/allmydata/test/test_client.py @@ -1,17 +1,7 @@ -""" -Ported to Python 3. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from __future__ import unicode_literals - -from future.utils import PY2 -if PY2: - from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401 +from __future__ import annotations import os -import sys +from unittest import skipIf from functools import ( partial, ) @@ -42,6 +32,7 @@ from twisted.internet import defer from twisted.python.filepath import ( FilePath, ) +from twisted.python.runtime import platform from testtools.matchers import ( Equals, AfterPreprocessing, @@ -156,12 +147,12 @@ class Basic(testutil.ReallyEqualMixin, unittest.TestCase): yield client.create_client(basedir) self.assertIn("[client]helper.furl", str(ctx.exception)) + # if somebody knows a clever way to do this (cause + # EnvironmentError when reading a file that really exists), on + # windows, please fix this + @skipIf(platform.isWindows(), "We don't know how to set permissions on Windows.") + @skipIf(os.getuid() == 0, "cannot test as superuser which all of the permissions") def test_unreadable_config(self): - if sys.platform == "win32": - # if somebody knows a clever way to do this (cause - # EnvironmentError when reading a file that really exists), on - # windows, please fix this - raise unittest.SkipTest("can't make unreadable files on windows") basedir = "test_client.Basic.test_unreadable_config" os.mkdir(basedir) fn = os.path.join(basedir, "tahoe.cfg") diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index 0b371569a..7d6b09d12 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -1,14 +1,4 @@ -""" -Ported to Python 3. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from __future__ import unicode_literals - -from future.utils import PY2 -if PY2: - from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401 +from __future__ import annotations import base64 import os @@ -31,6 +21,7 @@ from unittest import skipIf from twisted.python.filepath import ( FilePath, ) +from twisted.python.runtime import platform from twisted.trial import unittest from twisted.internet import defer @@ -333,10 +324,8 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): default = [("hello", "world")] self.assertEqual(config.items("nosuch", default), default) - @skipIf( - "win32" in sys.platform.lower() or "cygwin" in sys.platform.lower(), - "We don't know how to set permissions on Windows.", - ) + @skipIf(platform.isWindows(), "We don't know how to set permissions on Windows.") + @skipIf(os.getuid() == 0, "cannot test as superuser which all of the permissions") def test_private_config_unreadable(self): """ Asking for inaccessible private config is an error @@ -351,10 +340,8 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): with self.assertRaises(Exception): config.get_or_create_private_config("foo") - @skipIf( - "win32" in sys.platform.lower() or "cygwin" in sys.platform.lower(), - "We don't know how to set permissions on Windows.", - ) + @skipIf(platform.isWindows(), "We don't know how to set permissions on Windows.") + @skipIf(os.getuid() == 0, "cannot test as superuser which all of the permissions") def test_private_config_unreadable_preexisting(self): """ error if reading private config data fails @@ -411,6 +398,7 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): self.assertEqual(len(counter), 1) # don't call unless necessary self.assertEqual(value, "newer") + @skipIf(os.getuid() == 0, "cannot test as superuser which all of the permissions") def test_write_config_unwritable_file(self): """ Existing behavior merely logs any errors upon writing