Ejemplo n.º 1
0
    def test_add(self, header, payload_length):
        payload = payload_length * b"\0"

        message = header + payload
        assert isinstance(message, bytes)
        assert len(message) == len(header) + len(payload)
        assert FetcherHeader.from_bytes(message) == header
        assert FetcherHeader.from_bytes(message[:len(header)]) == header
        assert message[len(header):] == payload
Ejemplo n.º 2
0
    def test_result_answer(self, count):
        fetcher_payload = AgentResultMessage(AgentRawData(69 * b"\xff"))
        fetcher_stats = ResultStats(Snapshot.null())
        fetcher_message = FetcherMessage(
            FetcherHeader(
                FetcherType.TCP,
                PayloadType.AGENT,
                status=42,
                payload_length=len(fetcher_payload),
                stats_length=len(fetcher_stats),
            ),
            fetcher_payload,
            fetcher_stats,
        )
        fetcher_messages = list(repeat(fetcher_message, count))
        timeout = 7

        message = CMCMessage.result_answer(fetcher_messages, timeout,
                                           Snapshot.null())
        assert isinstance(repr(message), str)
        assert CMCMessage.from_bytes(bytes(message)) == message
        assert message.header.name == "fetch"
        assert message.header.state == CMCHeader.State.RESULT
        assert message.header.log_level.strip() == ""
        assert message.header.payload_length == len(message) - len(
            message.header)
        assert message.header.payload_length == len(message.payload)
Ejemplo n.º 3
0
 def header(self, stats):
     return FetcherHeader(
         FetcherType.TCP,
         PayloadType.AGENT,
         status=42,
         payload_length=69,
         stats_length=len(stats),
     )
Ejemplo n.º 4
0
 def header(self, fetcher_type, payload_type, status, payload_length, stats_length):
     return FetcherHeader(
         fetcher_type,
         payload_type,
         status=status,
         payload_length=payload_length,
         stats_length=stats_length,
     )
Ejemplo n.º 5
0
    def test_neq_other_payload_length(self, header, payload_length):
        other = payload_length + 1
        assert other != header.payload_length

        assert header != FetcherHeader(
            header.fetcher_type,
            header.payload_type,
            status=header.status,
            payload_length=other,
            stats_length=header.stats_length,
        )
Ejemplo n.º 6
0
    def test_neq_other_status(self, header, status):
        other = status + 1
        assert other != header.status

        assert header != FetcherHeader(
            header.fetcher_type,
            header.payload_type,
            status=other,
            payload_length=header.payload_length,
            stats_length=header.stats_length,
        )
Ejemplo n.º 7
0
    def test_neq_other_result_type(self, header):
        other = PayloadType.ERROR
        assert other != header.payload_type

        assert header != FetcherHeader(
            header.fetcher_type,
            other,
            status=header.status,
            payload_length=header.payload_length,
            stats_length=header.stats_length,
        )
Ejemplo n.º 8
0
    def test_neq_other_payload_type(self, header):
        other = FetcherType.TCP
        assert other != header.payload_type

        assert header != FetcherHeader(
            other,
            payload_type=header.payload_type,
            status=header.status,
            payload_length=header.payload_length,
            stats_length=header.stats_length,
        )
Ejemplo n.º 9
0
 def test_eq(self, header, fetcher_type, payload_type, status, payload_length, stats_length):
     assert header == bytes(header)
     assert bytes(header) == header
     assert bytes(header) == bytes(header)
     assert header == FetcherHeader(
         fetcher_type,
         payload_type,
         status=status,
         payload_length=payload_length,
         stats_length=stats_length,
     )
Ejemplo n.º 10
0
 def fetcher_message(self, fetcher_payload, fetcher_stats):
     return FetcherMessage(
         FetcherHeader(
             FetcherType.TCP,
             PayloadType.AGENT,
             status=42,
             payload_length=len(fetcher_payload),
             stats_length=len(fetcher_stats),
         ),
         fetcher_payload,
         fetcher_stats,
     )
Ejemplo n.º 11
0
 def messages(self):
     msg = []
     for payload, stats in (
         (AgentResultMessage(AgentRawData(42 * b"\0")), ResultStats(Snapshot.null())),
         (AgentResultMessage(AgentRawData(12 * b"\0")), ResultStats(Snapshot.null())),
     ):
         msg.append(
             FetcherMessage(
                 FetcherHeader(
                     FetcherType.TCP,
                     PayloadType.AGENT,
                     status=69,
                     payload_length=len(payload),
                     stats_length=len(stats),
                 ),
                 payload,
                 stats,
             )
         )
     return msg
Ejemplo n.º 12
0
    def test_serialization(self, count):
        fetcher_payload = AgentResultMessage(AgentRawData(69 * b"\xff"))
        fetcher_stats = ResultStats(Snapshot.null())
        fetcher_message = FetcherMessage(
            FetcherHeader(
                FetcherType.TCP,
                PayloadType.AGENT,
                status=42,
                payload_length=len(fetcher_payload),
                stats_length=len(fetcher_stats),
            ),
            fetcher_payload,
            fetcher_stats,
        )
        fetcher_messages = list(repeat(fetcher_message, count))
        serial = 1337
        host_name = "my_host_name"
        timeout = 7

        message = CMCMessage.result_answer(
            fetcher_messages,
            serial=serial,
            host_name=host_name,
            timeout=timeout,
            duration=Snapshot.null(),
        )

        other = CMCMessage.from_bytes(bytes(message))
        assert other == message
        assert isinstance(repr(other), str)
        assert other.header.name == "fetch"
        assert other.header.state == CMCHeader.State.RESULT
        assert other.header.log_level.strip() == ""
        assert other.header.payload_length == len(message) - len(
            message.header)
        assert other.header.payload_length == len(message.payload)
        assert isinstance(other.payload, CMCResults)
        assert other.payload.id_.serial == serial
        assert other.payload.id_.host_name == host_name
        assert other.payload.stats.timeout == timeout
        assert other.payload.stats.duration == Snapshot.null()
Ejemplo n.º 13
0
 def test_from_bytes_failure(self):
     with pytest.raises(ValueError):
         FetcherHeader.from_bytes(b"random bytes")
Ejemplo n.º 14
0
 def test_from_bytes_success(self, header):
     assert FetcherHeader.from_bytes(bytes(header) + 42 * b"*") == header