def test_get_service_instances_that_need_bouncing_at_risk(): with mock.patch( "paasta_tools.list_marathon_service_instances.get_desired_marathon_configs", autospec=True, ) as mock_get_desired_marathon_configs, mock.patch( "paasta_tools.list_marathon_service_instances.get_num_at_risk_tasks", autospec=True, ) as mock_get_num_at_risk_tasks, mock.patch( "paasta_tools.list_marathon_service_instances.get_draining_hosts", autospec=True): mock_get_desired_marathon_configs.return_value = ( { "fake--service.fake--instance.sha.config": { "instances": 5 } }, { "fake--service.fake--instance.sha.config": mock.Mock(get_marathon_shard=mock.Mock(return_value=None)) }, ) fake_apps = [ mock.MagicMock(instances=5, id="/fake--service.fake--instance.sha.config") ] mock_client = mock.MagicMock(list_apps=mock.MagicMock( return_value=fake_apps)) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) mock_get_num_at_risk_tasks.return_value = 1 assert set( list_marathon_service_instances. get_service_instances_that_need_bouncing( marathon_clients=fake_clients, soa_dir="/fake/soa/dir")) == {"fake_service.fake_instance"}
def test_create_marathon_dashboard( mock_get_services_for_cluster, mock_pscl, mock_load_system_paasta_config ): soa_dir = "/fake/soa/dir" cluster = "fake_cluster" mock_load_system_paasta_config.return_value = SystemPaastaConfig( {"dashboard_links": {}}, "fake_directory" ) mock_get_services_for_cluster.return_value = [ ("fake_service", "foo"), ("fake_service", "bar"), ] mock_pscl.return_value.instance_configs.return_value = [ MarathonServiceConfig("fake_service", "fake_cluster", "foo", {}, {}, soa_dir), MarathonServiceConfig("fake_service", "fake_cluster", "bar", {}, {}, soa_dir), ] mock_client = mock.Mock(servers=["hi"]) mock_clients = MarathonClients(current=[mock_client], previous=[mock_client]) expected_output = { "fake_cluster": [ {"service": "fake_service", "instance": "foo", "shard_url": "hi"}, {"service": "fake_service", "instance": "bar", "shard_url": "hi"}, ] } assert ( marathon_dashboard.create_marathon_dashboard( cluster=cluster, soa_dir=soa_dir, marathon_clients=mock_clients ) == expected_output )
def test_get_service_instances_that_need_bouncing(): with mock.patch( 'paasta_tools.list_marathon_service_instances.get_desired_marathon_configs', autospec=True, ) as mock_get_desired_marathon_configs, mock.patch( 'paasta_tools.list_marathon_service_instances.get_num_at_risk_tasks', autospec=True, ) as mock_get_num_at_risk_tasks, mock.patch( 'paasta_tools.list_marathon_service_instances.get_draining_hosts', autospec=True, ): mock_get_desired_marathon_configs.return_value = ( { 'fake--service.fake--instance.sha.config': { 'instances': 5 }, 'fake--service2.fake--instance.sha.config': { 'instances': 5 }, }, { 'fake--service.fake--instance.sha.config': mock.Mock(get_marathon_shard=mock.Mock(return_value=None)), 'fake--service2.fake--instance.sha.config': mock.Mock(get_marathon_shard=mock.Mock(return_value=None)), }, ) fake_apps = [ mock.MagicMock(instances=5, id='/fake--service.fake--instance.sha.config2'), mock.MagicMock(instances=5, id='/fake--service2.fake--instance.sha.config'), ] mock_client = mock.MagicMock(list_apps=mock.MagicMock( return_value=fake_apps)) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) mock_get_num_at_risk_tasks.return_value = 0 assert set( list_marathon_service_instances. get_service_instances_that_need_bouncing( marathon_clients=fake_clients, soa_dir='/fake/soa/dir', )) == {'fake_service.fake_instance'}
def test_create_marathon_dashboard(mock_get_services_for_cluster, mock_pscl, mock_load_system_paasta_config): soa_dir = '/fake/soa/dir' cluster = 'fake_cluster' mock_load_system_paasta_config.return_value = SystemPaastaConfig( { 'dashboard_links': {}, }, 'fake_directory', ) mock_get_services_for_cluster.return_value = [ ('fake_service', 'foo'), ('fake_service', 'bar'), ] mock_pscl.return_value.instance_configs.return_value = [ MarathonServiceConfig('fake_service', 'fake_cluster', 'foo', {}, {}, soa_dir), MarathonServiceConfig('fake_service', 'fake_cluster', 'bar', {}, {}, soa_dir), ] mock_client = mock.Mock(servers=['hi']) mock_clients = MarathonClients(current=[mock_client], previous=[mock_client]) expected_output = { 'fake_cluster': [ { 'service': 'fake_service', 'instance': 'foo', 'shard_url': 'hi', }, { 'service': 'fake_service', 'instance': 'bar', 'shard_url': 'hi', }, ], } assert marathon_dashboard.create_marathon_dashboard( cluster=cluster, soa_dir=soa_dir, marathon_clients=mock_clients, ) == expected_output
def test_get_service_instances_needing_update(): with mock.patch( "paasta_tools.deployd.common.get_all_marathon_apps", autospec=True ) as mock_get_marathon_apps, mock.patch( "paasta_tools.deployd.common.load_marathon_service_config_no_cache", autospec=True, ) as mock_load_marathon_service_config: mock_marathon_apps = [ mock.Mock(id="/universe.c137.c1.g1", instances=2), mock.Mock(id="/universe.c138.c1.g1", instances=2), ] mock_get_marathon_apps.return_value = mock_marathon_apps mock_service_instances = [("universe", "c137"), ("universe", "c138")] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ "id": "universe.c137.c1.g1", "instances": 2 })), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ "id": "universe.c138.c2.g2", "instances": 2 })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, "westeros-prod") assert mock_get_marathon_apps.called calls = [ mock.call( service="universe", instance="c137", cluster="westeros-prod", soa_dir=DEFAULT_SOA_DIR, ), mock.call( service="universe", instance="c138", cluster="westeros-prod", soa_dir=DEFAULT_SOA_DIR, ), ] mock_load_marathon_service_config.assert_has_calls(calls) assert ret == [("universe", "c138", mock.ANY)] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ "id": "universe.c137.c1.g1", "instances": 3 })), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ "id": "universe.c138.c2.g2", "instances": 2 })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, "westeros-prod") assert ret == [("universe", "c137", mock.ANY), ("universe", "c138", mock.ANY)] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock( side_effect=NoDockerImageError)), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ "id": "universe.c138.c2.g2", "instances": 2 })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, "westeros-prod") assert ret == [("universe", "c138", mock.ANY)] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock( side_effect=NoSlavesAvailableError)), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ "id": "universe.c138.c2.g2", "instances": 2 })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, "westeros-prod") assert ret == [("universe", "c138", mock.ANY)] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock( side_effect=InvalidJobNameError)), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ "id": "universe.c138.c2.g2", "instances": 2 })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, "westeros-prod") assert ret == [("universe", "c138", mock.ANY)] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock( side_effect=NoDeploymentsAvailable)), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ "id": "universe.c138.c2.g2", "instances": 2 })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, "westeros-prod") assert ret == [("universe", "c138", mock.ANY)] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock( side_effect=Exception)), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ "id": "universe.c138.c2.g2", "instances": 2 })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, "westeros-prod") assert ret == [("universe", "c138", mock.ANY)]
def test_get_service_instances_needing_update(): with mock.patch( 'paasta_tools.deployd.common.get_all_marathon_apps', autospec=True, ) as mock_get_marathon_apps, mock.patch( 'paasta_tools.deployd.common.load_marathon_service_config_no_cache', autospec=True, ) as mock_load_marathon_service_config: mock_marathon_apps = [ mock.Mock(id='/universe.c137.c1.g1', instances=2), mock.Mock(id='/universe.c138.c1.g1', instances=2), ] mock_get_marathon_apps.return_value = mock_marathon_apps mock_service_instances = [('universe', 'c137'), ('universe', 'c138')] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ 'id': 'universe.c137.c1.g1', 'instances': 2, })), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ 'id': 'universe.c138.c2.g2', 'instances': 2, })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, 'westeros-prod') assert mock_get_marathon_apps.called calls = [ mock.call( service='universe', instance='c137', cluster='westeros-prod', soa_dir=DEFAULT_SOA_DIR, ), mock.call( service='universe', instance='c138', cluster='westeros-prod', soa_dir=DEFAULT_SOA_DIR, ), ] mock_load_marathon_service_config.assert_has_calls(calls) assert ret == [('universe', 'c138')] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ 'id': 'universe.c137.c1.g1', 'instances': 3, })), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ 'id': 'universe.c138.c2.g2', 'instances': 2, })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, 'westeros-prod') assert ret == [('universe', 'c137'), ('universe', 'c138')] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock( side_effect=NoDockerImageError)), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ 'id': 'universe.c138.c2.g2', 'instances': 2, })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, 'westeros-prod') assert ret == [('universe', 'c138')] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock( side_effect=InvalidJobNameError)), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ 'id': 'universe.c138.c2.g2', 'instances': 2, })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, 'westeros-prod') assert ret == [('universe', 'c138')] mock_configs = [ mock.Mock(format_marathon_app_dict=mock.Mock( side_effect=NoDeploymentsAvailable)), mock.Mock(format_marathon_app_dict=mock.Mock(return_value={ 'id': 'universe.c138.c2.g2', 'instances': 2, })), ] mock_load_marathon_service_config.side_effect = mock_configs mock_client = mock.Mock(servers=["foo"]) fake_clients = MarathonClients(current=[mock_client], previous=[mock_client]) ret = get_service_instances_needing_update(fake_clients, mock_service_instances, 'westeros-prod') assert ret == [('universe', 'c138')]