def test_check_smartstack_replication_for_instance_crit_when_no_smartstack_info():
    service = 'test'
    instance = 'some_instance'
    cluster = 'fake_cluster'
    available = {}
    expected_replication_count = 2
    soa_dir = 'test_dir'
    crit = 90
    with contextlib.nested(
        mock.patch('check_marathon_services_replication.send_event', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance',
                   autospec=True, return_value=instance),
        mock.patch('check_marathon_services_replication.get_context', autospec=True),
        mock.patch('check_marathon_services_replication.load_smartstack_info_for_service', autospec=True),
        mock.patch('paasta_tools.marathon_tools.load_marathon_service_config', autospec=True)
    ) as (
        mock_send_event,
        mock_read_namespace_for_service_instance,
        mock_get_context,
        mock_load_smartstack_info_for_service,
        mock_load_marathon_service_config,
    ):
        mock_load_smartstack_info_for_service.return_value = available
        mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
        mock_load_marathon_service_config.return_value = mock_service_job_config
        check_marathon_services_replication.check_smartstack_replication_for_instance(
            service, instance, cluster, soa_dir, crit, expected_replication_count,
        )
        mock_send_event.assert_called_once_with(
            service=service,
            namespace=instance,
            cluster=cluster,
            soa_dir=soa_dir,
            status=pysensu_yelp.Status.CRITICAL,
            output=mock.ANY)
def test_check_smartstack_replication_for_instance_ignores_things_under_a_different_namespace():
    service = 'test'
    instance = 'main'
    namespace = 'canary'
    cluster = 'fake_cluster'
    available = {'fake_region': {'test.canary': 1, 'test.main': 4, 'test.fully_replicated': 8}}
    expected_replication_count = 8
    soa_dir = 'test_dir'
    crit = 90
    with contextlib.nested(
        mock.patch('check_marathon_services_replication.send_event_if_under_replication', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance',
                   autospec=True, return_value=namespace),
        mock.patch('check_marathon_services_replication.load_smartstack_info_for_service', autospec=True),
        mock.patch('marathon_tools.load_marathon_service_config', autospec=True)
    ) as (
        mock_send_event_if_under_replication,
        mock_read_namespace_for_service_instance,
        mock_load_smartstack_info_for_service,
        mock_load_marathon_service_config,
    ):
        mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
        mock_load_marathon_service_config.return_value = mock_service_job_config
        mock_load_smartstack_info_for_service.return_value = available
        check_marathon_services_replication.check_smartstack_replication_for_instance(
            service, instance, cluster, soa_dir, crit, expected_replication_count,
        )
        mock_send_event_if_under_replication.call_count == 0
def test_check_smartstack_replication_for_instance_ok_with_enough_replication_multilocation():
    service = 'test'
    instance = 'everything_up'
    cluster = 'fake_cluster'
    available = {'fake_region': {'test.everything_up': 1}, 'fake_other_region': {'test.everything_up': 1}}
    expected_replication_count = 2
    soa_dir = 'test_dir'
    crit = 90
    with contextlib.nested(
        mock.patch('check_marathon_services_replication.send_event', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance',
                   autospec=True, return_value=instance),
        mock.patch('check_marathon_services_replication.load_smartstack_info_for_service', autospec=True),
        mock.patch('paasta_tools.marathon_tools.load_marathon_service_config', autospec=True)
    ) as (
        mock_send_event,
        mock_read_namespace_for_service_instance,
        mock_load_smartstack_info_for_service,
        mock_load_marathon_service_config,
    ):
        mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
        mock_load_marathon_service_config.return_value = mock_service_job_config
        mock_load_smartstack_info_for_service.return_value = available
        check_marathon_services_replication.check_smartstack_replication_for_instance(
            service, instance, cluster, soa_dir, crit, expected_replication_count,
        )
        mock_send_event.assert_called_once_with(
            service=service,
            namespace=instance,
            cluster=cluster,
            soa_dir=soa_dir,
            status=pysensu_yelp.Status.OK,
            output=mock.ANY)
def test_check_smartstack_replication_for_instance_ok_when_expecting_zero():
    service = 'test'
    instance = 'main'
    cluster = 'fake_cluster'
    available = {'fake_region': {'test.main': 1, 'test.three': 4, 'test.four': 8}}
    expected_replication_count = 0
    soa_dir = 'test_dir'
    crit = 90
    with contextlib.nested(
        mock.patch('check_marathon_services_replication.send_event', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance',
                   autospec=True, return_value=instance),
        mock.patch('check_marathon_services_replication.get_context', autospec=True),
        mock.patch('check_marathon_services_replication.load_smartstack_info_for_service', autospec=True),
        mock.patch('paasta_tools.marathon_tools.load_marathon_service_config', autospec=True)
    ) as (
        mock_send_event,
        mock_read_namespace_for_service_instance,
        mock_get_context,
        mock_load_smartstack_info_for_service,
        mock_load_marathon_service_config,
    ):
        mock_load_smartstack_info_for_service.return_value = available
        check_marathon_services_replication.check_smartstack_replication_for_instance(
            service, instance, cluster, soa_dir, crit, expected_replication_count,
        )
        mock_send_event.assert_called_once_with(
            service=service,
            namespace=instance,
            cluster=cluster,
            soa_dir=soa_dir,
            status=pysensu_yelp.Status.OK,
            output=mock.ANY)
Exemplo n.º 5
0
def test_check_smartstack_replication_for_instance_crit_when_missing_replication_multilocation(
):
    service = 'test'
    instance = 'missing_instance'
    cluster = 'fake_cluster'
    available = {
        'fake_region': {
            'test.main': 0
        },
        'fake_other_region': {
            'test.main': 0
        }
    }
    expected_replication_count = 2
    soa_dir = 'test_dir'
    crit = 90
    with contextlib.nested(
            mock.patch('check_marathon_services_replication.send_event',
                       autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.read_namespace_for_service_instance',
                autospec=True,
                return_value=instance),
            mock.patch(
                'check_marathon_services_replication.load_smartstack_info_for_service',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.load_marathon_service_config',
                autospec=True)) as (
                    mock_send_event,
                    mock_read_namespace_for_service_instance,
                    mock_load_smartstack_info_for_service,
                    mock_load_marathon_service_config,
                ):
        mock_service_job_config = mock.MagicMock(
            spec_set=MarathonServiceConfig)
        mock_load_marathon_service_config.return_value = mock_service_job_config
        mock_load_smartstack_info_for_service.return_value = available
        check_marathon_services_replication.check_smartstack_replication_for_instance(
            service,
            instance,
            cluster,
            soa_dir,
            crit,
            expected_replication_count,
        )
        mock_send_event.assert_called_once_with(
            service=service,
            namespace=instance,
            cluster=cluster,
            soa_dir=soa_dir,
            status=pysensu_yelp.Status.CRITICAL,
            output=mock.ANY)
Exemplo n.º 6
0
def test_check_smartstack_replication_for_instance_ok_with_enough_replication(
):
    service = 'test'
    instance = 'everything_up'
    cluster = 'fake_cluster'
    available = {
        'fake_region': {
            'test.canary': 1,
            'test.low_replication': 4,
            'test.everything_up': 8
        }
    }
    expected_replication_count = 8
    soa_dir = 'test_dir'
    crit = 90
    with contextlib.nested(
            mock.patch('check_marathon_services_replication.send_event',
                       autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.read_namespace_for_service_instance',
                autospec=True,
                return_value=instance),
            mock.patch(
                'check_marathon_services_replication.load_smartstack_info_for_service',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.load_marathon_service_config',
                autospec=True)) as (
                    mock_send_event,
                    mock_read_namespace_for_service_instance,
                    mock_load_smartstack_info_for_service,
                    mock_load_marathon_service_config,
                ):
        mock_service_job_config = mock.MagicMock(
            spec_set=MarathonServiceConfig)
        mock_load_marathon_service_config.return_value = mock_service_job_config
        mock_load_smartstack_info_for_service.return_value = available
        check_marathon_services_replication.check_smartstack_replication_for_instance(
            service,
            instance,
            cluster,
            soa_dir,
            crit,
            expected_replication_count,
        )
        mock_send_event.assert_called_once_with(service=service,
                                                namespace=instance,
                                                cluster=cluster,
                                                soa_dir=soa_dir,
                                                status=pysensu_yelp.Status.OK,
                                                output=mock.ANY)
Exemplo n.º 7
0
def test_check_smartstack_replication_for_instance_ignores_things_under_a_different_namespace(
):
    service = 'test'
    instance = 'main'
    namespace = 'canary'
    cluster = 'fake_cluster'
    available = {
        'fake_region': {
            'test.canary': 1,
            'test.main': 4,
            'test.fully_replicated': 8
        }
    }
    expected_replication_count = 8
    soa_dir = 'test_dir'
    crit = 90
    with contextlib.nested(
            mock.patch(
                'check_marathon_services_replication.send_event_if_under_replication',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.read_namespace_for_service_instance',
                autospec=True,
                return_value=namespace),
            mock.patch(
                'check_marathon_services_replication.load_smartstack_info_for_service',
                autospec=True),
            mock.patch('marathon_tools.load_marathon_service_config',
                       autospec=True)) as (
                           mock_send_event_if_under_replication,
                           mock_read_namespace_for_service_instance,
                           mock_load_smartstack_info_for_service,
                           mock_load_marathon_service_config,
                       ):
        mock_service_job_config = mock.MagicMock(
            spec_set=MarathonServiceConfig)
        mock_load_marathon_service_config.return_value = mock_service_job_config
        mock_load_smartstack_info_for_service.return_value = available
        check_marathon_services_replication.check_smartstack_replication_for_instance(
            service,
            instance,
            cluster,
            soa_dir,
            crit,
            expected_replication_count,
        )
        mock_send_event_if_under_replication.call_count == 0
Exemplo n.º 8
0
def test_check_smartstack_replication_for_instance_ok_when_expecting_zero():
    service = 'test'
    instance = 'main'
    cluster = 'fake_cluster'
    available = {
        'fake_region': {
            'test.main': 1,
            'test.three': 4,
            'test.four': 8
        }
    }
    expected_replication_count = 0
    soa_dir = 'test_dir'
    crit = 90
    with contextlib.nested(
            mock.patch('check_marathon_services_replication.send_event',
                       autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.read_namespace_for_service_instance',
                autospec=True,
                return_value=instance),
            mock.patch(
                'check_marathon_services_replication.load_smartstack_info_for_service',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.load_marathon_service_config',
                autospec=True)) as (
                    mock_send_event,
                    mock_read_namespace_for_service_instance,
                    mock_load_smartstack_info_for_service,
                    mock_load_marathon_service_config,
                ):
        mock_load_smartstack_info_for_service.return_value = available
        check_marathon_services_replication.check_smartstack_replication_for_instance(
            service,
            instance,
            cluster,
            soa_dir,
            crit,
            expected_replication_count,
        )
        mock_send_event.assert_called_once_with(service=service,
                                                namespace=instance,
                                                cluster=cluster,
                                                soa_dir=soa_dir,
                                                status=pysensu_yelp.Status.OK,
                                                output=mock.ANY)