<s> from StringIO import StringIO <EOL> from mock import MagicMock <EOL> from mock import Mock <EOL> from mock import patch <EOL> from pytest import mark <EOL> from pytest import raises <EOL> from paasta_tools import utils <EOL> from paasta_tools . cli . cmds import status <EOL> from paasta_tools . cli . cmds . status import missing_deployments_message <EOL> from paasta_tools . cli . cmds . status import paasta_status <EOL> from paasta_tools . cli . cmds . status import report_invalid_whitelist_values <EOL> from paasta_tools . cli . cmds . status import report_status <EOL> from paasta_tools . cli . utils import NoSuchService <EOL> from paasta_tools . cli . utils import PaastaCheckMessages <EOL> from paasta_tools . cli . utils import PaastaColors <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_figure_out_service_name_not_found ( mock_stdout , <EOL> mock_validate_service_name ) : <EOL> mock_validate_service_name . side_effect = NoSuchService ( None ) <EOL> parsed_args = Mock ( ) <EOL> parsed_args . service = '<STR_LIT>' <EOL> expected_output = '<STR_LIT>' % NoSuchService . GUESS_ERROR_MSG <EOL> with raises ( SystemExit ) as sys_exit : <EOL> status . figure_out_service_name ( parsed_args ) <EOL> output = mock_stdout . getvalue ( ) <EOL> assert sys_exit . value . code == <NUM_LIT:1> <EOL> assert output == expected_output <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_status_arg_service_not_found ( mock_stdout , mock_guess_service_name , <EOL> mock_validate_service_name ) : <EOL> mock_guess_service_name . return_value = '<STR_LIT>' <EOL> error = NoSuchService ( '<STR_LIT>' ) <EOL> mock_validate_service_name . side_effect = error <EOL> expected_output = str ( error ) + "<STR_LIT:\n>" <EOL> args = MagicMock ( ) <EOL> args . service = False <EOL> with raises ( SystemExit ) as sys_exit : <EOL> paasta_status ( args ) <EOL> output = mock_stdout . getvalue ( ) <EOL> assert sys_exit . value . code == <NUM_LIT:1> <EOL> assert output == expected_output <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_report_status_for_cluster_displays_deployed_service ( <EOL> mock_stdout , <EOL> mock_report_invalid_whitelist_values , <EOL> mock_execute_paasta_serviceinit_on_remote_master , <EOL> ) : <EOL> service = '<STR_LIT>' <EOL> planned_deployments = [ '<STR_LIT>' ] <EOL> actual_deployments = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> instance_whitelist = [ ] <EOL> fake_status = '<STR_LIT>' <EOL> mock_execute_paasta_serviceinit_on_remote_master . return_value = fake_status <EOL> expected_output = ( <EOL> "<STR_LIT:\n>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( <EOL> PaastaColors . blue ( '<STR_LIT>' ) , <EOL> fake_status , <EOL> ) <EOL> ) <EOL> status . report_status_for_cluster ( <EOL> service = service , <EOL> cluster = '<STR_LIT>' , <EOL> deploy_pipeline = planned_deployments , <EOL> actual_deployments = actual_deployments , <EOL> instance_whitelist = instance_whitelist , <EOL> ) <EOL> output = mock_stdout . getvalue ( ) <EOL> assert expected_output in output <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_report_status_for_cluster_displays_multiple_lines_from_execute_paasta_serviceinit_on_remote_master ( <EOL> mock_stdout , <EOL> mock_report_invalid_whitelist_values , <EOL> mock_execute_paasta_serviceinit_on_remote_master , <EOL> ) : <EOL> service = '<STR_LIT>' <EOL> planned_deployments = [ '<STR_LIT>' ] <EOL> actual_deployments = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> instance_whitelist = [ ] <EOL> fake_status = '<STR_LIT>' <EOL> mock_execute_paasta_serviceinit_on_remote_master . return_value = fake_status <EOL> expected_output = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> status . report_status_for_cluster ( <EOL> service = service , <EOL> cluster = '<STR_LIT>' , <EOL> deploy_pipeline = planned_deployments , <EOL> actual_deployments = actual_deployments , <EOL> instance_whitelist = instance_whitelist , <EOL> ) <EOL> output = mock_stdout . getvalue ( ) <EOL> assert expected_output in output <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_report_status_for_cluster_instance_sorts_in_deploy_order ( <EOL> mock_stdout , <EOL> mock_report_invalid_whitelist_values , <EOL> mock_execute_paasta_serviceinit_on_remote_master , <EOL> ) : <EOL> service = '<STR_LIT>' <EOL> planned_deployments = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> actual_deployments = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> instance_whitelist = [ ] <EOL> fake_status = '<STR_LIT>' <EOL> mock_execute_paasta_serviceinit_on_remote_master . return_value = fake_status <EOL> expected_output = ( <EOL> "<STR_LIT:\n>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( <EOL> PaastaColors . blue ( '<STR_LIT>' ) , <EOL> fake_status , <EOL> PaastaColors . blue ( '<STR_LIT>' ) , <EOL> fake_status , <EOL> ) <EOL> ) <EOL> status . report_status_for_cluster ( <EOL> service = service , <EOL> cluster = '<STR_LIT>' , <EOL> deploy_pipeline = planned_deployments , <EOL> actual_deployments = actual_deployments , <EOL> instance_whitelist = instance_whitelist , <EOL> ) <EOL> output = mock_stdout . getvalue ( ) <EOL> assert expected_output in output <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_print_cluster_status_missing_deploys_in_red ( <EOL> mock_stdout , <EOL> mock_report_invalid_whitelist_values , <EOL> mock_execute_paasta_serviceinit_on_remote_master , <EOL> ) : <EOL> service = '<STR_LIT>' <EOL> planned_deployments = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> actual_deployments = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> instance_whitelist = [ ] <EOL> fake_status = '<STR_LIT>' <EOL> mock_execute_paasta_serviceinit_on_remote_master . return_value = fake_status <EOL> expected_output = ( <EOL> "<STR_LIT:\n>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( <EOL> PaastaColors . blue ( '<STR_LIT>' ) , <EOL> fake_status , <EOL> PaastaColors . red ( '<STR_LIT>' ) , <EOL> ) <EOL> ) <EOL> status . report_status_for_cluster ( <EOL> service = service , <EOL> cluster = '<STR_LIT>' , <EOL> deploy_pipeline = planned_deployments , <EOL> actual_deployments = actual_deployments , <EOL> instance_whitelist = instance_whitelist , <EOL> ) <EOL> output = mock_stdout . getvalue ( ) <EOL> assert expected_output in output <EOL> @ mark . parametrize ( '<STR_LIT>' , [ <NUM_LIT:0> , <NUM_LIT:2> ] ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_print_cluster_status_calls_execute_paasta_serviceinit_on_remote_master ( <EOL> mock_stdout , <EOL> mock_report_invalid_whitelist_values , <EOL> mock_execute_paasta_serviceinit_on_remote_master , <EOL> verbosity_level , <EOL> ) : <EOL> service = '<STR_LIT>' <EOL> planned_deployments = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> actual_deployments = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> instance_whitelist = [ ] <EOL> fake_output = "<STR_LIT>" <EOL> mock_execute_paasta_serviceinit_on_remote_master . return_value = fake_output <EOL> expected_output = "<STR_LIT>" % fake_output <EOL> status . report_status_for_cluster ( <EOL> service = service , <EOL> cluster = '<STR_LIT>' , <EOL> deploy_pipeline = planned_deployments , <EOL> actual_deployments = actual_deployments , <EOL> instance_whitelist = instance_whitelist , <EOL> verbose = verbosity_level , <EOL> ) <EOL> assert mock_execute_paasta_serviceinit_on_remote_master . call_count == <NUM_LIT:1> <EOL> mock_execute_paasta_serviceinit_on_remote_master . assert_any_call ( <EOL> '<STR_LIT:status>' , '<STR_LIT>' , service , '<STR_LIT>' , verbose = verbosity_level ) <EOL> output = mock_stdout . getvalue ( ) <EOL> assert expected_output in output <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_report_status_for_cluster_obeys_instance_whitelist ( <EOL> mock_stdout , <EOL> mock_report_invalid_whitelist_values , <EOL> mock_execute_paasta_serviceinit_on_remote_master , <EOL> ) : <EOL> service = '<STR_LIT>' <EOL> planned_deployments = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> actual_deployments = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> instance_whitelist = [ '<STR_LIT>' ] <EOL> status . report_status_for_cluster ( <EOL> service = service , <EOL> cluster = '<STR_LIT>' , <EOL> deploy_pipeline = planned_deployments , <EOL> actual_deployments = actual_deployments , <EOL> instance_whitelist = instance_whitelist , <EOL> ) <EOL> output = mock_stdout . getvalue ( ) <EOL> assert '<STR_LIT>' in output <EOL> assert '<STR_LIT>' not in output <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_report_status_calls_report_invalid_whitelist_values ( <EOL> mock_stdout , <EOL> mock_report_invalid_whitelist_values , <EOL> mock_execute_paasta_serviceinit_on_remote_master , <EOL> ) : <EOL> service = '<STR_LIT>' <EOL> planned_deployments = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> actual_deployments = { } <EOL> instance_whitelist = [ ] <EOL> status . report_status_for_cluster ( <EOL> service = service , <EOL> cluster = '<STR_LIT>' , <EOL> deploy_pipeline = planned_deployments , <EOL> actual_deployments = actual_deployments , <EOL> instance_whitelist = instance_whitelist , <EOL> ) <EOL> mock_report_invalid_whitelist_values . assert_called_once_with ( <EOL> instance_whitelist , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' , <EOL> ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_status_pending_pipeline_build_message ( <EOL> mock_stdout , mock_get_actual_deployments , mock_get_deploy_info , <EOL> mock_figure_out_service_name ) : <EOL> service = '<STR_LIT>' <EOL> mock_figure_out_service_name . return_value = service <EOL> pipeline = [ { '<STR_LIT>' : '<STR_LIT>' } ] <EOL> mock_get_deploy_info . return_value = { '<STR_LIT>' : pipeline } <EOL> actual_deployments = { } <EOL> mock_get_actual_deployments . return_value = actual_deployments <EOL> expected_output = missing_deployments_message ( service ) <EOL> args = MagicMock ( ) <EOL> args . service = service <EOL> paasta_status ( args ) <EOL> output = mock_stdout . getvalue ( ) <EOL> assert expected_output in output <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> def test_get_actual_deployments ( mock_get_deployments , ) : <EOL> mock_get_deployments . return_value = utils . DeploymentsJson ( { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> } ) <EOL> expected = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> actual = status . get_actual_deployments ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert expected == actual <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> def test_get_deploy_info_exists ( mock_read_deploy ) : <EOL> expected = '<STR_LIT>' <EOL> mock_read_deploy . return_value = expected <EOL> actual = status . get_deploy_info ( '<STR_LIT>' ) <EOL> assert expected == actual <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_get_deploy_info_does_not_exist ( mock_stdout , mock_read_deploy ) : <EOL> mock_read_deploy . return_value = False <EOL> expected_output = '<STR_LIT>' % PaastaCheckMessages . DEPLOY_YAML_MISSING <EOL> with raises ( SystemExit ) as sys_exit : <EOL> status . get_deploy_info ( '<STR_LIT>' ) <EOL> output = mock_stdout . getvalue ( ) <EOL> assert sys_exit . value . code == <NUM_LIT:1> <EOL> assert output == expected_output <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_status_calls_sergeants ( <EOL> mock_stdout , <EOL> mock_report_status , <EOL> mock_get_planned_deployments , <EOL> mock_get_actual_deployments , <EOL> mock_figure_out_service_name , <EOL> ) : <EOL> service = '<STR_LIT>' <EOL> mock_figure_out_service_name . return_value = service <EOL> planned_deployments = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> mock_get_planned_deployments . return_value = planned_deployments <EOL> actual_deployments = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> mock_get_actual_deployments . return_value = actual_deployments <EOL> args = MagicMock ( ) <EOL> args . service = service <EOL> args . clusters = None <EOL> args . instances = None <EOL> args . verbose = False <EOL> args . soa_dir = '<STR_LIT>' <EOL> paasta_status ( args ) <EOL> mock_figure_out_service_name . assert_called_once_with ( args , '<STR_LIT>' ) <EOL> mock_get_actual_deployments . assert_called_once_with ( service , '<STR_LIT>' ) <EOL> mock_report_status . assert_called_once_with ( <EOL> service = service , <EOL> deploy_pipeline = planned_deployments , <EOL> actual_deployments = actual_deployments , <EOL> cluster_whitelist = [ ] , <EOL> instance_whitelist = [ ] , <EOL> verbose = <NUM_LIT:0> , <EOL> ) <EOL> def test_report_invalid_whitelist_values_no_whitelists ( ) : <EOL> whitelist = [ ] <EOL> items = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> item_type = '<STR_LIT>' <EOL> actual = report_invalid_whitelist_values ( whitelist , items , item_type ) <EOL> assert actual == '<STR_LIT>' <EOL> def test_report_invalid_whitelist_values_with_whitelists ( ) : <EOL> whitelist = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> items = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> item_type = '<STR_LIT>' <EOL> actual = report_invalid_whitelist_values ( whitelist , items , item_type ) <EOL> assert '<STR_LIT>' in actual <EOL> assert item_type in actual <EOL> assert '<STR_LIT>' in actual <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_report_status_obeys_cluster_whitelist ( <EOL> mock_stdout , <EOL> mock_report_invalid_whitelist_values , <EOL> mock_report_status_for_cluster , <EOL> ) : <EOL> service = '<STR_LIT>' <EOL> cluster_whitelist = [ '<STR_LIT>' ] <EOL> instance_whitelist = [ ] <EOL> deploy_pipeline = actual_deployments = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> report_status ( <EOL> service = service , <EOL> deploy_pipeline = deploy_pipeline , <EOL> actual_deployments = actual_deployments , <EOL> cluster_whitelist = cluster_whitelist , <EOL> instance_whitelist = instance_whitelist , <EOL> ) <EOL> mock_report_invalid_whitelist_values . assert_called_once_with ( <EOL> cluster_whitelist , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> mock_report_status_for_cluster . assert_called_once_with ( <EOL> service = service , <EOL> cluster = '<STR_LIT>' , <EOL> deploy_pipeline = deploy_pipeline , <EOL> actual_deployments = actual_deployments , <EOL> instance_whitelist = instance_whitelist , <EOL> verbose = <NUM_LIT:0> <EOL> ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , new_callable = StringIO ) <EOL> def test_report_status_handle_none_whitelist ( <EOL> mock_stdout , <EOL> mock_report_invalid_whitelist_values , <EOL> mock_report_status_for_cluster , <EOL> ) : <EOL> service = '<STR_LIT>' <EOL> cluster_whitelist = [ ] <EOL> instance_whitelist = [ ] <EOL> deploy_pipeline = actual_deployments = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> report_status ( <EOL> service = service , <EOL> deploy_pipeline = deploy_pipeline , <EOL> actual_deployments = actual_deployments , <EOL> cluster_whitelist = cluster_whitelist , <EOL> instance_whitelist = instance_whitelist , <EOL> ) <EOL> mock_report_status_for_cluster . assert_any_call ( <EOL> service = service , <EOL> cluster = '<STR_LIT>' , <EOL> deploy_pipeline = deploy_pipeline , <EOL> actual_deployments = actual_deployments , <EOL> instance_whitelist = instance_whitelist , <EOL> verbose = <NUM_LIT:0> <EOL> ) <EOL> mock_report_status_for_cluster . assert_any_call ( <EOL> service = service , <EOL> cluster = '<STR_LIT>' , <EOL> deploy_pipeline = deploy_pipeline , <EOL> actual_deployments = actual_deployments , <EOL> instance_whitelist = instance_whitelist , <EOL> verbose = <NUM_LIT:0> <EOL> ) <EOL> mock_report_status_for_cluster . assert_any_call ( <EOL> service = service , <EOL> cluster = '<STR_LIT>' , <EOL> deploy_pipeline = deploy_pipeline , <EOL> actual_deployments = actual_deployments , <EOL> instance_whitelist = instance_whitelist , <EOL> verbose = <NUM_LIT:0> <EOL> ) </s>
