1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#!/usr/bin/env python
#############################################################################
# Copyright (c) 2023 Attila Szakacs
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# As an additional exemption you are allowed to compile & link against the
# OpenSSL libraries as published by the OpenSSL project. See the file
# COPYING for details.
#
#############################################################################
def test_python_custom_options(config, syslog_ng):
raw_config = r"""
python {
import syslogng
def assert_options(options):
assert options["string"] == "example_string", "Actual: {}".format(options["string"])
assert options["true"] == True, "Actual: {}".format(options["true"])
assert options["false"] == False, "Actual: {}".format(options["false"])
assert options["long"] == 123456789, "Actual: {}".format(options["long"])
assert options["double"] == 123.456789, "Actual: {}".format(options["double"])
assert options["string_list"] == ["string1", "string2"], "Actual: {}".format(options["string_list"])
assert isinstance(options["template"], syslogng.LogTemplate), "Actual: {}".format(type(options["template"]))
assert str(options["template"]) == "${template}", "Actual: {}".format(str(options["template"]))
class TestSource(syslogng.LogSource):
def init(self, options):
assert_options(options)
return True
class TestFetcher(syslogng.LogFetcher):
def init(self, options):
assert_options(options)
return True
class TestDestination(syslogng.LogDestination):
def init(self, options):
assert_options(options)
return True
class TestParser(syslogng.LogParser):
def init(self, options):
assert_options(options)
return True
class TestHttpHeader:
def __init__(self, options):
assert_options(options)
def get_headers():
pass
def on_http_response_received():
pass
def test_confgen(options):
return '''
python(
class("TestDestination")
options(
"string" => {}
"true" => {}
"false" => {}
"long" => {}
"double" => {}
"string-list" => [{}]
"template" => LogTemplate({})
)
persist-name("custom-persist-name")
);
'''.format(options["string"], options["true"], options["false"],
options["long"], options["double"], options["string_list"],
options["template"])
syslogng.register_config_generator(context="destination", name="test_confgen",
config_generator=test_confgen)
};
log {
source {
python(
class("TestSource")
options(
"string" => "example_string"
"true" => True
"false" => False
"long" => 123456789
"double" => 123.456789
"string-list" => ["string1", "string2"]
"template" => LogTemplate("${template}")
)
);
python-fetcher(
class("TestFetcher")
options(
"string" => "example_string"
"true" => True
"false" => False
"long" => 123456789
"double" => 123.456789
"string-list" => ["string1", "string2"]
"template" => LogTemplate("${template}")
)
);
};
parser {
python(
class("TestParser")
options(
"string" => "example_string"
"true" => True
"false" => False
"long" => 123456789
"double" => 123.456789
"string-list" => ["string1", "string2"]
"template" => LogTemplate("${template}")
)
);
};
destination {
python(
class("TestDestination")
options(
"string" => "example_string"
"true" => True
"false" => False
"long" => 123456789
"double" => 123.456789
"string-list" => ["string1", "string2"]
"template" => LogTemplate("${template}")
)
);
test-confgen(
string("example_string")
true(yes)
false(no)
long(123456789)
double(123.456789)
string-list("string1", "string2")
template("${template}")
);
};
};
"""
raw_config = "@version: {}\n".format(config.get_version()) + raw_config
config.set_raw_config(raw_config)
syslog_ng.start(config)
|