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
3 changes: 3 additions & 0 deletions tests/bgp/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tests.common.utilities import wait_until
from tests.common.helpers.assertions import pytest_assert as pt_assert
from tests.common.helpers.parallel import parallel_run
from tests.common.helpers.parallel import reset_ansible_local_tmp

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -44,6 +45,7 @@ def setup_bgp_graceful_restart(duthost, nbrhosts):
config_facts = duthost.config_facts(host=duthost.hostname, source="running")['ansible_facts']
bgp_neighbors = config_facts.get('BGP_NEIGHBOR', {})

@reset_ansible_local_tmp
def configure_nbr_gr(node=None, results=None):
"""Target function will be used by multiprocessing for configuring VM hosts.

Expand Down Expand Up @@ -89,6 +91,7 @@ def configure_nbr_gr(node=None, results=None):

yield

@reset_ansible_local_tmp
def restore_nbr_gr(node=None, results=None):
"""Target function will be used by multiprocessing for restoring configuration for the VM hosts.

Expand Down
27 changes: 27 additions & 0 deletions tests/common/helpers/parallel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
import logging
import os
import shutil
import tempfile
import signal
from multiprocessing import Process, Manager
from tests.common.helpers.assertions import pytest_assert as pt_assert
Expand Down Expand Up @@ -77,3 +79,28 @@ def parallel_run(target, args, kwargs, nodes, timeout=None):
logger.info('Completed running processes for target "{}" in {} seconds'.format(target.__name__, str(delta_time)))

return results


def reset_ansible_local_tmp(target):
"""Decorator for resetting ansible default local tmp dir for parallel multiprocessing.Process

Args:
target (function): The function to be decorated.
"""

def wrapper(*args, **kwargs):

# Reset the ansible default local tmp directory for the current subprocess
# Otherwise, multiple processes could share a same ansible default tmp directory and there could be conflicts
from ansible import constants
prefix = 'ansible-local-{}'.format(os.getpid())
constants.DEFAULT_LOCAL_TMP = tempfile.mkdtemp(prefix=prefix)
try:
target(*args, **kwargs)
finally:
# User of tempfile.mkdtemp need to take care of cleaning up.
shutil.rmtree(constants.DEFAULT_LOCAL_TMP)

wrapper.__name__ = target.__name__

return wrapper