<s> import cPickle <EOL> import json <EOL> import logging <EOL> from mock import * <EOL> from rackclient import exceptions <EOL> from rackclient . tests import utils <EOL> from rackclient . lib import initializing <EOL> LOG = logging . getLogger ( __name__ ) <EOL> LOG . setLevel ( logging . WARN ) <EOL> class InitializingTest ( utils . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( InitializingTest , self ) . setUp ( ) <EOL> p = patch ( "<STR_LIT>" ) <EOL> self . addCleanup ( p . stop ) <EOL> mock_request = p . start ( ) <EOL> mock_resp = Mock ( ) <EOL> mock_resp . text = json . dumps ( dict ( meta = dict ( <EOL> proxy_ip = "<STR_LIT>" , gid = "<STR_LIT>" , pid = "<STR_LIT>" , ppid = "<STR_LIT>" ) ) ) <EOL> mock_request . return_value = mock_resp <EOL> def test_get_rack_context ( self ) : <EOL> p = patch ( "<STR_LIT>" ) <EOL> self . addCleanup ( p . stop ) <EOL> mock_client = p . start ( ) <EOL> mock_client = mock_client . return_value <EOL> def proxy_info ( args ) : <EOL> info = type ( '<STR_LIT>' , ( object , ) , { } ) <EOL> info . ipc_endpoint = None <EOL> info . fs_endpoint = None <EOL> info . shm_endpoint = None <EOL> return info <EOL> mock_client . proxy = Mock ( ) <EOL> mock_client_processes = Mock ( ) <EOL> mock_client . proxy . get . side_effect = proxy_info <EOL> p2 = patch ( "<STR_LIT>" ) <EOL> self . addCleanup ( p2 . stop ) <EOL> mock_messaging = p2 . start ( ) <EOL> mock_messaging = mock_messaging . return_value <EOL> mock_messaging . receive_msg . return_value = dict ( pid = "<STR_LIT>" ) <EOL> actual_context = initializing . get_rack_context ( ) <EOL> expect_context = type ( '<STR_LIT>' , ( object , ) , dict ( <EOL> proxy_ip = "<STR_LIT>" , <EOL> gid = "<STR_LIT>" , pid = "<STR_LIT>" , <EOL> ppid = "<STR_LIT>" , <EOL> ipc_endpoint = None , <EOL> fs_endpoint = None , <EOL> shm_endpoint = None , <EOL> client = mock_client ) ) <EOL> self . assertEquals ( expect_context . pid , actual_context . pid ) <EOL> self . assertEquals ( expect_context . ppid , actual_context . ppid ) <EOL> self . assertEquals ( expect_context . proxy_ip , actual_context . proxy_ip ) <EOL> self . assertEquals ( expect_context . ipc_endpoint , actual_context . ipc_endpoint ) <EOL> self . assertEquals ( expect_context . fs_endpoint , actual_context . fs_endpoint ) <EOL> self . assertEquals ( expect_context . shm_endpoint , actual_context . shm_endpoint ) <EOL> def test_get_rack_cotext_ProcessInitError_due_to_proxy ( self ) : <EOL> self . p = patch ( "<STR_LIT>" ) <EOL> self . addCleanup ( self . p . stop ) <EOL> mock_client = self . p . start ( ) <EOL> mock_client = mock_client . return_value <EOL> mock_client . proxy = Mock ( ) <EOL> mock_client_processes = Mock ( ) <EOL> mock_client . proxy . get . side_effect = Exception ( ) <EOL> self . assertRaises ( Exception , initializing . get_rack_context ) <EOL> def test_get_rack_cotext_ProcessInitError_doe_to_processes ( self ) : <EOL> self . p = patch ( "<STR_LIT>" ) <EOL> self . addCleanup ( self . p . stop ) <EOL> mock_client = self . p . start ( ) <EOL> mock_client = mock_client . return_value <EOL> mock_client . proxy = Mock ( ) <EOL> mock_client_processes = Mock ( ) <EOL> mock_client . processes . get . side_effect = exceptions . NotFound ( "<STR_LIT>" ) <EOL> self . assertRaises ( Exception , initializing . get_rack_context ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_messaging_receive_msg ( self , mock_receive ) : <EOL> self . mock_connection = Mock ( ) <EOL> self . mock_channel = Mock ( ) <EOL> self . patch_pika_blocking = patch ( '<STR_LIT>' , autospec = True ) <EOL> self . addCleanup ( self . patch_pika_blocking . stop ) <EOL> self . mock_pika_blocking = self . patch_pika_blocking . start ( ) <EOL> self . mock_pika_blocking . return_value = self . mock_connection <EOL> self . mock_connection . channel . return_value = self . mock_channel <EOL> context = type ( '<STR_LIT>' , ( object , ) , dict ( <EOL> proxy_ip = "<STR_LIT>" , <EOL> gid = "<STR_LIT>" , pid = "<STR_LIT>" , <EOL> ppid = "<STR_LIT>" , <EOL> ipc_endpoint = None , <EOL> fs_endpoint = None , <EOL> shm_endpoint = None ) ) <EOL> timeout_limit = <NUM_LIT> <EOL> msg = initializing . _Messaging ( context ) <EOL> message = msg . receive_msg ( timeout_limit = timeout_limit ) <EOL> self . mock_connection . add_timeout . assert_called_with ( deadline = int ( timeout_limit ) , <EOL> callback_method = mock_receive ( ) . time_out ) <EOL> self . mock_channel . basic_consume . assert_called_with ( mock_receive ( ) . get_msg , <EOL> queue = "<STR_LIT>" , <EOL> no_ack = False ) <EOL> self . mock_channel . start_consuming . assert_called_with ( ) <EOL> self . assertEqual ( message , mock_receive ( ) . message ) <EOL> def test_messaging_send_msg ( self ) : <EOL> self . mock_connection = Mock ( ) <EOL> self . mock_channel = Mock ( ) <EOL> self . patch_pika_blocking = patch ( '<STR_LIT>' , autospec = True ) <EOL> self . addCleanup ( self . patch_pika_blocking . stop ) <EOL> self . mock_pika_blocking = self . patch_pika_blocking . start ( ) <EOL> self . mock_pika_blocking . return_value = self . mock_connection <EOL> self . mock_connection . channel . return_value = self . mock_channel <EOL> context = type ( '<STR_LIT>' , ( object , ) , dict ( <EOL> proxy_ip = "<STR_LIT>" , <EOL> gid = "<STR_LIT>" , pid = "<STR_LIT>" , <EOL> ppid = "<STR_LIT>" , <EOL> ipc_endpoint = None , <EOL> fs_endpoint = None , <EOL> shm_endpoint = None ) ) <EOL> send_msg = '<STR_LIT>' <EOL> target = '<STR_LIT>' <EOL> msg = initializing . _Messaging ( context ) <EOL> msg . send_msg ( target ) <EOL> routing_key = context . gid + '<STR_LIT:.>' + target <EOL> send_dict = { '<STR_LIT>' : context . pid } <EOL> send_msg = cPickle . dumps ( send_dict ) <EOL> self . mock_channel . basic_publish . assert_called_with ( exchange = context . gid , <EOL> routing_key = routing_key , <EOL> body = send_msg ) <EOL> def test_receive_get_msg ( self ) : <EOL> self . mock_connection = Mock ( ) <EOL> self . mock_channel = Mock ( ) <EOL> self . patch_pika_blocking = patch ( '<STR_LIT>' , autospec = True ) <EOL> self . addCleanup ( self . patch_pika_blocking . stop ) <EOL> self . mock_pika_blocking = self . patch_pika_blocking . start ( ) <EOL> self . mock_pika_blocking . return_value = self . mock_connection <EOL> self . mock_connection . channel . return_value = self . mock_channel <EOL> ch = Mock ( ) <EOL> method = Mock ( ) <EOL> properties = Mock ( ) <EOL> receive_msg = '<STR_LIT>' <EOL> body = cPickle . dumps ( receive_msg ) <EOL> ch_object = { '<STR_LIT>' : '<STR_LIT>' } <EOL> method . configure_mock ( ** ch_object ) <EOL> context = type ( '<STR_LIT>' , ( object , ) , dict ( <EOL> proxy_ip = "<STR_LIT>" , <EOL> gid = "<STR_LIT>" , pid = "<STR_LIT>" , <EOL> ppid = "<STR_LIT>" , <EOL> ipc_endpoint = None , <EOL> fs_endpoint = None , <EOL> shm_endpoint = None ) ) <EOL> msg = initializing . _Messaging ( context ) <EOL> receive = msg . Receive ( ) <EOL> receive . get_msg ( ch , method , properties , body ) <EOL> ch . basic_ack . assert_called_with ( delivery_tag = ch_object [ '<STR_LIT>' ] ) <EOL> ch . stop_consuming . assert_call_with ( ) <EOL> self . assertEqual ( receive . message , receive_msg ) <EOL> def test_receive_timeout ( self ) : <EOL> self . mock_connection = Mock ( ) <EOL> self . mock_channel = Mock ( ) <EOL> self . patch_pika_blocking = patch ( '<STR_LIT>' , autospec = True ) <EOL> self . addCleanup ( self . patch_pika_blocking . stop ) <EOL> self . mock_pika_blocking = self . patch_pika_blocking . start ( ) <EOL> self . mock_pika_blocking . return_value = self . mock_connection <EOL> self . mock_connection . channel . return_value = self . mock_channel <EOL> context = type ( '<STR_LIT>' , ( object , ) , dict ( <EOL> proxy_ip = "<STR_LIT>" , <EOL> gid = "<STR_LIT>" , pid = "<STR_LIT>" , <EOL> ppid = "<STR_LIT>" , <EOL> ipc_endpoint = "<STR_LIT:data>" , <EOL> fs_endpoint = None , <EOL> shm_endpoint = None ) ) <EOL> msg = initializing . _Messaging ( context ) <EOL> receive = msg . Receive ( ) <EOL> receive . channel = self . mock_channel <EOL> receive . time_out ( ) <EOL> self . mock_channel . stop_consuming . assert_called_with ( ) </s>
