Skip to content

Commit

Permalink
new: AgentConfig unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Nov 4, 2024
1 parent ba76053 commit 3dd6a42
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 1 deletion.
Empty file.
79 changes: 79 additions & 0 deletions dreadnode_cli/agent/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from pathlib import Path
from uuid import UUID

import pytest

from dreadnode_cli.agent.config import AgentConfig


def test_agent_config_read_not_initialized(tmp_path: Path) -> None:
with pytest.raises(Exception, match="is not initialized"):
AgentConfig.read(tmp_path)


def test_agent_config_active_link_no_links() -> None:
config = AgentConfig(project_name="test")
with pytest.raises(Exception, match="No agent is currently linked"):
_ = config.active_link


def test_agent_config_add_link() -> None:
config = AgentConfig(project_name="test")
id = UUID("00000000-0000-0000-0000-000000000000")

config.add_link("test", id)

assert config.active == "test"
assert config.links["test"].id == id
assert config.links["test"].runs == []


def test_agent_config_add_run() -> None:
config = AgentConfig(project_name="test")
agent_id = UUID("00000000-0000-0000-0000-000000000000")
run_id = UUID("11111111-1111-1111-1111-111111111111")

config.add_link("test", agent_id)
config.add_run(run_id)

assert config.links["test"].runs == [run_id]


def test_agent_config_write_read(tmp_path: Path) -> None:
config = AgentConfig(project_name="test")
agent_id = UUID("00000000-0000-0000-0000-000000000000")
run_id = UUID("11111111-1111-1111-1111-111111111111")

config.add_link("test", agent_id)
config.add_run(run_id)
config.write(tmp_path)

loaded = AgentConfig.read(tmp_path)
assert loaded.project_name == "test"
assert loaded.active == "test"
assert loaded.links["test"].id == agent_id
assert loaded.links["test"].runs == [run_id]


def test_agent_config_update_active() -> None:
config = AgentConfig(project_name="test")
id1 = UUID("00000000-0000-0000-0000-000000000000")
id2 = UUID("11111111-1111-1111-1111-111111111111")

# Add first link
config.add_link("test1", id1)
assert config.active == "test1"

# Add second link
config.add_link("test2", id2)
assert config.active == "test2"

# Remove active link
config.links.pop("test2")
config._update_active()
assert config.active == "test1"

# Remove all links
config.links.clear()
config._update_active()
assert config.active is None
79 changes: 78 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ types-requests = "^2.32.0.20240914"
httpx = "^0.27.2"
ruamel-yaml = "^0.18.6"
docker = "^7.1.0"
pytest = "^8.3.3"
pytest-asyncio = "^0.24.0"

[tool.pytest.ini_options]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"

[tool.poetry.group.dev.dependencies]
mypy = "^1.8.0"
Expand Down

0 comments on commit 3dd6a42

Please sign in to comment.