Skip to content

Commit aa3bab7

Browse files
committed
added test case for generating CSC and updated README.md
1 parent c8f793d commit aa3bab7

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,27 @@ class CustomStreamingCommand(StreamingCommand):
174174
record["odd_record"] = "true"
175175
yield record
176176
```
177+
### Customization for Generating Custom Search Command
178+
* Generating Custom Search Command is used to generate events using SDK code.
179+
* Make sure to use ``gen_record()`` method from SearchCommand to add a new record and pass event data as a key=value pair separated by , (mentioned in below example).
180+
181+
Do
182+
```python
183+
@Configuration()
184+
class GeneratorTest(GeneratingCommand):
185+
def generate(self):
186+
yield self.gen_record(_time=time.time(), one=1)
187+
yield self.gen_record(_time=time.time(), two=2)
188+
```
189+
190+
Don't
191+
```python
192+
@Configuration()
193+
class GeneratorTest(GeneratingCommand):
194+
def generate(self):
195+
yield {'_time': time.time(), 'one': 1}
196+
yield {'_time': time.time(), 'two': 2}
197+
```
177198

178199
### Changelog
179200

splunklib/searchcommands/generating_command.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,17 @@ def _execute_chunk_v2(self, process, chunk):
216216
records = []
217217
for row in process:
218218
records.append(row)
219-
count+=1
219+
count += 1
220220
if count == self._record_writer._maxresultrows:
221221
break
222222

223-
count = 0
224223
for row in records:
225224
self._record_writer.write_record(row)
226-
count += 1
227-
if count == self._record_writer._maxresultrows:
228-
self._finished = False
229-
return
230-
self._finished = True
225+
226+
if count == self._record_writer._maxresultrows:
227+
self._finished = False
228+
else:
229+
self._finished = True
231230

232231
def process(self, argv=sys.argv, ifile=sys.stdin, ofile=sys.stdout, allow_empty_input=True):
233232
""" Process data.

tests/searchcommands/test_generator_command.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def generate(self):
4040
assert expected.issubset(seen)
4141
assert finished_seen
4242

43-
4443
def test_allow_empty_input_for_generating_command():
4544
"""
4645
Passing allow_empty_input for generating command will cause an error
@@ -59,3 +58,29 @@ def generate(self):
5958
except ValueError as error:
6059
assert str(error) == "allow_empty_input cannot be False for Generating Commands"
6160

61+
def test_all_fieldnames_present_for_generated_records():
62+
@Configuration()
63+
class GeneratorTest(GeneratingCommand):
64+
def generate(self):
65+
yield self.gen_record(_time=time.time(), one=1)
66+
yield self.gen_record(_time=time.time(), two=2)
67+
yield self.gen_record(_time=time.time(), three=3)
68+
yield self.gen_record(_time=time.time(), four=4)
69+
yield self.gen_record(_time=time.time(), five=5)
70+
71+
generator = GeneratorTest()
72+
in_stream = io.BytesIO()
73+
in_stream.write(chunky.build_getinfo_chunk())
74+
in_stream.write(chunky.build_chunk({'action': 'execute'}))
75+
in_stream.seek(0)
76+
out_stream = io.BytesIO()
77+
generator._process_protocol_v2([], in_stream, out_stream)
78+
out_stream.seek(0)
79+
80+
ds = chunky.ChunkedDataStream(out_stream)
81+
fieldnames_expected = {'_time', 'one', 'two', 'three', 'four', 'five'}
82+
fieldnames_actual = set()
83+
for chunk in ds:
84+
for row in chunk.data:
85+
fieldnames_actual |= row.keys()
86+
assert fieldnames_expected.issubset(fieldnames_actual)

0 commit comments

Comments
 (0)