mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Reuse code instead of duplicating it
This commit is contained in:
parent
0c225de432
commit
a115ccfcb7
2 changed files with 45 additions and 50 deletions
|
@ -12,7 +12,6 @@
|
|||
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import fnmatch
|
||||
import logging
|
||||
import os
|
||||
import pprint
|
||||
|
@ -44,7 +43,7 @@ from salt.serializers import yaml
|
|||
from salt.utils.immutabletypes import freeze
|
||||
from tests.support.helpers import PRE_PYTEST_SKIP_OR_NOT, PRE_PYTEST_SKIP_REASON
|
||||
from tests.support.runtests import RUNTIME_VARS
|
||||
from tests.support.sminion import create_sminion
|
||||
from tests.support.sminion import check_required_sminion_attributes, create_sminion
|
||||
|
||||
TESTS_DIR = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
|
||||
CODE_DIR = os.path.dirname(TESTS_DIR)
|
||||
|
@ -654,27 +653,9 @@ def pytest_runtest_setup(item):
|
|||
):
|
||||
required_salt_modules = required_salt_modules[0]
|
||||
required_salt_modules = set(required_salt_modules)
|
||||
sminion = create_sminion()
|
||||
available_modules = list(sminion.functions)
|
||||
not_available_modules = set()
|
||||
try:
|
||||
cached_not_available_modules = sminion.__not_available_modules__
|
||||
except AttributeError:
|
||||
cached_not_available_modules = sminion.__not_available_modules__ = set()
|
||||
|
||||
if cached_not_available_modules:
|
||||
for not_available_module in cached_not_available_modules:
|
||||
if not_available_module in required_salt_modules:
|
||||
not_available_modules.add(not_available_module)
|
||||
required_salt_modules.remove(not_available_module)
|
||||
|
||||
for required_module_name in required_salt_modules:
|
||||
search_name = required_module_name
|
||||
if "." not in search_name:
|
||||
search_name += ".*"
|
||||
if not fnmatch.filter(available_modules, search_name):
|
||||
not_available_modules.add(required_module_name)
|
||||
cached_not_available_modules.add(required_module_name)
|
||||
not_available_modules = check_required_sminion_attributes(
|
||||
"functions", required_salt_modules
|
||||
)
|
||||
|
||||
if not_available_modules:
|
||||
item._skipped_by_mark = True
|
||||
|
@ -696,27 +677,9 @@ def pytest_runtest_setup(item):
|
|||
):
|
||||
required_salt_states = required_salt_states[0]
|
||||
required_salt_states = set(required_salt_states)
|
||||
sminion = create_sminion()
|
||||
available_modules = list(sminion.functions)
|
||||
not_available_states = set()
|
||||
try:
|
||||
cached_not_available_states = sminion.__not_available_states__
|
||||
except AttributeError:
|
||||
cached_not_available_states = sminion.__not_available_states__ = set()
|
||||
|
||||
if cached_not_available_states:
|
||||
for not_available_module in cached_not_available_states:
|
||||
if not_available_module in required_salt_states:
|
||||
not_available_states.add(not_available_module)
|
||||
required_salt_states.remove(not_available_module)
|
||||
|
||||
for required_state_name in required_salt_states:
|
||||
search_name = required_state_name
|
||||
if "." not in search_name:
|
||||
search_name += ".*"
|
||||
if not fnmatch.filter(available_modules, search_name):
|
||||
not_available_states.add(required_state_name)
|
||||
cached_not_available_states.add(required_state_name)
|
||||
not_available_states = check_required_sminion_attributes(
|
||||
"states", required_salt_states
|
||||
)
|
||||
|
||||
if not_available_states:
|
||||
item._skipped_by_mark = True
|
||||
|
|
|
@ -5,25 +5,24 @@ tests.support.sminion
|
|||
|
||||
SMinion's support functions
|
||||
"""
|
||||
# Import python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import fnmatch
|
||||
import hashlib
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
# Import salt libs
|
||||
import salt.minion
|
||||
import salt.utils.path
|
||||
import salt.utils.stringutils
|
||||
|
||||
# Import testing libs
|
||||
from tests.support.runtests import RUNTIME_VARS
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_SMINION_ID = "pytest-internal-sminion"
|
||||
|
||||
|
||||
def build_minion_opts(
|
||||
minion_id=None,
|
||||
|
@ -35,7 +34,7 @@ def build_minion_opts(
|
|||
minion_role=None,
|
||||
):
|
||||
if minion_id is None:
|
||||
minion_id = "pytest-internal-sminion"
|
||||
minion_id = DEFAULT_SMINION_ID
|
||||
if skip_cached_opts is False:
|
||||
try:
|
||||
opts_cache = build_minion_opts.__cached_opts__
|
||||
|
@ -198,7 +197,7 @@ def create_sminion(
|
|||
cache_sminion=True,
|
||||
):
|
||||
if minion_id is None:
|
||||
minion_id = "pytest-internal-sminion"
|
||||
minion_id = DEFAULT_SMINION_ID
|
||||
if skip_cached_minion is False:
|
||||
try:
|
||||
minions_cache = create_sminion.__cached_minions__
|
||||
|
@ -224,3 +223,36 @@ def create_sminion(
|
|||
minions_cache = create_sminion.__cached_minions__ = {}
|
||||
minions_cache[minion_id] = sminion
|
||||
return sminion
|
||||
|
||||
|
||||
def check_required_sminion_attributes(sminion_attr, required_items):
|
||||
"""
|
||||
:param sminion_attr: The name of the sminion attribute to check, such as 'functions' or 'states'
|
||||
:param required_items: The items that must be part of the designated sminion attribute for the decorated test
|
||||
:return The packages that are not available
|
||||
"""
|
||||
required_salt_items = set(required_items)
|
||||
sminion = create_sminion(minion_id=DEFAULT_SMINION_ID)
|
||||
available_items = list(getattr(sminion, sminion_attr))
|
||||
not_available_items = set()
|
||||
|
||||
name = "__not_available_{items}s__".format(items=sminion_attr)
|
||||
if not hasattr(sminion, name):
|
||||
setattr(sminion, name, set())
|
||||
|
||||
cached_not_available_items = getattr(sminion, name)
|
||||
|
||||
for not_available_item in cached_not_available_items:
|
||||
if not_available_item in required_salt_items:
|
||||
not_available_items.add(not_available_item)
|
||||
required_salt_items.remove(not_available_item)
|
||||
|
||||
for required_item_name in required_salt_items:
|
||||
search_name = required_item_name
|
||||
if "." not in search_name:
|
||||
search_name += ".*"
|
||||
if not fnmatch.filter(available_items, search_name):
|
||||
not_available_items.add(required_item_name)
|
||||
cached_not_available_items.add(required_item_name)
|
||||
|
||||
return not_available_items
|
||||
|
|
Loading…
Add table
Reference in a new issue