Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #292 from ansible/AAP-7694-handle-null-source-config
Browse files Browse the repository at this point in the history
Handles null source config
  • Loading branch information
Red-HAP authored Dec 16, 2022
2 parents 0849ada + 35c9249 commit 752604d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/eda_server/db/sql/rulebook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ def expand_ruleset_sources(rulebook_data: dict) -> dict:
for ruleset_data in rulebook_data:
xp_sources = []
expanded_ruleset_sources[ruleset_data["name"]] = xp_sources
for source in ruleset_data.get("sources", []):
for source in ruleset_data.get("sources") or []:
xp_src = {"name": "<unnamed>"}
for src_key, src_val in source.items():
if src_key == "name":
Expand Down
2 changes: 1 addition & 1 deletion src/eda_server/schema/rulebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class RulesetSourceRef(BaseModel):
name: str
type: str
source: str
config: dict
config: Optional[dict]


class RulesetSource(BaseModel):
Expand Down
49 changes: 49 additions & 0 deletions tests/integration/api/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Tuple
from unittest import mock

import sqlalchemy as sa
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession
from starlette import status as status_codes
Expand All @@ -41,6 +42,21 @@
"""


TEST_RULESET_BAD = """
---
- name: Test bad
hosts: all
sources:
- blah:
rules:
- name: Test it is bad
condition: event.i == 1
action:
debug:
"""


DBTestData = namedtuple(
"DBTestData",
(
Expand Down Expand Up @@ -419,6 +435,39 @@ async def test_read_ruleset(
)


async def test_read_ruleset_no_source_config(
client: AsyncClient, db: AsyncSession
):
test_data = await _create_rules(db)
project = test_data.project
response = await client.post(
"/api/rulebooks",
json={
"name": "test-ruleset-1.yml",
"rulesets": TEST_RULESET_BAD,
},
)
assert response.status_code == status_codes.HTTP_200_OK
data = response.json()
await db.execute(
sa.update(models.rulebooks)
.where(models.rulebooks.c.id == data["id"])
.values(project_id=project.id)
)
ruleset = await bsql.get_object(
db,
models.rulesets,
select_cols=[models.rulesets.c.id, models.rulesets.c.sources],
filters=(models.rulesets.c.rulebook_id == data["id"]),
)
assert ruleset is not None
assert ruleset.sources[0]["config"] is None
response = await client.get(f"/api/rulesets/{ruleset.id}")
resp_obj = response.json()
assert response.status_code == status_codes.HTTP_200_OK
assert resp_obj["sources"][0]["config"] is None


async def test_read_ruleset_not_found(client: AsyncClient):
response = await client.get("/api/rulesets/-1")
assert response.status_code == status_codes.HTTP_404_NOT_FOUND
Expand Down
36 changes: 34 additions & 2 deletions tests/integration/db/sql/rulebook/test_rulebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@
debug:
"""

TEST_RULESET_BAD = """
---
- name: Test bad
hosts: all
sources:
- blah:
rules:
- name: Test it is bad
condition: event.i == 1
action:
debug:
"""

DBTestData = namedtuple(
"DBTestData",
Expand Down Expand Up @@ -262,6 +275,23 @@ async def test_process_ruleset_sources(db: AsyncSession):
assert rule.name == "Test simple rule 1"


async def test_process_ruleset_sources_no_config(db: AsyncSession):
project = await insert_project(db)
rulebooks = await insert_rulebooks(db, project, TEST_RULESET_BAD)
assert len(rulebooks) == 2
rulebook_data = yaml.safe_load(rulebooks[1].rulesets)
await rsql.insert_rulebook_related_data(db, rulebooks[1].id, rulebook_data)
ruleset = await bsql.get_object(
db,
models.rulesets,
filters=models.rulesets.c.rulebook_id == rulebooks[1].id,
)
assert ruleset is not None
assert ruleset.name == "Test bad"
assert len(ruleset.sources) == 1
assert ruleset.sources[0]["config"] is None


# -------------------------------------------------------------------------
# Test Data
# -------------------------------------------------------------------------
Expand All @@ -284,7 +314,9 @@ async def insert_project(db: AsyncSession) -> sa.engine.Row:


async def insert_rulebooks(
db: AsyncSession, project: sa.engine.Row
db: AsyncSession,
project: sa.engine.Row,
rulesets: str = TEST_RULESET_SIMPLE,
) -> sa.engine.Row:
vals = [
{
Expand All @@ -295,7 +327,7 @@ async def insert_rulebooks(
{
"name": "rulebook-2",
"project_id": project.id,
"rulesets": TEST_RULESET_SIMPLE,
"rulesets": rulesets,
},
]
rulebooks = (
Expand Down

0 comments on commit 752604d

Please sign in to comment.