<s> from __future__ import absolute_import <EOL> import getpass <EOL> import grp <EOL> import pwd <EOL> import os <EOL> import shutil <EOL> import sys <EOL> from salttesting . mock import patch , MagicMock <EOL> from salttesting import skipIf <EOL> from salttesting . helpers import ensure_in_syspath <EOL> ensure_in_syspath ( '<STR_LIT>' ) <EOL> import integration <EOL> import salt . utils <EOL> from salt . modules import file as filemod <EOL> class FileModuleTest ( integration . ModuleCase ) : <EOL> '''<STR_LIT>''' <EOL> def setUp ( self ) : <EOL> self . myfile = os . path . join ( integration . TMP , '<STR_LIT>' ) <EOL> with salt . utils . fopen ( self . myfile , '<STR_LIT>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> self . mydir = os . path . join ( integration . TMP , '<STR_LIT>' ) <EOL> if not os . path . isdir ( self . mydir ) : <EOL> os . makedirs ( self . mydir ) <EOL> self . mysymlink = os . path . join ( integration . TMP , '<STR_LIT>' ) <EOL> if os . path . islink ( self . mysymlink ) : <EOL> os . remove ( self . mysymlink ) <EOL> os . symlink ( self . myfile , self . mysymlink ) <EOL> self . mybadsymlink = os . path . join ( integration . TMP , '<STR_LIT>' ) <EOL> if os . path . islink ( self . mybadsymlink ) : <EOL> os . remove ( self . mybadsymlink ) <EOL> os . symlink ( '<STR_LIT>' , self . mybadsymlink ) <EOL> super ( FileModuleTest , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> if os . path . isfile ( self . myfile ) : <EOL> os . remove ( self . myfile ) <EOL> if os . path . islink ( self . mysymlink ) : <EOL> os . remove ( self . mysymlink ) <EOL> if os . path . islink ( self . mybadsymlink ) : <EOL> os . remove ( self . mybadsymlink ) <EOL> shutil . rmtree ( self . mydir , ignore_errors = True ) <EOL> super ( FileModuleTest , self ) . tearDown ( ) <EOL> @ skipIf ( salt . utils . is_windows ( ) , '<STR_LIT>' ) <EOL> def test_chown ( self ) : <EOL> user = getpass . getuser ( ) <EOL> if sys . platform == '<STR_LIT>' : <EOL> group = '<STR_LIT>' <EOL> elif sys . platform . startswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> group = grp . getgrgid ( pwd . getpwuid ( os . getuid ( ) ) . pw_gid ) . gr_name <EOL> ret = self . run_function ( '<STR_LIT>' , arg = [ self . myfile , user , group ] ) <EOL> self . assertIsNone ( ret ) <EOL> fstat = os . stat ( self . myfile ) <EOL> self . assertEqual ( fstat . st_uid , os . getuid ( ) ) <EOL> self . assertEqual ( fstat . st_gid , grp . getgrnam ( group ) . gr_gid ) <EOL> @ skipIf ( salt . utils . is_windows ( ) , '<STR_LIT>' ) <EOL> def test_chown_no_user ( self ) : <EOL> user = '<STR_LIT>' <EOL> group = grp . getgrgid ( pwd . getpwuid ( os . getuid ( ) ) . pw_gid ) . gr_name <EOL> ret = self . run_function ( '<STR_LIT>' , arg = [ self . myfile , user , group ] ) <EOL> self . assertIn ( '<STR_LIT>' , ret ) <EOL> @ skipIf ( salt . utils . is_windows ( ) , '<STR_LIT>' ) <EOL> def test_chown_no_user_no_group ( self ) : <EOL> user = '<STR_LIT>' <EOL> group = '<STR_LIT>' <EOL> ret = self . run_function ( '<STR_LIT>' , arg = [ self . myfile , user , group ] ) <EOL> self . assertIn ( '<STR_LIT>' , ret ) <EOL> self . assertIn ( '<STR_LIT>' , ret ) <EOL> @ skipIf ( salt . utils . is_windows ( ) , '<STR_LIT>' ) <EOL> def test_chown_no_path ( self ) : <EOL> user = getpass . getuser ( ) <EOL> if sys . platform == '<STR_LIT>' : <EOL> group = '<STR_LIT>' <EOL> elif sys . platform . startswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> group = grp . getgrgid ( pwd . getpwuid ( os . getuid ( ) ) . pw_gid ) . gr_name <EOL> ret = self . run_function ( '<STR_LIT>' , <EOL> arg = [ '<STR_LIT>' , user , group ] ) <EOL> self . assertIn ( '<STR_LIT>' , ret ) <EOL> @ skipIf ( salt . utils . is_windows ( ) , '<STR_LIT>' ) <EOL> def test_chown_noop ( self ) : <EOL> user = '<STR_LIT>' <EOL> group = '<STR_LIT>' <EOL> ret = self . run_function ( '<STR_LIT>' , arg = [ self . myfile , user , group ] ) <EOL> self . assertIsNone ( ret ) <EOL> fstat = os . stat ( self . myfile ) <EOL> self . assertEqual ( fstat . st_uid , os . getuid ( ) ) <EOL> self . assertEqual ( fstat . st_gid , os . getgid ( ) ) <EOL> @ skipIf ( salt . utils . is_windows ( ) , '<STR_LIT>' ) <EOL> def test_chgrp ( self ) : <EOL> if sys . platform == '<STR_LIT>' : <EOL> group = '<STR_LIT>' <EOL> elif sys . platform . startswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> group = grp . getgrgid ( pwd . getpwuid ( os . getuid ( ) ) . pw_gid ) . gr_name <EOL> ret = self . run_function ( '<STR_LIT>' , arg = [ self . myfile , group ] ) <EOL> self . assertIsNone ( ret ) <EOL> fstat = os . stat ( self . myfile ) <EOL> self . assertEqual ( fstat . st_gid , grp . getgrnam ( group ) . gr_gid ) <EOL> @ skipIf ( salt . utils . is_windows ( ) , '<STR_LIT>' ) <EOL> def test_chgrp_failure ( self ) : <EOL> group = '<STR_LIT>' <EOL> ret = self . run_function ( '<STR_LIT>' , arg = [ self . myfile , group ] ) <EOL> self . assertIn ( '<STR_LIT>' , ret ) <EOL> def test_patch ( self ) : <EOL> if not self . run_function ( '<STR_LIT>' , [ '<STR_LIT>' ] ) : <EOL> self . skipTest ( '<STR_LIT>' ) <EOL> src_patch = os . path . join ( <EOL> integration . FILES , '<STR_LIT:file>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> src_file = os . path . join ( integration . TMP , '<STR_LIT>' ) <EOL> with salt . utils . fopen ( src_file , '<STR_LIT>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> ret = self . minion_run ( '<STR_LIT>' , src_file , src_patch , dry_run = True ) <EOL> assert ret [ '<STR_LIT>' ] == <NUM_LIT:0> , repr ( ret ) <EOL> with salt . utils . fopen ( src_file ) as fp : <EOL> self . assertEqual ( fp . read ( ) , '<STR_LIT>' ) <EOL> ret = self . minion_run ( '<STR_LIT>' , src_file , src_patch ) <EOL> assert ret [ '<STR_LIT>' ] == <NUM_LIT:0> , repr ( ret ) <EOL> with salt . utils . fopen ( src_file ) as fp : <EOL> self . assertEqual ( fp . read ( ) , '<STR_LIT>' ) <EOL> def test_remove_file ( self ) : <EOL> ret = self . run_function ( '<STR_LIT>' , arg = [ self . myfile ] ) <EOL> self . assertTrue ( ret ) <EOL> def test_remove_dir ( self ) : <EOL> ret = self . run_function ( '<STR_LIT>' , arg = [ self . mydir ] ) <EOL> self . assertTrue ( ret ) <EOL> def test_remove_symlink ( self ) : <EOL> ret = self . run_function ( '<STR_LIT>' , arg = [ self . mysymlink ] ) <EOL> self . assertTrue ( ret ) <EOL> def test_remove_broken_symlink ( self ) : <EOL> ret = self . run_function ( '<STR_LIT>' , arg = [ self . mybadsymlink ] ) <EOL> self . assertTrue ( ret ) <EOL> def test_cannot_remove ( self ) : <EOL> ret = self . run_function ( '<STR_LIT>' , arg = [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <EOL> '<STR_LIT>' , ret <EOL> ) <EOL> def test_source_list_for_single_file_returns_unchanged ( self ) : <EOL> ret = self . run_function ( '<STR_LIT>' , [ '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertItemsEqual ( ret , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_source_list_for_list_returns_existing_file ( self ) : <EOL> filemod . __salt__ = { <EOL> '<STR_LIT>' : MagicMock ( <EOL> return_value = [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : MagicMock ( return_value = [ ] ) , <EOL> } <EOL> filemod . __context__ = { } <EOL> ret = filemod . source_list ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertItemsEqual ( ret , [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def test_source_list_for_list_returns_file_from_other_env ( self ) : <EOL> def list_master ( env ) : <EOL> dct = { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> return dct [ env ] <EOL> filemod . __salt__ = { <EOL> '<STR_LIT>' : MagicMock ( side_effect = list_master ) , <EOL> '<STR_LIT>' : MagicMock ( return_value = [ ] ) , <EOL> } <EOL> filemod . __context__ = { } <EOL> ret = filemod . source_list ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertItemsEqual ( ret , [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def test_source_list_for_list_returns_file_from_dict ( self ) : <EOL> filemod . __salt__ = { <EOL> '<STR_LIT>' : MagicMock ( return_value = [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : MagicMock ( return_value = [ ] ) , <EOL> } <EOL> filemod . __context__ = { } <EOL> ret = filemod . source_list ( <EOL> [ { '<STR_LIT>' : '<STR_LIT>' } ] , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertItemsEqual ( ret , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_source_list_for_list_returns_file_from_dict_via_http ( self , remove ) : <EOL> remove . return_value = None <EOL> filemod . __salt__ = { <EOL> '<STR_LIT>' : MagicMock ( return_value = [ ] ) , <EOL> '<STR_LIT>' : MagicMock ( return_value = [ ] ) , <EOL> '<STR_LIT>' : MagicMock ( return_value = '<STR_LIT>' ) , <EOL> } <EOL> filemod . __context__ = { } <EOL> ret = filemod . source_list ( <EOL> [ { '<STR_LIT>' : '<STR_LIT>' } ] , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertItemsEqual ( ret , [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def test_source_list_for_single_local_file_slash_returns_unchanged ( self ) : <EOL> ret = self . run_function ( '<STR_LIT>' , [ self . myfile , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertItemsEqual ( ret , [ self . myfile , '<STR_LIT>' ] ) <EOL> def test_source_list_for_single_local_file_proto_returns_unchanged ( self ) : <EOL> ret = self . run_function ( '<STR_LIT>' , [ '<STR_LIT>' + self . myfile , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertItemsEqual ( ret , [ '<STR_LIT>' + self . myfile , '<STR_LIT>' ] ) <EOL> def test_source_list_for_list_returns_existing_local_file_slash ( self ) : <EOL> ret = filemod . source_list ( [ self . myfile + '<STR_LIT>' , <EOL> self . myfile ] , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertItemsEqual ( ret , [ self . myfile , '<STR_LIT>' ] ) <EOL> def test_source_list_for_list_returns_existing_local_file_proto ( self ) : <EOL> ret = filemod . source_list ( [ '<STR_LIT>' + self . myfile + '<STR_LIT>' , <EOL> '<STR_LIT>' + self . myfile ] , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertItemsEqual ( ret , [ '<STR_LIT>' + self . myfile , '<STR_LIT>' ] ) <EOL> def test_source_list_for_list_returns_local_file_slash_from_dict ( self ) : <EOL> ret = filemod . source_list ( <EOL> [ { self . myfile : '<STR_LIT>' } ] , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertItemsEqual ( ret , [ self . myfile , '<STR_LIT>' ] ) <EOL> def test_source_list_for_list_returns_local_file_proto_from_dict ( self ) : <EOL> ret = filemod . source_list ( <EOL> [ { '<STR_LIT>' + self . myfile : '<STR_LIT>' } ] , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertItemsEqual ( ret , [ '<STR_LIT>' + self . myfile , '<STR_LIT>' ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from integration import run_tests <EOL> run_tests ( FileModuleTest ) </s>
