-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add test for jinja template test #177
Changes from all commits
95cadae
41d89f4
a2980bb
aa34db6
13a0c2f
e504541
c537b12
aa57e3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ def __init__(self, msg): | |
self.msg = msg | ||
super(CantFindTemplate, self).__init__(msg) | ||
|
||
|
||
class CantProcessTemplate(Exception): | ||
def __init__(self, msg): | ||
"""Exception raised when we / Jinja can't find the template | ||
|
@@ -188,30 +189,35 @@ def gen(self, count, earliest, latest, samplename=None): | |
startTime = datetime.datetime.now() | ||
|
||
# if eventgen is running as Splunk app the configfile is None | ||
if self.config.configfile: | ||
working_dir, working_config_file = os.path.split(self.config.configfile) | ||
else: | ||
sample_dir = self._sample.sampleDir | ||
if self._sample.splunkEmbedded is True: | ||
splunk_home = os.environ["SPLUNK_HOME"] | ||
app_name = getattr(self._sample, 'app', 'SA-Eventgen') | ||
working_dir = os.path.join(splunk_home, 'etc', 'apps', app_name, 'default') | ||
sample_dir = os.path.join(splunk_home, 'etc', 'apps', app_name, | ||
'default', self._sample.DEFAULT_SAMPLE_DIR) | ||
|
||
if not hasattr(self._sample, "jinja_template_dir"): | ||
template_dir = os.path.join(os.path.dirname(working_dir), 'samples', 'templates') | ||
template_dir = 'templates' | ||
else: | ||
template_dir = self._sample.jinja_template_dir | ||
|
||
if not os.path.isabs(template_dir): | ||
target_template_dir = os.path.join(working_dir, template_dir) | ||
target_template_dir = os.path.join(sample_dir, template_dir) | ||
else: | ||
target_template_dir = template_dir | ||
self.logger.info('set jinja template path to %s', target_template_dir) | ||
|
||
if not hasattr(self._sample, "jinja_target_template"): | ||
raise CantFindTemplate("Template to load not specified in eventgen conf for stanza. Skipping Stanza") | ||
jinja_env = Environment( | ||
loader=FileSystemLoader([target_template_dir, working_dir, template_dir], encoding='utf-8', | ||
followlinks=False), extensions=[ | ||
'jinja2.ext.do', 'jinja2.ext.with_', 'jinja2.ext.loopcontrols', JinjaTime], | ||
line_statement_prefix="#", line_comment_prefix="##") | ||
loader=FileSystemLoader( | ||
[target_template_dir], encoding='utf-8', followlinks=False), | ||
extensions=[ | ||
'jinja2.ext.do', 'jinja2.ext.with_', 'jinja2.ext.loopcontrols', | ||
JinjaTime | ||
], | ||
line_statement_prefix="#", | ||
line_comment_prefix="##") | ||
|
||
jinja_loaded_template = jinja_env.get_template(str(self._sample.jinja_target_template)) | ||
if hasattr(self._sample, 'jinja_variables'): | ||
|
@@ -238,8 +244,11 @@ def gen(self, count, earliest, latest, samplename=None): | |
self.jinja_stream = jinja_loaded_template.stream(jinja_loaded_vars) | ||
lines_out = [] | ||
try: | ||
for line in self.jinja_stream: | ||
if line != "\n": | ||
for raw_line in self.jinja_stream: | ||
# trim the newline char for jinja output | ||
# it is quite normal to output empty newlines in jinja | ||
line = raw_line.strip() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to trim the newline and empty lines for the output of jinja template.
skipping empty lines and breaking the loop is the root cause of #174 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how do I do multiline events then? We don't want to trim "every" "\n" just the ones where the only thing in the line is "\n". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the issue and get what you're trying to solve, but I don't think we can just use .strip(), gotta be another way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To generate multiline events in the ‘raw’ field in one json formatted events, use can do this in the jinja template directly. Just like this {% for _ in range(0, large_number) %}
{%- time_now -%}
{"_time":"{{ time_now_epoch }}", "_raw":"{{ time_now_formatted }}this is the first line, seq: {{loop.index}}/{{large_number}}\nthis is the second line, seq: {{loop.index}}/{{large_number}}\nthis is the thrid line, seq: {{loop.index}}/{{large_number}}"}
{% endfor %} the output of this jinja template is
Make sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should also say, are we sure we want to force people to write this single line, instead of letting them just hit return on the template? https://github.com/splunk/eventgen/blob/develop/tests/sample_eventgen_conf/jinja/templates/test_jinja_loop.template#L4. Specifically, doesn't it make sense to just hit return and put them on a new line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries. User can hit the enter key to make the new line directly in jinja template file. So, this template
the output is
|
||
if line: | ||
# TODO: Time can be supported by self._sample.timestamp, should probably set that up here. | ||
try: | ||
target_line = json.loads(line) | ||
|
@@ -268,8 +277,6 @@ def gen(self, count, earliest, latest, samplename=None): | |
if "index" not in current_line_keys: | ||
target_line["index"] = self._sample.index | ||
lines_out.append(target_line) | ||
else: | ||
break | ||
except TypeError as e: | ||
self.logger.exception(e) | ||
self.end_of_cycle = True | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
import logging | ||
import logging.handlers | ||
import random | ||
import os | ||
|
||
|
||
class ConfigRater(object): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this part is changed to get the sampleDir according to the conf file directory.