Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hardcoded file loader from test_rule.py #1100

Merged
merged 7 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Breaking changes
- [Alerta] All matches will now be sent with the alert - [#1068](https://github.com/jertel/elastalert2/pull/1068) - @dakotacody
- Renamed the `overwrites` parameter to `overrides` in the load_conf method of config.py - [#1100](https://github.com/jertel/elastalert2/pull/1100) - @akusei

## New features
- [Graylog GELF] Alerter added. [#1050](https://github.com/jertel/elastalert2/pull/1050) - @malinkinsa
Expand Down
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
.PHONY: all production test docs clean

COMPOSE = "-compose"
ifeq ($(shell docker$(COMPOSE) 2> /dev/null),)
COMPOSE = " compose"
endif

all: production

production:
Expand All @@ -20,9 +25,9 @@ test-elasticsearch:
tox -c tests/tox.ini -- --runelasticsearch

test-docker:
docker-compose -f tests/docker-compose.yml --project-name elastalert build tox
docker-compose -f tests/docker-compose.yml --project-name elastalert run --rm tox \
tox -c tests/tox.ini -- $(filter-out $@,$(MAKECMDGOALS))
$(shell echo docker$(COMPOSE)) -f tests/docker-compose.yml --project-name elastalert build tox
$(shell echo docker$(COMPOSE)) -f tests/docker-compose.yml --project-name elastalert run --rm tox \
tox -c tests/tox.ini -- $(filter-out $@,$(MAKECMDGOALS))

clean:
make -C docs clean
Expand Down
6 changes: 3 additions & 3 deletions elastalert/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
}


def load_conf(args, defaults=None, overwrites=None):
def load_conf(args, defaults=None, overrides=None):
""" Creates a conf dictionary for ElastAlerter. Loads the global
config file and then each rule found in rules_folder.

:param args: The parsed arguments to ElastAlert
:param defaults: Dictionary of default conf values
:param overwrites: Dictionary of conf values to override
:param overrides: Dictionary of conf values to override
:return: The global configuration, a dictionary.
"""
filename = args.config
Expand All @@ -65,7 +65,7 @@ def load_conf(args, defaults=None, overwrites=None):
if key not in conf:
conf[key] = value

for key, value in (iter(overwrites.items()) if overwrites is not None else []):
for key, value in (iter(overrides.items()) if overrides is not None else []):
conf[key] = value

# Make sure we have all required globals
Expand Down
5 changes: 1 addition & 4 deletions elastalert/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,8 @@ def run_rule_test(self):
'buffer_time': {'minutes': 45},
'scroll_keepalive': '30s'
}
overwrites = {
'rules_loader': 'file',
}

conf = load_conf(self.args, defaults, overwrites)
conf = load_conf(self.args, defaults)
rule_yaml = conf['rules_loader'].load_yaml(self.args.file)
conf['rules_loader'].load_options(rule_yaml, conf, self.args.file)

Expand Down
42 changes: 42 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,48 @@ def test_config_loads():
assert conf['alert_time_limit'] == datetime.timedelta(days=2)


def test_config_defaults():
dir_path = os.path.dirname(os.path.realpath(__file__))

test_args = mock.Mock()
test_args.config = dir_path + '/example.config.yaml'
test_args.rule = None
test_args.debug = False
test_args.es_debug_trace = None

conf = load_conf(
test_args,
defaults={
'new_key': 'new_value',
'rules_folder': '/new/rules/folder'
}
)

assert conf['new_key'] == 'new_value'
assert conf['rules_folder'] == '/opt/elastalert/rules'


def test_config_overrides():
dir_path = os.path.dirname(os.path.realpath(__file__))

test_args = mock.Mock()
test_args.config = dir_path + '/example.config.yaml'
test_args.rule = None
test_args.debug = False
test_args.es_debug_trace = None

conf = load_conf(
test_args,
overrides={
'new_key': 'new_value',
'rules_folder': '/new/rules/folder'
}
)

assert conf['new_key'] == 'new_value'
assert conf['rules_folder'] == '/new/rules/folder'


def test_config_loads_ea_execption():
with pytest.raises(EAException) as ea:
os.environ['ELASTIC_PASS'] = 'password_from_env'
Expand Down