예제 #1
0
def test_load_rules():
    test_rule_copy = copy.deepcopy(test_rule)
    test_config_copy = copy.deepcopy(test_config)
    with contextlib.ExitStack() as stack:
        mock_open = stack.enter_context(
            mock.patch('elastalert.config.yaml_loader'))
        mock_ls = stack.enter_context(mock.patch('os.listdir'))
        mock_isfile = stack.enter_context(mock.patch('os.path.isfile'))
        mock_ls.return_value = ['testrule.yaml']
        mock_open.side_effect = [test_config_copy, test_rule_copy]
        mock_isfile.return_value = True

        rules = load_rules(test_args)

        assert isinstance(rules['rules'][0]['type'],
                          elastalert.ruletypes.RuleType)
        assert isinstance(rules['rules'][0]['alert'][0],
                          elastalert.alerts.Alerter)
        assert isinstance(rules['rules'][0]['timeframe'], datetime.timedelta)
        assert isinstance(rules['run_every'], datetime.timedelta)
        for included_key in ['comparekey', 'testkey', '@timestamp']:
            assert included_key in rules['rules'][0]['include']

        # Assert include doesn't contain duplicates
        assert rules['rules'][0]['include'].count('@timestamp') == 1
        assert rules['rules'][0]['include'].count('comparekey') == 1
예제 #2
0
def test_raises_on_missing_config():
    optional_keys = ('aggregation', 'use_count_query', 'query_key', 'compare_key', 'filter', 'include')
    test_rule_copy = copy.deepcopy(test_rule)
    for key in test_rule_copy.keys():
        test_rule_copy = copy.deepcopy(test_rule)
        test_config_copy = copy.deepcopy(test_config)
        test_rule_copy.pop(key)

        # Non required keys
        if key in optional_keys:
            continue

        with mock.patch('elastalert.config.yaml_loader') as mock_open:
            mock_open.side_effect = [test_config_copy, test_rule_copy]
            with mock.patch('os.listdir') as mock_ls:
                mock_ls.return_value = ['testrule.yaml']
                with pytest.raises(EAException):
                    load_rules(test_args)
예제 #3
0
def test_raises_on_missing_config():
    optional_keys = ('aggregation', 'use_count_query', 'query_key', 'compare_key', 'filter', 'include', 'es_host', 'es_port')
    test_rule_copy = copy.deepcopy(test_rule)
    for key in test_rule_copy.keys():
        test_rule_copy = copy.deepcopy(test_rule)
        test_config_copy = copy.deepcopy(test_config)
        test_rule_copy.pop(key)

        # Non required keys
        if key in optional_keys:
            continue

        with mock.patch('elastalert.config.yaml_loader') as mock_open:
            mock_open.side_effect = [test_config_copy, test_rule_copy]
            with mock.patch('os.listdir') as mock_ls:
                mock_ls.return_value = ['testrule.yaml']
                with pytest.raises(EAException):
                    load_rules(test_args)
예제 #4
0
def test_load_disabled_rules():
    test_rule_copy = copy.deepcopy(test_rule)
    test_rule_copy['is_enabled'] = False
    test_config_copy = copy.deepcopy(test_config)
    with mock.patch('elastalert.config.yaml_loader') as mock_open:
        mock_open.side_effect = [test_config_copy, test_rule_copy]

        with mock.patch('os.listdir') as mock_ls:
            mock_ls.return_value = ['testrule.yaml']
            rules = load_rules(test_args)
            # The rule is not loaded for it has "is_enabled=False"
            assert len(rules['rules']) == 0
예제 #5
0
def test_load_disabled_rules():
    test_rule_copy = copy.deepcopy(test_rule)
    test_rule_copy['is_enabled'] = False
    test_config_copy = copy.deepcopy(test_config)
    with mock.patch('elastalert.config.yaml_loader') as mock_open:
        mock_open.side_effect = [test_config_copy, test_rule_copy]

        with mock.patch('os.listdir') as mock_ls:
            mock_ls.return_value = ['testrule.yaml']
            rules = load_rules(test_args)
            # The rule is not loaded for it has "is_enabled=False"
            assert len(rules['rules']) == 0
예제 #6
0
def test_load_ssl_env_true():
    test_rule_copy = copy.deepcopy(test_rule)
    test_rule_copy.pop('es_host')
    test_rule_copy.pop('es_port')
    test_config_copy = copy.deepcopy(test_config)
    with mock.patch('elastalert.config.yaml_loader') as mock_open:
        mock_open.side_effect = [test_config_copy, test_rule_copy]

        with mock.patch('os.listdir') as mock_ls:
            with mock.patch.dict(os.environ, {'ES_USE_SSL': 'true'}):
                mock_ls.return_value = ['testrule.yaml']
                rules = load_rules(test_args)

                assert rules['use_ssl'] is True
예제 #7
0
def test_load_ssl_env_true():
    test_rule_copy = copy.deepcopy(test_rule)
    test_rule_copy.pop('es_host')
    test_rule_copy.pop('es_port')
    test_config_copy = copy.deepcopy(test_config)
    with mock.patch('elastalert.config.yaml_loader') as mock_open:
        mock_open.side_effect = [test_config_copy, test_rule_copy]

        with mock.patch('os.listdir') as mock_ls:
            with mock.patch.dict(os.environ, {'ES_USE_SSL': 'true'}):
                mock_ls.return_value = ['testrule.yaml']
                rules = load_rules(test_args)

                assert rules['use_ssl'] is True
예제 #8
0
def test_load_url_prefix_env():
    test_rule_copy = copy.deepcopy(test_rule)
    test_rule_copy.pop('es_host')
    test_rule_copy.pop('es_port')
    test_config_copy = copy.deepcopy(test_config)
    with mock.patch('elastalert.config.yaml_loader') as mock_open:
        mock_open.side_effect = [test_config_copy, test_rule_copy]

        with mock.patch('os.listdir') as mock_ls:
            with mock.patch.dict(os.environ, {'ES_URL_PREFIX': 'es/'}):
                mock_ls.return_value = ['testrule.yaml']
                rules = load_rules(test_args)

                assert rules['es_url_prefix'] == 'es/'
예제 #9
0
def test_load_default_host_port():
    test_rule_copy = copy.deepcopy(test_rule)
    test_rule_copy.pop('es_host')
    test_rule_copy.pop('es_port')
    test_config_copy = copy.deepcopy(test_config)
    with mock.patch('elastalert.config.yaml_loader') as mock_open:
        mock_open.side_effect = [test_config_copy, test_rule_copy]

        with mock.patch('os.listdir') as mock_ls:
            mock_ls.return_value = ['testrule.yaml']
            rules = load_rules(test_args)

            # Assert include doesn't contain duplicates
            assert rules['es_port'] == 12345
            assert rules['es_host'] == 'elasticsearch.test'
예제 #10
0
def test_load_default_host_port():
    test_rule_copy = copy.deepcopy(test_rule)
    test_rule_copy.pop('es_host')
    test_rule_copy.pop('es_port')
    test_config_copy = copy.deepcopy(test_config)
    with mock.patch('elastalert.config.yaml_loader') as mock_open:
        mock_open.side_effect = [test_config_copy, test_rule_copy]

        with mock.patch('os.listdir') as mock_ls:
            mock_ls.return_value = ['testrule.yaml']
            rules = load_rules(test_args)

            # Assert include doesn't contain duplicates
            assert rules['es_port'] == 12345
            assert rules['es_host'] == 'elasticsearch.test'
예제 #11
0
def test_load_rules():
    test_rule_copy = copy.deepcopy(test_rule)
    test_config_copy = copy.deepcopy(test_config)
    with mock.patch('elastalert.config.yaml_loader') as mock_open:
        mock_open.side_effect = [test_config_copy, test_rule_copy]

        with mock.patch('os.listdir') as mock_ls:
            mock_ls.return_value = ['testrule.yaml']
            rules = load_rules(test_args)
            assert isinstance(rules['rules'][0]['type'], elastalert.ruletypes.RuleType)
            assert isinstance(rules['rules'][0]['alert'][0], elastalert.alerts.Alerter)
            assert isinstance(rules['rules'][0]['timeframe'], datetime.timedelta)
            assert isinstance(rules['run_every'], datetime.timedelta)
            for included_key in ['comparekey', 'testkey', '@timestamp']:
                assert included_key in rules['rules'][0]['include']

            # Assert include doesn't contain duplicates
            assert rules['rules'][0]['include'].count('@timestamp') == 1
            assert rules['rules'][0]['include'].count('comparekey') == 1
예제 #12
0
def test_load_rules():
    test_rule_copy = copy.deepcopy(test_rule)
    test_config_copy = copy.deepcopy(test_config)
    with mock.patch('elastalert.config.yaml_loader') as mock_open:
        mock_open.side_effect = [test_config_copy, test_rule_copy]

        with mock.patch('os.listdir') as mock_ls:
            mock_ls.return_value = ['testrule.yaml']
            rules = load_rules('test_config')
            assert isinstance(rules['rules'][0]['type'],
                              elastalert.ruletypes.RuleType)
            assert isinstance(rules['rules'][0]['alert'][0],
                              elastalert.alerts.Alerter)
            assert isinstance(rules['rules'][0]['timeframe'],
                              datetime.timedelta)
            assert isinstance(rules['run_every'], datetime.timedelta)
            for included_key in ['comparekey', 'testkey', '@timestamp']:
                assert included_key in rules['rules'][0]['include']

            # Assert include doesn't contain duplicates
            assert rules['rules'][0]['include'].count('@timestamp') == 1
            assert rules['rules'][0]['include'].count('comparekey') == 1