generated from Ostorlab/template_agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
55 additions
and
66 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
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 |
---|---|---|
@@ -1,46 +1,33 @@ | ||
"""Unit tests for Nebula agent.""" | ||
|
||
from ostorlab.agent.message import message as msg | ||
from pytest_mock import plugin | ||
import json | ||
import pathlib | ||
|
||
from agent import nebula_agent | ||
|
||
|
||
def testAgentNebula_whenFileTypeIsJson_shouldPersistMessageToJSONFile( | ||
nebula_test_agent: nebula_agent.NebulaAgent, | ||
link_message: msg.Message, | ||
mocker: plugin.MockerFixture, | ||
) -> None: | ||
"""Test Nebula Agent when file_type is json and single message, | ||
should persist message to JSON file.""" | ||
open_mocker = mocker.patch("builtins.open", mocker.mock_open()) | ||
|
||
nebula_test_agent.process(link_message) | ||
|
||
assert open_mocker.called is True | ||
path, _ = open_mocker.call_args[0] | ||
assert nebula_agent.CONFIG_HOME + "/messages_" in path | ||
assert any( | ||
"https://ostorlab.co" in call_arg | ||
for call_arg in open_mocker().write.call_args[0] | ||
) | ||
from ostorlab.agent import definitions as agent_definitions | ||
from ostorlab.runtimes import definitions as runtime_definitions | ||
from ostorlab.utils import defintions as utils_definitions | ||
|
||
|
||
def testAgentNebula_whenMultipleMessages_shouldAppendMessagesToJSONFile( | ||
nebula_test_agent: nebula_agent.NebulaAgent, | ||
multiple_link_messages: list[msg.Message], | ||
mocker: plugin.MockerFixture, | ||
) -> None: | ||
"""Test Nebula Agent when multiple messages, should append messages to JSON file.""" | ||
open_mocker = mocker.patch("builtins.open", mocker.mock_open()) | ||
|
||
for message in multiple_link_messages: | ||
nebula_test_agent.process(message) | ||
|
||
assert open_mocker.called is True | ||
assert open_mocker().write.call_count == len(multiple_link_messages) | ||
assert all( | ||
f"https://www.domain{i}.com" in call_arg | ||
for i, args_list in enumerate(open_mocker().write.call_args_list) | ||
for call_arg in args_list[0] | ||
) | ||
from agent import nebula_agent | ||
import pytest | ||
|
||
|
||
def testAgentNebula_whenUnsupportedFileType_raisesValueError() -> None: | ||
"""Test that NebulaAgent raises ValueError when file type is not supported.""" | ||
with pytest.raises(ValueError): | ||
with (pathlib.Path(__file__).parent.parent / "ostorlab.yaml").open() as yaml_o: | ||
definition = agent_definitions.AgentDefinition.from_yaml(yaml_o) | ||
settings = runtime_definitions.AgentSettings( | ||
key="agent/ostorlab/nebula", | ||
bus_url="NA", | ||
bus_exchange_topic="NA", | ||
args=[ | ||
utils_definitions.Arg( | ||
name="file_type", | ||
type="string", | ||
value=json.dumps("txt").encode(), | ||
), | ||
], | ||
healthcheck_port=5301, | ||
redis_url="redis://guest:guest@localhost:6379", | ||
) | ||
nebula_agent.NebulaAgent(definition, settings) |