<s> import os <EOL> import sys <EOL> import copy <EOL> import stat <EOL> import lxml . etree <EOL> from mock import Mock , MagicMock , patch <EOL> import Bcfg2 . Client . Tools <EOL> from Bcfg2 . Client . Tools . POSIX . base import * <EOL> path = os . path . dirname ( __file__ ) <EOL> while path != "<STR_LIT:/>" : <EOL> if os . path . basename ( path ) . lower ( ) . startswith ( "<STR_LIT:test>" ) : <EOL> sys . path . append ( path ) <EOL> if os . path . basename ( path ) == "<STR_LIT>" : <EOL> break <EOL> path = os . path . dirname ( path ) <EOL> from TestTools . Test_init import TestTool <EOL> from common import * <EOL> try : <EOL> import selinux <EOL> HAS_SELINUX = True <EOL> except ImportError : <EOL> HAS_SELINUX = False <EOL> try : <EOL> import posix1e <EOL> HAS_ACLS = True <EOL> except ImportError : <EOL> HAS_ACLS = False <EOL> class TestPOSIXTool ( TestTool ) : <EOL> test_obj = POSIXTool <EOL> def test_fully_specified ( self ) : <EOL> ptool = self . get_obj ( ) <EOL> self . assertTrue ( ptool . fully_specified ( Mock ( ) ) ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_verify ( self , mock_walk , mock_stat ) : <EOL> ptool = self . get_obj ( ) <EOL> ptool . _verify_metadata = Mock ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT:file>" ) <EOL> mock_stat . return_value = MagicMock ( ) <EOL> ptool . _verify_metadata . return_value = False <EOL> self . assertFalse ( ptool . verify ( entry , [ ] ) ) <EOL> ptool . _verify_metadata . assert_called_with ( entry ) <EOL> ptool . _verify_metadata . reset_mock ( ) <EOL> ptool . _verify_metadata . return_value = True <EOL> self . assertTrue ( ptool . verify ( entry , [ ] ) ) <EOL> ptool . _verify_metadata . assert_called_with ( entry ) <EOL> ptool . _verify_metadata . reset_mock ( ) <EOL> entry . set ( "<STR_LIT>" , "<STR_LIT:true>" ) <EOL> walk_rv = [ ( "<STR_LIT:/>" , [ "<STR_LIT>" , "<STR_LIT>" ] , [ "<STR_LIT>" , "<STR_LIT>" ] ) , <EOL> ( "<STR_LIT>" , [ "<STR_LIT>" ] , [ ] ) , <EOL> ( "<STR_LIT>" , [ ] , [ "<STR_LIT>" , "<STR_LIT>" ] ) ] <EOL> mock_walk . return_value = walk_rv <EOL> self . assertTrue ( ptool . verify ( entry , [ ] ) ) <EOL> mock_walk . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> all_verifies = [ call ( entry ) ] <EOL> for root , dirs , files in walk_rv : <EOL> all_verifies . extend ( [ call ( entry , path = os . path . join ( root , p ) ) <EOL> for p in dirs + files ] ) <EOL> self . assertItemsEqual ( ptool . _verify_metadata . call_args_list , all_verifies ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_install ( self , mock_walk ) : <EOL> ptool = self . get_obj ( ) <EOL> ptool . _set_perms = Mock ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT:file>" ) <EOL> ptool . _set_perms . return_value = True <EOL> self . assertTrue ( ptool . install ( entry ) ) <EOL> ptool . _set_perms . assert_called_with ( entry ) <EOL> ptool . _set_perms . reset_mock ( ) <EOL> entry . set ( "<STR_LIT>" , "<STR_LIT:true>" ) <EOL> walk_rv = [ ( "<STR_LIT:/>" , [ "<STR_LIT>" , "<STR_LIT>" ] , [ "<STR_LIT>" , "<STR_LIT>" ] ) , <EOL> ( "<STR_LIT>" , [ "<STR_LIT>" ] , [ ] ) , <EOL> ( "<STR_LIT>" , [ ] , [ "<STR_LIT>" , "<STR_LIT>" ] ) ] <EOL> mock_walk . return_value = walk_rv <EOL> ptool . _set_perms . return_value = True <EOL> self . assertTrue ( ptool . install ( entry ) ) <EOL> mock_walk . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> all_set_perms = [ call ( entry ) ] <EOL> for root , dirs , files in walk_rv : <EOL> all_set_perms . extend ( [ call ( entry , path = os . path . join ( root , p ) ) <EOL> for p in dirs + files ] ) <EOL> self . assertItemsEqual ( ptool . _set_perms . call_args_list , <EOL> all_set_perms ) <EOL> mock_walk . reset_mock ( ) <EOL> ptool . _set_perms . reset_mock ( ) <EOL> def set_perms_rv ( entry , path = None ) : <EOL> if path == '<STR_LIT>' : <EOL> return False <EOL> else : <EOL> return True <EOL> ptool . _set_perms . side_effect = set_perms_rv <EOL> self . assertFalse ( ptool . install ( entry ) ) <EOL> mock_walk . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> self . assertItemsEqual ( ptool . _set_perms . call_args_list , all_set_perms ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_remove ( self , mock_islink , mock_isdir , mock_rmtree , mock_unlink , <EOL> mock_rmdir ) : <EOL> ptool = self . get_obj ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" ) <EOL> def reset ( ) : <EOL> mock_islink . reset_mock ( ) <EOL> mock_isdir . reset_mock ( ) <EOL> mock_rmtree . reset_mock ( ) <EOL> mock_unlink . reset_mock ( ) <EOL> mock_rmdir . reset_mock ( ) <EOL> mock_islink . return_value = True <EOL> mock_isdir . return_value = False <EOL> ptool . _remove ( entry ) <EOL> mock_unlink . assert_called_with ( entry . get ( '<STR_LIT:name>' ) ) <EOL> self . assertFalse ( mock_rmtree . called ) <EOL> self . assertFalse ( mock_rmdir . called ) <EOL> reset ( ) <EOL> mock_islink . return_value = False <EOL> mock_isdir . return_value = True <EOL> ptool . _remove ( entry ) <EOL> mock_rmtree . assert_called_with ( entry . get ( '<STR_LIT:name>' ) ) <EOL> self . assertFalse ( mock_unlink . called ) <EOL> self . assertFalse ( mock_rmdir . called ) <EOL> reset ( ) <EOL> ptool . _remove ( entry , recursive = False ) <EOL> mock_rmdir . assert_called_with ( entry . get ( '<STR_LIT:name>' ) ) <EOL> self . assertFalse ( mock_unlink . called ) <EOL> self . assertFalse ( mock_rmtree . called ) <EOL> reset ( ) <EOL> mock_islink . return_value = False <EOL> mock_isdir . return_value = False <EOL> ptool . _remove ( entry , recursive = False ) <EOL> mock_unlink . assert_called_with ( entry . get ( '<STR_LIT:name>' ) ) <EOL> self . assertFalse ( mock_rmtree . called ) <EOL> self . assertFalse ( mock_rmdir . called ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_exists ( self , mock_lstat ) : <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT:file>" ) <EOL> ptool = self . get_obj ( ) <EOL> ptool . _remove = Mock ( ) <EOL> def reset ( ) : <EOL> mock_lstat . reset_mock ( ) <EOL> ptool . _remove . reset_mock ( ) <EOL> mock_lstat . side_effect = OSError <EOL> self . assertFalse ( ptool . _exists ( entry ) ) <EOL> mock_lstat . assert_called_with ( entry . get ( '<STR_LIT:name>' ) ) <EOL> self . assertFalse ( ptool . _remove . called ) <EOL> reset ( ) <EOL> rv = MagicMock ( ) <EOL> mock_lstat . return_value = rv <EOL> mock_lstat . side_effect = None <EOL> self . assertEqual ( ptool . _exists ( entry ) , rv ) <EOL> mock_lstat . assert_called_with ( entry . get ( '<STR_LIT:name>' ) ) <EOL> self . assertFalse ( ptool . _remove . called ) <EOL> reset ( ) <EOL> self . assertEqual ( ptool . _exists ( entry , remove = True ) , None ) <EOL> mock_lstat . assert_called_with ( entry . get ( '<STR_LIT:name>' ) ) <EOL> ptool . _remove . assert_called_with ( entry ) <EOL> reset ( ) <EOL> ptool . _remove . side_effect = OSError <EOL> self . assertEqual ( ptool . _exists ( entry , remove = True ) , rv ) <EOL> mock_lstat . assert_called_with ( entry . get ( '<STR_LIT:name>' ) ) <EOL> ptool . _remove . assert_called_with ( entry ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_set_perms ( self , mock_geteuid , mock_utime , mock_chmod , mock_chown ) : <EOL> ptool = self . get_obj ( ) <EOL> ptool . _norm_entry_uid = Mock ( ) <EOL> ptool . _norm_entry_gid = Mock ( ) <EOL> ptool . _set_acls = Mock ( ) <EOL> ptool . _set_secontext = Mock ( ) <EOL> def reset ( ) : <EOL> ptool . _set_secontext . reset_mock ( ) <EOL> ptool . _set_acls . reset_mock ( ) <EOL> ptool . _norm_entry_gid . reset_mock ( ) <EOL> ptool . _norm_entry_uid . reset_mock ( ) <EOL> mock_chmod . reset_mock ( ) <EOL> mock_chown . reset_mock ( ) <EOL> mock_utime . reset_mock ( ) <EOL> mock_geteuid . reset_mock ( ) <EOL> mock_geteuid . return_value = <NUM_LIT:0> <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , to = "<STR_LIT>" , <EOL> type = "<STR_LIT>" ) <EOL> ptool . _set_acls . return_value = True <EOL> ptool . _set_secontext . return_value = True <EOL> self . assertTrue ( ptool . _set_perms ( entry ) ) <EOL> ptool . _set_secontext . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _set_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> reset ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , owner = "<STR_LIT>" , <EOL> group = "<STR_LIT>" , mode = "<STR_LIT>" , type = "<STR_LIT:file>" ) <EOL> ptool . _norm_entry_uid . return_value = <NUM_LIT:10> <EOL> ptool . _norm_entry_gid . return_value = <NUM_LIT:100> <EOL> self . assertTrue ( ptool . _set_perms ( entry ) ) <EOL> ptool . _norm_entry_uid . assert_called_with ( entry ) <EOL> ptool . _norm_entry_gid . assert_called_with ( entry ) <EOL> mock_chown . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <NUM_LIT:10> , <NUM_LIT:100> ) <EOL> mock_chmod . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <EOL> int ( entry . get ( "<STR_LIT>" ) , <NUM_LIT:8> ) ) <EOL> self . assertFalse ( mock_utime . called ) <EOL> ptool . _set_secontext . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _set_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> mock_geteuid . return_value = <NUM_LIT:1000> <EOL> reset ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , owner = "<STR_LIT>" , <EOL> group = "<STR_LIT>" , mode = "<STR_LIT>" , type = "<STR_LIT:file>" ) <EOL> self . assertTrue ( ptool . _set_perms ( entry ) ) <EOL> self . assertFalse ( ptool . _norm_entry_uid . called ) <EOL> self . assertFalse ( ptool . _norm_entry_gid . called ) <EOL> self . assertFalse ( mock_chown . called ) <EOL> mock_chmod . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <EOL> int ( entry . get ( "<STR_LIT>" ) , <NUM_LIT:8> ) ) <EOL> self . assertFalse ( mock_utime . called ) <EOL> ptool . _set_secontext . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _set_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> mock_geteuid . return_value = <NUM_LIT:0> <EOL> reset ( ) <EOL> mtime = <NUM_LIT> <EOL> entry . set ( "<STR_LIT>" , str ( mtime ) ) <EOL> self . assertTrue ( ptool . _set_perms ( entry ) ) <EOL> ptool . _norm_entry_uid . assert_called_with ( entry ) <EOL> ptool . _norm_entry_gid . assert_called_with ( entry ) <EOL> mock_chown . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <NUM_LIT:10> , <NUM_LIT:100> ) <EOL> mock_chmod . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <EOL> int ( entry . get ( "<STR_LIT>" ) , <NUM_LIT:8> ) ) <EOL> mock_utime . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , ( mtime , mtime ) ) <EOL> ptool . _set_secontext . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _set_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> reset ( ) <EOL> self . assertTrue ( ptool . _set_perms ( entry , path = '<STR_LIT>' ) ) <EOL> ptool . _norm_entry_uid . assert_called_with ( entry ) <EOL> ptool . _norm_entry_gid . assert_called_with ( entry ) <EOL> mock_chown . assert_called_with ( '<STR_LIT>' , <NUM_LIT:10> , <NUM_LIT:100> ) <EOL> mock_chmod . assert_called_with ( '<STR_LIT>' , int ( entry . get ( "<STR_LIT>" ) , <NUM_LIT:8> ) ) <EOL> mock_utime . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , ( mtime , mtime ) ) <EOL> ptool . _set_secontext . assert_called_with ( entry , path = '<STR_LIT>' ) <EOL> ptool . _set_acls . assert_called_with ( entry , path = '<STR_LIT>' ) <EOL> reset ( ) <EOL> def chown_rv ( path , owner , group ) : <EOL> if owner == <NUM_LIT:0> and group == <NUM_LIT:0> : <EOL> return True <EOL> else : <EOL> raise KeyError <EOL> os . chown . side_effect = chown_rv <EOL> entry . set ( "<STR_LIT:type>" , "<STR_LIT>" ) <EOL> entry . set ( "<STR_LIT>" , list ( device_map . keys ( ) ) [ <NUM_LIT:0> ] ) <EOL> self . assertFalse ( ptool . _set_perms ( entry ) ) <EOL> ptool . _norm_entry_uid . assert_called_with ( entry ) <EOL> ptool . _norm_entry_gid . assert_called_with ( entry ) <EOL> mock_chown . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> mock_chmod . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <EOL> int ( entry . get ( "<STR_LIT>" ) , <NUM_LIT:8> ) | list ( device_map . values ( ) ) [ <NUM_LIT:0> ] ) <EOL> mock_utime . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , ( mtime , mtime ) ) <EOL> ptool . _set_secontext . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _set_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> reset ( ) <EOL> os . chown . side_effect = None <EOL> os . chmod . side_effect = OSError <EOL> entry . set ( "<STR_LIT:type>" , "<STR_LIT:file>" ) <EOL> del entry . attrib [ "<STR_LIT>" ] <EOL> self . assertFalse ( ptool . _set_perms ( entry ) ) <EOL> ptool . _norm_entry_uid . assert_called_with ( entry ) <EOL> ptool . _norm_entry_gid . assert_called_with ( entry ) <EOL> mock_chown . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <NUM_LIT:10> , <NUM_LIT:100> ) <EOL> mock_chmod . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <EOL> int ( entry . get ( "<STR_LIT>" ) , <NUM_LIT:8> ) ) <EOL> mock_utime . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , ( mtime , mtime ) ) <EOL> ptool . _set_secontext . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _set_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> reset ( ) <EOL> os . chown . side_effect = OSError <EOL> os . utime . side_effect = OSError <EOL> ptool . _set_acls . return_value = False <EOL> ptool . _set_secontext . return_value = False <EOL> self . assertFalse ( ptool . _set_perms ( entry ) ) <EOL> ptool . _norm_entry_uid . assert_called_with ( entry ) <EOL> ptool . _norm_entry_gid . assert_called_with ( entry ) <EOL> mock_chown . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <NUM_LIT:10> , <NUM_LIT:100> ) <EOL> mock_chmod . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <EOL> int ( entry . get ( "<STR_LIT>" ) , <NUM_LIT:8> ) ) <EOL> mock_utime . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , ( mtime , mtime ) ) <EOL> ptool . _set_secontext . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _set_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> @ skipUnless ( HAS_ACLS , "<STR_LIT>" ) <EOL> @ patchIf ( HAS_ACLS , "<STR_LIT>" ) <EOL> @ patchIf ( HAS_ACLS , "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_set_acls ( self , mock_isdir , mock_Entry , mock_ACL ) : <EOL> ptool = self . get_obj ( ) <EOL> ptool . _list_entry_acls = Mock ( ) <EOL> ptool . _norm_uid = Mock ( ) <EOL> ptool . _norm_gid = Mock ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT:file>" ) <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_ACLS = False <EOL> self . assertTrue ( ptool . _set_acls ( entry ) ) <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_ACLS = True <EOL> file_acls = [ ] <EOL> acl = Mock ( ) <EOL> acl . tag_type = posix1e . ACL_USER <EOL> acl . name = "<STR_LIT>" <EOL> file_acls . append ( acl ) <EOL> acl = Mock ( ) <EOL> acl . tag_type = posix1e . ACL_GROUP <EOL> acl . name = "<STR_LIT>" <EOL> file_acls . append ( acl ) <EOL> acl = Mock ( ) <EOL> acl . tag_type = posix1e . ACL_MASK <EOL> acl . name = "<STR_LIT>" <EOL> file_acls . append ( acl ) <EOL> remove_acls = [ a for a in file_acls if a . name == "<STR_LIT>" ] <EOL> entry_acls = { ( "<STR_LIT:default>" , posix1e . ACL_USER , "<STR_LIT:user>" ) : <NUM_LIT:7> , <EOL> ( "<STR_LIT>" , posix1e . ACL_GROUP , "<STR_LIT>" ) : <NUM_LIT:5> } <EOL> ptool . _list_entry_acls . return_value = entry_acls <EOL> ptool . _norm_uid . return_value = <NUM_LIT:10> <EOL> ptool . _norm_gid . return_value = <NUM_LIT:100> <EOL> fileacl_rv = MagicMock ( ) <EOL> fileacl_rv . valid . return_value = True <EOL> fileacl_rv . __iter__ . return_value = iter ( file_acls ) <EOL> filedef_rv = MagicMock ( ) <EOL> filedef_rv . valid . return_value = True <EOL> filedef_rv . __iter__ . return_value = iter ( file_acls ) <EOL> acl_rv = MagicMock ( ) <EOL> def mock_acl_rv ( file = None , filedef = None , acl = None ) : <EOL> if file : <EOL> return fileacl_rv <EOL> elif filedef : <EOL> return filedef_rv <EOL> elif acl : <EOL> return acl_rv <EOL> acl_entries = [ ] <EOL> def mock_entry_rv ( acl ) : <EOL> rv = MagicMock ( ) <EOL> rv . acl = acl <EOL> rv . permset = set ( ) <EOL> acl_entries . append ( rv ) <EOL> return rv <EOL> mock_Entry . side_effect = mock_entry_rv <EOL> def reset ( ) : <EOL> mock_isdir . reset_mock ( ) <EOL> mock_ACL . reset_mock ( ) <EOL> mock_Entry . reset_mock ( ) <EOL> fileacl_rv . reset_mock ( ) <EOL> mock_ACL . side_effect = IOError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> self . assertFalse ( ptool . _set_acls ( entry ) ) <EOL> reset ( ) <EOL> mock_ACL . side_effect = IOError <EOL> self . assertFalse ( ptool . _set_acls ( entry ) ) <EOL> reset ( ) <EOL> mock_ACL . side_effect = mock_acl_rv <EOL> mock_isdir . return_value = True <EOL> self . assertTrue ( ptool . _set_acls ( entry ) ) <EOL> self . assertItemsEqual ( mock_ACL . call_args_list , <EOL> [ call ( file = entry . get ( "<STR_LIT:name>" ) ) , <EOL> call ( filedef = entry . get ( "<STR_LIT:name>" ) ) ] ) <EOL> self . assertItemsEqual ( fileacl_rv . delete_entry . call_args_list , <EOL> [ call ( a ) for a in remove_acls ] ) <EOL> self . assertItemsEqual ( filedef_rv . delete_entry . call_args_list , <EOL> [ call ( a ) for a in remove_acls ] ) <EOL> ptool . _list_entry_acls . assert_called_with ( entry ) <EOL> ptool . _norm_uid . assert_called_with ( "<STR_LIT:user>" ) <EOL> ptool . _norm_gid . assert_called_with ( "<STR_LIT>" ) <EOL> fileacl_rv . calc_mask . assert_any_call ( ) <EOL> fileacl_rv . applyto . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <EOL> posix1e . ACL_TYPE_ACCESS ) <EOL> filedef_rv . calc_mask . assert_any_call ( ) <EOL> filedef_rv . applyto . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , <EOL> posix1e . ACL_TYPE_DEFAULT ) <EOL> added_acls = [ ] <EOL> for acl in acl_entries : <EOL> added_acls . append ( ( acl . acl , acl . tag_type , acl . qualifier , <EOL> sum ( acl . permset ) ) ) <EOL> self . assertItemsEqual ( added_acls , <EOL> [ ( filedef_rv , posix1e . ACL_USER , <NUM_LIT:10> , <NUM_LIT:7> ) , <EOL> ( fileacl_rv , posix1e . ACL_GROUP , <NUM_LIT:100> , <NUM_LIT:5> ) ] ) <EOL> reset ( ) <EOL> fileacl_rv . __iter__ . return_value = iter ( file_acls ) <EOL> filedef_rv . __iter__ . return_value = iter ( file_acls ) <EOL> ptool . _list_entry_acls . reset_mock ( ) <EOL> ptool . _norm_uid . reset_mock ( ) <EOL> ptool . _norm_gid . reset_mock ( ) <EOL> mock_isdir . return_value = False <EOL> acl_entries = [ ] <EOL> self . assertTrue ( ptool . _set_acls ( entry , path = "<STR_LIT>" ) ) <EOL> mock_ACL . assert_called_with ( file = "<STR_LIT>" ) <EOL> self . assertItemsEqual ( fileacl_rv . delete_entry . call_args_list , <EOL> [ call ( a ) for a in remove_acls ] ) <EOL> ptool . _list_entry_acls . assert_called_with ( entry ) <EOL> ptool . _norm_gid . assert_called_with ( "<STR_LIT>" ) <EOL> fileacl_rv . calc_mask . assert_any_call ( ) <EOL> fileacl_rv . applyto . assert_called_with ( "<STR_LIT>" , <EOL> posix1e . ACL_TYPE_ACCESS ) <EOL> added_acls = [ ] <EOL> for acl in acl_entries : <EOL> added_acls . append ( ( acl . acl , acl . tag_type , acl . qualifier , <EOL> sum ( acl . permset ) ) ) <EOL> self . assertItemsEqual ( added_acls , <EOL> [ ( fileacl_rv , posix1e . ACL_GROUP , <NUM_LIT:100> , <NUM_LIT:5> ) ] ) <EOL> @ skipUnless ( HAS_SELINUX , "<STR_LIT>" ) <EOL> @ patchIf ( HAS_SELINUX , "<STR_LIT>" ) <EOL> @ patchIf ( HAS_SELINUX , "<STR_LIT>" ) <EOL> @ patchIf ( HAS_SELINUX , "<STR_LIT>" ) <EOL> def test_set_secontext ( self , mock_lsetfilecon , mock_lgetfilecon , <EOL> mock_restorecon ) : <EOL> Bcfg2 . Options . setup . secontext_ignore = [ '<STR_LIT>' ] <EOL> ptool = self . get_obj ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT:file>" ) <EOL> mock_lgetfilecon . return_value = ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX = False <EOL> self . assertTrue ( ptool . _set_secontext ( entry ) ) <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX = True <EOL> self . assertTrue ( ptool . _set_secontext ( entry ) ) <EOL> self . assertFalse ( mock_restorecon . called ) <EOL> self . assertFalse ( mock_lsetfilecon . called ) <EOL> self . assertFalse ( mock_lgetfilecon . called ) <EOL> mock_restorecon . reset_mock ( ) <EOL> mock_lsetfilecon . reset_mock ( ) <EOL> mock_lgetfilecon . reset_mock ( ) <EOL> entry . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertTrue ( ptool . _set_secontext ( entry ) ) <EOL> mock_restorecon . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> mock_lgetfilecon . assert_called_once_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> self . assertFalse ( mock_lsetfilecon . called ) <EOL> mock_restorecon . reset_mock ( ) <EOL> mock_lsetfilecon . reset_mock ( ) <EOL> mock_lgetfilecon . reset_mock ( ) <EOL> mock_lsetfilecon . return_value = <NUM_LIT:0> <EOL> entry . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertTrue ( ptool . _set_secontext ( entry ) ) <EOL> self . assertFalse ( mock_restorecon . called ) <EOL> mock_lgetfilecon . assert_called_once_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> mock_lsetfilecon . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , "<STR_LIT>" ) <EOL> mock_restorecon . reset_mock ( ) <EOL> mock_lsetfilecon . reset_mock ( ) <EOL> mock_lgetfilecon . reset_mock ( ) <EOL> mock_lsetfilecon . return_value = <NUM_LIT:1> <EOL> self . assertFalse ( ptool . _set_secontext ( entry ) ) <EOL> self . assertFalse ( mock_restorecon . called ) <EOL> mock_lgetfilecon . assert_called_once_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> mock_lsetfilecon . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , "<STR_LIT>" ) <EOL> mock_restorecon . reset_mock ( ) <EOL> mock_lsetfilecon . reset_mock ( ) <EOL> mock_lgetfilecon . reset_mock ( ) <EOL> mock_lgetfilecon . return_value = ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> self . assertTrue ( ptool . _set_secontext ( entry ) ) <EOL> self . assertFalse ( mock_restorecon . called ) <EOL> self . assertFalse ( mock_lsetfilecon . called ) <EOL> mock_lgetfilecon . assert_called_once_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_norm_gid ( self , mock_getgrnam ) : <EOL> ptool = self . get_obj ( ) <EOL> self . assertEqual ( <NUM_LIT:5> , ptool . _norm_gid ( "<STR_LIT:5>" ) ) <EOL> self . assertFalse ( mock_getgrnam . called ) <EOL> mock_getgrnam . reset_mock ( ) <EOL> mock_getgrnam . return_value = ( "<STR_LIT>" , "<STR_LIT:x>" , <NUM_LIT:5> , [ ] ) <EOL> self . assertEqual ( <NUM_LIT:5> , ptool . _norm_gid ( "<STR_LIT>" ) ) <EOL> mock_getgrnam . assert_called_with ( "<STR_LIT>" ) <EOL> def test_norm_entry_gid ( self ) : <EOL> ptool = self . get_obj ( ) <EOL> ptool . _norm_gid = Mock ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT:file>" , <EOL> group = "<STR_LIT>" , owner = "<STR_LIT:user>" ) <EOL> self . assertEqual ( ptool . _norm_entry_gid ( entry ) , <EOL> ptool . _norm_gid . return_value ) <EOL> ptool . _norm_gid . assert_called_with ( entry . get ( "<STR_LIT>" ) ) <EOL> ptool . _norm_gid . reset_mock ( ) <EOL> ptool . _norm_gid . side_effect = KeyError <EOL> self . assertEqual ( ptool . _norm_entry_gid ( entry ) , <NUM_LIT:0> ) <EOL> ptool . _norm_gid . assert_called_with ( entry . get ( "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_norm_uid ( self , mock_getpwnam ) : <EOL> ptool = self . get_obj ( ) <EOL> self . assertEqual ( <NUM_LIT:5> , ptool . _norm_uid ( "<STR_LIT:5>" ) ) <EOL> self . assertFalse ( mock_getpwnam . called ) <EOL> mock_getpwnam . reset_mock ( ) <EOL> mock_getpwnam . return_value = ( "<STR_LIT:user>" , "<STR_LIT:x>" , <NUM_LIT:5> , <NUM_LIT:5> , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( <NUM_LIT:5> , ptool . _norm_uid ( "<STR_LIT:user>" ) ) <EOL> mock_getpwnam . assert_called_with ( "<STR_LIT:user>" ) <EOL> def test_norm_entry_uid ( self ) : <EOL> ptool = self . get_obj ( ) <EOL> ptool . _norm_uid = Mock ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT:file>" , <EOL> group = "<STR_LIT>" , owner = "<STR_LIT:user>" ) <EOL> self . assertEqual ( ptool . _norm_entry_uid ( entry ) , <EOL> ptool . _norm_uid . return_value ) <EOL> ptool . _norm_uid . assert_called_with ( entry . get ( "<STR_LIT>" ) ) <EOL> ptool . _norm_uid . reset_mock ( ) <EOL> ptool . _norm_uid . side_effect = KeyError <EOL> self . assertEqual ( ptool . _norm_entry_uid ( entry ) , <NUM_LIT:0> ) <EOL> ptool . _norm_uid . assert_called_with ( entry . get ( "<STR_LIT>" ) ) <EOL> def test_norm_acl_perms ( self ) : <EOL> ptool = self . get_obj ( ) <EOL> self . assertEqual ( <NUM_LIT:5> , ptool . _norm_acl_perms ( "<STR_LIT:5>" ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , ptool . _norm_acl_perms ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:5> , ptool . _norm_acl_perms ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:5> , ptool . _norm_acl_perms ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:6> , ptool . _norm_acl_perms ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , ptool . _norm_acl_perms ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , ptool . _norm_acl_perms ( "<STR_LIT:->" ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , ptool . _norm_acl_perms ( "<STR_LIT:a>" ) ) <EOL> self . assertEqual ( <NUM_LIT:6> , ptool . _norm_acl_perms ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , ptool . _norm_acl_perms ( "<STR_LIT>" ) ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test__gather_data ( self , mock_lstat ) : <EOL> ptool = self . get_obj ( ) <EOL> path = '<STR_LIT>' <EOL> mock_lstat . side_effect = OSError <EOL> self . assertFalse ( ptool . _gather_data ( path ) [ <NUM_LIT:0> ] ) <EOL> mock_lstat . assert_called_with ( path ) <EOL> mock_lstat . reset_mock ( ) <EOL> mock_lstat . side_effect = None <EOL> stat_rv = MagicMock ( ) <EOL> def stat_getitem ( key ) : <EOL> if int ( key ) == stat . ST_UID : <EOL> return <NUM_LIT:0> <EOL> elif int ( key ) == stat . ST_GID : <EOL> return <NUM_LIT:10> <EOL> elif int ( key ) == stat . ST_MODE : <EOL> return int ( '<STR_LIT>' , <NUM_LIT:8> ) <EOL> stat_rv . __getitem__ = Mock ( side_effect = stat_getitem ) <EOL> mock_lstat . return_value = stat_rv <EOL> states = ( Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX , <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_ACLS ) <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX = False <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_ACLS = False <EOL> self . assertEqual ( ptool . _gather_data ( path ) , <EOL> ( stat_rv , '<STR_LIT:0>' , '<STR_LIT>' , '<STR_LIT>' , None , None ) ) <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX , Bcfg2 . Client . Tools . POSIX . base . HAS_ACLS = states <EOL> mock_lstat . assert_called_with ( path ) <EOL> @ skipUnless ( HAS_SELINUX , "<STR_LIT>" ) <EOL> def test__gather_data_selinux ( self ) : <EOL> ptool = self . get_obj ( ) <EOL> context = '<STR_LIT>' <EOL> path = '<STR_LIT>' <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patchIf ( HAS_SELINUX , "<STR_LIT>" ) <EOL> def inner ( mock_lgetfilecon , mock_lstat ) : <EOL> mock_lgetfilecon . return_value = [ len ( context ) + <NUM_LIT:1> , context ] <EOL> mock_lstat . return_value = MagicMock ( ) <EOL> mock_lstat . return_value . __getitem__ . return_value = MagicMock ( ) <EOL> state = ( Bcfg2 . Client . Tools . POSIX . base . HAS_ACLS , <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX ) <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_ACLS = False <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX = True <EOL> self . assertEqual ( ptool . _gather_data ( path ) [ <NUM_LIT:4> ] , '<STR_LIT>' ) <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_ACLS , Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX = state <EOL> mock_lgetfilecon . assert_called_with ( path ) <EOL> inner ( ) <EOL> @ skipUnless ( HAS_ACLS , "<STR_LIT>" ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test__gather_data_acls ( self , mock_S_ISLNK , mock_lstat ) : <EOL> ptool = self . get_obj ( ) <EOL> ptool . _list_file_acls = Mock ( ) <EOL> acls = { ( "<STR_LIT:default>" , posix1e . ACL_USER , "<STR_LIT>" ) : "<STR_LIT>" , <EOL> ( "<STR_LIT>" , posix1e . ACL_GROUP , "<STR_LIT>" ) : "<STR_LIT>" } <EOL> ptool . _list_file_acls . return_value = acls <EOL> path = '<STR_LIT>' <EOL> mock_lstat . return_value = MagicMock ( ) <EOL> mock_lstat . return_value . __getitem__ . return_value = MagicMock ( ) <EOL> mock_S_ISLNK . return_value = False <EOL> state = ( Bcfg2 . Client . Tools . POSIX . base . HAS_ACLS , <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX ) <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_ACLS = True <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX = False <EOL> self . assertItemsEqual ( ptool . _gather_data ( path ) [ <NUM_LIT:5> ] , acls ) <EOL> ptool . _list_file_acls . assert_called_with ( path ) <EOL> mock_S_ISLNK . return_value = True <EOL> ptool . _list_file_acls . reset_mock ( ) <EOL> self . assertEqual ( ptool . _gather_data ( path ) [ <NUM_LIT:5> ] , None ) <EOL> self . assertFalse ( ptool . _list_file_acls . called ) <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_ACLS , Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX = state <EOL> @ patchIf ( HAS_SELINUX , "<STR_LIT>" ) <EOL> def test_verify_metadata ( self , mock_matchpathcon ) : <EOL> ptool = self . get_obj ( ) <EOL> ptool . _norm_entry_uid = Mock ( ) <EOL> ptool . _norm_entry_gid = Mock ( ) <EOL> ptool . _verify_acls = Mock ( ) <EOL> ptool . _gather_data = Mock ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT:file>" , <EOL> group = "<STR_LIT>" , owner = "<STR_LIT:user>" , mode = "<STR_LIT>" , <EOL> secontext = '<STR_LIT>' ) <EOL> orig_entry = copy . deepcopy ( entry ) <EOL> def reset ( ) : <EOL> ptool . _gather_data . reset_mock ( ) <EOL> ptool . _verify_acls . reset_mock ( ) <EOL> ptool . _norm_entry_uid . reset_mock ( ) <EOL> ptool . _norm_entry_gid . reset_mock ( ) <EOL> return copy . deepcopy ( orig_entry ) <EOL> ptool . _gather_data . return_value = ( False , None , None , None , None , None ) <EOL> self . assertFalse ( ptool . _verify_metadata ( entry ) ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" , "<STR_LIT>" ) . lower ( ) , "<STR_LIT:false>" ) <EOL> ptool . _gather_data . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> expected = [ ( '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT:0>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:3> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:4> , '<STR_LIT>' ) ] <EOL> ptool . _norm_entry_uid . return_value = <NUM_LIT:0> <EOL> ptool . _norm_entry_gid . return_value = <NUM_LIT:10> <EOL> gather_data_rv = [ MagicMock ( ) , None , None , None , None , [ ] ] <EOL> for attr , idx , val in expected : <EOL> gather_data_rv [ idx ] = val <EOL> entry = reset ( ) <EOL> ptool . _gather_data . return_value = tuple ( gather_data_rv ) <EOL> self . assertTrue ( ptool . _verify_metadata ( entry ) ) <EOL> ptool . _gather_data . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _verify_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" , '<STR_LIT:true>' ) , '<STR_LIT:true>' ) <EOL> for attr , idx , val in expected : <EOL> self . assertEqual ( entry . get ( attr ) , val ) <EOL> entry = reset ( ) <EOL> gather_data_rv [ <NUM_LIT:4> ] = None <EOL> sestate = Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX = False <EOL> ptool . _gather_data . return_value = tuple ( gather_data_rv ) <EOL> self . assertTrue ( ptool . _verify_metadata ( entry ) ) <EOL> ptool . _gather_data . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _verify_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" , '<STR_LIT:true>' ) , '<STR_LIT:true>' ) <EOL> for attr , idx , val in expected : <EOL> if attr != '<STR_LIT>' : <EOL> self . assertEqual ( entry . get ( attr ) , val ) <EOL> Bcfg2 . Client . Tools . POSIX . base . HAS_SELINUX = sestate <EOL> gather_data_rv = [ MagicMock ( ) , None , None , None , None , [ ] ] <EOL> for attr , idx , val in expected : <EOL> gather_data_rv [ idx ] = val <EOL> ptool . _gather_data . return_value = tuple ( gather_data_rv ) <EOL> stat_mode = <NUM_LIT> <EOL> mtime = <NUM_LIT> <EOL> stat_rv = ( stat_mode , Mock ( ) , Mock ( ) , Mock ( ) , Mock ( ) , Mock ( ) , Mock ( ) , <EOL> Mock ( ) , mtime , Mock ( ) ) <EOL> gather_data_rv [ <NUM_LIT:0> ] = stat_rv <EOL> entry = reset ( ) <EOL> entry . set ( "<STR_LIT>" , str ( mtime ) ) <EOL> ptool . _gather_data . return_value = tuple ( gather_data_rv ) <EOL> self . assertTrue ( ptool . _verify_metadata ( entry ) ) <EOL> ptool . _gather_data . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _verify_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" , '<STR_LIT:true>' ) , '<STR_LIT:true>' ) <EOL> for attr , idx , val in expected : <EOL> self . assertEqual ( entry . get ( attr ) , val ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" ) , str ( mtime ) ) <EOL> failures = [ ( '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> , '<STR_LIT:100>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:3> , '<STR_LIT>' ) ] <EOL> if HAS_SELINUX : <EOL> failures . append ( ( '<STR_LIT>' , <NUM_LIT:4> , '<STR_LIT>' ) ) <EOL> for fail_attr , fail_idx , fail_val in failures : <EOL> entry = reset ( ) <EOL> entry . set ( "<STR_LIT>" , str ( mtime ) ) <EOL> gather_data_rv = [ stat_rv , None , None , None , None , [ ] ] <EOL> for attr , idx , val in expected : <EOL> gather_data_rv [ idx ] = val <EOL> gather_data_rv [ fail_idx ] = fail_val <EOL> ptool . _gather_data . return_value = tuple ( gather_data_rv ) <EOL> self . assertFalse ( ptool . _verify_metadata ( entry ) ) <EOL> ptool . _gather_data . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _verify_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" , '<STR_LIT:true>' ) , '<STR_LIT:true>' ) <EOL> self . assertEqual ( entry . get ( fail_attr ) , fail_val ) <EOL> for attr , idx , val in expected : <EOL> if attr != fail_attr : <EOL> self . assertEqual ( entry . get ( attr ) , val ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" ) , str ( mtime ) ) <EOL> fail_mtime = <NUM_LIT> <EOL> entry = reset ( ) <EOL> entry . set ( "<STR_LIT>" , str ( mtime ) ) <EOL> fail_stat_rv = MagicMock ( ) <EOL> fail_stat_rv . __getitem__ . return_value = fail_mtime <EOL> gather_data_rv = [ fail_stat_rv , None , None , None , None , [ ] ] <EOL> for attr , idx , val in expected : <EOL> gather_data_rv [ idx ] = val <EOL> ptool . _gather_data . return_value = tuple ( gather_data_rv ) <EOL> self . assertFalse ( ptool . _verify_metadata ( entry ) ) <EOL> ptool . _gather_data . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _verify_acls . assert_called_with ( entry , path = entry . get ( "<STR_LIT:name>" ) ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" , '<STR_LIT:true>' ) , '<STR_LIT:true>' ) <EOL> for attr , idx , val in expected : <EOL> self . assertEqual ( entry . get ( attr ) , val ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" ) , str ( fail_mtime ) ) <EOL> if HAS_SELINUX : <EOL> entry = reset ( ) <EOL> entry . set ( "<STR_LIT>" , str ( mtime ) ) <EOL> entry . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> context1 = "<STR_LIT>" <EOL> context2 = "<STR_LIT>" <EOL> mock_matchpathcon . return_value = [ <NUM_LIT:1> + len ( context1 ) , <EOL> context1 ] <EOL> gather_data_rv = [ stat_rv , None , None , None , None , [ ] ] <EOL> for attr , idx , val in expected : <EOL> gather_data_rv [ idx ] = val <EOL> ptool . _gather_data . return_value = tuple ( gather_data_rv ) <EOL> self . assertTrue ( ptool . _verify_metadata ( entry ) ) <EOL> ptool . _gather_data . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _verify_acls . assert_called_with ( entry , <EOL> path = entry . get ( "<STR_LIT:name>" ) ) <EOL> mock_matchpathcon . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , stat_mode ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" , '<STR_LIT:true>' ) , '<STR_LIT:true>' ) <EOL> for attr , idx , val in expected : <EOL> self . assertEqual ( entry . get ( attr ) , val ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" ) , str ( mtime ) ) <EOL> entry = reset ( ) <EOL> entry . set ( "<STR_LIT>" , str ( mtime ) ) <EOL> entry . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> mock_matchpathcon . return_value = [ <NUM_LIT:1> + len ( context2 ) , <EOL> context2 ] <EOL> self . assertFalse ( ptool . _verify_metadata ( entry ) ) <EOL> ptool . _gather_data . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _verify_acls . assert_called_with ( entry , <EOL> path = entry . get ( "<STR_LIT:name>" ) ) <EOL> mock_matchpathcon . assert_called_with ( entry . get ( "<STR_LIT:name>" ) , stat_mode ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" , '<STR_LIT:true>' ) , '<STR_LIT:true>' ) <EOL> for attr , idx , val in expected : <EOL> self . assertEqual ( entry . get ( attr ) , val ) <EOL> self . assertEqual ( entry . get ( "<STR_LIT>" ) , str ( mtime ) ) <EOL> @ skipUnless ( HAS_ACLS , "<STR_LIT>" ) <EOL> def test_list_entry_acls ( self ) : <EOL> ptool = self . get_obj ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT:file>" ) <EOL> lxml . etree . SubElement ( entry , "<STR_LIT>" , scope = "<STR_LIT:user>" , type = "<STR_LIT:default>" , <EOL> user = "<STR_LIT:user>" , perms = "<STR_LIT>" ) <EOL> lxml . etree . SubElement ( entry , "<STR_LIT>" , scope = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> group = "<STR_LIT>" , perms = "<STR_LIT:5>" ) <EOL> self . assertItemsEqual ( ptool . _list_entry_acls ( entry ) , <EOL> { ( "<STR_LIT:default>" , posix1e . ACL_USER , "<STR_LIT:user>" ) : <NUM_LIT:7> , <EOL> ( "<STR_LIT>" , posix1e . ACL_GROUP , "<STR_LIT>" ) : <NUM_LIT:5> } ) <EOL> @ skipUnless ( HAS_ACLS , "<STR_LIT>" ) <EOL> @ patchIf ( HAS_ACLS , "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_list_file_acls ( self , mock_isdir , mock_getgrgid , mock_getpwuid , <EOL> mock_ACL ) : <EOL> ptool = self . get_obj ( ) <EOL> path = '<STR_LIT>' <EOL> file_acls = [ ] <EOL> acl = Mock ( ) <EOL> acl . tag_type = posix1e . ACL_USER <EOL> acl . qualifier = <NUM_LIT:10> <EOL> acl . permset = '<STR_LIT>' <EOL> file_acls . append ( acl ) <EOL> acl = Mock ( ) <EOL> acl . tag_type = posix1e . ACL_GROUP <EOL> acl . qualifier = <NUM_LIT:100> <EOL> acl . permset = '<STR_LIT>' <EOL> file_acls . append ( acl ) <EOL> acl = Mock ( ) <EOL> acl . tag_type = posix1e . ACL_MASK <EOL> file_acls . append ( acl ) <EOL> acls = { ( "<STR_LIT>" , posix1e . ACL_USER , "<STR_LIT:user>" ) : <NUM_LIT:7> , <EOL> ( "<STR_LIT>" , posix1e . ACL_GROUP , "<STR_LIT>" ) : <NUM_LIT:5> } <EOL> fileacl_rv = MagicMock ( ) <EOL> fileacl_rv . valid . return_value = True <EOL> fileacl_rv . __iter__ . return_value = iter ( file_acls ) <EOL> filedef_rv = MagicMock ( ) <EOL> filedef_rv . valid . return_value = True <EOL> filedef_rv . __iter__ . return_value = iter ( file_acls ) <EOL> def mock_acl_rv ( file = None , filedef = None ) : <EOL> if file : <EOL> return fileacl_rv <EOL> elif filedef : <EOL> return filedef_rv <EOL> mock_isdir . return_value = False <EOL> mock_getgrgid . return_value = ( "<STR_LIT>" , "<STR_LIT:x>" , <NUM_LIT:5> , [ ] ) <EOL> mock_getpwuid . return_value = ( "<STR_LIT:user>" , "<STR_LIT:x>" , <NUM_LIT:5> , <NUM_LIT:5> , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> def reset ( ) : <EOL> mock_isdir . reset_mock ( ) <EOL> mock_getgrgid . reset_mock ( ) <EOL> mock_getpwuid . reset_mock ( ) <EOL> mock_ACL . reset_mock ( ) <EOL> mock_ACL . side_effect = IOError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> self . assertItemsEqual ( ptool . _list_file_acls ( path ) , dict ( ) ) <EOL> reset ( ) <EOL> mock_ACL . side_effect = IOError <EOL> self . assertItemsEqual ( ptool . _list_file_acls ( path ) , dict ( ) ) <EOL> reset ( ) <EOL> mock_ACL . side_effect = mock_acl_rv <EOL> self . assertItemsEqual ( ptool . _list_file_acls ( path ) , acls ) <EOL> mock_isdir . assert_called_with ( path ) <EOL> mock_getgrgid . assert_called_with ( <NUM_LIT:100> ) <EOL> mock_getpwuid . assert_called_with ( <NUM_LIT:10> ) <EOL> mock_ACL . assert_called_with ( file = path ) <EOL> reset ( ) <EOL> mock_isdir . return_value = True <EOL> fileacl_rv . __iter__ . return_value = iter ( file_acls ) <EOL> filedef_rv . __iter__ . return_value = iter ( file_acls ) <EOL> defacls = acls <EOL> for akey , perms in list ( acls . items ( ) ) : <EOL> defacls [ ( '<STR_LIT:default>' , akey [ <NUM_LIT:1> ] , akey [ <NUM_LIT:2> ] ) ] = perms <EOL> self . assertItemsEqual ( ptool . _list_file_acls ( path ) , defacls ) <EOL> mock_isdir . assert_called_with ( path ) <EOL> self . assertItemsEqual ( mock_getgrgid . call_args_list , <EOL> [ call ( <NUM_LIT:100> ) , call ( <NUM_LIT:100> ) ] ) <EOL> self . assertItemsEqual ( mock_getpwuid . call_args_list , <EOL> [ call ( <NUM_LIT:10> ) , call ( <NUM_LIT:10> ) ] ) <EOL> self . assertItemsEqual ( mock_ACL . call_args_list , <EOL> [ call ( file = path ) , call ( filedef = path ) ] ) <EOL> @ skipUnless ( HAS_ACLS , "<STR_LIT>" ) <EOL> def test_verify_acls ( self ) : <EOL> ptool = self . get_obj ( ) <EOL> ptool . _list_file_acls = Mock ( ) <EOL> ptool . _list_entry_acls = Mock ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT:file>" ) <EOL> acls = { ( "<STR_LIT>" , posix1e . ACL_USER , "<STR_LIT:user>" ) : <NUM_LIT:7> , <EOL> ( "<STR_LIT:default>" , posix1e . ACL_GROUP , "<STR_LIT>" ) : <NUM_LIT:5> } <EOL> extra_acls = copy . deepcopy ( acls ) <EOL> extra_acls [ ( "<STR_LIT>" , posix1e . ACL_USER , "<STR_LIT>" ) ] = <NUM_LIT:4> <EOL> ptool . _list_entry_acls . return_value = acls <EOL> ptool . _list_file_acls . return_value = acls <EOL> self . assertTrue ( ptool . _verify_acls ( entry ) ) <EOL> ptool . _list_entry_acls . assert_called_with ( entry ) <EOL> ptool . _list_file_acls . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _list_entry_acls . reset_mock ( ) <EOL> ptool . _list_file_acls . reset_mock ( ) <EOL> ptool . _list_file_acls . return_value = extra_acls <EOL> self . assertFalse ( ptool . _verify_acls ( entry ) ) <EOL> ptool . _list_entry_acls . assert_called_with ( entry ) <EOL> ptool . _list_file_acls . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> ptool . _list_entry_acls . reset_mock ( ) <EOL> ptool . _list_file_acls . reset_mock ( ) <EOL> ptool . _list_entry_acls . return_value = extra_acls <EOL> ptool . _list_file_acls . return_value = acls <EOL> self . assertFalse ( ptool . _verify_acls ( entry ) ) <EOL> ptool . _list_entry_acls . assert_called_with ( entry ) <EOL> ptool . _list_file_acls . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> wrong_acls = copy . deepcopy ( extra_acls ) <EOL> wrong_acls [ ( "<STR_LIT>" , posix1e . ACL_USER , "<STR_LIT>" ) ] = <NUM_LIT:5> <EOL> ptool . _list_entry_acls . reset_mock ( ) <EOL> ptool . _list_file_acls . reset_mock ( ) <EOL> ptool . _list_entry_acls . return_value = extra_acls <EOL> ptool . _list_file_acls . return_value = wrong_acls <EOL> self . assertFalse ( ptool . _verify_acls ( entry ) ) <EOL> ptool . _list_entry_acls . assert_called_with ( entry ) <EOL> ptool . _list_file_acls . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_makedirs ( self , mock_exists , mock_makedirs ) : <EOL> ptool = self . get_obj ( ) <EOL> ptool . _set_perms = Mock ( ) <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , <EOL> type = "<STR_LIT>" , mode = "<STR_LIT>" ) <EOL> parent_entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , <EOL> type = "<STR_LIT>" , mode = "<STR_LIT>" ) <EOL> def reset ( ) : <EOL> mock_exists . reset_mock ( ) <EOL> ptool . _set_perms . reset_mock ( ) <EOL> mock_makedirs . reset_mock ( ) <EOL> ptool . _set_perms . return_value = True <EOL> def path_exists_rv ( path ) : <EOL> if path == "<STR_LIT>" : <EOL> return True <EOL> else : <EOL> return False <EOL> mock_exists . side_effect = path_exists_rv <EOL> self . assertTrue ( ptool . _makedirs ( entry ) ) <EOL> self . assertItemsEqual ( mock_exists . call_args_list , <EOL> [ call ( "<STR_LIT>" ) , call ( "<STR_LIT>" ) , <EOL> call ( "<STR_LIT>" ) ] ) <EOL> for args in ptool . _set_perms . call_args_list : <EOL> self . assertXMLEqual ( args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , parent_entry ) <EOL> self . assertItemsEqual ( [ a [ <NUM_LIT:1> ] for a in ptool . _set_perms . call_args_list ] , <EOL> [ dict ( path = "<STR_LIT>" ) , <EOL> dict ( path = "<STR_LIT>" ) ] ) <EOL> mock_makedirs . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> reset ( ) <EOL> mock_makedirs . side_effect = OSError <EOL> self . assertFalse ( ptool . _makedirs ( entry ) ) <EOL> for args in ptool . _set_perms . call_args_list : <EOL> self . assertXMLEqual ( args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , parent_entry ) <EOL> self . assertItemsEqual ( [ a [ <NUM_LIT:1> ] for a in ptool . _set_perms . call_args_list ] , <EOL> [ dict ( path = "<STR_LIT>" ) , <EOL> dict ( path = "<STR_LIT>" ) ] ) <EOL> reset ( ) <EOL> mock_makedirs . side_effect = None <EOL> def set_perms_rv ( entry , path = None ) : <EOL> if path == '<STR_LIT>' : <EOL> return False <EOL> else : <EOL> return True <EOL> ptool . _set_perms . side_effect = set_perms_rv <EOL> self . assertTrue ( ptool . _makedirs ( entry ) ) <EOL> self . assertItemsEqual ( mock_exists . call_args_list , <EOL> [ call ( "<STR_LIT>" ) , call ( "<STR_LIT>" ) , <EOL> call ( "<STR_LIT>" ) ] ) <EOL> for args in ptool . _set_perms . call_args_list : <EOL> self . assertXMLEqual ( args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , parent_entry ) <EOL> self . assertItemsEqual ( [ a [ <NUM_LIT:1> ] for a in ptool . _set_perms . call_args_list ] , <EOL> [ dict ( path = "<STR_LIT>" ) , <EOL> dict ( path = "<STR_LIT>" ) ] ) <EOL> mock_makedirs . assert_called_with ( entry . get ( "<STR_LIT:name>" ) ) <EOL> class TestPOSIXLinkTool ( TestPOSIXTool ) : <EOL> test_obj = POSIXLinkTool <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_verify ( self , mock_verify ) : <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> to = "<STR_LIT>" ) <EOL> ptool = self . get_obj ( ) <EOL> linktype = ptool . __linktype__ <EOL> ptool . __linktype__ = "<STR_LIT:test>" <EOL> ptool . _verify = Mock ( ) <EOL> ptool . _verify . return_value = True <EOL> mock_verify . return_value = False <EOL> self . assertFalse ( ptool . verify ( entry , [ ] ) ) <EOL> ptool . _verify . assert_called_with ( entry ) <EOL> mock_verify . assert_called_with ( ptool , entry , [ ] ) <EOL> ptool . _verify . reset_mock ( ) <EOL> mock_verify . reset_mock ( ) <EOL> mock_verify . return_value = True <EOL> self . assertTrue ( ptool . verify ( entry , [ ] ) ) <EOL> ptool . _verify . assert_called_with ( entry ) <EOL> mock_verify . assert_called_with ( ptool , entry , [ ] ) <EOL> ptool . _verify . reset_mock ( ) <EOL> mock_verify . reset_mock ( ) <EOL> ptool . _verify . return_value = False <EOL> self . assertFalse ( ptool . verify ( entry , [ ] ) ) <EOL> ptool . _verify . assert_called_with ( entry ) <EOL> mock_verify . assert_called_with ( ptool , entry , [ ] ) <EOL> ptool . _verify . reset_mock ( ) <EOL> mock_verify . reset_mock ( ) <EOL> ptool . _verify . side_effect = OSError <EOL> self . assertFalse ( ptool . verify ( entry , [ ] ) ) <EOL> ptool . _verify . assert_called_with ( entry ) <EOL> ptool . __linktype__ = linktype <EOL> def test__verify ( self ) : <EOL> ptool = self . get_obj ( ) <EOL> self . assertRaises ( NotImplementedError , ptool . _verify , Mock ( ) ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_install ( self , mock_install ) : <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> to = "<STR_LIT>" ) <EOL> ptool = self . get_obj ( ) <EOL> linktype = ptool . __linktype__ <EOL> ptool . __linktype__ = "<STR_LIT:test>" <EOL> ptool . _exists = Mock ( ) <EOL> ptool . _link = Mock ( ) <EOL> ptool . _exists . return_value = False <EOL> mock_install . return_value = True <EOL> self . assertTrue ( ptool . install ( entry ) ) <EOL> ptool . _exists . assert_called_with ( entry , remove = True ) <EOL> ptool . _link . assert_called_with ( entry ) <EOL> mock_install . assert_called_with ( ptool , entry ) <EOL> ptool . _link . reset_mock ( ) <EOL> ptool . _exists . reset_mock ( ) <EOL> mock_install . reset_mock ( ) <EOL> ptool . _link . side_effect = OSError <EOL> self . assertFalse ( ptool . install ( entry ) ) <EOL> ptool . _exists . assert_called_with ( entry , remove = True ) <EOL> ptool . _link . assert_called_with ( entry ) <EOL> mock_install . assert_called_with ( ptool , entry ) <EOL> ptool . __linktype__ = linktype <EOL> def test__link ( self ) : <EOL> ptool = self . get_obj ( ) <EOL> self . assertRaises ( NotImplementedError , ptool . _link , Mock ( ) ) </s>
