Skip to content

Commit

Permalink
Testing query
Browse files Browse the repository at this point in the history
  • Loading branch information
Wh1isper committed Sep 15, 2023
1 parent 4d795c3 commit 335111c
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = duetector/cli/*
5 changes: 5 additions & 0 deletions duetector/analyzer/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from duetector.analyzer.base import Analyzer
from duetector.analyzer.models import AnalyzerBrief, Brief, Tracking
from duetector.db import SessionManager
from duetector.log import logger


class DBAnalyzer(Analyzer):
Expand Down Expand Up @@ -140,6 +141,7 @@ def query(
if order_by_desc:
statm = statm.order_by(*[getattr(m, k).desc() for k in order_by_desc])

logger.debug(f"Querying {tracer}@{collector_id} with statm: {statm}")
with self.sm.begin() as session:
r.extend(
[
Expand Down Expand Up @@ -192,6 +194,7 @@ def _table_brief(
m = self.sm.get_tracking_model(tracer, collector_id)

if not inspect:
logger.debug(f"Briefing {tracer}@{collector_id} without inspect")
return Brief(
tracer=tracer,
collector_id=collector_id,
Expand All @@ -209,6 +212,7 @@ def _table_brief(
start_statm = statm.order_by(m.dt.asc())
end_statm = statm.order_by(m.dt.desc())
count_statm = select(func.count()).select_from(statm.subquery())
logger.debug(f"Briefing {tracer}@{collector_id} with statm: {start_statm}")
with self.sm.begin() as session:
start_tracking = self._convert_row_to_tracking(
columns, session.execute(start_statm).first(), tracer
Expand Down Expand Up @@ -271,6 +275,7 @@ def brief(
Returns:
AnalyzerBrief: A brief of this analyzer.
"""

tables = self.sm.inspect_all_tables()
if tracers:
tables = [t for t in tables if self.sm.table_name_to_tracer(t) in tracers]
Expand Down
2 changes: 1 addition & 1 deletion duetector/service/query/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def root(
@r.post("/{analyzer_name}", response_model=QueryResult)
async def query(
analyzer_name: str,
query_param: QueryBody = Body(),
query_param: QueryBody = Body(default=QueryBody()),
controller: AnalyzerController = Depends(get_controller(AnalyzerController)),
):
"""
Expand Down
34 changes: 33 additions & 1 deletion tests/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

[filter]
disabled = false

include_extension = true

[filter.patternfilter]
disabled = false
Expand All @@ -31,21 +31,37 @@ re_exclude_gcustom = ["ignore_custom*"]

[tracer]
disabled = false
include_extension = true

[tracer.clonetracer]
disabled = false
attach_event = "__x64_sys_clone"
poll_timeout = 10

[tracer.tcpconnecttracer]
disabled = false
poll_timeout = 10

[tracer.unametracer]
disabled = false
enable_cache = true

[tracer.opentracer]
disabled = false
attach_event = "do_sys_openat2"
poll_timeout = 10

[collector]
disabled = false
include_extension = true

[collector.dbcollector]
disabled = false
id = "unittest"

[collector.dbcollector.backend_args]
max_workers = 10

[collector.dbcollector.db]
table_prefix = "duetector_tracking"

Expand All @@ -57,15 +73,31 @@ disabled = true
id = "unittest"
maxlen = 1024

[collector.dequecollector.backend_args]
max_workers = 10

[monitor.bcc]
disabled = false
auto_init = true
continue_on_exception = true

[monitor.bcc.backend_args]
max_workers = 10

[monitor.bcc.poller]
interval_ms = 500

[monitor.sh]
disabled = false
auto_init = true
timeout = 5

[monitor.sh.backend_args]
max_workers = 10

[monitor.sh.poller]
interval_ms = 500

[db_analyzer.db]
table_prefix = "duetector_tracking"

Expand Down
40 changes: 40 additions & 0 deletions tests/service/test_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest
from fastapi.testclient import TestClient

from duetector.analyzer.db import DBAnalyzer
from duetector.service.app import app
from duetector.service.config import get_config


@pytest.fixture
def configed_app(full_config):
app.dependency_overrides = {get_config: lambda: full_config}
return app


@pytest.fixture
def client(configed_app):
with TestClient(configed_app) as client:
yield client


def test_query(client: TestClient):
response = client.get(f"/query/")
assert response.status_code == 200
assert response.json() == {"analyzers": [DBAnalyzer.config_scope]}


def test_query_brief(client: TestClient):
response = client.get(f"/query/{DBAnalyzer.config_scope}/brief")
assert response.status_code == 200
assert response.json()


def test_query_analyzer(client: TestClient):
response = client.post(f"/query/{DBAnalyzer.config_scope}")
assert response.status_code == 200
assert response.json()


if __name__ == "__main__":
pytest.main(["-vv", "-s", __file__])
4 changes: 4 additions & 0 deletions tests/service/test_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ def client():
def test_root(client: TestClient):
response = client.get("/", params={"token": "test_token"})
assert response.status_code == 200


if __name__ == "__main__":
pytest.main(["-vv", "-s", __file__])
5 changes: 0 additions & 5 deletions tests/test_service.py

This file was deleted.

0 comments on commit 335111c

Please sign in to comment.