def test_classic_replication_check():
    base_module = 'paasta_tools.monitoring'
    check_classic_module = base_module + '.check_classic_service_replication'

    read_config_method = check_classic_module + '.read_services_configuration'
    replication_method = check_classic_module + '.get_replication_for_services'
    extract_method = check_classic_module + '.extract_replication_info'
    check_method = check_classic_module + '.do_replication_check'
    load_system_paasta_config_module = check_classic_module + '.load_system_paasta_config'

    mock_service_config = {'pow': {'wat': 1}}
    mock_replication = {'pow': -1}
    mock_monitoring = {'pow': 'bar'}
    mock_check = {'team': 'testing', 'output': 'testing looks good'}

    with nested(
            mock.patch(read_config_method, return_value=mock_service_config),
            mock.patch(replication_method, return_value=mock_replication),
            mock.patch(extract_method, return_value=(True, mock_monitoring)),
            mock.patch(check_method, return_value=mock_check),
            mock.patch('pysensu_yelp.send_event'),
            mock.patch.object(sys, 'argv', ['check_classic_service_replication.py']),
            mock.patch(load_system_paasta_config_module, return_value=SystemPaastaConfig({}, '/fake/config'))
    ) as (_, _, _, mcheck, _, _, _):
        with pytest.raises(SystemExit) as error:
            check = ClassicServiceReplicationCheck()
            check.run()
            assert mcheck.assert_called_with('pow', {'wat': 1}, -1)
        assert error.value.code == 0
def test_classic_replication_check_connectionerror():
    with nested(
        mock.patch('paasta_tools.monitoring.check_classic_service_replication.get_replication_for_services'),
        mock.patch('paasta_tools.monitoring.check_classic_service_replication.ClassicServiceReplicationCheck.__init__'),
    ) as (
        mock_get_replication_for_services,
        mock_init,
    ):
        mock_get_replication_for_services.side_effect = requests.exceptions.ConnectionError
        mock_init.return_value = None
        check = ClassicServiceReplicationCheck()
        check.critical = mock.Mock()
        check.get_service_replication(['this', 'that'], 'localhost', 12345, DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT)
        check.critical.assert_called_once_with('Failed to connect synapse haproxy on localhost:12345')
def test_classic_replication_check_unknownexception():
    with mock.patch(
        'paasta_tools.monitoring.check_classic_service_replication.get_replication_for_services',
        autospec=True,
    ) as mock_get_replication_for_services, mock.patch(
        'paasta_tools.monitoring.check_classic_service_replication.ClassicServiceReplicationCheck.__init__',
        autospec=True,
    ) as mock_init:
        mock_get_replication_for_services.side_effect = Exception
        mock_init.return_value = None
        check = ClassicServiceReplicationCheck()
        check.critical = mock.Mock()
        check.get_service_replication(['this', 'that'], 'localhost', 12345, DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT)
        check.critical.assert_called_once_with(mock.ANY)
def test_classic_replication_check_unknownexception():
    with nested(
        mock.patch('paasta_tools.monitoring.check_classic_service_replication.get_replication_for_services'),
        mock.patch(
            'paasta_tools.monitoring.check_classic_service_replication.ClassicServiceReplicationCheck.__init__'),
    ) as (
        mock_get_replication_for_services,
        mock_init,
    ):
        mock_get_replication_for_services.side_effect = Exception
        mock_init.return_value = None
        check = ClassicServiceReplicationCheck()
        check.critical = mock.Mock()
        check.get_service_replication(['this', 'that'], 'localhost', 12345, DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT)
        check.critical.assert_called_once_with(mock.ANY)
def test_classic_replication_check_unknownexception():
    with nested(
            mock.patch(
                'paasta_tools.monitoring.check_classic_service_replication.get_replication_for_services'
            ),
            mock.patch(
                'paasta_tools.monitoring.check_classic_service_replication.ClassicServiceReplicationCheck.__init__'
            ),
    ) as (
            mock_get_replication_for_services,
            mock_init,
    ):
        mock_get_replication_for_services.side_effect = Exception
        mock_init.return_value = None
        check = ClassicServiceReplicationCheck()
        check.critical = mock.Mock()
        check.get_service_replication(['this', 'that'])
        check.critical.assert_called_once_with(mock.ANY)
def test_classic_replication_check(system_paasta_config):
    base_module = 'paasta_tools.monitoring'
    check_classic_module = base_module + '.check_classic_service_replication'

    read_config_method = check_classic_module + '.read_services_configuration'
    replication_method = check_classic_module + '.get_replication_for_services'
    extract_method = check_classic_module + '.extract_replication_info'
    check_method = check_classic_module + '.do_replication_check'
    load_system_paasta_config_module = check_classic_module + '.load_system_paasta_config'

    mock_service_config = {'pow': {'wat': 1}}
    mock_replication = {'pow': -1}
    mock_monitoring = {'pow': 'bar'}
    mock_check = {
        'team': 'testing',
        'output': 'testing looks good',
        'runbook': 'fake-runbook',
        'status': 'fake-status',
        'name': 'fake-name',
    }

    with mock.patch(
            read_config_method,
            return_value=mock_service_config,
            autospec=True,
    ), mock.patch(
            replication_method,
            return_value=mock_replication,
            autospec=True,
    ), mock.patch(
            extract_method,
            return_value=(True, mock_monitoring),
            autospec=True,
    ), mock.patch(
            check_method,
            return_value=mock_check,
            autospec=True,
    ) as mcheck, mock.patch(
            'pysensu_yelp.send_event',
            autospec=True,
    ), mock.patch.object(
            sys,
            'argv',
        ['check_classic_service_replication.py'],
    ), mock.patch(
            load_system_paasta_config_module,
            return_value=system_paasta_config,
            autospec=True,
    ):
        with pytest.raises(SystemExit) as error:
            ClassicServiceReplicationCheck()
        mcheck.assert_called_with('pow', {'pow': 'bar'}, 0)
        assert error.value.code == 0