%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/modules/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyc

�
���^c@@sPdZddlmZmZmZddlZddlZddlZddlm	Z	m
Z
ddlZddl
ZddlZddlZddlZddlmZeje�ZdZd�Zd�Zd	�Zd
�Zd�Zd�Zd
�Zd�Zd�Zd�Z d�Z!de"fd��YZ#de"fd��YZ$dS(u�
A module for testing the logic of states and highstates

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

Saltcheck provides unittest like functionality requiring only the knowledge of
salt module execution and yaml.

In order to run state and highstate saltcheck tests a sub-folder of a state 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.

Multiple tests can be created in a file. Multiple ``*.tst`` files can be
created in the ``saltcheck-tests`` folder. Salt rendering is supported in test
files (e.g. ``yaml + jinja``). The ``id`` of a test works in the same manner as
in salt state files. They should be unique and descriptive.

Example file system layout:

.. code-block: txt

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

Example:

.. code-block:: yaml

    echo-test-hello:
      module_and_function: test.echo
      args:
        - "hello"
      kwargs:
      assertion: assertEqual
      expected-return:  'hello'

i(tabsolute_importtunicode_literalstprint_functionN(tloadstdumps(tsixu	saltcheckcC@stS(u6
    Check dependencies - may be useful in future
    (t__virtualname__(((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt__virtual__CscC@std�tS(uL
    Updates the master cache onto the minion - transfers all salt-check-tests
    Should be done one time before running tests, and if tests are updated
    Can be automated by setting "auto_update_master_cache: True" in minion config

    CLI Example:

    .. code-block:: bash

        salt '*' saltcheck.update_master_cache
    ucp.cache_master(t__salt__tTrue(((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pytupdate_master_cacheJscK@sEt�}|jdd�}|r=t|t�r=|j|�SdSdS(um
    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!"] }'
    utestuTest must be a dictionaryN(t	SaltChecktgettNonet
isinstancetdicttrun_test(tkwargstscheckttest((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyRZs
	
cC@s�t�}|j�}td|�}i}t|�}x}|D]u}|j|�}|j|�|j�i}x6|jj�D]%\}	}
|j	|
�}|||	<qW|||<q=Wd}d}
d}x�|D]�}t
||j��dkr�|d}q�xi||j�D]W\}}tjd||�|j
d�rJ|d}n|j
d�r|
d}
qqWq�Wg}x.|j�D] \}	}
|ji|
|	6�q�W|j�|jii|d6|
d6|d	6d
6�|S(u�
    Execute all tests for a salt state and return results
    Nested states will also be tested

    :param str state: the name of a user defined state

    CLI Example:

    .. code-block:: bash

        salt '*' saltcheck.run_state_tests postfix
    tsearch_pathsiiudummy=%s, val=%suPassuFailuPasseduFailedu
Missing TestsuTEST RESULTS(Rtget_state_search_path_listtStateTestLoadert_get_state_slstconvert_sls_to_pathtadd_test_files_for_slstload_test_suitet	test_dicttitemsRtlentlogtinfot
startswithtappendtsort(tstateRtpathststltresultstsls_listt
state_nametmypathtresults_dicttkeytvaluetresulttpassedtfailedt
missing_teststdummytvaltout_list((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pytrun_state_testsss@
	






)cC@s)t�}|j�}td|�}i}t�}g}xG|D]?}t|�}x*|D]"}||krY|j|�qYqYWq@Wx}|D]u}|j|�}	|j|	�|j�i}
x6|j	j
�D]%\}}|j|�}
|
|
|<q�W|
||<q�Wd}d}d}x�|D]�}t||j
��dkrK|d}qxi||j
�D]W\}}t
jd||�|jd�r�|d}n|jd�r\|d}q\q\WqWg}x.|j
�D] \}}|ji||6�q�W|j�|jii|d6|d6|d	6d
6�|S(u�
    Execute all tests for a salt highstate and return results

    CLI Example:

    .. code-block:: bash

        salt '*' saltcheck.run_highstate_tests
    Riiudummy=%s, val=%suPassuFailuPasseduFailedu
Missing TestsuTEST RESULTS(RRRt_get_top_statesRR!RRRRRRRRRR R"(RR$R%R&R't
all_statest	top_stateR#R(R)R*R+R,R-R.R/R0R1R2R3((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pytrun_highstate_tests�sL
		








)cC@s$td|�}tjd|�|S(u0
    call the salt utility to render a file
    uslsutil.rendererurendered: %s(RRR(t	file_pathtrendered((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt_render_file�scC@std�}t||k�S(u:
    Return a list of all modules available on minion
    usys.list_modules(Rtbool(tmoduletmodules((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt_is_valid_module�s
cC@stdd�tS(u=
    Return the config value of auto_update_master_cache
    u
config.getuauto_update_master_cache(RR	(((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt_get_auto_update_cache_value�scC@sMytd|�}Wn tjjk
r6dg}nXdj||�|kS(u7
    Determine if a function is valid for a module
    usys.list_functionsuunable to look up functionsu{0}.{1}(Rtsaltt
exceptionst
SaltExceptiontformat(tmodule_nametfunctiont	functions((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt_is_valid_function�s

cC@sTg}y3td�}x|dD]}|j|�q!WWntk
rO�nX|S(u;
    Equivalent to a salt cli: salt web state.show_top
    ustate.show_topubase(RR!t	Exception(t
alt_statestreturnedti((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyR5�s

cC@sjg}yItd|�}x2|D]*}|d|kr |j|d�q q WWntk
re�nX|S(uE
    Equivalent to a salt cli: salt web state.show_low_sls STATE
    ustate.show_low_slsu__sls__(RR!RI(R#tsls_list_stateRKRL((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyRs

RcB@s�eZdZd�Zd�Zd�Zd�Zed��Zed��Z	ed��Z
ed��Zed	��Zed
��Z
ed��Zed��Zed
��Zed��Zed��Zed��ZRS(u-
    This class implements the saltcheck
    cC@seg|_g|_i|_i|_dj�|_t|_tj	j
�|_|jrat�ndS(Nu,assertEqual assertNotEqual
                                  assertTrue assertFalse
                                  assertIn assertNotIn
                                  assertGreater
                                  assertGreaterEqual
                                  assertLess assertLessEqual(
RMR>R*tresults_dict_summarytsplittassertions_listR@tauto_update_master_cacheRAtclienttCallertsalt_lcR
(tself((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt__init__s						cC@s,d}|jdd
�}|jdd
�}|jdd
�}tjd|�|r�|d7}|jd�\}}t|�r�|d7}nt||�r�|d7}ntjd�n|r�|d7}||jkr�|d7}tjd	�q�n|r|d7}tjd
�ntjd|�|dkS(u�
        Determine if a test contains:

        - a test name
        - a valid module and function
        - a valid assertion
        - an expected return value
        iumodule_and_functionu	assertionuexpected-returnu__is_valid_test has test: %siu.u!__is_valid_test has valid m_and_fu#__is_valid_test has valid_assertionu)__is_valid_test has valid_expected_returnu__is_valid_test score: %siN(RR
RRROR?RHRP(RURttotstm_and_ft	assertiontexpected_returnR=RF((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt__is_valid_test1s.	





cC@s�t}y�|r0|r0|jj|||�}n\|rU|rU|jj||�}n7|rz|rz|jj||�}n|jj|�}Wn-tjjk
r��ntk
r��nX|S(u5
        Generic call of salt Caller command
        (tFalseRTtcmdRARBRCRI(RUtfuntargsRR,((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pytcall_salt_commandRs


c	C@s�|j|�r�|d}|jdd�}|jdd�}|d}|d}|j|||�}|dkr�|j||�}n|dkr�|j||�}q�|dkr�|j||�}q�|d	kr�|j|�}q�|d
kr|j|�}q�|dkr&|j	||�}q�|dkrG|j
||�}q�|dkrh|j||�}q�|d
kr�|j||�}q�|dkr�|j
||�}q�|dkr�|j||�}q�d}ndS|S(u-
        Run a single saltcheck test
        umodule_and_functionuargsukwargsu	assertionuexpected-returnuassertInuassertEqualuassertNotEqualu
assertTrueuassertFalseuassertNotInu
assertGreateruassertGreaterEqualu
assertLessuassertLessEqualuFail - bas assertionuFail - invalid testN(t_SaltCheck__is_valid_testRR
R`tcast_expected_to_returned_typet_SaltCheck__assert_equalt_SaltCheck__assert_not_equalt_SaltCheck__assert_truet_SaltCheck__assert_falset_SaltCheck__assert_int_SaltCheck__assert_not_int_SaltCheck__assert_greatert _SaltCheck__assert_greater_equalt_SaltCheck__assert_lesst_SaltCheck__assert_less_equal(	RURtmod_and_funcR_RRYRZt
actual_returnR,((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyRis@


	cC@s�t|�}|}|dkr3|tkr3t}ny||�}Wnjtk
r�tjd�tjd|�tjdt|��tjd|�tjdt|��nX|S(ut
        Determine the type of variable returned
        Cast the expected to the type of variable returned
        uFalseu-Unable to cast expected into type of returnedu
returned = %sutype of returned = %su
expected = %sutype of expected = %s(ttypeR<R\t
ValueErrorRR(texpectedRKtret_typetnew_expected((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyRb�s	

cC@s[d}y(||ks-tdj||���Wn&tk
rV}dtj|�}nX|S(u/
        Test if two objects are equal
        uPassu{0} is not equal to {1}uFail: (tAssertionErrorRDRt	text_type(RqRKR-terr((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt__assert_equal�s(cC@s[d}y(||ks-tdj||���Wn&tk
rV}dtj|�}nX|S(u3
        Test if two objects are not equal
        uPassu{0} is equal to {1}uFail: (RtRDRRu(RqRKR-Rv((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt__assert_not_equal�s(cC@sXd}y%|tks*tdj|���Wn&tk
rS}dtj|�}nX|S(u,
        Test if an boolean is True
        uPassu{0} not TrueuFail: (R	RtRDRRu(RKR-Rv((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt
__assert_true�s%cC@s�d}t|t�r?yt|�}Wq?tk
r;�q?Xny%|tksctdj|���Wn&tk
r�}dtj|�}nX|S(u-
        Test if an boolean is False
        uPassu
{0} not FalseuFail: (	RtstrR<RpR\RtRDRRu(RKR-Rv((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt__assert_false�s

%cC@sXd}y%||ks*tdj|���Wn&tk
rS}dtj|�}nX|S(uC
        Test if a value is in the list of returned values
        uPassu
{0} not FalseuFail: (RtRDRRu(RqRKR-Rv((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt__assert_in�s%cC@sXd}y%||ks*tdj|���Wn&tk
rS}dtj|�}nX|S(uG
        Test if a value is not in the list of returned values
        uPassu
{0} not FalseuFail: (RtRDRRu(RqRKR-Rv((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt__assert_not_in�s%cC@sXd}y%||ks*tdj|���Wn&tk
rS}dtj|�}nX|S(uD
        Test if a value is greater than the returned value
        uPassu
{0} not FalseuFail: (RtRDRRu(RqRKR-Rv((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt__assert_greater�s%cC@sXd}y%||ks*tdj|���Wn&tk
rS}dtj|�}nX|S(uP
        Test if a value is greater than or equal to the returned value
        uPassu
{0} not FalseuFail: (RtRDRRu(RqRKR-Rv((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt__assert_greater_equal�s%cC@sXd}y%||ks*tdj|���Wn&tk
rS}dtj|�}nX|S(uA
        Test if a value is less than the returned value
        uPassu
{0} not FalseuFail: (RtRDRRu(RqRKR-Rv((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt
__assert_less
s%cC@sXd}y%||ks*tdj|���Wn&tk
rS}dtj|�}nX|S(uM
        Test if a value is less than or equal to the returned value
        uPassu
{0} not FalseuFail: (RtRDRRu(RqRKR-Rv((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt__assert_less_equals%cC@s�g}tjdd�}td}|rT|tjdtj|}|j|�n|tjdtjd}|j|�|S(uX
        For the state file system, return a list of paths to search for states
        ucachedirusaltenvufilesubaseN(t__opts__RR
tostsepR!(tsearch_listtcachedirtenvironmenttpath((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyR"s

(t__name__t
__module__t__doc__RVRaR`RtstaticmethodRbRcRdReRfRgRhRiRjRkRlR(((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyRs"		!		'
RcB@sbeZdZd�Zd�Zd�Zd�Zd�Zed��Z	ed��Z
d�ZRS(	uh
    Class loads in test files for a state
    e.g. state_dir/saltcheck-tests/[1.tst, 2.tst, 3.tst]
    cC@s(||_d|_g|_i|_dS(N(RR
t	path_typet
test_filesR(RUR((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyRV9s			cC@s7i|_x|jD]}|j|�qWg|_dS(uD
        Load tests either from one file, or a set of files
        N(RR�tload_file_salt_rendered(RUtmyfile((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyR?s	cC@syntd|d��U}tjjjtjjj|��}x'|j�D]\}}||j|<qJWWdQXWn
�nXdS(u(
        loads in one test file
        ufiles.fopenurN(	t	__utils__RAtutilstdatatdecodetyamlt	safe_loadRR(RUtfilepathR�t
contents_yamlR+R,((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt	load_fileIs$cC@sLt|�}tt|��}x'|j�D]\}}||j|<q+WdS(u(
        loads in one test file
        N(R;RRRR(RUR�tteststmydictR+R,((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyR�Xs
c	C@s�g|_tjdtj��|tjd}|}x|tjjj	|�D]e\}}}xS|D]K}|j
d�rb|tj|}tjj|�}|jj|�qbqbWqLWdS(u/
        Gather files for a test suite
        ugather_files: %susaltcheck-testsu.tstN(
R�RRttimeR�R�RAR�R�tos_walktendswithtabspathR!(	RUR�trootdirtdirnameR1tfilelisttfnamet
start_patht	full_path((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pytgather_filesds	%
cC@s=g}x0|D](}|jdtj�}|j|�q
W|S(u)
        Converting sls to paths
        u.(treplaceR�R�R!(R'tnew_sls_listtsls((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pytconvert_sls_to_pathsus

cC@s|jdtj�}|S(u)
        Converting sls to paths
        u.(R�R�R�(R�((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyR�scC@s�x�|jD]�}|tj|}|}tjj|�r�tjd|�x�tjjj	|dt
�D]b\}}}d|kr�|j|�tjd|j�tjd|�ntjd|�|2qeWq
tjd|�q
WdS(	u#
        Adding test files
        usearching path= %sttopdownusaltcheck-testsutest_files list: %sufound subdir match in = %su!did not find subdir match in = %supath is not a directory= %sN(
RR�R�R�tisdirRRRAR�R�R	R�R�(RUtsls_pathR�R�R�R�t
subdirlistR1((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyR�s+
(R�R�R�RVRR�R�R�R�R�RR(((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyR3s		
			(%R�t
__future__RRRtloggingR�R�tjsonRRtsalt.utils.filesRAtsalt.utils.pathtsalt.utils.yamltsalt.clienttsalt.exceptionstsalt.extRt	getLoggerR�RRRR
RR4R8R;R?R@RHR5RtobjectRR(((s:/usr/lib/python2.7/site-packages/salt/modules/saltcheck.pyt<module>-s6				0	4	
					�

Zerion Mini Shell 1.0