|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Syslog Property Filtering |
| 3 | +
|
| 4 | +Verify property-filter feature: filtering syslog messages based on various |
| 5 | +message properties with different operators, case-insensitivity, and negation. |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import infamy |
| 10 | +import time |
| 11 | + |
| 12 | +TEST_MESSAGES = [ |
| 13 | + ("myapp", "Application startup"), |
| 14 | + ("myapp", "Processing request"), |
| 15 | + ("otherapp", "Different program"), |
| 16 | + ("test", "ERROR: Connection failed"), |
| 17 | + ("test", "INFO: Normal message"), |
| 18 | + ("test", "WARNING: Check config"), |
| 19 | + ("test", "Warning: lowercase"), |
| 20 | +] |
| 21 | + |
| 22 | +with infamy.Test() as test: |
| 23 | + with test.step("Set up topology and attach to target DUT"): |
| 24 | + env = infamy.Env() |
| 25 | + target = env.attach("target", "mgmt") |
| 26 | + tgtssh = env.attach("target", "mgmt", "ssh") |
| 27 | + |
| 28 | + with test.step("Clean up old log files"): |
| 29 | + tgtssh.runsh("sudo rm -f /var/log/{myapp,not-error,case-test,baseline}") |
| 30 | + |
| 31 | + with test.step("Configure syslog with property filters"): |
| 32 | + target.put_config_dicts({ |
| 33 | + "ietf-syslog": { |
| 34 | + "syslog": { |
| 35 | + "actions": { |
| 36 | + "file": { |
| 37 | + "log-file": [{ |
| 38 | + "name": "file:myapp", |
| 39 | + "infix-syslog:property-filter": { |
| 40 | + "property": "programname", |
| 41 | + "operator": "isequal", |
| 42 | + "value": "myapp" |
| 43 | + }, |
| 44 | + "facility-filter": { |
| 45 | + "facility-list": [{ |
| 46 | + "facility": "all", |
| 47 | + "severity": "info" |
| 48 | + }] |
| 49 | + } |
| 50 | + }, { |
| 51 | + "name": "file:not-error", |
| 52 | + "infix-syslog:property-filter": { |
| 53 | + "property": "msg", |
| 54 | + "operator": "contains", |
| 55 | + "value": "ERROR", |
| 56 | + "negate": True |
| 57 | + }, |
| 58 | + "facility-filter": { |
| 59 | + "facility-list": [{ |
| 60 | + "facility": "all", |
| 61 | + "severity": "info" |
| 62 | + }] |
| 63 | + } |
| 64 | + }, { |
| 65 | + "name": "file:case-test", |
| 66 | + "infix-syslog:property-filter": { |
| 67 | + "property": "msg", |
| 68 | + "operator": "contains", |
| 69 | + "value": "warning", |
| 70 | + "case-insensitive": True |
| 71 | + }, |
| 72 | + "facility-filter": { |
| 73 | + "facility-list": [{ |
| 74 | + "facility": "all", |
| 75 | + "severity": "info" |
| 76 | + }] |
| 77 | + } |
| 78 | + }, { |
| 79 | + "name": "file:baseline", |
| 80 | + "facility-filter": { |
| 81 | + "facility-list": [{ |
| 82 | + "facility": "all", |
| 83 | + "severity": "info" |
| 84 | + }] |
| 85 | + } |
| 86 | + }] |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + }) |
| 92 | + |
| 93 | + time.sleep(2) |
| 94 | + |
| 95 | + with test.step("Send test messages"): |
| 96 | + for tag, msg in TEST_MESSAGES: |
| 97 | + tgtssh.runsh(f"logger -t {tag} -p daemon.info '{msg}'") |
| 98 | + time.sleep(1) |
| 99 | + |
| 100 | + with test.step("Verify myapp log contains only myapp messages"): |
| 101 | + rc = tgtssh.runsh("grep -c 'myapp' /var/log/myapp 2>/dev/null") |
| 102 | + count = int(rc.stdout.strip()) if rc.returncode == 0 else 0 |
| 103 | + if count != 2: |
| 104 | + test.fail(f"Expected 2 myapp messages in /var/log/myapp, got {count}") |
| 105 | + |
| 106 | + rc = tgtssh.runsh("grep -c 'otherapp' /var/log/myapp 2>/dev/null") |
| 107 | + count = int(rc.stdout.strip()) if rc.returncode == 0 else 0 |
| 108 | + if count != 0: |
| 109 | + test.fail(f"Expected 0 otherapp messages in /var/log/myapp, got {count}") |
| 110 | + |
| 111 | + with test.step("Verify not-error log excludes ERROR messages"): |
| 112 | + rc = tgtssh.runsh("grep -c 'test' /var/log/not-error 2>/dev/null") |
| 113 | + count = int(rc.stdout.strip()) if rc.returncode == 0 else 0 |
| 114 | + if count != 3: |
| 115 | + test.fail(f"Expected 3 non-ERROR messages in /var/log/not-error, got {count}") |
| 116 | + |
| 117 | + rc = tgtssh.runsh("grep -c 'ERROR' /var/log/not-error 2>/dev/null") |
| 118 | + count = int(rc.stdout.strip()) if rc.returncode == 0 else 0 |
| 119 | + if count != 0: |
| 120 | + test.fail(f"Expected 0 ERROR messages in /var/log/not-error, got {count}") |
| 121 | + |
| 122 | + with test.step("Verify case-test log matches case-insensitive 'warning'"): |
| 123 | + rc = tgtssh.runsh("grep -c 'WARNING\\|Warning' /var/log/case-test 2>/dev/null") |
| 124 | + count = int(rc.stdout.strip()) if rc.returncode == 0 else 0 |
| 125 | + if count != 2: |
| 126 | + test.fail(f"Expected 2 warning messages in /var/log/case-test, got {count}") |
| 127 | + |
| 128 | + with test.step("Verify baseline log contains all messages"): |
| 129 | + for tag, msg in TEST_MESSAGES: |
| 130 | + rc = tgtssh.runsh(f"grep -q '{msg}' /var/log/baseline 2>/dev/null") |
| 131 | + if rc.returncode != 0: |
| 132 | + test.fail(f"Expected message '{msg}' not found in /var/log/baseline") |
| 133 | + |
| 134 | + test.succeed() |
0 commit comments