Skip to content

Commit 8b4ca37

Browse files
committed
update unit tests
1 parent 7783412 commit 8b4ca37

7 files changed

Lines changed: 67 additions & 30 deletions

File tree

conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import sys
1111
import inspect
12+
import importlib
1213
from multiprocessing import Queue
1314
from unittest.mock import patch
1415
from slips_files.core.database.database_manager import DBManager
@@ -23,6 +24,11 @@
2324
parent_dir = os.path.dirname(current_dir)
2425
sys.path.insert(0, parent_dir)
2526

27+
# Keep legacy test imports working after the fides package rename.
28+
sys.modules.setdefault(
29+
"modules.fides_module", importlib.import_module("modules.fides")
30+
)
31+
2632

2733
# Suppress TensorFlow logs from C++ backend
2834
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" # 3 = ERROR

tests/integration/test_zeek_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
"dataset/test9-mixed-zeek-dir/conn.log",
125125
4,
126126
"non-HTTP established connection to port 80. "
127-
"destination IP: 194.132.197.198", # the flows with uid
127+
"destination IP: 194.132.197.198", # the flow with uid
128128
# CAwUdr34dVnyOwbUuj should trigger this
129129
"test9-conn_log_only/",
130130
6659,

tests/unit/managers/test_slips.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def test_load_modules():
1010
proc_manager.modules_to_ignore = [
1111
"template",
1212
"mldetection-1",
13+
"fides",
1314
]
1415
failed_to_load_modules = proc_manager.get_modules()[1]
1516
assert failed_to_load_modules == 0

tests/unit/slips_files/common/test_output_paths.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
from slips_files.common.output_paths import (
44
DATABASES_DIRNAME,
5-
get_output_databases_dir,
6-
get_output_sqlite_path,
5+
get_databases_dir_path_inside_output_dir,
6+
get_this_db_path_inside_output_dir,
77
)
88
from tests.module_factory import ModuleFactory
99

1010

11-
def test_get_output_databases_dir_creates_directory(tmp_path):
11+
def test_get_databases_dir_path_inside_output_dir_creates_directory(tmp_path):
1212
"""The databases helper should create and return the output databases directory."""
1313
module_factory = ModuleFactory()
1414
assert module_factory is not None
1515

16-
databases_dir = get_output_databases_dir(str(tmp_path / "output"))
16+
databases_dir = get_databases_dir_path_inside_output_dir(
17+
str(tmp_path / "output")
18+
)
1719

1820
assert databases_dir.endswith(DATABASES_DIRNAME)
1921
assert (tmp_path / "output" / DATABASES_DIRNAME).is_dir()
@@ -24,6 +26,8 @@ def test_get_output_sqlite_path_joins_filename_under_databases_dir(tmp_path):
2426
module_factory = ModuleFactory()
2527
assert module_factory is not None
2628

27-
sqlite_path = get_output_sqlite_path(str(tmp_path / "output"), "test.db")
29+
sqlite_path = get_this_db_path_inside_output_dir(
30+
str(tmp_path / "output"), "test.db"
31+
)
2832

2933
assert sqlite_path.endswith(f"{DATABASES_DIRNAME}/test.db")

tests/unit/slips_files/core/database/test_database.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111

1212
from slips_files.core.flows.zeek import Conn
13+
from slips_files.core.database.database_manager import DBManager
1314
from slips_files.core.database.redis_db.database import RedisDB
1415
from tests.module_factory import ModuleFactory
1516

@@ -163,44 +164,33 @@ def test_setup_config_file_uses_isolated_path_and_preserves_save(
163164
)
164165

165166
monkeypatch.setattr(RedisDB, "_conf_file_template", str(template))
166-
monkeypatch.setattr(RedisDB, "output_dir", tmp_path)
167-
monkeypatch.setattr(RedisDB, "redis_port", 6379)
168-
monkeypatch.setattr(RedisDB, "args", Mock(save=False))
167+
monkeypatch.setattr(RedisDB, "output_dir", tmp_path, raising=False)
168+
monkeypatch.setattr(RedisDB, "redis_port", 6379, raising=False)
169+
monkeypatch.setattr(RedisDB, "args", Mock(save=False), raising=False)
169170

170171
RedisDB._setup_config_file()
171172

172173
expected_conf = (
173-
tmp_path / f"redis-server-port-{RedisDB.redis_port}-{os.getpid()}.conf"
174+
tmp_path / "redis" / f"redis-server-port-{RedisDB.redis_port}.conf"
174175
)
175176
assert RedisDB._conf_file == str(expected_conf)
176177

177178
conf_contents = expected_conf.read_text(encoding="utf-8").splitlines()
178179
assert 'save ""' in conf_contents
179180
assert (
180-
f"logfile {tmp_path / f'redis-server-port-{RedisDB.redis_port}.log'}"
181+
f"logfile {tmp_path / 'redis' / f'redis-server-port-{RedisDB.redis_port}.log'}"
181182
in conf_contents
182183
)
183184

184185

185-
def test_get_permanent_database_path_uses_configured_dir(
186-
tmp_path, monkeypatch
187-
):
188-
db = ModuleFactory().create_db_manager_obj(6379)
189-
monkeypatch.chdir(tmp_path)
190-
db.conf.permanent_dir = Mock(return_value="persistent_state")
191-
192-
path = db.get_permanent_database_path("shared.sqlite")
193-
194-
assert path == os.path.join(
195-
"persistent_state", "databases", "shared.sqlite"
196-
)
197-
assert os.path.isdir(os.path.join("persistent_state", "databases"))
198-
199-
200186
def test_init_p2p_trust_db_uses_permanent_dir(tmp_path, monkeypatch):
201187
db = ModuleFactory().create_db_manager_obj(6379)
202188
monkeypatch.chdir(tmp_path)
203-
db.conf.permanent_dir = Mock(return_value="persistent_state")
189+
db.init_p2p_trust_db = DBManager.init_p2p_trust_db.__get__(db, DBManager)
190+
monkeypatch.setattr(
191+
"slips_files.core.database.database_manager.get_this_filepath_inside_permanent_dir",
192+
lambda filename: os.path.join("persistent_state", filename),
193+
)
204194

205195
db_path = db.init_p2p_trust_db()
206196

tests/unit/slips_files/core/test_evidence_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_start_evidence_worker(mock_worker_cls):
5555

5656
mock_worker_cls.assert_called_once_with(
5757
logger=handler.logger,
58-
output_dir=handler.output_dir,
58+
output_dir=handler.parent_output_dir,
5959
redis_port=handler.redis_port,
6060
termination_event=handler.termination_event,
6161
conf=handler.conf,

tests/unit/slips_files/core/test_profiler.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# SPDX-FileCopyrightText: 2021 Sebastian Garcia <sebastian.garcia@agents.fel.cvut.cz>
22
# SPDX-License-Identifier: GPL-2.0-only
3-
"""Unit test for slips_files/core/iperformance_profiler.py"""
3+
"""Unit tests for the profiler core process."""
44

55
from unittest.mock import Mock, patch
66

7-
from tests.module_factory import ModuleFactory
87
import pytest
8+
from tests.module_factory import ModuleFactory
99

1010

1111
def mock_print(*args, **kwargs):
@@ -136,3 +136,39 @@ def test_notify_observers_with_correct_message():
136136
test_msg = {"action": "test_action"}
137137
profiler.notify_observers(test_msg)
138138
observer_mock.update.assert_called_once_with(test_msg)
139+
140+
141+
@patch("slips_files.core.profiler.ProfilerWorker")
142+
def test_start_profiler_worker_uses_parent_output_dir(mock_worker_cls):
143+
profiler = ModuleFactory().create_profiler_obj()
144+
worker = mock_worker_cls.return_value
145+
profiler.profiler_child_processes = []
146+
profiler.workers = []
147+
profiler.profiler_queue = Mock()
148+
profiler.input_handler_obj = Mock()
149+
profiler.aid_queue = Mock()
150+
profiler.aid_manager = Mock()
151+
profiler.is_input_done_event = Mock()
152+
153+
profiler.start_profiler_worker(7)
154+
155+
mock_worker_cls.assert_called_once_with(
156+
logger=profiler.logger,
157+
output_dir=profiler.parent_output_dir,
158+
redis_port=profiler.redis_port,
159+
termination_event=profiler.termination_event,
160+
conf=profiler.conf,
161+
ppid=profiler.ppid,
162+
slips_args=profiler.args,
163+
bloom_filters_manager=profiler.bloom_filters,
164+
name="profiler_worker_process_7",
165+
profiler_queue=profiler.profiler_queue,
166+
input_handler=profiler.input_handler_obj,
167+
aid_queue=profiler.aid_queue,
168+
aid_manager=profiler.aid_manager,
169+
is_input_done_event=profiler.is_input_done_event,
170+
)
171+
worker.start.assert_called_once()
172+
assert profiler.profiler_child_processes == [worker]
173+
assert profiler.workers == []
174+
profiler.db.increment_profiler_workers_started.assert_called_once()

0 commit comments

Comments
 (0)