|
| 1 | +from insights.combiners.rsyslog_confs import RsyslogAllConf |
| 2 | +from insights.combiners import rsyslog_confs |
| 3 | +from insights.parsers.rsyslog_conf import RsyslogConf |
| 4 | +from insights.tests import context_wrap |
| 5 | +import doctest |
| 6 | + |
| 7 | + |
| 8 | +RSYSLOG_CONF_MAIN_7 = r""" |
| 9 | +$ModLoad imuxsock # provides support for local system logging (e.g. via logger command) |
| 10 | +$ModLoad imjournal # provides access to the systemd journal |
| 11 | +$WorkDirectory /var/lib/rsyslog |
| 12 | +$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat |
| 13 | +$IncludeConfig /etc/rsyslog.d/*.conf |
| 14 | +$OmitLocalLogging on |
| 15 | +$IMJournalStateFile imjournal.state |
| 16 | +*.info;mail.none;authpriv.none;cron.none /var/log/messages |
| 17 | +authpriv.* /var/log/secure |
| 18 | +mail.* -/var/log/maillog |
| 19 | +cron.* /var/log/cron |
| 20 | +*.emerg :omusrmsg:* |
| 21 | +uucp,news.crit /var/log/spooler |
| 22 | +local7.* /var/log/boot.log |
| 23 | +""".strip() |
| 24 | + |
| 25 | +RSYSLOG_CONF_MAIN_NO_INCLUDE_7 = r""" |
| 26 | +$ModLoad imuxsock # provides support for local system logging (e.g. via logger command) |
| 27 | +$ModLoad imjournal # provides access to the systemd journal |
| 28 | +$WorkDirectory /var/lib/rsyslog |
| 29 | +$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat |
| 30 | +$OmitLocalLogging on |
| 31 | +$IMJournalStateFile imjournal.state |
| 32 | +*.info;mail.none;authpriv.none;cron.none /var/log/messages |
| 33 | +authpriv.* /var/log/secure |
| 34 | +mail.* -/var/log/maillog |
| 35 | +cron.* /var/log/cron |
| 36 | +*.emerg :omusrmsg:* |
| 37 | +uucp,news.crit /var/log/spooler |
| 38 | +local7.* /var/log/boot.log |
| 39 | +""".strip() |
| 40 | + |
| 41 | +RSYSLOG_CONF_MAIN_CONF_DIR_7 = r""" |
| 42 | +$WorkDirectory /tmp |
| 43 | +*.info -/var/log/test.log |
| 44 | +""".strip() |
| 45 | + |
| 46 | +RSYSLOG_CONF_MAIN_8 = r""" |
| 47 | +module(load="imuxsock" # provides support for local system logging (e.g. via logger command) |
| 48 | + SysSock.Use="off") # Turn off message reception via local log socket; |
| 49 | +module(load="imjournal" # provides access to the systemd journal |
| 50 | + StateFile="imjournal.state") # File to store the position in the journal |
| 51 | +global(workDirectory="/var/lib/rsyslog") |
| 52 | +module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat") |
| 53 | +include(file="/etc/rsyslog.d/*.conf" mode="optional") |
| 54 | +*.info;mail.none;authpriv.none;cron.none /var/log/messages |
| 55 | +authpriv.* /var/log/secure |
| 56 | +mail.* -/var/log/maillog |
| 57 | +cron.* /var/log/cron |
| 58 | +if $programname == 'prog1' then { |
| 59 | + action(type="omfile" file="/var/log/prog1.log") |
| 60 | + if $msg contains 'test' then |
| 61 | + action(type="omfile" file="/var/log/prog1test.log") |
| 62 | + else |
| 63 | + action(type="omfile" file="/var/log/prog1notest.log") |
| 64 | +} |
| 65 | +""".strip() |
| 66 | + |
| 67 | +RSYSLOG_CONF_MAIN_NO_INCLUDE_8 = r""" |
| 68 | +module(load="imuxsock" # provides support for local system logging (e.g. via logger command) |
| 69 | + SysSock.Use="off") # Turn off message reception via local log socket; |
| 70 | +module(load="imjournal" # provides access to the systemd journal |
| 71 | + StateFile="imjournal.state") # File to store the position in the journal |
| 72 | +global(workDirectory="/var/lib/rsyslog") |
| 73 | +module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat") |
| 74 | +*.info;mail.none;authpriv.none;cron.none /var/log/messages |
| 75 | +authpriv.* /var/log/secure |
| 76 | +mail.* -/var/log/maillog |
| 77 | +cron.* /var/log/cron |
| 78 | +if $programname == 'prog1' then { |
| 79 | + action(type="omfile" file="/var/log/prog1.log") |
| 80 | + if $msg contains 'test' then |
| 81 | + action(type="omfile" file="/var/log/prog1test.log") |
| 82 | + else |
| 83 | + action(type="omfile" file="/var/log/prog1notest.log") |
| 84 | +} |
| 85 | +""".strip() |
| 86 | + |
| 87 | +RSYSLOG_CONF_MAIN_CONF_DIR_8 = r""" |
| 88 | +*.info { |
| 89 | + action( |
| 90 | + type="omfile" |
| 91 | + name="hehe" |
| 92 | + file="/tmp/test") |
| 93 | +} |
| 94 | +""".strip() |
| 95 | + |
| 96 | + |
| 97 | +def test_conf_7_include_dir(): |
| 98 | + rsyslog1 = RsyslogConf(context_wrap(RSYSLOG_CONF_MAIN_7, path="/etc/rsyslog.conf")) |
| 99 | + rsyslog2 = RsyslogConf(context_wrap(RSYSLOG_CONF_MAIN_CONF_DIR_7, path="/etc/rsyslog.d/test.conf")) |
| 100 | + result = RsyslogAllConf([rsyslog1, rsyslog2]) |
| 101 | + assert len(result) == 2 |
| 102 | + assert result['/etc/rsyslog.conf'][0] == '$ModLoad imuxsock' |
| 103 | + |
| 104 | + |
| 105 | +def test_conf_7_no_include_dir(): |
| 106 | + rsyslog1 = RsyslogConf(context_wrap(RSYSLOG_CONF_MAIN_NO_INCLUDE_7, path="/etc/rsyslog.conf")) |
| 107 | + rsyslog2 = RsyslogConf(context_wrap(RSYSLOG_CONF_MAIN_CONF_DIR_7, path="/etc/rsyslog.d/test.conf")) |
| 108 | + result = RsyslogAllConf([rsyslog1, rsyslog2]) |
| 109 | + assert len(result) == 1 |
| 110 | + assert result['/etc/rsyslog.conf'][0] == '$ModLoad imuxsock' |
| 111 | + |
| 112 | + |
| 113 | +def test_conf_8_include_dir(): |
| 114 | + rsyslog1 = RsyslogConf(context_wrap(RSYSLOG_CONF_MAIN_8, path="/etc/rsyslog.conf")) |
| 115 | + rsyslog2 = RsyslogConf(context_wrap(RSYSLOG_CONF_MAIN_CONF_DIR_8, path="/etc/rsyslog.d/test.conf")) |
| 116 | + result = RsyslogAllConf([rsyslog1, rsyslog2]) |
| 117 | + assert len(result) == 2 |
| 118 | + assert result['/etc/rsyslog.d/test.conf'] == ['*.info { action( type="omfile" name="hehe" file="/tmp/test") }'] |
| 119 | + |
| 120 | + |
| 121 | +def test_conf_8_no_include_dir(): |
| 122 | + rsyslog1 = RsyslogConf(context_wrap(RSYSLOG_CONF_MAIN_NO_INCLUDE_8, path="/etc/rsyslog.conf")) |
| 123 | + rsyslog2 = RsyslogConf(context_wrap(RSYSLOG_CONF_MAIN_CONF_DIR_8, path="/etc/rsyslog.d/test.conf")) |
| 124 | + result = RsyslogAllConf([rsyslog1, rsyslog2]) |
| 125 | + assert len(result) == 1 |
| 126 | + assert result['/etc/rsyslog.conf'][2] == 'global(workDirectory="/var/lib/rsyslog")' |
| 127 | + |
| 128 | + |
| 129 | +def test_rsyslog_confs_doc_examples(): |
| 130 | + env = { |
| 131 | + 'confs': RsyslogAllConf( |
| 132 | + [ |
| 133 | + RsyslogConf(context_wrap(RSYSLOG_CONF_MAIN_7, path='/etc/rsyslog.conf')), |
| 134 | + RsyslogConf(context_wrap(RSYSLOG_CONF_MAIN_CONF_DIR_7, path='/etc/rsyslog.d/test.conf')) |
| 135 | + ]) |
| 136 | + } |
| 137 | + failed, total = doctest.testmod(rsyslog_confs, globs=env) |
| 138 | + assert failed == 0 |
0 commit comments