403Webshell
Server IP : 198.38.94.13  /  Your IP : 216.73.217.33
Web Server : Apache
System : Linux d4744.dxb1.stableserver.net 5.14.0-611.49.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Apr 21 16:39:08 EDT 2026 x86_64
User : revivere ( 1140)
PHP Version : 8.2.33
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/saltcheck.cpython-310.pyc
o

;j7��@s�dZddlZddlZddlZddlZddlZddlmZddlZ	ddl
Z	ddlZ	ddlZ	ddl
Z	ddlZ	ddlZ	ddlZ	ddlmZddlmZddlmZmZe�e�ZzeWneydiZYnwded<dZd	d
�Zdd�Zd
d�Z d*dd�Z!d+dd�Z"dd�Z#e	j$j%�&e"d�Z'd,dd�Z(dd�Z)d-dd�Z*dd�Z+edd ��Z,ed!d"��Z-d.d$d%�Z.Gd&d'�d'�Z/Gd(d)�d)�Z0dS)/a�#
A module for testing the logic of states and highstates on salt minions

:codeauthor:    William Cannon <william.cannon@gmail.com>
:maturity:      new

Saltcheck provides unittest like functionality requiring only the knowledge of
salt module execution and yaml. Saltcheck uses salt modules to return data, then
runs an assertion against that return. This allows for testing with all the
features included in salt modules.

In order to run state and highstate saltcheck tests, a sub-folder in the state directory
must be created and named ``saltcheck-tests``. Tests for a state should be created in files
ending in ``*.tst`` and placed in the ``saltcheck-tests`` folder. ``tst`` files are run
through the salt rendering system, enabling tests to be written in yaml (or renderer of choice),
and include jinja, as well as the usual grain and pillar information. Like states, multiple tests can
be specified in a ``tst`` file. Multiple ``tst`` files can be created in the ``saltcheck-tests``
folder, and should be named the same as the associated state. The ``id`` of a test works in the
same manner as in salt state files and should be unique and descriptive.


.. versionadded:: 3000
    The ``saltcheck-tests`` folder can be customized using the ``saltcheck_test_location`` minion
    configuration setting.  This setting is a relative path from the formula's ``salt://`` path
    to the test files.

Usage
=====

Example Default file system layout:

.. code-block:: text

    /srv/salt/apache/
        init.sls
        config.sls
        saltcheck-tests/
            init.tst
            config.tst
            deployment_validation.tst

Alternative example file system layout with custom saltcheck_test_location:

Minion configuration:
---------------------

.. code-block:: yaml

    saltcheck_test_location: tests/integration/saltcheck

Filesystem layout:
------------------

.. code-block:: text

    /srv/salt/apache/
        init.sls
        config.sls
        tests/integration/saltcheck/
            init.tst
            config.tst
            deployment_validation.tst

Tests can be run for each state by name, for all ``apache/saltcheck/*.tst``
files, or for all states assigned to the minion in top.sls. Tests may also be
created with no associated state. These tests will be run through the use of
``saltcheck.run_state_tests``, but will not be automatically run by
``saltcheck.run_highstate_tests``.

.. code-block:: bash

    salt '*' saltcheck.run_state_tests apache,apache.config
    salt '*' saltcheck.run_state_tests apache check_all=True
    salt '*' saltcheck.run_highstate_tests
    salt '*' saltcheck.run_state_tests apache.deployment_validation

Saltcheck Keywords
==================

**module_and_function:**
    (str) This is the salt module which will be run locally,
    the same as ``salt-call --local <module>``. The ``saltcheck.state_apply`` module name is
    special as it bypasses the local option in order to resolve state names when run in
    a master/minion environment.
**args:**
    (list) Optional arguments passed to the salt module
**kwargs:**
    (dict) Optional keyword arguments to be passed to the salt module
**assertion:**
    (str) One of the supported assertions and required except for ``saltcheck.state_apply``
    Tests which fail the assertion and expected_return, cause saltcheck to exit which a non-zero exit code.
**expected_return:**
    (str) Required except by ``assertEmpty``, ``assertNotEmpty``, ``assertTrue``,
    ``assertFalse``. The return of module_and_function is compared to this value in the assertion.
**assertion_section:**
    (str) Optional keyword used to parse the module_and_function return. If a salt module
    returns a dictionary as a result, the ``assertion_section`` value is used to lookup a specific value
    in that return for the assertion comparison.
**assertion_section_delimiter:**
    (str) Optional delimiter to use when splitting a nested structure.
    Defaults to ':'
**print_result:**
    (bool) Optional keyword to show results in the ``assertEqual``, ``assertNotEqual``,
    ``assertIn``, and ``assertNotIn`` output. Defaults to True.
**output_details:**
    (bool) Optional keyword to display ``module_and_function``, ``args``, ``assertion_section``,
    and assertion results text in the output. If print_result is False, assertion results will be hidden.
    This is a per test setting, but can be set globally for all tests by adding ``saltcheck_output_details: True``
    in the minion configuration file.
    Defaults to False
**pillar_data:**
    (dict) Optional keyword for passing in pillar data. Intended for use in potential test
    setup or teardown with the ``saltcheck.state_apply`` function.
**skip:**
    (bool) Optional keyword to skip running the individual test

.. versionadded:: 3000
    Multiple assertions can be run against the output of a single ``module_and_function`` call. The ``assertion``,
    ``expected_return``, ``assertion_section``, and ``assertion_section_delimiter`` keys can be placed in a list under an
    ``assertions`` key. See the multiple assertions example below.

Sample Cases/Examples
=====================

Basic Example
-------------

.. code-block:: yaml

    echo_test_hello:
      module_and_function: test.echo
      args:
        - "hello"
      kwargs:
      assertion: assertEqual
      expected_return:  'hello'

Example with jinja
------------------

.. code-block:: jinja

    {% for package in ["apache2", "openssh"] %}
    {# or another example #}
    {# for package in salt['pillar.get']("packages") #}
    test_{{ package }}_latest:
      module_and_function: pkg.upgrade_available
      args:
        - {{ package }}
      assertion: assertFalse
    {% endfor %}

Example with setup state including pillar
-----------------------------------------

.. code-block:: yaml

    setup_test_environment:
      module_and_function: saltcheck.state_apply
      args:
        - common
      pillar_data:
        data: value

    verify_vim:
      module_and_function: pkg.version
      args:
        - vim
      assertion: assertNotEmpty

Example with jinja
------------------

.. code-block:: jinja

    {% for package in ["apache2", "openssh"] %}
    {# or another example #}
    {# for package in salt['pillar.get']("packages") #}
    test_{{ package }}_latest:
      module_and_function: pkg.upgrade_available
      args:
        - {{ package }}
      assertion: assertFalse
    {% endfor %}

Example with setup state including pillar
-----------------------------------------

.. code-block:: yaml

    setup_test_environment:
      module_and_function: saltcheck.state_apply
      args:
        - common
      pillar-data:
        data: value

    verify_vim:
      module_and_function: pkg.version
      args:
        - vim
      assertion: assertNotEmpty

Example with skip
-----------------

.. code-block:: yaml

    package_latest:
      module_and_function: pkg.upgrade_available
      args:
        - apache2
      assertion: assertFalse
      skip: True

Example with assertion_section
------------------------------

.. code-block:: yaml

    validate_shell:
      module_and_function: user.info
      args:
        - root
      assertion: assertEqual
      expected_return: /bin/bash
      assertion_section: shell

Example with a nested assertion_section
---------------------------------------

.. code-block:: yaml

    validate_smb_signing:
      module_and_function: lgpo.get
      args:
        - 'Machine'
      kwargs:
        return_full_policy_names: True
      assertion: assertEqual
      expected_return: Enabled
      assertion_section: 'Computer Configuration|Microsoft network client: Digitally sign communications (always)'
      assertion_section_delimiter: '|'

Example suppressing print results
---------------------------------

.. code-block:: yaml

    validate_env_nameNode:
      module_and_function: hadoop.dfs
      args:
        - text
        - /oozie/common/env.properties
      expected_return: nameNode = hdfs://nameservice2
      assertion: assertNotIn
      print_result: False

Example with multiple assertions and output_details
---------------------------------------------------

.. code-block:: yaml

    multiple_validations:
      module_and_function: network.netstat
      assertions:
        - assertion: assertEqual
          assertion_section: "0:program"
          expected_return: "systemd-resolve"
        - assertion: assertEqual
          assertion_section: "0:proto"
          expected_return: "udp"
      output_details: True

Supported assertions
====================

* assertEqual
* assertNotEqual
* assertTrue
* assertFalse
* assertIn
* assertNotIn
* assertGreater
* assertGreaterEqual
* assertLess
* assertLessEqual
* assertEmpty
* assertNotEmpty

.. warning::

  The saltcheck.state_apply function is an alias for
  :py:func:`state.apply <salt.modules.state.apply>`. If using the
  :ref:`ACL system <acl-eauth>` ``saltcheck.*`` might provide more capability
  than intended if only ``saltcheck.run_state_tests`` and
  ``saltcheck.run_highstate_tests`` are needed.
�N)�OrderedDict)�DEFAULT_TARGET_DELIM)�memoize)�dumps�loads�
global_scheckZ	saltcheckcCstjj��stSdS)z>
    Set the virtual pkg module if not running as a proxy
    )FzIThe saltcheck execution module failed to load: only available on minions.)�salt�utils�platformZis_proxy�__virtualname__�rr�J/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/saltcheck.py�__virtual__JsrcKs.t�}|�dd�}|rt|t�r|�|�SdS)am
    Execute one saltcheck test and return result

    :param keyword arg test:

    CLI Example:

    .. code-block:: bash

        salt '*' saltcheck.run_test
            test='{"module_and_function": "test.echo",
                   "assertion": "assertEqual",
                   "expected_return": "This works!",
                   "args":["This works!"] }'
    �testNz"Test argument must be a dictionary)�	SaltCheck�get�
isinstance�dict�run_test)�kwargsZscheckrrrr
rVs

rcKsnt�td�}tj�|�}dt�dd�vrd|d<t�d�tj	j
|d�}|r1|jd	|fi|��S|�d	|�S)
az
    Runs :py:func:`state.apply <salt.modules.state.apply>` with given options to set up test data.
    Intended to be used for optional test setup or teardown

    Reference the :py:func:`state.apply <salt.modules.state.apply>` module documentation for arguments and usage options

    CLI Example:

    .. code-block:: bash

        salt '*' saltcheck.state_apply postfix
    �	conf_file�$running_data/var/run/salt-minion.pid�pidfileF�local�file_clientz#Detected salt-ssh, running as local�Zmopts�state.apply)�copy�deepcopy�__opts__r�config�
minion_configr�log�debug�client�Caller�cmd)�
state_namerr�
local_optsZcallerrrr
�state_applyos
r)cCs�|sdtvrtdrtd}nd}g}t|�}t|�}d}g}|D]}|�|d�||jvr7|d}|�|�q!|td<d|||jd�iS)	a
    Report on tests for states assigned to the minion through highstate.
    Quits with the exit code for the number of missing tests.

    CLI Example:

    .. code-block:: bash

        salt '*' saltcheck.report_highstate_tests

    .. versionadded:: 3000
    �saltenv�baserF��retcodezTEST REPORT RESULTS)�
Missing TestszStates missing testszStates with tests)r�_get_top_states�StateTestLoader�add_test_files_for_sls�found_states�append�__context__)r*�sls_list�stl�
missing_testsZstates_missing_testsr'rrr
�report_highstate_tests�s,



���r8FcCs�|sdtvrtdrtd}nd}t|�td<tdd�}tdd�}t|�}t�}tjj�	|�}|D]�}	|�
|	|�|��t�}
|r~t|t
�rMt|�}t��dkr[d}t�d	�n#|d
krgd}t�d�n|j��D]}d|�d
g�vr}d}t�d�ql|r�|r�|}n
tt|j�t���}t�d|�t�|�jt|j��d�}
|
D]}|��D]\}}||
|<q�q�n|j��D]\}}td�|�}||
|<q�|�|	�s�|
||	<q3t||d�S)a
    Execute tests for a salt state and return results
    Nested states will also be tested

    :param str state: state name for which to run associated .tst test files
    :param str saltenv: optional saltenv. Defaults to base
    :param bool check_all: boolean to run all tests in state/saltcheck-tests directory
    :param bool only_fails: boolean to only print failure results

    CLI Example:

    .. code-block:: bash

        salt '*' saltcheck.run_state_tests postfix,common

    Tests will be run in parallel by adding "saltcheck_parallel: True" in minion config.
    When enabled, saltcheck will use up to the number of cores detected. This can be limited
    by setting the "saltcheck_processes" value to an integer to set the maximum number
    of parallel processes.
    r*r+r�
config.getZsaltcheck_parallelZsaltcheck_processes�Fz$Only 1 CPU. Disabling parallization.r,z8Configuration limited to 1 CPU. Disabling parallization.r�module_and_functionz3Tests include state.apply. Disabling parallization.z+Running tests in parallel with %s processes)�func�iterable)�
only_fails)rrr4�__salt__r0rrr	�argsZsplit_inputr1�load_test_suiter�float�int�multiprocessing�	cpu_countr"r#�	test_dict�valuesr�warning�min�lenZPool�map�parallel_scheck�itemsr�_generate_out_list)�stater*�	check_allr>ZparallelZnum_procr6�resultsr5r'�results_dictrMZ	pool_sizeZpresults�item�key�value�resultrrr
�run_state_tests�sd

��

�
��

�rWcCs*|d}|d}i}td�|�||<|S)ztriggers salt-call in parallelrr,r)r4r)�datarTrUrQrrr
rL
s
rL�run_state_tests_sshcCsF|sdtvrtdrtd}nd}g}t|�}d�|�}t|||d�S)a<
    Execute all tests for states assigned to the minion through highstate and return results

    :param str saltenv: optional saltenv. Defaults to base
    :param bool only_fails: boolean to only print failure results

    CLI Example:

    .. code-block:: bash

        salt '*' saltcheck.run_highstate_tests
    r*r+�,)r*r>)rr/�joinrW)r*r>r5�
all_statesrrr
�run_highstate_testss


r]cCsv|r5i}||D]*}|||d�d�r2|�|�r(||�||||i�q||||i||<q|S|||iS)z<
    For given results, only return failures if desired
    �status�Fail)�
startswithr�update)r'rQr>Zfailed_testsrrrr
�_eval_failure_only_print1s
�rbc	Cs�d}d}d}d}d}g}|D]J}||��s|d}n4||��D]-\}	}
|
d�d�r0|d}|
d�d�r;|d}|
d�d�rF|d}|t|
d�}q!|�t|||��qt|d	d
�d�}|�dt|d
�||||d�i�|rudndtd<|S)z+
    generate test results output list
    r�r,r^�Passr_�Skip�durationcSst|���S�N)�sorted�keys)�xrrr
�<lambda>Zsz$_generate_out_list.<locals>.<lambda>)rTzTEST RESULTS�)zExecution TimeZPassedZFailedZSkippedr.r-)rMr`rBr3rbrh�roundr4)rQr>ZpassedZfailedZskippedr7Z
total_timeZout_listrO�_�valrrr
rNCs>
���
rNcCs&td|tdjd�}t�d|�|S)z0
    call the salt utility to render a file
    zslsutil.rendererr�r*zrendered: %s)r?r4r*r"�info)Z	file_pathZrenderedrrr
�_render_filels

�rrcCstd�}t||v�S)z:
    Return a list of all modules available on minion
    zsys.list_modules)r?�bool)�module�modulesrrr
�_is_valid_modulexs
rvcCs@ztd|�}Wntjjydg}Ynw|�d|��|vS)z7
    Determine if a function is valid for a module
    zsys.list_functionszunable to look up functions�.)r?r�
exceptionsZ
SaltException)�module_name�functionZ	functionsrrr
�_is_valid_function�s
�r{r+cCs(g}td|d�|}t�d||�|S)z;
    Equivalent to a salt cli: salt web state.show_top
    zstate.show_toprpz.saltcheck for saltenv: %s found top states: %s)r?r"r#)r*Z
top_statesrrr
r/�sr/c@s�eZdZdZd+dd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	e
dd��Ze
d,dd��Ze
d,dd��Z
e
dd��Ze
dd��Ze
d,dd��Ze
d,dd��Ze
dd��Ze
d d!��Ze
d"d#��Ze
d$d%��Ze
d&d'��Ze
d(d)��Zd*S)-rz6
    This class validates and runs the saltchecks
    r+cCs,g|_g|_i|_i|_||_d��|_dS)NaiassertEqual assertNotEqual
                                  assertTrue assertFalse
                                  assertIn assertNotIn
                                  assertGreater
                                  assertGreaterEqual
                                  assertLess assertLessEqual
                                  assertEmpty assertNotEmpty)Zsls_list_staterurRZresults_dict_summaryr*�split�assertions_list��selfr*rrr
�__init__�s�zSaltCheck.__init__cs�d}��dd�}t�fdd�dD��}��d��dd��}||jvr*t�d	|�d
}|dvrD|dur9t�d�d
}|durDt�d
�d
}|S)zValidate assertion keysT�	assertionNc3s�|]	}|���vVqdSrg)ri)�.0rT�rrr
�	<genexpr>�s�
�z.SaltCheck._check_assertions.<locals>.<genexpr>)�expected_return�expected-returnr�r�z+Saltcheck: %s is not in the assertions listF)�assertEmpty�assertNotEmpty�
assertTrue�assertFalsez"Saltcheck: missing expected_returnz*Saltcheck: expected_return missing a value)r�anyr}r"�error)rr�is_validr�Zexp_ret_keyZexp_ret_valrr�r
�_check_assertions�s"�


zSaltCheck._check_assertionscCs�t�d|�d}|�dd�}|�dd�}|dkr|S|�d�r-|�d�D]}|�|�}q$n|�|�}|rV|�d	�\}}t|�sGd}t�d
|�t||�sTd}t�d|�|St�d�d}|S)
z�
        Determine if a test contains:

        - a test name
        - a valid module and function
        - a valid assertion, or valid grouping under an assertions key
        - an expected return value - if assertion type requires it
        zSaltcheck: validating data: %sT�skipFr;N�saltcheck.state_apply�
assertionsrwz#Saltcheck: %s is not a valid modulez%Saltcheck: %s is not a valid functionz&Saltcheck: missing module_and_function)r"rqrr�r|rvr�r{)rrFr�r�Zm_and_fZassertion_grouprtrzrrr
Z__is_valid_test�s.	
�


�zSaltCheck.__is_valid_testcCs�dg}dg}|jddd�d}td}tj�|�}|d}	t�|�}
||vr.||vr.d	|
d<d
}|rF|rFtjj|
d�j	|g|�Ri|��}n0|rY|sYtjj|
d�j	|g|�R�}n|sl|rltjj|
d�j	|fi|��}n
tjj|
d��	|�}|	td<|S)z5
        Generic call of salt Caller command
        zfile.check_managed_changesZcprwr,)�maxsplitrrrrFr)
r|rrr r!rrr$r%r&)rZfunr@rZremote_functionsZremote_modules�modrr(Zorig_file_clientZmlocal_optsrUrrr
�_call_salt_command�s&
$zSaltCheck._call_salt_commandcCs^i}|�dd�}|�dt�}	|rtjjj||d|	d�}|dvr"d}
n|d}
|�d	|�d
d��}|
dvr:|�||�}|
dkrJd
}|�|||�|d<n�|
dkrZd}|�|||�|d<n�|
dkrhd}|�	|�|d<n�|
dkrvd}|�
|�|d<n||
dkr�d}|�|||�|d<nl|
dkr�d}|�|||�|d<n\|
dkr�d}|�
||�|d<nM|
dkr�d}|�||�|d<n>|
dkr�d}|�||�|d<n/|
dkr�d }|�||�|d<n |
d!kr�d"}|�|�|d<n|
dkr�d#}|�|�|d<nd$|d<|�r-|�rd%�d�}
d&|��}nd'}
d'}d(�|t|�|�|d)|
��<d*�|du�rd'n|�d&�||�s(d+n|�|d,<|S)-z-
        Run assertion against input
        �assertion_sectionN�assertion_section_delimiterF)�default�	delimiter)r�r�r�r�r�)�assertIn�assertNotInr�r�r�r�ZassertEqualz==r^ZassertNotEqualz!=r�zTrue isr�zFalse isr��INr�zNOT INZ
assertGreater�>ZassertGreaterEqualz>=Z
assertLess�<ZassertLessEqualz<=r�zIS EMPTYzIS NOT EMPTYzFail - bad assertionz {}� �z{} {}{}zmodule.function [args]z{}{} {}Zhiddenzsaltcheck assertion)rrrr	rXZtraverse_dict_and_list�_cast_expected_to_returned_type�_SaltCheck__assert_equal�_SaltCheck__assert_not_equal�_SaltCheck__assert_true�_SaltCheck__assert_false�_SaltCheck__assert_in�_SaltCheck__assert_not_in�_SaltCheck__assert_greater� _SaltCheck__assert_greater_equal�_SaltCheck__assert_less�_SaltCheck__assert_less_equal�_SaltCheck__assert_empty�_SaltCheck__assert_not_empty�formatr)r�mod_and_funcr@rXZ
module_output�output_details�assert_print_resultrUr�r�r�r�Zassertion_descZassertion_section_repr_titleZassertion_section_repr_valuerrr
�_run_assertions
s����
�
�
�
�
�
���zSaltCheck._run_assertionsc
Cs�i}t��}tddd�}|�d|�}|�|�r�|�dd�}|r%ddd�S|d	}|�d
d�}|�dd�}	|�d
|�dd��}
|
rJ|	sEi}	|
|	d<n|	rR|	�dd�|�dd�}|�|||	�}|�d�r�t|�d�dd�D]\}
}|�||||||�|d|
��<qmt�	|��
�D]%\}}|�d�r�||�
�D]\}}|�d�r�|||dkr�d|d<q�q�|�d�s�d|d<n|�|�||||||��nd|d<t��}t
||d�|d<|S)z-
        Run a single saltcheck test
        r9Zsaltcheck_output_detailsFr�r�rerc)r^rfr;r@Nr�pillar_datazpillar-dataZpillarZprint_resultTr�r,)�startr�r^rdr_zFail - invalid testrlrf)�timer?r�_SaltCheck__is_valid_test�popr��	enumerater�rrrMr`rarm)rrFrVr�Zglobal_output_detailsr�r�r�r@rr�r�Z
actual_return�numZassert_group�k�vZassert_kZassert_v�endrrr
r�sx�

�


��


�
���zSaltCheck.run_testcCs�|}|durGt|�}|dkr|tkrd}z||�}W|StyFt�d�t�d|�t�dt|��t�d|�t�dt|��Y|Sw|S)	zt
        Determine the type of variable returned
        Cast the expected to the type of variable returned
        N�FalseFz-Unable to cast expected into type of returnedz
returned = %sztype of returned = %sz
expected = %sztype of expected = %s)�typers�
ValueErrorr"rq)�expected�returnedZnew_expectedZret_typerrr
r��s"
�
�z)SaltCheck._cast_expected_to_returned_typeTc
Cspd}z|r||ksJd�||���W|S||ksJd��W|Sty7}z
dt|�}WYd}~|Sd}~ww)z/
        Test if two objects are equal
        rdz{} is not equal to {}zResult is not equal�Fail: N�r��AssertionError�str�r�r�r�rV�errrrr
Z__assert_equal�s�����zSaltCheck.__assert_equalc
Cspd}z|r||ksJd�||���W|S||ksJd��W|Sty7}z
dt|�}WYd}~|Sd}~ww)z3
        Test if two objects are not equal
        rdz{} is equal to {}zResult is equalr�Nr�r�rrr
Z__assert_not_equal�������zSaltCheck.__assert_not_equalc
CsTd}z|dusJ|�d���W|Sty)}z
dt|�}WYd}~|Sd}~ww)z,
        Test if an boolean is True
        rdTz	 not Truer�N�r�r��r�rVr�rrr
Z
__assert_true����zSaltCheck.__assert_truec
Csfd}t|t�rt|�}z|dusJ|�d���W|Sty2}z
dt|�}WYd}~|Sd}~ww)z-
        Test if an boolean is False
        rdF�
 not Falser�N)rr�rsr�r�rrr
Z__assert_falses
���zSaltCheck.__assert_falsec
Cspd}z|r||vsJd�||���W|S||vsJd��W|Sty7}z
dt|�}WYd}~|Sd}~ww)zC
        Test if a value is in the list of returned values
        rdz{} not found in {}zResult not foundr�Nr�r�rrr
Z__assert_inr�zSaltCheck.__assert_inc
Cspd}z|r||vsJd�||���W|S||vsJd��W|Sty7}z
dt|�}WYd}~|Sd}~ww)zG
        Test if a value is not in the list of returned values
        rdz{} was found in {}zResult was foundr�Nr�r�rrr
Z__assert_not_in.r�zSaltCheck.__assert_not_inc
CsTd}z||ksJ|�d���W|Sty)}z
dt|�}WYd}~|Sd}~ww)zD
        Test if a value is greater than the returned value
        rdr�r�Nr��r�r�rVr�rrr
Z__assert_greater?r�zSaltCheck.__assert_greaterc
CsTd}z||ksJ|�d���W|Sty)}z
dt|�}WYd}~|Sd}~ww)zP
        Test if a value is greater than or equal to the returned value
        rdr�r�Nr�r�rrr
Z__assert_greater_equalKr�z SaltCheck.__assert_greater_equalc
CsTd}z||ksJ|�d���W|Sty)}z
dt|�}WYd}~|Sd}~ww)zA
        Test if a value is less than the returned value
        rdr�r�Nr�r�rrr
Z
__assert_lessWr�zSaltCheck.__assert_lessc
CsTd}z||ksJ|�d���W|Sty)}z
dt|�}WYd}~|Sd}~ww)zM
        Test if a value is less than or equal to the returned value
        rdr�r�Nr�r�rrr
Z__assert_less_equalcr�zSaltCheck.__assert_less_equalc
CsPd}z|rJ|�d���W|Sty'}z
dt|�}WYd}~|Sd}~ww)z3
        Test if a returned value is empty
        rdz
 is not emptyr�Nr�r�rrr
Z__assert_emptyos���zSaltCheck.__assert_emptyc
CsJd}z	|s	Jd��W|Sty$}z
dt|�}WYd}~|Sd}~ww)z7
        Test if a returned value is not empty
        rdzvalue is emptyr�Nr�r�rrr
Z__assert_not_empty{s���zSaltCheck.__assert_not_emptyN�r+)T)�__name__�
__module__�__qualname__�__doc__r�r�r�r�r�r�staticmethodr�r�r�r�r�r�r�r�r�r�r�r�r�rrrr
r�sD
&sK








rc@sPeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Ze	d
d��Z
ddd�ZdS)r0zh
    Class loads in test files for a state
    e.g. state_dir/saltcheck-tests/[1.tst, 2.tst, 3.tst]
    r+cCs6d|_t�|_t�|_||_tddd�|_g|_dS)Nr9�saltcheck_test_locationzsaltcheck-tests)	�	path_type�set�
test_filesrrFr*r?r�r2r~rrr
r��s�
zStateTestLoader.__init__cCs*t�|_|jD]}|�|�qt�|_dS)zD
        Load tests either from one file, or a set of files
        N)rrFr��_load_file_salt_renderedr�)rZmyfilerrr
rA�s
zStateTestLoader.load_test_suitecCs8t|�}tt|�td�}|��D]	\}}||j|<qdS)z(
        loads in one test file
        )�object_pairs_hookN)rrrrrrMrF)r�filepathZtestsZmydictrTrUrrr
r��s
z(StateTestLoader._load_file_salt_renderedcCs�g}||jvr7t�d|�td||jdd�}|r5|r)t�d|�|j�|�|St�d|�|j�|�|St�d|j�|S)z�
        Copy tst files for a given path and return results of the copy.
        If check_all is enabled, also add all tests found
        zlooking in %s to cache testszcp.cache_dirz*.tst)r*Zinclude_patzAdding all found test files: %szMarking found_state: %sz#Not copying already found_state: %s)r2r"r#r?r*r�rar3)r�sls_pathr'rPZ	cache_retrrr
�_copy_state_files�s 
���z!StateTestLoader._copy_state_filescCs�g}d�|�dd�|j�}|�|�|�d�}|��d�d�|�|j�}|�|�|�d�d}d|�d|j��}|�|�t|�}tt	|t
dd��S)a�
        For a given state_name, return list of paths to search for .tst files

        possible formula paths are then
         path/to/formula.sls
           with tests of
             path/to/saltcheck-tests/formula.tst
         path/to/formula/init.sls
           with tests of
              path/to/formula/saltcheck-tests/init.tst
         or if a custom saltcheck_test_location is used
         path/to/forumla.sls
           with tests of
              path/saltcheck_test_location/init.tst
        zsalt://{}/{}rw�/rzsalt://T)rT�reverse)r��replacer�r3r|r�r[r��listrhrJ)rr'Z
all_sls_pathsZ	test_pathZ	sls_splitZstate_name_baseZunique_pathsrrr
�_generate_sls_path�s �

�

z"StateTestLoader._generate_sls_pathcCstd|jd�S)z@
        Returns (cached) list of states for the minion
        zcp.list_statesrp)r?r*)rrrr
�_get_states�szStateTestLoader._get_statesFcCs�d}dt�dd�vrBd}t�d|�tj�tdd|jd�}tj	j
�|d	��}ttj	j
�|����}Wd
�n1s<wYn|��}g}g}|r�t�d�tj�tdd|j|d�}	z(tj	j
�|	d	��}|�ttj	j
�|�����Wd
�n1s�wYWnKty�|�d
�}
d
�|
d
d��}tj�tdd|j|d�}	tj	j
�|	d	��}|�ttj	j
�|�����Wd
�n1s�wYYnw||v�r!|�rt�d|d�tj�tdd|j|d�}tj	j
�|d	��}ttj	j
�|����}Wd
�n	1�swYntd||jdd�}nd|ig}|D]�}
t|
t��s9t�d|
�d
Sd
}d|
v�r�|
d}|�|�D]}|�|||�}|�r`t�d|�|�|��qI|�ru|�rudd�|D�}|j�|�|�s�|
d�d
�}tj�tj�|�tj�|j�d�tj�tj�|d
t|�d��tj�|j�|d�d��tj�|dtj�|j�tj�|dd��|d�d��h}t t!|��}|D]}|�"t#|���r�|j�$|�|�%|�t�d|��qҐq(d
S)zQ
        Detects states used, caches needed files, and adds to test list
        FrrTz+Running on salt-ssh minion. Reading file %sZcachedir�filesz
cp_output.txt�rNz8Running on salt-ssh minion. Populating test file resultsz.copyrw���z.lowzstate.show_low_sls)r*rZ__sls__z6low data from show_low_sls is not formed as a dict: %szfound tests: %scSsg|]	}|�d�r|�qS)�.tst)�endswith)r�Zfile_stringrrr
�
<listcomp>Gs��z:StateTestLoader.add_test_files_for_sls.<locals>.<listcomp>zinit.tstr,r�rzAdding .tst file: %s)&rrr"r#�os�pathr[r*rr	r�ZfopenrZstringutilsZ
to_unicode�readr��extend�OSErrorr|r?rrr�r�r�r�ra�sep�normpathr�rJr�r�r��tuple�add�remove)rZsls_namerPZsalt_sshZcp_output_file�fpr\�retZcached_copied_filesZstate_copy_fileZ
sls_name_listZ
sls_root_nameZstate_low_fileZlow_dataZthis_cache_retr'r�Z	tst_filesZ	split_slsZsls_path_namesZthis_cached_test_filerrr
r1�s����
����
�����
�����
�
�
��
����
��z&StateTestLoader.add_test_files_for_slsNr��F)r�r�r�r�r�rAr�r�r�rr�r1rrrr
r0�s

	*
r0rg)NFF)NFr�r�)1r�r�loggingrDr�r��collectionsrZsalt.clientrZsalt.exceptionsZsalt.utils.dataZsalt.utils.filesZsalt.utils.functoolsZsalt.utils.pathZsalt.utils.platformZsalt.utils.yamlZ
salt.defaultsrZsalt.utils.decoratorsrZsalt.utils.jsonrr�	getLoggerr�r"r4�	NameErrorrrrr)r8rWrLr	�	functoolsZalias_functionrYr]rbrNrrrvr{r/rr0rrrr
�<module>sf-
�

'W	�

)



t

Youez - 2016 - github.com/yon3zu
LinuXploit