<s> __author__ = "<STR_LIT>" <EOL> __credits__ = "<STR_LIT>" <EOL> import unittest <EOL> from nose . plugins . attrib import attr <EOL> from mock import patch , MagicMock <EOL> from lxml import etree <EOL> import os <EOL> from jnpr . junos import Device <EOL> from jnpr . junos . facts . chassis import facts_chassis as chassis <EOL> from jnpr . junos . exception import ConnectNotMasterError <EOL> from ncclient . manager import Manager , make_device_handler <EOL> from ncclient . transport import SSHSession <EOL> @ attr ( '<STR_LIT>' ) <EOL> class TestChassis ( 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 . facts = { } <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_2RE_true ( self , mock_execute ) : <EOL> mock_execute . side_effect = self . _mock_manager <EOL> chassis ( self . dev , self . facts ) <EOL> self . assertTrue ( self . facts [ '<STR_LIT>' ] ) <EOL> def test_chassis_exception_ConnectNotMasterError ( self ) : <EOL> xmldata = etree . XML ( '<STR_LIT>' ) <EOL> self . dev . rpc . get_chassis_inventory = MagicMock ( side_effect = xmldata ) <EOL> self . assertRaises ( ConnectNotMasterError , chassis , self . dev , self . facts ) <EOL> def test_chassis_exception_RuntimeError ( self ) : <EOL> xmldata = etree . XML ( '<STR_LIT>' ) <EOL> self . dev . rpc . get_chassis_inventory = MagicMock ( side_effect = xmldata ) <EOL> chassis ( self . dev , self . facts ) <EOL> self . assertFalse ( self . facts [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( self . facts [ '<STR_LIT>' ] , self . facts [ '<STR_LIT>' ] ) <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> rpc_reply = NCElement ( foo , <EOL> 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> 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> if args : <EOL> return self . _read_file ( '<STR_LIT>' + args [ <NUM_LIT:0> ] . tag + '<STR_LIT>' ) </s>
