Ejemplo n.º 1
0
def test_get_resources_to_monitor():
    # type: () -> None
    check = MarklogicCheck('marklogic', {}, [INSTANCE_FILTERS])
    response_value = read_fixture_file('cluster-query.yaml')

    # Expected output when there is no exclude list
    complete_filtered = {
        'forest': [
            {
                'name': 'Security',
                'id': '1112331563215633422',
                'type': 'forest',
                'uri': '/forests/Security'
            },
            {
                'name': 'Schemas',
                'id': '5750304059804042419',
                'type': 'forest',
                'uri': '/forests/Schemas'
            },
        ],
        'database': [{
            'id': '5004266825873163057',
            'name': 'Documents',
            'type': 'database',
            'uri': '/databases/Documents'
        }],
        'host': [],
        'server': [{
            'name': 'Admin',
            'id': '9403936238896063877',
            'type': 'server',
            'uri': "/servers/Admin?group-id=Default",
            'group': 'Default',
        }],
    }  # type: Dict[str, List[Any]]

    # Called in the check function
    check.resources = parse_resources(response_value)
    # Include list + exclude list
    filtered_res = check.get_resources_to_monitor()
    assert filtered_res == {
        'forest': [complete_filtered['forest'][0]],
        'database': complete_filtered['database'],
        'host': [],
        'server': complete_filtered['server'],
    }

    # No exclude list
    check._config.resource_filters['excluded'] = []
    filtered_res = check.get_resources_to_monitor()
    assert filtered_res == complete_filtered

    # Useless exclude list
    check._config.resource_filters[
        'excluded'] = check._config.build_resource_filters([{
            'resource_type': 'forest',
            'pattern': 'Security',
            'group': 'Default'
        }])['excluded']
    filtered_res = check.get_resources_to_monitor()
    assert filtered_res == complete_filtered

    # No include list
    check._config.resource_filters['included'] = []
    filtered_res = check.get_resources_to_monitor()
    assert filtered_res == {
        'forest': [],
        'database': [],
        'host': [],
        'server': [],
    }
Ejemplo n.º 2
0
def test_submit_health_service_checks(aggregator, caplog):
    # type: (AggregatorStub, Any) -> None
    check = MarklogicCheck('marklogic', {}, [INSTANCE])

    health_mocked_data = {
        'cluster-health-report': [
            {
                "state": "info",
                "resource-type": "database",
                "resource-name": "Security",
                "code": "HEALTH-DATABASE-NO-BACKUP",
                "message": "Database has never been backed up.",
            },
            {
                'resource-type': 'database',
                'resource-name': 'Fab',
                'code': 'UNKNOWN'
            },
        ]
    }

    check.resources = [
        {
            'id': '255818103205892753',
            'type': 'database',
            'name': 'Security',
            'uri': "/databases/Security"
        },
        {
            'id': '5004266825873163057',
            'type': 'database',
            'name': 'Fab',
            'uri': "/databases/Fab"
        },
        {
            'id': '16024526243775340149',
            'type': 'forest',
            'name': 'Modules',
            'uri': "/forests/Modules"
        },
        {
            'id': '17254568917360711355',
            'type': 'forest',
            'name': 'Extensions',
            'uri': "/forests/Extensions"
        },
    ]

    # If there is no error
    with mock.patch('datadog_checks.marklogic.api.MarkLogicApi.get_health',
                    return_value=health_mocked_data):
        check.submit_health_service_checks()

        aggregator.assert_service_check(
            'marklogic.database.health',
            MarklogicCheck.OK,
            tags=['foo:bar', 'database_name:Security'],
            message=None,
            count=1,
        )
        aggregator.assert_service_check(
            'marklogic.database.health',
            MarklogicCheck.UNKNOWN,
            tags=['foo:bar', 'database_name:Fab'],
            message='UNKNOWN (unknown): No message.',
            count=1,
        )
        aggregator.assert_service_check(
            'marklogic.forest.health',
            MarklogicCheck.OK,
            tags=['foo:bar', 'forest_name:Modules'],
            message=None,
            count=1)
        aggregator.assert_service_check(
            'marklogic.forest.health',
            MarklogicCheck.OK,
            tags=['foo:bar', 'forest_name:Extensions'],
            message=None,
            count=1,
        )

    aggregator.reset()
    caplog.clear()

    # If the user doesn't have enough permissions
    with mock.patch('datadog_checks.marklogic.api.MarkLogicApi.get_health',
                    return_value={'code': 'HEALTH-CLUSTER-ERROR'}):
        check.submit_health_service_checks()

        assert "The user needs `manage-admin` permission to monitor databases health." in caplog.text

    aggregator.reset()
    caplog.clear()

    # If MarkLogic can't be reached
    with mock.patch('datadog_checks.marklogic.api.MarkLogicApi.get_health',
                    side_effect=Exception("exception")):
        check.submit_health_service_checks()

        assert "Failed to monitor databases health" in caplog.text