Ejemplo n.º 1
0
class TestStructuredData:
    @pytest.mark.parametrize(
        "structured_data, expected_result",
        [
            pytest.param(
                StructuredData({}),
                "-",
                id="no data (NILVALUE)",
            ),
            pytest.param(
                StructuredData({
                    StructuredDataID("exampleSDID@32473"):
                    StructuredDataParameters({}),
                }),
                "[exampleSDID@32473]",
                id="one element with id only",
            ),
            pytest.param(
                StructuredData({
                    StructuredDataID("Checkmk@18662"):
                    StructuredDataParameters({
                        StructuredDataName("sl"):
                        StructuredDataValue("20"),
                        StructuredDataName("ipaddress"):
                        StructuredDataValue("127.0.0.1"),
                    }),
                }),
                '[Checkmk@18662 sl="20" ipaddress="127.0.0.1"]',
                id="one element with id and data",
            ),
            pytest.param(
                StructuredData({
                    StructuredDataID("examplePriority@32473"):
                    StructuredDataParameters({}),
                    StructuredDataID("Checkmk@18662"):
                    StructuredDataParameters({
                        StructuredDataName("sl"):
                        StructuredDataValue("20"),
                        StructuredDataName("ipaddress"):
                        StructuredDataValue("127.0.0.1"),
                    }),
                    StructuredDataID("whatever"):
                    StructuredDataParameters({
                        StructuredDataName("abc"):
                        StructuredDataValue('x"yz'),
                        StructuredDataName("hurz"):
                        StructuredDataValue("bärz"),
                    }),
                }),
                r'[examplePriority@32473][Checkmk@18662 sl="20" ipaddress="127.0.0.1"][whatever abc="x\"yz" hurz="bärz"]',
                id="multiple elements",
            ),
        ],
    )
    def test_repr(
        self,
        structured_data: StructuredData,
        expected_result: str,
    ) -> None:
        assert repr(structured_data) == expected_result
Ejemplo n.º 2
0
 def test_validate(
     self,
     id_: str,
     expected_result: bool,
 ) -> None:
     assert StructuredDataID._validate(id_) is expected_result
Ejemplo n.º 3
0
 def test_repr(self) -> None:
     assert repr(StructuredDataID("enterprise@123")) == "enterprise@123"
Ejemplo n.º 4
0
 def test_validation_on_init(self) -> None:
     with pytest.raises(ValueError):
         StructuredDataID("a@b")
Ejemplo n.º 5
0
class TestSyslogMessage:
    @pytest.mark.parametrize(
        "facility, severity, stuctured_data",
        [
            pytest.param(
                30,
                2,
                StructuredData({}),
                id="invalid facility",
            ),
            pytest.param(
                3,
                9,
                StructuredData({}),
                id="invalid severity",
            ),
            pytest.param(
                4,
                5,
                StructuredData({
                    StructuredDataID("Checkmk@18662"):
                    StructuredDataParameters({}),
                }),
                id="invalid structured data",
            ),
        ],
    )
    def test_init_validation(
        self,
        facility: int,
        severity: int,
        stuctured_data: StructuredData,
    ) -> None:
        with pytest.raises(ValueError):
            SyslogMessage(
                facility=facility,
                severity=severity,
                structured_data=stuctured_data,
            )

    @pytest.mark.parametrize(
        "syslog_message, expected_result",
        [
            pytest.param(
                SyslogMessage(
                    facility=1,
                    severity=2,
                ),
                "<10>1 - - - - - [Checkmk@18662]",
                id="minimal case",
            ),
            pytest.param(
                SyslogMessage(
                    facility=1,
                    severity=2,
                    timestamp=1617864437,
                    host_name="herbert",
                    application="some_deamon",
                    proc_id="procid",
                    msg_id="msgid",
                    structured_data=StructuredData({
                        StructuredDataID("exampleSDID@32473"):
                        StructuredDataParameters({
                            StructuredDataName("iut"):
                            StructuredDataValue("3"),
                            StructuredDataName("eventSource"):
                            StructuredDataValue("Application"),
                        }),
                    }),
                    text="something is wrong with herbert",
                ),
                '<10>1 2021-04-08T06:47:17+00:00 herbert some_deamon procid msgid [exampleSDID@32473 iut="3" eventSource="Application"][Checkmk@18662] something is wrong with herbert',
                id="standard case, ascii",
            ),
            pytest.param(
                SyslogMessage(
                    facility=1,
                    severity=2,
                    timestamp=1617864437,
                    host_name="herbert",
                    application="some_deamon",
                    proc_id="procid",
                    msg_id="msgid",
                    structured_data=StructuredData({
                        StructuredDataID("exampleSDID@32473"):
                        StructuredDataParameters({
                            StructuredDataName("iut"):
                            StructuredDataValue("3"),
                            StructuredDataName("eventSource"):
                            StructuredDataValue("Application"),
                        }),
                    }),
                    text="something is wrong with herbert",
                    ip_address="127.0.0.2",
                    service_level=13,
                ),
                '<10>1 2021-04-08T06:47:17+00:00 herbert some_deamon procid msgid [exampleSDID@32473 iut="3" eventSource="Application"][Checkmk@18662 ipaddress="127.0.0.2" sl="13"] something is wrong with herbert',
                id="with ip address and service level, ascii",
            ),
            pytest.param(
                SyslogMessage(
                    facility=1,
                    severity=2,
                    timestamp=1617864437,
                    host_name="herbert",
                    application="some_deamon",
                    proc_id="procid",
                    msg_id="msgid",
                    structured_data=StructuredData({
                        StructuredDataID("exampleSDID@32473"):
                        StructuredDataParameters({
                            StructuredDataName("iut"):
                            StructuredDataValue("3"),
                            StructuredDataName("eventSource"):
                            StructuredDataValue("Application"),
                        }),
                    }),
                    text="something is wrong with härbört",
                    ip_address="127.0.0.2",
                    service_level=13,
                ),
                '<10>1 2021-04-08T06:47:17+00:00 herbert some_deamon procid msgid [exampleSDID@32473 iut="3" eventSource="Application"][Checkmk@18662 ipaddress="127.0.0.2" sl="13"] \ufeffsomething is wrong with härbört',
                id="with ip address and service level, utf-8",
            ),
            pytest.param(
                SyslogMessage(
                    facility=1,
                    severity=2,
                    timestamp=1617864437.23,
                    host_name="herbert härry",
                    application="somé_deamon",
                    proc_id="ìd",
                    msg_id="mässage",
                    structured_data=StructuredData({
                        StructuredDataID("exampleSDID@32473"):
                        StructuredDataParameters({
                            StructuredDataName("iut"):
                            StructuredDataValue("3"),
                            StructuredDataName("eventSource"):
                            StructuredDataValue("Application"),
                        }),
                        StructuredDataID("needEscaping@123"):
                        StructuredDataParameters({
                            StructuredDataName("escapeMe"):
                            StructuredDataValue(r'"abc\]'),
                        }),
                    }),
                    text="something is wrong with he\ufeffrbert härry",
                    ip_address="1.2.3.4",
                    service_level=20,
                ),
                r'<10>1 2021-04-08T06:47:17.230000+00:00 - - - - [exampleSDID@32473 iut="3" eventSource="Application"][needEscaping@123 escapeMe="\"abc\\\]"][Checkmk@18662 ipaddress="1.2.3.4" sl="20" host="herbert härry" application="somé_deamon" pid="ìd" msg_id="mässage"]'
                + " \ufeffsomething is wrong with he\ufeffrbert härry",
                id="nothing rfc conform",
            ),
        ],
    )
    def test_repr(
        self,
        syslog_message: SyslogMessage,
        expected_result: str,
    ) -> None:
        assert str(syslog_message) == expected_result