From 6537c0bc28c8c314e9647110697a0177ad19f542 Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Thu, 9 Feb 2023 19:55:36 +0100 Subject: [PATCH 1/4] Add ugly hack for netsnmp import on MacOS MacOS' System Integrity Protection wreaks havoc with NAV and its test suite, due to its dependency on pynetsnmp / NET-SNMP: pynetsnmp will load libcrypto using ctypes, which aborts the python process on MacOS with an error message like this: > WARNING: .tox/integration-py39-django32/bin/python is loading libcrypto > in an unsafe way When OpenSSL is installed on MacOS through homebrew, the environment variable DYLD_LIBRARY_PATH must be set for processes to load the correct version of libcrypto. However, newer versions of MacOS sanitizes this environment variable on many levels. The whole debaucle of trying to fix this in a sane way is described at https://hynek.me/articles/macos-dyld-env/ This workaround is no prettier, and will only work if libssl is installed using Homebrew, or if the value of DYLD_LIBRARY_PATH can be gleaned from LD_LIBRARY_PATH. --- python/nav/Snmp/__init__.py | 8 ++++++++ tox.ini | 2 ++ 2 files changed, 10 insertions(+) diff --git a/python/nav/Snmp/__init__.py b/python/nav/Snmp/__init__.py index 41fa44e845..572d777f17 100644 --- a/python/nav/Snmp/__init__.py +++ b/python/nav/Snmp/__init__.py @@ -21,6 +21,8 @@ """ from __future__ import absolute_import +import os +import sys BACKEND = None @@ -35,6 +37,12 @@ # These wildcard imports are informed, not just accidents. # pylint: disable=W0401 if BACKEND == 'pynetsnmp': + if sys.platform == "darwin" and not os.getenv("DYLD_LIBRARY_PATH"): + # horrible workaround for MacOS problems, described at length at + # https://hynek.me/articles/macos-dyld-env/ + os.environ["DYLD_LIBRARY_PATH"] = os.getenv( + "LD_LIBRARY_PATH", "/usr/local/opt/openssl/lib" + ) from .pynetsnmp import * else: raise ImportError("No supported SNMP backend was found") diff --git a/tox.ini b/tox.ini index edd85b533d..cf30fcf061 100644 --- a/tox.ini +++ b/tox.ini @@ -56,6 +56,8 @@ passenv = PGPASSWORD WORKSPACE DISPLAY + DYLD_LIBRARY_PATH + LD_LIBRARY_PATH allowlist_externals = sh sed From f175ad646a4b4010b86437424fece3c380ca1820 Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Fri, 10 Feb 2023 11:07:32 +0100 Subject: [PATCH 2/4] Install custom epollreactor for tests on Linux The epollreactor2 implementation is no good on other platforms. Without this, the test suite would not run on MacOS, e.g. --- tests/integration/conftest.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 437d0278cc..a7ddecd539 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -2,6 +2,7 @@ import os import importlib.util import io +import platform import re import shlex from itertools import cycle @@ -41,10 +42,11 @@ def pytest_configure(config): bootstrap_django('pytest') - # Install custom reactor for Twisted tests - from nav.ipdevpoll.epollreactor2 import install + if platform.system() == 'Linux': + # Install custom reactor for Twisted tests + from nav.ipdevpoll.epollreactor2 import install - install() + install() # Setup test environment for Django from django.test.utils import setup_test_environment From 5ba8c78015b8ef3528e40ed279b2b68b2d21bf81 Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Thu, 12 Sep 2024 18:40:05 +0200 Subject: [PATCH 3/4] Add news fragment. --- changelog.d/+macos-testsuite-fixes.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/+macos-testsuite-fixes.fixed.md diff --git a/changelog.d/+macos-testsuite-fixes.fixed.md b/changelog.d/+macos-testsuite-fixes.fixed.md new file mode 100644 index 0000000000..e6c9fae6ad --- /dev/null +++ b/changelog.d/+macos-testsuite-fixes.fixed.md @@ -0,0 +1 @@ +Make the test suite easier to run under MacOS \ No newline at end of file From 796a6a1cfa63f7b6630d8202fab29e9891acaa49 Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Tue, 5 Sep 2023 15:58:46 +0200 Subject: [PATCH 4/4] Pass C_INCLUDE_PATH to test environment At least two or three of NAVs dependencies may need to be built from source when pulled in by pip: psycopg2, python-ldap and Pillow. On platforms like NixOS, the include files for the necessary C libraries may not be in standardized FHS locations, so the C_INCLUDE_PATH is used to convey their locations to the C compiler. --- tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tox.ini b/tox.ini index cf30fcf061..37c676541c 100644 --- a/tox.ini +++ b/tox.ini @@ -46,6 +46,7 @@ setenv = django32: DJANGO_VER=32 django40: DJANGO_VER=40 passenv = + C_INCLUDE_PATH GITHUB_ACTIONS GITHUB_RUN_ID USER