<s> from __future__ import absolute_import <EOL> import os <EOL> import shutil <EOL> import tempfile <EOL> import uuid <EOL> from salttesting import TestCase <EOL> from salttesting . helpers import ensure_in_syspath <EOL> ensure_in_syspath ( '<STR_LIT>' ) <EOL> import integration <EOL> import salt . config <EOL> import salt . state <EOL> import salt . utils <EOL> from salt . template import compile_template <EOL> from salt . utils . odict import OrderedDict <EOL> from salt . utils . pyobjects import ( StateFactory , State , Registry , <EOL> SaltObject , InvalidFunction , DuplicateState ) <EOL> File = StateFactory ( '<STR_LIT:file>' ) <EOL> Service = StateFactory ( '<STR_LIT>' ) <EOL> pydmesg_expected = { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : '<STR_LIT:root>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : [ { '<STR_LIT:file>' : '<STR_LIT>' } ] } , <EOL> { '<STR_LIT:source>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:user>' : '<STR_LIT:root>' } , <EOL> ] <EOL> } <EOL> pydmesg_salt_expected = OrderedDict ( [ <EOL> ( '<STR_LIT>' , pydmesg_expected ) <EOL> ] ) <EOL> pydmesg_kwargs = dict ( user = '<STR_LIT:root>' , group = '<STR_LIT:root>' , mode = '<STR_LIT>' , <EOL> source = '<STR_LIT>' ) <EOL> basic_template = '''<STR_LIT>''' <EOL> invalid_template = '''<STR_LIT>''' <EOL> include_template = '''<STR_LIT>''' <EOL> extend_template = '''<STR_LIT>''' <EOL> map_template = '''<STR_LIT>''' <EOL> import_template = '''<STR_LIT>''' <EOL> recursive_map_template = '''<STR_LIT>''' <EOL> recursive_import_template = '''<STR_LIT>''' <EOL> scope_test_import_template = '''<STR_LIT>''' <EOL> from_import_template = '''<STR_LIT>''' <EOL> import_as_template = '''<STR_LIT>''' <EOL> random_password_template = '''<STR_LIT>''' <EOL> random_password_import_template = '''<STR_LIT>''' <EOL> requisite_implicit_list_template = '''<STR_LIT>''' <EOL> class StateTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> Registry . empty ( ) <EOL> def test_serialization ( self ) : <EOL> f = State ( '<STR_LIT>' , '<STR_LIT:file>' , '<STR_LIT>' , <EOL> require = File ( '<STR_LIT>' ) , <EOL> ** pydmesg_kwargs ) <EOL> self . assertEqual ( f ( ) , pydmesg_expected ) <EOL> def test_factory_serialization ( self ) : <EOL> File . managed ( '<STR_LIT>' , <EOL> require = File ( '<STR_LIT>' ) , <EOL> ** pydmesg_kwargs ) <EOL> self . assertEqual ( <EOL> Registry . states [ '<STR_LIT>' ] , <EOL> pydmesg_expected <EOL> ) <EOL> def test_context_manager ( self ) : <EOL> with File ( '<STR_LIT>' ) : <EOL> pydmesg = File . managed ( '<STR_LIT>' , ** pydmesg_kwargs ) <EOL> self . assertEqual ( <EOL> Registry . states [ '<STR_LIT>' ] , <EOL> pydmesg_expected <EOL> ) <EOL> with pydmesg : <EOL> File . managed ( '<STR_LIT>' , owner = '<STR_LIT:root>' ) <EOL> self . assertEqual ( <EOL> Registry . states [ '<STR_LIT>' ] , <EOL> { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : '<STR_LIT:root>' } , <EOL> { '<STR_LIT>' : [ <EOL> { '<STR_LIT:file>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:file>' : '<STR_LIT>' } <EOL> ] } , <EOL> ] <EOL> } <EOL> ) <EOL> def test_salt_data ( self ) : <EOL> File . managed ( '<STR_LIT>' , <EOL> require = File ( '<STR_LIT>' ) , <EOL> ** pydmesg_kwargs ) <EOL> self . assertEqual ( <EOL> Registry . states [ '<STR_LIT>' ] , <EOL> pydmesg_expected <EOL> ) <EOL> self . assertEqual ( <EOL> Registry . salt_data ( ) , <EOL> pydmesg_salt_expected <EOL> ) <EOL> self . assertEqual ( <EOL> Registry . states , <EOL> OrderedDict ( ) <EOL> ) <EOL> def test_duplicates ( self ) : <EOL> def add_dup ( ) : <EOL> File . managed ( '<STR_LIT>' , name = '<STR_LIT>' ) <EOL> add_dup ( ) <EOL> self . assertRaises ( DuplicateState , add_dup ) <EOL> Service . running ( '<STR_LIT>' , name = '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> Registry . states , <EOL> OrderedDict ( [ <EOL> ( '<STR_LIT>' , <EOL> OrderedDict ( [ <EOL> ( '<STR_LIT>' , [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> ] ) , <EOL> ( '<STR_LIT>' , [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> ] ) <EOL> ] ) ) <EOL> ] ) <EOL> ) <EOL> class RendererMixin ( object ) : <EOL> '''<STR_LIT>''' <EOL> def setUp ( self , * args , ** kwargs ) : <EOL> super ( RendererMixin , self ) . setUp ( * args , ** kwargs ) <EOL> self . root_dir = tempfile . mkdtemp ( '<STR_LIT>' , dir = integration . TMP ) <EOL> self . state_tree_dir = os . path . join ( self . root_dir , '<STR_LIT>' ) <EOL> self . cache_dir = os . path . join ( self . root_dir , '<STR_LIT>' ) <EOL> if not os . path . isdir ( self . root_dir ) : <EOL> os . makedirs ( self . root_dir ) <EOL> if not os . path . isdir ( self . state_tree_dir ) : <EOL> os . makedirs ( self . state_tree_dir ) <EOL> if not os . path . isdir ( self . cache_dir ) : <EOL> os . makedirs ( self . cache_dir ) <EOL> self . config = salt . config . minion_config ( None ) <EOL> self . config [ '<STR_LIT>' ] = self . root_dir <EOL> self . config [ '<STR_LIT>' ] = False <EOL> self . config [ '<STR_LIT:id>' ] = '<STR_LIT>' <EOL> self . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . config [ '<STR_LIT>' ] = dict ( base = [ self . state_tree_dir ] ) <EOL> self . config [ '<STR_LIT>' ] = self . cache_dir <EOL> self . config [ '<STR_LIT:test>' ] = False <EOL> def tearDown ( self , * args , ** kwargs ) : <EOL> shutil . rmtree ( self . root_dir ) <EOL> super ( RendererMixin , self ) . tearDown ( * args , ** kwargs ) <EOL> def write_template_file ( self , filename , content ) : <EOL> full_path = os . path . join ( self . state_tree_dir , filename ) <EOL> with salt . utils . fopen ( full_path , '<STR_LIT:w>' ) as f : <EOL> f . write ( content ) <EOL> return full_path <EOL> def render ( self , template , opts = None , filename = None ) : <EOL> if opts : <EOL> self . config . update ( opts ) <EOL> if not filename : <EOL> filename = "<STR_LIT:.>" . join ( [ <EOL> str ( uuid . uuid4 ( ) ) , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> full_path = self . write_template_file ( filename , template ) <EOL> state = salt . state . State ( self . config ) <EOL> return compile_template ( full_path , <EOL> state . rend , <EOL> state . opts [ '<STR_LIT>' ] ) <EOL> class RendererTests ( RendererMixin , StateTests ) : <EOL> def test_basic ( self ) : <EOL> ret = self . render ( basic_template ) <EOL> self . assertEqual ( ret , OrderedDict ( [ <EOL> ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : '<STR_LIT:root>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT:root>' } <EOL> ] <EOL> } ) , <EOL> ] ) ) <EOL> self . assertEqual ( Registry . states , OrderedDict ( ) ) <EOL> def test_invalid_function ( self ) : <EOL> def _test ( ) : <EOL> self . render ( invalid_template ) <EOL> self . assertRaises ( InvalidFunction , _test ) <EOL> def test_include ( self ) : <EOL> ret = self . render ( include_template ) <EOL> self . assertEqual ( ret , OrderedDict ( [ <EOL> ( '<STR_LIT>' , [ '<STR_LIT:http>' ] ) , <EOL> ] ) ) <EOL> def test_extend ( self ) : <EOL> ret = self . render ( extend_template , <EOL> { '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } } ) <EOL> self . assertEqual ( ret , OrderedDict ( [ <EOL> ( '<STR_LIT>' , [ '<STR_LIT:http>' ] ) , <EOL> ( '<STR_LIT>' , OrderedDict ( [ <EOL> ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : [ { '<STR_LIT:file>' : '<STR_LIT>' } ] } <EOL> ] <EOL> } ) , <EOL> ] ) ) , <EOL> ] ) ) <EOL> def test_sls_imports ( self ) : <EOL> def render_and_assert ( template ) : <EOL> ret = self . render ( template , <EOL> { '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } } ) <EOL> self . assertEqual ( ret , OrderedDict ( [ <EOL> ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] } , <EOL> ] <EOL> } ) <EOL> ] ) ) <EOL> self . write_template_file ( "<STR_LIT>" , map_template ) <EOL> render_and_assert ( import_template ) <EOL> render_and_assert ( from_import_template ) <EOL> render_and_assert ( import_as_template ) <EOL> self . write_template_file ( "<STR_LIT>" , recursive_map_template ) <EOL> render_and_assert ( recursive_import_template ) <EOL> def test_import_scope ( self ) : <EOL> self . write_template_file ( "<STR_LIT>" , map_template ) <EOL> self . write_template_file ( "<STR_LIT>" , recursive_map_template ) <EOL> def do_render ( ) : <EOL> ret = self . render ( scope_test_import_template , <EOL> { '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } } ) <EOL> self . assertRaises ( NameError , do_render ) <EOL> def test_random_password ( self ) : <EOL> '''<STR_LIT>''' <EOL> ret = self . render ( random_password_template ) <EOL> def test_import_random_password ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . write_template_file ( "<STR_LIT>" , random_password_template ) <EOL> ret = self . render ( random_password_import_template ) <EOL> def test_requisite_implicit_list ( self ) : <EOL> '''<STR_LIT>''' <EOL> ret = self . render ( requisite_implicit_list_template , <EOL> { '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } } ) <EOL> self . assertEqual ( ret , OrderedDict ( [ <EOL> ( '<STR_LIT>' , OrderedDict ( [ <EOL> ( '<STR_LIT>' , [ ] ) <EOL> ] ) ) , <EOL> ( '<STR_LIT>' , OrderedDict ( [ <EOL> ( '<STR_LIT>' , [ <EOL> { '<STR_LIT>' : [ { '<STR_LIT>' : '<STR_LIT>' } , { '<STR_LIT>' : '<STR_LIT>' } ] } , <EOL> { '<STR_LIT>' : [ { '<STR_LIT:file>' : '<STR_LIT:file>' } ] } , <EOL> ] ) <EOL> ] ) ) <EOL> ] ) ) <EOL> class MapTests ( RendererMixin , TestCase ) : <EOL> def test_map ( self ) : <EOL> def samba_with_grains ( grains ) : <EOL> return self . render ( map_template , { '<STR_LIT>' : grains } ) <EOL> def assert_ret ( ret , server , client , service ) : <EOL> self . assertEqual ( ret , OrderedDict ( [ <EOL> ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : [ server , client ] } <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:name>' : service } , <EOL> { '<STR_LIT>' : [ { '<STR_LIT>' : '<STR_LIT>' } ] } <EOL> ] <EOL> } ) <EOL> ] ) ) <EOL> ret = samba_with_grains ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert_ret ( ret , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ret = samba_with_grains ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert_ret ( ret , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ret = samba_with_grains ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert_ret ( ret , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class SaltObjectTests ( TestCase ) : <EOL> def test_salt_object ( self ) : <EOL> def attr_fail ( ) : <EOL> Salt . fail . blah ( ) <EOL> def times2 ( x ) : <EOL> return x * <NUM_LIT:2> <EOL> __salt__ = { <EOL> '<STR_LIT>' : times2 <EOL> } <EOL> Salt = SaltObject ( __salt__ ) <EOL> self . assertRaises ( AttributeError , attr_fail ) <EOL> self . assertEqual ( Salt . math . times2 , times2 ) <EOL> self . assertEqual ( Salt . math . times2 ( <NUM_LIT:2> ) , <NUM_LIT:4> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from integration import run_tests <EOL> run_tests ( StateTests , RendererTests , MapTests , SaltObjectTests , needs_daemon = False ) </s>
