Skip to content

Commit 7723f8c

Browse files
committed
feat: update test instructions
1 parent 2fc909b commit 7723f8c

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

.agents/skills/parser-creator/references/testing-parsers.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
Below is a template for the unit test file:
1+
Below is an example for the unit test file:
22

33
```
4-
# Copyright <current-year> Splunk, Inc.
4+
# Copyright <current year> Splunk, Inc.
55
#
66
# Use of this source code is governed by a BSD-2-clause-style
77
# license that can be found in the LICENSE-BSD2 file or at
88
# https://opensource.org/licenses/BSD-2-Clause
99
import datetime
1010
import pytest
11+
import random
12+
import pytz
1113
1214
from jinja2 import Environment, select_autoescape
1315
1416
from .sendmessage import sendsingle
15-
from .splunkutils import splunk_single
17+
from .splunkutils import splunk_single
1618
from .timeutils import time_operations
1719
1820
env = Environment(autoescape=select_autoescape(default_for_string=False))
1921
2022
21-
@pytest.mark.addons("<addon-name>")
22-
def test_palo_alto_test_os_cef(
23-
record_property, setup_splunk, setup_sc4s, get_host_key
23+
@pytest.mark.addons("a10networks")
24+
def test_a10_vthunder(
25+
record_property, setup_splunk, setup_sc4s
2426
):
25-
host = get_host_key
2627
mt = env.from_string(
27-
"{{ mark }}{{ bsd }} {{ host }} <test-message>"
28+
"{{ mark }} CEF:0|A10|vThunder|4.1.4-GR1-P12|WAF|session-id|2|rt={{ bsd }} src=1.1.1.1 spt=34860 dst=1.1.1.1 dpt=80 dhost=test.host.local cs1=uiext_sec_waf cs2=1 act=learn cs3=learn app=HTTP requestMethod=GET cn1=0 request=/sales/ msg=New session created: Id\=1\n"
2829
)
29-
3030
dt = datetime.datetime.now(datetime.timezone.utc)
31-
_, bsd, _, _, _, _, epoch = time_operations(dt)
32-
message = mt.render(mark="<134>", bsd=bsd, host=host)
31+
_, _, _, _, _, _, epoch = time_operations(dt)
32+
message = mt.render(mark="<6>", bsd=dt.strftime("%b %d %Y %H:%M:%S"))
3333
3434
# Tune time functions
3535
epoch = epoch[:-7]
3636
sendsingle(message, setup_sc4s[0], setup_sc4s[1][514])
3737
st = env.from_string(
38-
f'search _time={epoch} index=netfw host="{host}" sourcetype="<sourcetype>"'
38+
f'search index=netwaf sourcetype="a10networks:vThunder:cef" earliest={epoch}'
3939
)
4040
search = st.render(epoch=epoch)
4141
@@ -45,6 +45,7 @@ def test_palo_alto_test_os_cef(
4545
record_property("message", message)
4646
4747
assert result_count == 1
48+
4849
```
4950

5051
When creating a unit test, pay close attention to time handling. You can use the `.timeutils` module to generate timestamps. The timestamp format you generate should match the original event format. In most cases, start by getting the current UTC time:
@@ -70,4 +71,15 @@ mt = env.from_string(
7071

7172
the timestamp is part of the `rt` field.
7273

74+
When creating tests for CEF messages, pay attention to the message format. There are two possibilities:
75+
- Provided samples have pri marking, BSD date, and hostname. This means they use BSD syslog format as transport for CEF-formatted log messages. In this case, remember that the time in the search has to be truncated only to seconds precision. Also replace the time and hostname in the original log with the generated values, e.g.:
76+
```
77+
Dec 6 16:52:22 IP Dec 06 16:51:38 hostname CEF:0|JATP|Cortex|3.6.0.1444|email|Phishing|8|externalId=1504 ...
78+
```
79+
to
80+
```
81+
{{ bsd }} {{ host }} CEF:0|JATP|Cortex|3.6.0.1444|email|Phishing|8|externalId=1504 ...
82+
```
83+
84+
- Provided samples don't have pri marking. In this case, the user samples should have an rt CEF field; if it's missing, don't create the test and explain the reason. Put the date into this field. Use the same format as the original message.
7385
Always use the full event in the test; do not truncate it. If the user provides multiple events (fewer than 10), use all of them in the tests (parameterize the test).

0 commit comments

Comments
 (0)