Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cosmo/clients/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import multiprocessing


def get_client_mp_context(method=None):
# this will set and return the global context, by default
# since method is None. the reason why we use this
# boilerplate is to have an anchor point for
# monkeypatching the multiprocessing context
# during tests.
return multiprocessing.get_context(method=method)
3 changes: 2 additions & 1 deletion cosmo/clients/netbox_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from multiprocessing import Manager
from string import Template

from cosmo.clients import get_client_mp_context
from cosmo.clients.netbox_client import NetboxAPIClient


Expand Down Expand Up @@ -658,7 +659,7 @@ def get_data(self, device_config):
# from the main thread. by default this is not possible, see
# https://stackoverflow.com/questions/72411392/can-you-do-nested-parallelization-using-multiprocessing-in-python
# this avoids having to re-architecture completely using worker/task/queue model.
with Manager() as manager:
with get_client_mp_context().Manager() as manager:
client = NetboxAPIClient(self.url, self.token, manager.dict())

for d in device_list:
Expand Down
7 changes: 7 additions & 0 deletions cosmo/tests/test_netboxclient.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from unittest.mock import patch

import pytest
from packaging.version import Version

import cosmo.tests.utils as utils
from cosmo.clients.netbox import NetboxClient
from cosmo.tests.utils import override_get_client_mp_context

TEST_URL = "https://netbox.example.com"
TEST_TOKEN = "token123"
TEST_DEVICE_CFG = {"router": ["router1", "router2"], "switch": ["switch1", "switch2"]}


@patch(
"cosmo.clients.netbox_v4.get_client_mp_context",
new=override_get_client_mp_context("fork"),
)
def test_case_get_data(mocker):
mockAnswer = {
"device_list": [],
Expand Down
17 changes: 17 additions & 0 deletions cosmo/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import json
import multiprocessing


def override_get_client_mp_context(method=None):
def overriden_get_client_mp_context():
return multiprocessing.get_context(method=method)

return overriden_get_client_mp_context


class CommonSetup:
Expand All @@ -17,6 +25,15 @@ def __init__(
self.patches.append(self.mocker.patch.dict("os.environ", environ))
# patch args, since current ones are from pytest call
self.patches.append(self.mocker.patch("sys.argv", args))
# patch netbox client default mp context for the time of the tests
# patch may be ineffective since not in correct namespace(?)
self.patches.append(
self.mocker.patch(
"cosmo.clients.netbox_v4.get_client_mp_context",
new=override_get_client_mp_context("fork"),
)
)

# patch configuration file lookup if requested

# Note: If there is no configuration file given, we still need to patch this.
Expand Down
14 changes: 1 addition & 13 deletions package.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{ lib,
stdenv,
buildPythonApplication,
{ buildPythonApplication,
python,
poetry-core,
requests,
Expand Down Expand Up @@ -41,16 +39,6 @@ buildPythonApplication rec {
coverage
];

disabledTests = lib.optionals stdenv.hostPlatform.isDarwin [
# https://github.com/wobcom/cosmo/issues/85
"test_limit_argument_with_commas"
"test_limit_arguments_with_repeat"
"test_device_generation_ansible"
"test_device_generation_nix"
"test_device_processing_error"
"test_case_get_data"
];

passthru = {
pythonEnv = python.withPackages (_: dependencies);
};
Expand Down