<s> __author__ = "<STR_LIT>" <EOL> __credits__ = "<STR_LIT>" <EOL> import unittest <EOL> from nose . plugins . attrib import attr <EOL> import os <EOL> from ncclient . manager import Manager , make_device_handler <EOL> from ncclient . transport import SSHSession <EOL> from jnpr . junos import Device <EOL> from jnpr . junos . utils . fs import FS <EOL> from mock import patch , MagicMock , call <EOL> @ attr ( '<STR_LIT>' ) <EOL> class TestFS ( unittest . TestCase ) : <EOL> @ patch ( '<STR_LIT>' ) <EOL> def setUp ( self , mock_connect ) : <EOL> mock_connect . side_effect = self . _mock_manager <EOL> self . dev = Device ( host = '<STR_LIT>' , user = '<STR_LIT>' , password = '<STR_LIT>' , <EOL> gather_facts = False ) <EOL> self . dev . open ( ) <EOL> self . fs = FS ( self . dev ) <EOL> def test_cat_wrong_path_return_none ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . assertEqual ( self . fs . cat ( path ) , None ) <EOL> def test_cat ( self ) : <EOL> self . fs . _dev . rpc . file_show = MagicMock ( side_effect = self . _mock_manager ) <EOL> path = '<STR_LIT>' <EOL> self . assertTrue ( '<STR_LIT>' in self . fs . cat ( path ) ) <EOL> self . fs . _dev . rpc . file_show . assert_called_with ( filename = '<STR_LIT>' ) <EOL> def test_cwd ( self ) : <EOL> self . fs . _dev . rpc . set_cli_working_directory = MagicMock ( ) <EOL> folder = '<STR_LIT>' <EOL> self . fs . cwd ( folder ) <EOL> self . fs . _dev . rpc . set_cli_working_directory . assert_called_with ( directory = '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_pwd ( self , mock_execute ) : <EOL> mock_execute . side_effect = MagicMock ( side_effect = self . _mock_manager ) <EOL> self . fs . pwd ( ) <EOL> self . assertEqual ( self . fs . pwd ( ) , '<STR_LIT>' ) <EOL> def test_checksum_return_none ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . assertEqual ( self . fs . checksum ( path ) , None ) <EOL> def test_checksum_unknown_calc ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . assertRaises ( ValueError , self . fs . checksum , path = path , calc = '<STR_LIT:abc>' ) <EOL> def test_checksum_return_rsp ( self ) : <EOL> self . fs . dev . rpc . get_sha256_checksum_information = MagicMock ( side_effect = self . _mock_manager ) <EOL> path = '<STR_LIT>' <EOL> self . assertEqual ( self . fs . checksum ( path , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . fs . dev . rpc . get_sha256_checksum_information . assert_called_with ( path = '<STR_LIT>' ) <EOL> def test_stat_calling___decode_file ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_list = MagicMock ( side_effect = self . _mock_manager ) <EOL> self . assertEqual ( self . fs . stat ( path ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:path>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:size>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:file>' } ) <EOL> def test_stat_calling___decode_dir ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_list = MagicMock ( side_effect = self . _mock_manager ) <EOL> self . assertEqual ( self . fs . stat ( path ) , <EOL> { '<STR_LIT:path>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:size>' : <NUM_LIT:2> } ) <EOL> def test_stat_return_none ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_list = MagicMock ( ) <EOL> self . fs . dev . rpc . file_list . find . return_value = '<STR_LIT>' <EOL> self . assertEqual ( self . fs . stat ( path ) , None ) <EOL> def test_ls_calling___decode_file ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_list = MagicMock ( side_effect = self . _mock_manager ) <EOL> self . assertEqual ( self . fs . ls ( path ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:path>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:size>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:file>' } ) <EOL> def test_ls_calling___decode_dir ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_list = MagicMock ( side_effect = self . _mock_manager ) <EOL> self . assertEqual ( self . fs . ls ( path ) , <EOL> { '<STR_LIT>' : <EOL> { '<STR_LIT:abc>' : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:root>' , '<STR_LIT:path>' : '<STR_LIT:abc>' , <EOL> '<STR_LIT:size>' : <NUM_LIT:2> , '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> } } , <EOL> '<STR_LIT:path>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:size>' : <NUM_LIT:2> } ) <EOL> def test_ls_return_none ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_list = MagicMock ( ) <EOL> self . fs . dev . rpc . file_list . find . return_value = '<STR_LIT>' <EOL> self . assertEqual ( self . fs . ls ( path ) , None ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_ls_link_path_false ( self , mock_decode_file ) : <EOL> mock_decode_file . get . return_value = False <EOL> path = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_list = MagicMock ( side_effect = self . _mock_manager ) <EOL> self . fs . ls ( path , followlink = False ) <EOL> mock_decode_file . assert_has_calls ( call ( ) . get ( '<STR_LIT>' ) ) <EOL> def test_ls_brief_true ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_list = MagicMock ( side_effect = self . _mock_manager ) <EOL> self . assertEqual ( self . fs . ls ( path , brief = True ) , <EOL> { '<STR_LIT>' : [ '<STR_LIT:abc>' ] , '<STR_LIT:path>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:size>' : <NUM_LIT:2> } ) <EOL> def test_ls_calling___decode_dir_type_symbolic_link ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_list = MagicMock ( side_effect = self . _mock_manager ) <EOL> self . assertEqual ( self . fs . ls ( path ) , <EOL> { '<STR_LIT>' : <EOL> { '<STR_LIT:abc>' : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:root>' , '<STR_LIT:path>' : '<STR_LIT:abc>' , <EOL> '<STR_LIT:size>' : <NUM_LIT:2> , '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> } } , <EOL> '<STR_LIT:path>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:size>' : <NUM_LIT:2> } ) <EOL> def test_rm_return_true ( self ) : <EOL> self . fs . dev . rpc . file_delete = MagicMock ( return_value = True ) <EOL> path = '<STR_LIT>' <EOL> self . assertTrue ( self . fs . rm ( path ) ) <EOL> self . fs . dev . rpc . file_delete . assert_called_once_with ( <EOL> path = '<STR_LIT>' ) <EOL> def test_rm_return_false ( self ) : <EOL> path = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_delete = MagicMock ( return_value = False ) <EOL> self . assertFalse ( self . fs . rm ( path ) ) <EOL> self . fs . dev . rpc . file_delete . assert_called_once_with ( <EOL> path = '<STR_LIT>' ) <EOL> def test_copy_return_true ( self ) : <EOL> self . fs . dev . rpc . file_copy = MagicMock ( ) <EOL> initial = '<STR_LIT>' <EOL> final = '<STR_LIT>' <EOL> self . assertTrue ( self . fs . cp ( initial , final ) ) <EOL> self . fs . dev . rpc . file_copy . assert_called_once_with ( <EOL> source = '<STR_LIT>' , <EOL> destination = '<STR_LIT>' ) <EOL> def test_copy_return_false ( self ) : <EOL> initial = '<STR_LIT>' <EOL> final = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_copy = MagicMock ( side_effect = Exception ) <EOL> self . assertFalse ( self . fs . cp ( initial , final ) ) <EOL> self . fs . dev . rpc . file_copy . assert_called_once_with ( <EOL> source = '<STR_LIT>' , <EOL> destination = '<STR_LIT>' ) <EOL> def test_move_return_true ( self ) : <EOL> self . fs . dev . rpc . file_rename = MagicMock ( return_value = True ) <EOL> initial = '<STR_LIT>' <EOL> final = '<STR_LIT>' <EOL> self . assertTrue ( self . fs . mv ( initial , final ) ) <EOL> self . fs . dev . rpc . file_rename . assert_called_once_with ( <EOL> source = '<STR_LIT>' , <EOL> destination = '<STR_LIT>' ) <EOL> def test_move_return_false ( self ) : <EOL> initial = '<STR_LIT>' <EOL> final = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_rename = MagicMock ( return_value = False ) <EOL> self . assertFalse ( self . fs . mv ( initial , final ) ) <EOL> self . fs . dev . rpc . file_rename . assert_called_once_with ( <EOL> source = '<STR_LIT>' , <EOL> destination = '<STR_LIT>' ) <EOL> def test_tgz_return_true ( self ) : <EOL> src = '<STR_LIT>' <EOL> dst = '<STR_LIT>' <EOL> self . fs . dev . rpc . file_archive = MagicMock ( return_value = True ) <EOL> self . assertTrue ( self . fs . tgz ( src , dst ) ) <EOL> self . fs . dev . rpc . file_archive . assert_called_once_with ( <EOL> source = '<STR_LIT>' , <EOL> destination = '<STR_LIT>' , compress = True ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_tgz_return_error ( self , mock_execute ) : <EOL> mock_execute . side_effect = self . _mock_manager <EOL> src = '<STR_LIT>' <EOL> dst = '<STR_LIT>' <EOL> self . assertTrue ( '<STR_LIT>' in self . fs . tgz ( src , dst ) ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_rmdir ( self , mock_StartShell ) : <EOL> path = '<STR_LIT>' <EOL> print ( self . fs . rmdir ( path ) ) <EOL> calls = [ <EOL> call ( ) . __enter__ ( ) , <EOL> call ( ) . __enter__ ( ) . run ( '<STR_LIT>' ) , <EOL> call ( ) . __exit__ ( None , None , None ) ] <EOL> mock_StartShell . assert_has_calls ( calls ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_mkdir ( self , mock_StartShell ) : <EOL> path = '<STR_LIT>' <EOL> print ( self . fs . mkdir ( path ) ) <EOL> calls = [ <EOL> call ( ) . __enter__ ( ) , <EOL> call ( ) . __enter__ ( ) . run ( '<STR_LIT>' ) , <EOL> call ( ) . __exit__ ( None , None , None ) ] <EOL> mock_StartShell . assert_has_calls ( calls ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_symlink ( self , mock_StartShell ) : <EOL> src = '<STR_LIT>' <EOL> dst = '<STR_LIT>' <EOL> print ( self . fs . symlink ( src , dst ) ) <EOL> calls = [ <EOL> call ( ) . __enter__ ( ) , <EOL> call ( ) . __enter__ ( ) . run ( '<STR_LIT>' ) , <EOL> call ( ) . __exit__ ( None , None , None ) ] <EOL> mock_StartShell . assert_has_calls ( calls ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_storage_usage ( self , mock_execute ) : <EOL> mock_execute . side_effect = self . _mock_manager <EOL> self . assertEqual ( self . fs . storage_usage ( ) , <EOL> { '<STR_LIT>' : <EOL> { '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:/>' , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_storage_cleanup ( self , mock_execute ) : <EOL> mock_execute . side_effect = self . _mock_manager <EOL> self . assertEqual ( self . fs . storage_cleanup ( ) , <EOL> { '<STR_LIT>' : <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:size>' : <NUM_LIT:11> } } ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_storage_cleanup_check ( self , mock_execute ) : <EOL> mock_execute . side_effect = self . _mock_manager <EOL> self . assertEqual ( self . fs . storage_cleanup_check ( ) , <EOL> { '<STR_LIT>' : <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:size>' : <NUM_LIT:11> } } ) <EOL> def _read_file ( self , fname ) : <EOL> from ncclient . xml_ import NCElement <EOL> fpath = os . path . join ( os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' , fname ) <EOL> foo = open ( fpath ) . read ( ) <EOL> if ( fname == '<STR_LIT>' or <EOL> fname == '<STR_LIT>' or <EOL> fname == '<STR_LIT>' ) : <EOL> rpc_reply = NCElement ( foo , self . dev . _conn . _device_handler <EOL> . transform_reply ( ) ) <EOL> elif ( fname == '<STR_LIT>' or <EOL> fname == '<STR_LIT>' ) : <EOL> rpc_reply = NCElement ( foo , self . dev . _conn . _device_handler <EOL> . transform_reply ( ) ) . _NCElement__doc <EOL> else : <EOL> rpc_reply = NCElement ( foo , self . dev . _conn . _device_handler <EOL> . transform_reply ( ) ) . _NCElement__doc [ <NUM_LIT:0> ] <EOL> return rpc_reply <EOL> def _mock_manager ( self , * args , ** kwargs ) : <EOL> if kwargs : <EOL> if '<STR_LIT:path>' in kwargs : <EOL> if kwargs [ '<STR_LIT:path>' ] == '<STR_LIT>' : <EOL> return self . _read_file ( '<STR_LIT>' ) <EOL> elif kwargs [ '<STR_LIT:path>' ] == '<STR_LIT>' : <EOL> return self . _read_file ( '<STR_LIT>' ) <EOL> elif kwargs [ '<STR_LIT:path>' ] == '<STR_LIT>' : <EOL> return self . _read_file ( '<STR_LIT>' ) <EOL> elif kwargs [ '<STR_LIT:path>' ] == '<STR_LIT>' : <EOL> return self . _read_file ( '<STR_LIT>' ) <EOL> if '<STR_LIT:filename>' in kwargs : <EOL> if kwargs [ '<STR_LIT:filename>' ] == '<STR_LIT>' : <EOL> return self . _read_file ( '<STR_LIT>' ) <EOL> device_params = kwargs [ '<STR_LIT>' ] <EOL> device_handler = make_device_handler ( device_params ) <EOL> session = SSHSession ( device_handler ) <EOL> return Manager ( session , device_handler ) <EOL> elif args : <EOL> if args [ <NUM_LIT:0> ] . tag == '<STR_LIT>' : <EOL> if args [ <NUM_LIT:0> ] . text == '<STR_LIT>' : <EOL> return self . _read_file ( '<STR_LIT>' ) <EOL> elif args [ <NUM_LIT:0> ] . tag == '<STR_LIT>' : <EOL> return self . _read_file ( '<STR_LIT>' ) <EOL> elif args [ <NUM_LIT:0> ] . tag == '<STR_LIT>' : <EOL> return self . _read_file ( '<STR_LIT>' ) <EOL> elif args [ <NUM_LIT:0> ] . tag == '<STR_LIT>' : <EOL> return self . _read_file ( '<STR_LIT>' ) </s>
