Skip to content

Commit 007d2c9

Browse files
committed
Tests now check gRPC messages instead of metrics
1 parent 7b389c6 commit 007d2c9

2 files changed

Lines changed: 113 additions & 91 deletions

File tree

tests/event.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class EventType(Enum):
3636
PERMISSION = 4
3737
OWNERSHIP = 5
3838
RENAME = 6
39+
XATTR = 7
3940

4041

4142
class Process:
@@ -223,6 +224,7 @@ def __init__(
223224
owner_gid: int | None = None,
224225
old_file: str | Pattern[str] | None = None,
225226
old_host_path: str | Pattern[str] | None = None,
227+
xattr_name: str | None = None,
226228
):
227229
self._type: EventType = event_type
228230
self._process: Process = process
@@ -233,6 +235,7 @@ def __init__(
233235
self._owner_gid: int | None = owner_gid
234236
self._old_file: str | Pattern[str] | None = old_file
235237
self._old_host_path: str | Pattern[str] | None = old_host_path
238+
self._xattr_name: str | None = xattr_name
236239

237240
@property
238241
def event_type(self) -> EventType:
@@ -270,6 +273,10 @@ def old_file(self) -> str | Pattern[str] | None:
270273
def old_host_path(self) -> str | Pattern[str] | None:
271274
return self._old_host_path
272275

276+
@property
277+
def xattr_name(self) -> str | None:
278+
return self._xattr_name
279+
273280
@classmethod
274281
def _diff_field(cls, diff: dict, name: str, expected: Any, actual: Any):
275282
if expected != actual:
@@ -378,6 +385,13 @@ def diff(self, other: FileActivity) -> dict | None:
378385
self.owner_gid,
379386
event_field.gid,
380387
)
388+
elif self.event_type == EventType.XATTR:
389+
Event._diff_field(
390+
diff,
391+
'xattr_name',
392+
self.xattr_name,
393+
event_field.xattr_name,
394+
)
381395

382396
return diff if diff else None
383397

@@ -401,6 +415,9 @@ def __str__(self) -> str:
401415
f', old_host_path="{self.old_host_path}"'
402416
)
403417

418+
if self.event_type == EventType.XATTR:
419+
s += f', xattr_name="{self.xattr_name}"'
420+
404421
s += ')'
405422

406423
return s

tests/test_xattr.py

Lines changed: 96 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,127 +4,122 @@
44

55
from event import Event, EventType, Process
66
from server import FileActivityService
7-
from utils import get_metric_value
8-
9-
10-
def get_kernel_setxattr_added(fact_config: tuple[dict, str]):
11-
"""
12-
Query Prometheus metrics to get the count of setxattr events
13-
added to the ring buffer.
14-
15-
Args:
16-
fact_config: The fact configuration tuple
17-
(config dict, config file path).
18-
19-
Returns:
20-
The current value of
21-
kernel_inode_setxattr_events{label="Added"} metric.
22-
"""
23-
value = get_metric_value(
24-
fact_config,
25-
'kernel_inode_setxattr_events',
26-
{'label': 'Added'},
27-
)
28-
return int(value) if value is not None else 0
29-
30-
31-
def get_kernel_removexattr_added(fact_config: tuple[dict, str]):
32-
"""
33-
Query Prometheus metrics to get the count of removexattr events
34-
added to the ring buffer.
35-
36-
Args:
37-
fact_config: The fact configuration tuple
38-
(config dict, config file path).
39-
40-
Returns:
41-
The current value of
42-
kernel_inode_removexattr_events{label="Added"} metric.
43-
"""
44-
value = get_metric_value(
45-
fact_config,
46-
'kernel_inode_removexattr_events',
47-
{'label': 'Added'},
48-
)
49-
return int(value) if value is not None else 0
507

518

529
def test_setxattr(
5310
test_file: str,
54-
fact_config: tuple[dict, str],
11+
server: FileActivityService,
5512
):
5613
"""
57-
Tests that setting a user xattr on a monitored file is tracked
58-
via kernel metrics.
14+
Tests that setting a user xattr on a monitored file generates
15+
a gRPC xattr event.
5916
6017
The test_file fixture creates a file before fact starts, so it is
6118
picked up by the initial scan and its inode is already tracked.
6219
6320
Args:
6421
test_file: File monitored on the host.
65-
fact_config: The fact configuration.
22+
server: The server instance to communicate with.
6623
"""
67-
initial = get_kernel_setxattr_added(fact_config)
24+
process = Process.from_proc()
6825

6926
os.setxattr(test_file, 'user.fact_test', b'test_value')
7027

71-
final = get_kernel_setxattr_added(fact_config)
72-
delta = final - initial
73-
assert delta == 1, f'Expected exactly 1 setxattr event added, got {delta}'
28+
server.wait_events(
29+
[
30+
Event(
31+
process=process,
32+
event_type=EventType.XATTR,
33+
file='',
34+
host_path=test_file,
35+
xattr_name='user.fact_test',
36+
),
37+
],
38+
strict=False,
39+
)
7440

7541

7642
def test_removexattr(
7743
test_file: str,
78-
fact_config: tuple[dict, str],
44+
server: FileActivityService,
7945
):
8046
"""
81-
Tests that removing a user xattr from a monitored file is tracked
82-
via kernel metrics.
47+
Tests that removing a user xattr from a monitored file generates
48+
a gRPC xattr event.
8349
8450
Args:
8551
test_file: File monitored on the host.
86-
fact_config: The fact configuration.
52+
server: The server instance to communicate with.
8753
"""
88-
os.setxattr(test_file, 'user.fact_remove', b'to_remove')
89-
90-
initial = get_kernel_removexattr_added(fact_config)
54+
process = Process.from_proc()
9155

56+
os.setxattr(test_file, 'user.fact_remove', b'to_remove')
9257
os.removexattr(test_file, 'user.fact_remove')
9358

94-
final = get_kernel_removexattr_added(fact_config)
95-
delta = final - initial
96-
assert delta == 1, (
97-
f'Expected exactly 1 removexattr event added, got {delta}'
59+
server.wait_events(
60+
[
61+
Event(
62+
process=process,
63+
event_type=EventType.XATTR,
64+
file='',
65+
host_path=test_file,
66+
xattr_name='user.fact_remove',
67+
),
68+
],
69+
strict=False,
9870
)
9971

10072

10173
def test_setxattr_multiple(
10274
test_file: str,
103-
fact_config: tuple[dict, str],
75+
server: FileActivityService,
10476
):
10577
"""
106-
Tests that setting multiple xattrs on a monitored file tracks
107-
all of them.
78+
Tests that setting multiple xattrs on a monitored file generates
79+
a gRPC event for each.
10880
10981
Args:
11082
test_file: File monitored on the host.
111-
fact_config: The fact configuration.
83+
server: The server instance to communicate with.
11284
"""
113-
initial = get_kernel_setxattr_added(fact_config)
85+
process = Process.from_proc()
11486

11587
os.setxattr(test_file, 'user.attr1', b'value1')
11688
os.setxattr(test_file, 'user.attr2', b'value2')
11789
os.setxattr(test_file, 'user.attr3', b'value3')
11890

119-
final = get_kernel_setxattr_added(fact_config)
120-
delta = final - initial
121-
assert delta == 3, f'Expected exactly 3 setxattr events added, got {delta}'
91+
server.wait_events(
92+
[
93+
Event(
94+
process=process,
95+
event_type=EventType.XATTR,
96+
file='',
97+
host_path=test_file,
98+
xattr_name='user.attr1',
99+
),
100+
Event(
101+
process=process,
102+
event_type=EventType.XATTR,
103+
file='',
104+
host_path=test_file,
105+
xattr_name='user.attr2',
106+
),
107+
Event(
108+
process=process,
109+
event_type=EventType.XATTR,
110+
file='',
111+
host_path=test_file,
112+
xattr_name='user.attr3',
113+
),
114+
],
115+
strict=False,
116+
)
122117

123118

124119
def test_setxattr_ignored(
125120
test_file: str,
126121
ignored_dir: str,
127-
fact_config: tuple[dict, str],
122+
server: FileActivityService,
128123
):
129124
"""
130125
Tests that xattr changes on unmonitored files are not tracked,
@@ -133,34 +128,38 @@ def test_setxattr_ignored(
133128
Args:
134129
test_file: File monitored on the host.
135130
ignored_dir: Temporary directory that is not monitored by fact.
136-
fact_config: The fact configuration.
131+
server: The server instance to communicate with.
137132
"""
133+
process = Process.from_proc()
134+
138135
ignored_file = os.path.join(ignored_dir, 'ignored.txt')
139136
with open(ignored_file, 'w') as f:
140137
f.write('ignored')
141138

142-
initial = get_kernel_setxattr_added(fact_config)
143-
139+
# Set xattr on ignored file - should NOT generate an event
144140
os.setxattr(ignored_file, 'user.ignored', b'value')
145141

146-
after_ignored = get_kernel_setxattr_added(fact_config)
147-
assert after_ignored == initial, (
148-
'Setting xattr on ignored file should not increment Added metric'
149-
)
150-
142+
# Set xattr on monitored file - should generate an event
151143
os.setxattr(test_file, 'user.monitored', b'value')
152144

153-
final = get_kernel_setxattr_added(fact_config)
154-
delta = final - initial
155-
assert delta == 1, (
156-
f'Expected exactly 1 setxattr event (monitored file only), got {delta}'
145+
# Only the monitored file's xattr event should arrive
146+
server.wait_events(
147+
[
148+
Event(
149+
process=process,
150+
event_type=EventType.XATTR,
151+
file='',
152+
host_path=test_file,
153+
xattr_name='user.monitored',
154+
),
155+
],
156+
strict=False,
157157
)
158158

159159

160160
def test_setxattr_new_file(
161161
monitored_dir: str,
162162
server: FileActivityService,
163-
fact_config: tuple[dict, str],
164163
):
165164
"""
166165
Tests that xattr tracking works for files created while fact is
@@ -173,7 +172,6 @@ def test_setxattr_new_file(
173172
Args:
174173
monitored_dir: Temporary directory path that is monitored.
175174
server: The server instance to communicate with.
176-
fact_config: The fact configuration.
177175
"""
178176
process = Process.from_proc()
179177

@@ -189,13 +187,20 @@ def test_setxattr_new_file(
189187
file=test_file,
190188
host_path=test_file,
191189
),
192-
]
190+
],
193191
)
194192

195-
initial = get_kernel_setxattr_added(fact_config)
196-
197193
os.setxattr(test_file, 'user.new_file', b'value')
198194

199-
final = get_kernel_setxattr_added(fact_config)
200-
delta = final - initial
201-
assert delta == 1, f'Expected exactly 1 setxattr event added, got {delta}'
195+
server.wait_events(
196+
[
197+
Event(
198+
process=process,
199+
event_type=EventType.XATTR,
200+
file='',
201+
host_path=test_file,
202+
xattr_name='user.new_file',
203+
),
204+
],
205+
strict=False,
206+
)

0 commit comments

Comments
 (0)