github.com/crowdsecurity/crowdsec@v1.6.1/docker/test/tests/test_hub_parsers.py (about) 1 #!/usr/bin/env python 2 3 """ 4 Test parser management 5 """ 6 7 from http import HTTPStatus 8 import json 9 10 import pytest 11 12 pytestmark = pytest.mark.docker 13 14 15 def test_install_two_parsers(crowdsec, flavor): 16 """Test installing parsers at startup""" 17 it1 = 'crowdsecurity/cpanel-logs' 18 it2 = 'crowdsecurity/cowrie-logs' 19 env = { 20 'PARSERS': f'{it1} {it2}' 21 } 22 with crowdsec(flavor=flavor, environment=env) as cs: 23 cs.wait_for_log([ 24 f'*parsers install "{it1}"*', 25 f'*parsers install "{it2}"*', 26 "*Starting processing data*" 27 ]) 28 cs.wait_for_http(8080, '/health', want_status=HTTPStatus.OK) 29 res = cs.cont.exec_run('cscli parsers list -o json') 30 assert res.exit_code == 0 31 j = json.loads(res.output) 32 items = {c['name']: c for c in j['parsers']} 33 assert items[it1]['status'] == 'enabled' 34 assert items[it2]['status'] == 'enabled' 35 36 37 # XXX check that the parser is preinstalled by default 38 def test_disable_parser(crowdsec, flavor): 39 """Test removing a pre-installed parser at startup""" 40 it = 'crowdsecurity/whitelists' 41 env = { 42 'DISABLE_PARSERS': it 43 } 44 with crowdsec(flavor=flavor, environment=env) as cs: 45 cs.wait_for_log([ 46 f'*parsers remove "{it}"*', 47 "*Starting processing data*", 48 ]) 49 cs.wait_for_http(8080, '/health', want_status=HTTPStatus.OK) 50 res = cs.cont.exec_run('cscli parsers list -o json') 51 assert res.exit_code == 0 52 j = json.loads(res.output) 53 items = {c['name'] for c in j['parsers']} 54 assert it not in items 55 56 57 def test_install_and_disable_parser(crowdsec, flavor): 58 """Declare a parser to install AND disable: disable wins""" 59 it = 'crowdsecurity/cpanel-logs' 60 env = { 61 'PARSERS': it, 62 'DISABLE_PARSERS': it, 63 } 64 with crowdsec(flavor=flavor, environment=env) as cs: 65 cs.wait_for_log("*Starting processing data*") 66 cs.wait_for_http(8080, '/health', want_status=HTTPStatus.OK) 67 res = cs.cont.exec_run('cscli parsers list -o json') 68 assert res.exit_code == 0 69 j = json.loads(res.output) 70 items = {c['name'] for c in j['parsers']} 71 assert it not in items 72 logs = cs.log_lines() 73 # check that there was no attempt to install 74 assert not any(f'parsers install "{it}"' in line for line in logs)