-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow configuring the forwarders to run concurrently or not.
Fixes #15 Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
- Loading branch information
Showing
5 changed files
with
165 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2021-2022 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
import asyncio | ||
from typing import Any | ||
from typing import Dict | ||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
import pytest_asyncio | ||
|
||
from saf.manager import Manager | ||
from saf.models import AnalyticsConfig | ||
|
||
|
||
if TYPE_CHECKING: | ||
try: | ||
from typing import AsyncGenerator | ||
except ImportError: | ||
from typing_extensions import AsyncGenerator | ||
|
||
try: | ||
asyncio_fixture = pytest_asyncio.fixture | ||
except AttributeError: | ||
# On Py3.6 and older version of the pytest gets installed which does not | ||
# have the `.fixture` function. | ||
# Fallback to `pytest.fixture`. | ||
asyncio_fixture = pytest.fixture | ||
|
||
|
||
async def _run_manager(manager): | ||
try: | ||
await manager.run() | ||
except asyncio.CancelledError: | ||
pass | ||
finally: | ||
await manager.await_stopped() | ||
|
||
|
||
@pytest.fixture | ||
def analytics_config(analytics_config_dict: Dict[str, Any]): | ||
return AnalyticsConfig.parse_obj(analytics_config_dict) | ||
|
||
|
||
@asyncio_fixture | ||
async def manager(analytics_config: AnalyticsConfig) -> "AsyncGenerator[Manager, None]": | ||
_manager = Manager(analytics_config) | ||
loop = asyncio.get_event_loop() | ||
task = loop.create_task(_run_manager(_manager)) | ||
try: | ||
yield _manager | ||
finally: | ||
if not task.done(): | ||
task.cancel() | ||
await task |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright 2022 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
import asyncio | ||
import pathlib | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def forwarder_dump_path(tmp_path): | ||
return tmp_path / "forwarder-dump.txt" | ||
|
||
|
||
@pytest.fixture(params=(True, False), ids=lambda x: f"ConcurrentForwarders({x})") | ||
def concurrent_forwarders(request): | ||
return request.param | ||
|
||
|
||
@pytest.fixture | ||
def analytics_config_dict(forwarder_dump_path, concurrent_forwarders): | ||
return { | ||
"collectors": { | ||
"noop-collector": {"plugin": "noop", "interval": 5}, | ||
}, | ||
"forwarders": { | ||
"forwarder-1": { | ||
"plugin": "test", | ||
"sleep": 0.1, | ||
"path": forwarder_dump_path, | ||
"message": "1", | ||
}, | ||
"forwarder-2": { | ||
"plugin": "test", | ||
"sleep": 0.3, | ||
"path": forwarder_dump_path, | ||
"message": "2", | ||
}, | ||
"forwarder-3": { | ||
"plugin": "test", | ||
"sleep": 0.5, | ||
"path": forwarder_dump_path, | ||
"message": "3", | ||
}, | ||
}, | ||
"pipelines": { | ||
"my-pipeline": { | ||
"enabled": True, | ||
"concurrent_forwarders": concurrent_forwarders, | ||
"collect": "noop-collector", | ||
"forward": [ | ||
"forwarder-3", | ||
"forwarder-2", | ||
"forwarder-1", | ||
], | ||
} | ||
}, | ||
"salt_config": {}, | ||
} | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.usefixtures("manager") | ||
async def test_pipeline(forwarder_dump_path: pathlib.Path, concurrent_forwarders): | ||
synchronous_outcome = ["3", "2", "1"] | ||
timeout = 5 | ||
while timeout: | ||
await asyncio.sleep(1) | ||
timeout -= 1 | ||
if not forwarder_dump_path.exists(): | ||
continue | ||
lines = forwarder_dump_path.read_text().splitlines() | ||
if len(lines) >= 3: | ||
if concurrent_forwarders is False: | ||
assert lines[:3] == synchronous_outcome | ||
else: | ||
assert lines[:3] != synchronous_outcome | ||
break | ||
else: | ||
pytest.fail(f"Failed to find dumped events in {forwarder_dump_path}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters