tempesta-tech / tempesta-test

Test suite for Tempesta FW
10 stars 4 forks source link

Use helpers/asserts.py layer for all potentially reused asserts #519

Open voodam opened 9 months ago

voodam commented 9 months ago

Problem: When we want to use some custom assert method in several tests, we don't have place where to place it (except TempestaTest, FunctionalTest or something).

Proposal: Create and use separate layer where we place asserts (helpers/asserts.py) - just set of methods added to every testcase where we need them. It can be separate functions, composed classes or something, but I suggest to use mixins just for brevity:

class Sniffer:
...

    def assert_reset_socks(self, packets):
        rst_packet_dports = {p[TCP].dport for p in packets if p[TCP].flags & RST}
        self.assertTrue(
            self.must_rst_dports.issubset(rst_packet_dports),
            f"Ports must be reset: {self.must_rst_dports}, but the actual state is: {rst_packet_dports}",
        )

    def assert_unreset_socks(self, packets):
        rst_packet_dports = {p[TCP].dport for p in packets if p[TCP].flags & RST}
        self.assertFalse(
            rst_packet_dports.intersection(self.must_not_rst_dports),
            f"Ports mustn't be reset: {self.must_not_rst_dports}, but the actual state is: {rst_packet_dports}",
        )

...
from helpers import asserts
class FrangIpBlockBase(FrangTestCase, asserts.Sniffer, base=True):
...
    def test_on(self):
...
        self.assert_reset_socks(self.sniffer.packets)
        self.assert_unreset_socks(self.sniffer.packets)