<s> from unittest . mock import Mock , MagicMock <EOL> from app . master . atom import Atom , AtomState <EOL> from app . master . build import Build <EOL> from app . master . build_request import BuildRequest <EOL> from app . master . slave import DeadSlaveError , SlaveMarkedForShutdownError , Slave <EOL> from app . master . subjob import Subjob <EOL> from app . util . secret import Secret <EOL> from app . util . session_id import SessionId <EOL> from test . framework . base_unit_test_case import BaseUnitTestCase <EOL> class TestSlave ( BaseUnitTestCase ) : <EOL> _FAKE_SLAVE_URL = '<STR_LIT>' <EOL> _FAKE_NUM_EXECUTORS = <NUM_LIT:10> <EOL> def setUp ( self ) : <EOL> super ( ) . setUp ( ) <EOL> self . mock_network = self . patch ( '<STR_LIT>' ) . return_value <EOL> def test_disconnect_command_is_sent_during_teardown_when_slave_is_still_connected ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> slave . current_build_id = <NUM_LIT:3> <EOL> slave . _is_alive = True <EOL> slave . teardown ( ) <EOL> expected_teardown_url = '<STR_LIT>' <EOL> self . mock_network . post . assert_called_once_with ( expected_teardown_url ) <EOL> def test_disconnect_command_is_not_sent_during_teardown_when_slave_has_disconnected ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> slave . current_build_id = <NUM_LIT:3> <EOL> slave . _is_alive = False <EOL> slave . teardown ( ) <EOL> self . assertEqual ( self . mock_network . post . call_count , <NUM_LIT:0> , <EOL> '<STR_LIT>' ) <EOL> def test_git_project_params_are_modified_for_slave ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> slave . _network . post_with_digest = Mock ( ) <EOL> build_request = BuildRequest ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> } ) <EOL> mock_git = Mock ( slave_param_overrides = Mock ( return_value = { <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) ) <EOL> mock_build = MagicMock ( spec = Build , build_request = build_request , <EOL> build_id = Mock ( return_value = <NUM_LIT> ) , project_type = mock_git ) <EOL> slave . setup ( mock_build , executor_start_index = <NUM_LIT> ) <EOL> slave . _network . post_with_digest . assert_called_with ( <EOL> '<STR_LIT>' . format ( self . _FAKE_SLAVE_URL ) , <EOL> { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> } , <EOL> Secret . get ( ) <EOL> ) <EOL> def test_build_id_is_set_on_master_before_telling_slave_to_setup ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> mock_build = Mock ( ) <EOL> def assert_slave_build_id_is_already_set ( * args , ** kwargs ) : <EOL> self . assertEqual ( slave . current_build_id , mock_build . build_id ( ) , <EOL> '<STR_LIT>' ) <EOL> slave . _network . post_with_digest = Mock ( side_effect = assert_slave_build_id_is_already_set ) <EOL> slave . setup ( mock_build , executor_start_index = <NUM_LIT:0> ) <EOL> self . assertEqual ( slave . _network . post_with_digest . call_count , <NUM_LIT:1> , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def test_is_alive_returns_cached_value_if_use_cache_is_true ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> slave . _is_alive = False <EOL> is_slave_alive = slave . is_alive ( use_cached = True ) <EOL> self . assertFalse ( is_slave_alive ) <EOL> self . assertFalse ( self . mock_network . get . called ) <EOL> def test_is_alive_returns_false_if_response_not_ok ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> response_mock = self . mock_network . get . return_value <EOL> response_mock . ok = False <EOL> is_slave_alive = slave . is_alive ( use_cached = False ) <EOL> self . assertFalse ( is_slave_alive ) <EOL> self . assertFalse ( response_mock . json . called ) <EOL> def test_is_alive_returns_false_if_response_is_ok_but_is_alive_is_false ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> response_mock = self . mock_network . get . return_value <EOL> response_mock . ok = True <EOL> response_mock . json . return_value = { '<STR_LIT>' : { '<STR_LIT>' : False } } <EOL> is_slave_alive = slave . is_alive ( use_cached = False ) <EOL> self . assertFalse ( is_slave_alive ) <EOL> def test_is_alive_returns_true_if_response_is_ok_and_is_alive_is_true ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> response_mock = self . mock_network . get . return_value <EOL> response_mock . ok = True <EOL> response_mock . json . return_value = { '<STR_LIT>' : { '<STR_LIT>' : True } } <EOL> is_slave_alive = slave . is_alive ( use_cached = False ) <EOL> self . assertTrue ( is_slave_alive ) <EOL> def test_is_alive_makes_correct_network_call_to_slave ( self ) : <EOL> slave = self . _create_slave ( <EOL> slave_url = '<STR_LIT>' , <EOL> slave_session_id = '<STR_LIT>' ) <EOL> slave . is_alive ( use_cached = False ) <EOL> self . mock_network . get . assert_called_once_with ( <EOL> '<STR_LIT>' , <EOL> headers = { SessionId . EXPECTED_SESSION_HEADER_KEY : '<STR_LIT>' } ) <EOL> def test_mark_as_idle_raises_when_executors_are_in_use ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> slave . _num_executors_in_use . increment ( ) <EOL> self . assertRaises ( Exception , slave . mark_as_idle ) <EOL> def test_mark_as_idle_raises_when_slave_is_in_shutdown_mode ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> slave . _is_in_shutdown_mode = True <EOL> slave . kill = Mock ( ) <EOL> self . assertRaises ( SlaveMarkedForShutdownError , slave . mark_as_idle ) <EOL> slave . kill . assert_called_once_with ( ) <EOL> def test_start_subjob_raises_if_slave_is_dead ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> slave . _is_alive = False <EOL> self . assertRaises ( DeadSlaveError , slave . start_subjob , Mock ( ) ) <EOL> def test_start_subjob_raises_if_slave_is_shutdown ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> slave . _is_in_shutdown_mode = True <EOL> self . assertRaises ( SlaveMarkedForShutdownError , slave . start_subjob , Mock ( ) ) <EOL> def test_set_shutdown_mode_should_set_is_shutdown_and_not_kill_slave_if_slave_has_a_build ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> slave . current_build_id = <NUM_LIT:1> <EOL> slave . kill = Mock ( ) <EOL> slave . set_shutdown_mode ( ) <EOL> self . assertTrue ( slave . _is_in_shutdown_mode ) <EOL> assert not slave . kill . called <EOL> def test_set_shutdown_mode_should_kill_slave_if_slave_has_no_build ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> slave . kill = Mock ( ) <EOL> slave . set_shutdown_mode ( ) <EOL> slave . kill . assert_called_once_with ( ) <EOL> def test_kill_should_post_to_slave_api ( self ) : <EOL> slave = self . _create_slave ( ) <EOL> slave . _network . post_with_digest = Mock ( ) <EOL> slave . kill ( ) <EOL> assert slave . _network . post_with_digest . called <EOL> def _create_slave ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs . setdefault ( '<STR_LIT>' , self . _FAKE_SLAVE_URL ) <EOL> kwargs . setdefault ( '<STR_LIT>' , self . _FAKE_NUM_EXECUTORS ) <EOL> return Slave ( ** kwargs ) </s>
