From 801894c7dae111b592ef22f796e1e5f5ff617fb8 Mon Sep 17 00:00:00 2001 From: ohemorange Date: Thu, 14 Aug 2025 09:58:06 -0700 Subject: [PATCH] Speed up mac tests using various strategies (#10419) Alternative to https://github.com/certbot/certbot/pull/10408/ and https://github.com/certbot/certbot/pull/10415/ that fixes production code for account meta and puts autouse fixtures in certbot and acme tests. Overrides all `time.sleep` calls while we're at it. Fixes the production code where it's simple/clean, and fixes the tests for HTTPServer-based code because we just don't have that many mac users using standalone. --- acme/src/acme/_internal/tests/conftest.py | 16 ++++++++++++++++ certbot/src/certbot/_internal/account.py | 2 +- .../src/certbot/_internal/tests/account_test.py | 2 +- certbot/src/certbot/_internal/tests/conftest.py | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 acme/src/acme/_internal/tests/conftest.py create mode 100644 certbot/src/certbot/_internal/tests/conftest.py diff --git a/acme/src/acme/_internal/tests/conftest.py b/acme/src/acme/_internal/tests/conftest.py new file mode 100644 index 000000000..6e929364d --- /dev/null +++ b/acme/src/acme/_internal/tests/conftest.py @@ -0,0 +1,16 @@ +from unittest import mock + +import pytest + +# This avoids a bug on mac where getfqdn errors after a long timeout. +# See https://bugs.python.org/issue35164 +# and discussion at https://github.com/certbot/certbot/pull/10408 +@pytest.fixture(autouse=True) +def mock_getfqdn(): + with mock.patch("socket.getfqdn", return_value="server_name") as mocked: + yield mocked + +@pytest.fixture(autouse=True) +def mock_sleep(): + with mock.patch("time.sleep") as mocked: + yield mocked diff --git a/certbot/src/certbot/_internal/account.py b/certbot/src/certbot/_internal/account.py index 548d2ae0b..f2b281f12 100644 --- a/certbot/src/certbot/_internal/account.py +++ b/certbot/src/certbot/_internal/account.py @@ -62,7 +62,7 @@ class Account: self.meta = self.Meta( # pyrfc3339 drops microseconds, make sure __eq__ is sane creation_dt=datetime.datetime.now(tz=datetime.timezone.utc).replace(microsecond=0), - creation_host=socket.getfqdn(), + creation_host=socket.gethostname(), register_to_eff=None) if meta is None else meta # try MD5, else use MD5 in non-security mode (e.g. for FIPS systems / RHEL) diff --git a/certbot/src/certbot/_internal/tests/account_test.py b/certbot/src/certbot/_internal/tests/account_test.py index c36b10969..f856779bb 100644 --- a/certbot/src/certbot/_internal/tests/account_test.py +++ b/certbot/src/certbot/_internal/tests/account_test.py @@ -31,7 +31,7 @@ class AccountTest(unittest.TestCase): self.regr.__repr__ = mock.MagicMock(return_value="i_am_a_regr") with mock.patch("certbot._internal.account.socket") as mock_socket: - mock_socket.getfqdn.return_value = "test.certbot.org" + mock_socket.gethostname.return_value = "test.certbot.org" with mock.patch("certbot._internal.account.datetime") as mock_dt: mock_dt.datetime.now.return_value = self.meta.creation_dt self.acc_no_meta = Account(self.regr, KEY) diff --git a/certbot/src/certbot/_internal/tests/conftest.py b/certbot/src/certbot/_internal/tests/conftest.py new file mode 100644 index 000000000..6e929364d --- /dev/null +++ b/certbot/src/certbot/_internal/tests/conftest.py @@ -0,0 +1,16 @@ +from unittest import mock + +import pytest + +# This avoids a bug on mac where getfqdn errors after a long timeout. +# See https://bugs.python.org/issue35164 +# and discussion at https://github.com/certbot/certbot/pull/10408 +@pytest.fixture(autouse=True) +def mock_getfqdn(): + with mock.patch("socket.getfqdn", return_value="server_name") as mocked: + yield mocked + +@pytest.fixture(autouse=True) +def mock_sleep(): + with mock.patch("time.sleep") as mocked: + yield mocked