github.com/crowdsecurity/crowdsec@v1.6.1/docker/test/tests/test_bouncer.py (about)

     1  #!/usr/bin/env python
     2  
     3  """
     4  Test bouncer management: pre-installed, run-time installation and removal.
     5  """
     6  
     7  import hashlib
     8  from http import HTTPStatus
     9  import json
    10  
    11  import pytest
    12  
    13  pytestmark = pytest.mark.docker
    14  
    15  
    16  def hex512(s):
    17      """Return the sha512 hash of a string as a hex string"""
    18      return hashlib.sha512(s.encode()).hexdigest()
    19  
    20  
    21  def test_register_bouncer_env(crowdsec, flavor):
    22      """Test installing bouncers at startup, from envvar"""
    23  
    24      env = {
    25          'BOUNCER_KEY_bouncer1name': 'bouncer1key',
    26          'BOUNCER_KEY_bouncer2name': 'bouncer2key'
    27      }
    28  
    29      with crowdsec(flavor=flavor, environment=env) as cs:
    30          cs.wait_for_log("*Starting processing data*")
    31          cs.wait_for_http(8080, '/health', want_status=HTTPStatus.OK)
    32          res = cs.cont.exec_run('cscli bouncers list -o json')
    33          assert res.exit_code == 0
    34          j = json.loads(res.output)
    35          assert len(j) == 2
    36          bouncer1, bouncer2 = j
    37          assert bouncer1['name'] == 'bouncer1name'
    38          assert bouncer2['name'] == 'bouncer2name'
    39  
    40          # add a second bouncer at runtime
    41          res = cs.cont.exec_run('cscli bouncers add bouncer3name -k bouncer3key')
    42          assert res.exit_code == 0
    43          res = cs.cont.exec_run('cscli bouncers list -o json')
    44          assert res.exit_code == 0
    45          j = json.loads(res.output)
    46          assert len(j) == 3
    47          bouncer3 = j[2]
    48          assert bouncer3['name'] == 'bouncer3name'
    49  
    50          # remove all bouncers
    51          res = cs.cont.exec_run('cscli bouncers delete bouncer1name bouncer2name bouncer3name')
    52          assert res.exit_code == 0
    53          res = cs.cont.exec_run('cscli bouncers list -o json')
    54          assert res.exit_code == 0
    55          j = json.loads(res.output)
    56          assert len(j) == 0