|
1 | 1 | import secrets
|
| 2 | +import time |
2 | 3 | from http import HTTPStatus
|
3 | 4 |
|
4 | 5 | import pytest
|
@@ -35,3 +36,52 @@ def test_split_lapi_agent(crowdsec, flavor: str) -> None:
|
35 | 36 | assert res.exit_code == 0
|
36 | 37 | stdout = res.output.decode()
|
37 | 38 | assert "You can successfully interact with Local API (LAPI)" in stdout
|
| 39 | + |
| 40 | + |
| 41 | +def test_unregister_on_exit(crowdsec, flavor: str) -> None: |
| 42 | + rand = str(secrets.randbelow(10000)) |
| 43 | + lapiname = f"lapi-{rand}" |
| 44 | + agentname = f"agent-{rand}" |
| 45 | + |
| 46 | + lapi_env = { |
| 47 | + "AGENT_USERNAME": "testagent", |
| 48 | + "AGENT_PASSWORD": "testpassword", |
| 49 | + } |
| 50 | + |
| 51 | + agent_env = { |
| 52 | + "AGENT_USERNAME": "testagent", |
| 53 | + "AGENT_PASSWORD": "testpassword", |
| 54 | + "DISABLE_LOCAL_API": "true", |
| 55 | + "LOCAL_API_URL": f"http://{lapiname}:8080", |
| 56 | + "UNREGISTER_ON_EXIT": "true", |
| 57 | + } |
| 58 | + |
| 59 | + cs_lapi = crowdsec(name=lapiname, environment=lapi_env, flavor=flavor) |
| 60 | + cs_agent = crowdsec(name=agentname, environment=agent_env, flavor=flavor, stop_timeout=5) |
| 61 | + |
| 62 | + with cs_lapi as lapi: |
| 63 | + lapi.wait_for_log("*CrowdSec Local API listening on *:8080*") |
| 64 | + lapi.wait_for_http(8080, "/health", want_status=HTTPStatus.OK) |
| 65 | + |
| 66 | + res = lapi.cont.exec_run("cscli machines list") |
| 67 | + assert res.exit_code == 0 |
| 68 | + # the machine is created in the lapi entrypoint |
| 69 | + assert "testagent" in res.output.decode() |
| 70 | + |
| 71 | + with cs_agent as agent: |
| 72 | + agent.wait_for_log("*Starting processing data*") |
| 73 | + res = agent.cont.exec_run("cscli lapi status") |
| 74 | + assert res.exit_code == 0 |
| 75 | + stdout = res.output.decode() |
| 76 | + assert "You can successfully interact with Local API (LAPI)" in stdout |
| 77 | + |
| 78 | + res = lapi.cont.exec_run("cscli machines list") |
| 79 | + assert res.exit_code == 0 |
| 80 | + assert "testagent" in res.output.decode() |
| 81 | + |
| 82 | + time.sleep(2) |
| 83 | + |
| 84 | + res = lapi.cont.exec_run("cscli machines list") |
| 85 | + assert res.exit_code == 0 |
| 86 | + # and it's not there anymore |
| 87 | + assert "testagent" not in res.output.decode() |
0 commit comments