diff --git a/tests/unit/test_class_inheritance/test_inheritance.py b/tests/unit/test_class_inheritance/test_inheritance.py index 025962814..04ccc27f7 100644 --- a/tests/unit/test_class_inheritance/test_inheritance.py +++ b/tests/unit/test_class_inheritance/test_inheritance.py @@ -1,14 +1,12 @@ """Testing class inheritance attributes changes.""" -import unittest - from pyof.foundation.base import GenericStruct from pyof.foundation.basic_types import UBInt8, UBInt16, UBInt32, UBInt64 -class TestInheritance(unittest.TestCase): +class TestInheritance: """Testing GenericStruct class inheritance.""" - def setUp(self): + def setup_method(self): """Basic Test Setup.""" class MyClassA(GenericStruct): """Example class.""" @@ -41,30 +39,30 @@ def test_modifications(self): m1 = self.MyClassA() m2 = self.MyClassB() # Checking keys (attributes names) and its ordering - self.assertEqual([attr[0] for attr in m1.get_class_attributes()], - self.a_expected_names) - self.assertEqual([attr[0] for attr in m2.get_class_attributes()], - self.b_expected_names) + assert [attr[0] for attr in m1.get_class_attributes()] == \ + self.a_expected_names + assert [attr[0] for attr in m2.get_class_attributes()] == \ + self.b_expected_names # Check if there is no shared attribute between instances - self.assertIsNot(m1, m2) - self.assertIsNot(m1.a1, m2.a1) - self.assertIsNot(m1.a2, m2.a2) - self.assertIsNot(m1.a3, m2.a4) - self.assertIsNot(m1.a4, m2.a4) - self.assertIsNot(m1.a5, m2.a5) + assert m1 is not m2 + assert m1.a1 is not m2.a1 + assert m1.a2 is not m2.a2 + assert m1.a3 is not m2.a4 + assert m1.a4 is not m2.a4 + assert m1.a5 is not m2.a5 # Check attributes types on MyClassA - self.assertIsInstance(self.MyClassA.a1, UBInt8) - self.assertIsInstance(self.MyClassA.a2, UBInt16) - self.assertIsInstance(self.MyClassA.a3, UBInt8) - self.assertIsInstance(self.MyClassA.a4, UBInt16) - self.assertIsInstance(self.MyClassA.a5, UBInt32) + assert isinstance(self.MyClassA.a1, UBInt8) + assert isinstance(self.MyClassA.a2, UBInt16) + assert isinstance(self.MyClassA.a3, UBInt8) + assert isinstance(self.MyClassA.a4, UBInt16) + assert isinstance(self.MyClassA.a5, UBInt32) # Check attributes types on MyClassA - self.assertIsInstance(self.MyClassB.a1, UBInt8) - self.assertIsInstance(self.MyClassB.a2, UBInt64) - self.assertIsInstance(self.MyClassB.a4, UBInt16) - self.assertIsInstance(self.MyClassB.a5, UBInt32) - self.assertIsInstance(self.MyClassB.a0, UBInt32) - self.assertIsInstance(self.MyClassB.b6, UBInt8) + assert isinstance(self.MyClassB.a1, UBInt8) + assert isinstance(self.MyClassB.a2, UBInt64) + assert isinstance(self.MyClassB.a4, UBInt16) + assert isinstance(self.MyClassB.a5, UBInt32) + assert isinstance(self.MyClassB.a0, UBInt32) + assert isinstance(self.MyClassB.b6, UBInt8) diff --git a/tests/unit/test_foundation/test_base.py b/tests/unit/test_foundation/test_base.py index 3d18f3daf..cff827035 100644 --- a/tests/unit/test_foundation/test_base.py +++ b/tests/unit/test_foundation/test_base.py @@ -1,13 +1,11 @@ """Test Base module of python-openflow.""" -import unittest - from pyof.foundation import base, basic_types -class TestGenericStruct(unittest.TestCase): +class TestGenericStruct: """Testing GenericStruct class.""" - def setUp(self): + def setup_method(self): """Basic Test Setup.""" class AttributeA(base.GenericStruct): """Example class.""" @@ -52,18 +50,18 @@ def test_basic_attributes(self): """[Foundation/Base/GenericStruct] - Attributes Creation.""" message1 = self.MyMessage() message2 = self.MyMessage() - self.assertIsNot(message1, message2) - self.assertIsNot(message1.i, message2.i) - self.assertIsNot(message1.a, message2.a) - self.assertIsNot(message1.b, message2.b) - self.assertIsNot(message1.a.a1, message2.a.a1) - self.assertIsNot(message1.a.a2, message2.a.a2) - self.assertIsNot(message1.b.c, message2.b.c) - self.assertIsNot(message1.b.c.c1, message2.b.c.c1) - self.assertIsNot(message1.b.c.c2, message2.b.c.c2) - - -class TestGenericType(unittest.TestCase): + assert message1 is not message2 + assert message1.i is not message2.i + assert message1.a is not message2.a + assert message1.b is not message2.b + assert message1.a.a1 is not message2.a.a1 + assert message1.a.a2 is not message2.a.a2 + assert message1.b.c is not message2.b.c + assert message1.b.c.c1 is not message2.b.c.c1 + assert message1.b.c.c2 is not message2.b.c.c2 + + +class TestGenericType: """Testing GenericType class.""" def test_basic_operator(self): @@ -71,27 +69,27 @@ def test_basic_operator(self): a = basic_types.UBInt32(1) b = basic_types.UBInt32(2) - self.assertEqual(a + 1, 2) - self.assertEqual(1 + a, 2) - self.assertEqual(b + 1, 3) - self.assertEqual(1 + b, 3) - - self.assertEqual(a - 1, 0) - self.assertEqual(1 - a, 0) - self.assertEqual(b - 1, 1) - self.assertEqual(1 - b, 1) - - self.assertEqual(a & 1, 1) - self.assertEqual(1 & a, 1) - self.assertEqual(b & 1, 0) - self.assertEqual(1 & b, 0) - - self.assertEqual(a | 1, 1) - self.assertEqual(1 | a, 1) - self.assertEqual(b | 1, 3) - self.assertEqual(1 | b, 3) - - self.assertEqual(a ^ 1, 0) - self.assertEqual(1 ^ a, 0) - self.assertEqual(b ^ 1, 3) - self.assertEqual(1 ^ b, 3) + assert a + 1 == 2 + assert 1 + a == 2 + assert b + 1 == 3 + assert 1 + b == 3 + + assert a - 1 == 0 + assert 1 - a == 0 + assert b - 1 == 1 + assert 1 - b == 1 + + assert a & 1 == 1 + assert 1 & a == 1 + assert b & 1 == 0 + assert 1 & b == 0 + + assert a | 1 == 1 + assert 1 | a == 1 + assert b | 1 == 3 + assert 1 | b == 3 + + assert a ^ 1 == 0 + assert 1 ^ a == 0 + assert b ^ 1 == 3 + assert 1 ^ b == 3 diff --git a/tests/unit/test_foundation/test_basic_types.py b/tests/unit/test_foundation/test_basic_types.py index 59a6b9c14..bd7034c55 100644 --- a/tests/unit/test_foundation/test_basic_types.py +++ b/tests/unit/test_foundation/test_basic_types.py @@ -1,106 +1,106 @@ """Tests for Python-openflow BasicTypes.""" -import unittest +import pytest from pyof.foundation import basic_types from pyof.foundation.exceptions import PackException from pyof.foundation.basic_types import BinaryData -class TestUBInt8(unittest.TestCase): +class TestUBInt8: """Test of UBInt8 BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.ubint8 = basic_types.UBInt8(255) def test_get_size(self): """[Foundation/BasicTypes/UBInt8] - size 1.""" - self.assertEqual(self.ubint8.get_size(), 1) + assert self.ubint8.get_size() == 1 def test_pack(self): """[Foundation/BasicTypes/UBInt8] - packing.""" - self.assertEqual(self.ubint8.pack(), b'\xff') + assert self.ubint8.pack() == b'\xff' def test_unpack(self): """[Foundation/BasicTypes/UBInt8] - unpacking.""" u = basic_types.UBInt8() u.unpack(b'\xfe') - self.assertEqual(u.value, 254) + assert u.value == 254 def test_pack_error(self): """[Foundation/BasicTypes/UBInt8] - packing exception.""" u = basic_types.UBInt8(256) - self.assertRaises(PackException, u.pack) + pytest.raises(PackException, u.pack) def test_cast_to_int(self): - self.assertEqual(255, int(self.ubint8)) + assert 255 == int(self.ubint8) -class TestUBInt16(unittest.TestCase): +class TestUBInt16: """Test of UBInt16 BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.ubint16 = basic_types.UBInt16() def test_get_size(self): """[Foundation/BasicTypes/UBInt16] - size 2.""" - self.assertEqual(self.ubint16.get_size(), 2) + assert self.ubint16.get_size() == 2 -class TestUBInt32(unittest.TestCase): +class TestUBInt32: """Test of UBInt32 BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.ubint32 = basic_types.UBInt32() def test_get_size(self): """[Foundation/BasicTypes/UBInt32] - size 4.""" - self.assertEqual(self.ubint32.get_size(), 4) + assert self.ubint32.get_size() == 4 -class TestUBInt64(unittest.TestCase): +class TestUBInt64: """Test of UBInt64 BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.ubint64 = basic_types.UBInt64() def test_get_size(self): """[Foundation/BasicTypes/UBInt64] - size 8.""" - self.assertEqual(self.ubint64.get_size(), 8) + assert self.ubint64.get_size() == 8 -class TestUBInt128(unittest.TestCase): +class TestUBInt128: """Test of UBInt128 BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.ubint128 = basic_types.UBInt128() def test_get_size(self): """[Foundation/BasicTypes/UBInt128] - size 16.""" - self.assertEqual(self.ubint128.get_size(), 16) + assert self.ubint128.get_size() == 16 -class TestChar(unittest.TestCase): +class TestChar: """Test of Char BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.char1 = basic_types.Char('foo', length=3) self.char2 = basic_types.Char('foo', length=5) def test_get_size(self): """[Foundation/BasicTypes/Char] - get_size.""" - self.assertEqual(self.char1.get_size(), 3) - self.assertEqual(self.char2.get_size(), 5) + assert self.char1.get_size() == 3 + assert self.char2.get_size() == 5 def test_pack(self): """[Foundation/BasicTypes/Char] - packing.""" - self.assertEqual(self.char1.pack(), b'fo\x00') - self.assertEqual(self.char2.pack(), b'foo\x00\x00') + assert self.char1.pack() == b'fo\x00' + assert self.char2.pack() == b'foo\x00\x00' def test_unpack(self): """[Foundation/BasicTypes/Char] - unpacking.""" @@ -109,11 +109,11 @@ def test_unpack(self): char1.unpack(b'fo\x00') char2.unpack(b'foo\x00\x00') - self.assertEqual(char1.value, 'fo') - self.assertEqual(char2.value, 'foo') + assert char1.value == 'fo' + assert char2.value == 'foo' -class TestHWAddress(unittest.TestCase): +class TestHWAddress: """Test of HWAddress BasicType.""" def test_unpack_packed(self): @@ -123,7 +123,7 @@ def test_unpack_packed(self): packed = hw_addr.pack() unpacked = basic_types.HWAddress() unpacked.unpack(packed) - self.assertEqual(mac, unpacked.value) + assert mac == unpacked.value def test_default_value(self): """Testing default_value for HWAddress.""" @@ -132,10 +132,10 @@ def test_default_value(self): packed = hw_addr.pack() unpacked = basic_types.HWAddress() unpacked.unpack(packed) - self.assertEqual(mac, unpacked.value) + assert mac == unpacked.value -class TestIPAddress(unittest.TestCase): +class TestIPAddress: """Test of IPAddress BasicType.""" def test_unpack_packed(self): @@ -144,7 +144,7 @@ def test_unpack_packed(self): packed = ip_addr.pack() unpacked = basic_types.IPAddress() unpacked.unpack(packed) - self.assertEqual(ip_addr.value, unpacked.value) + assert ip_addr.value == unpacked.value def test_unpack_packed_with_netmask(self): """Testing unpack of packed IPAddress with netmask.""" @@ -152,65 +152,65 @@ def test_unpack_packed_with_netmask(self): packed = ip_addr.pack() unpacked = basic_types.IPAddress() unpacked.unpack(packed) - self.assertEqual(ip_addr.value, unpacked.value) + assert ip_addr.value == unpacked.value def test_netmask(self): """Testing get netmask from IPAddress.""" ip_addr = basic_types.IPAddress('192.168.0.1/24') - self.assertEqual(ip_addr.netmask, 24) + assert ip_addr.netmask == 24 ip_addr = basic_types.IPAddress('192.168.0.1/16') - self.assertEqual(ip_addr.netmask, 16) + assert ip_addr.netmask == 16 ip_addr = basic_types.IPAddress('192.168.0.1') - self.assertEqual(ip_addr.netmask, 32) + assert ip_addr.netmask == 32 def test_max_prefix(self): """Testing get max_prefix from IPAddress.""" ip_addr = basic_types.IPAddress() - self.assertEqual(ip_addr.max_prefix, 32) + assert ip_addr.max_prefix == 32 ip_addr = basic_types.IPAddress('192.168.0.35/16') - self.assertEqual(ip_addr.max_prefix, 32) + assert ip_addr.max_prefix == 32 def test_get_size(self): """Testing get_size from IPAddress.""" ip_addr = basic_types.IPAddress('192.168.0.1/24') - self.assertEqual(ip_addr.get_size(), 4) + assert ip_addr.get_size() == 4 -class TestBinaryData(unittest.TestCase): +class TestBinaryData: """Test Binary data type.""" def test_default_value(self): """Default packed value should be an empty byte.""" expected = b'' actual = BinaryData().pack() - self.assertEqual(expected, actual) + assert expected == actual def test_pack_none_value(self): """Test packing None value.""" expected = b'' actual = BinaryData(None).pack() - self.assertEqual(expected, actual) + assert expected == actual def test_pack_bytes_value(self): """Test packing some bytes.""" expected = b'forty two' actual = BinaryData(expected).pack() - self.assertEqual(expected, actual) + assert expected == actual def test_pack_empty_bytes(self): """Test packing empty bytes.""" expected = b'' actual = BinaryData(expected).pack() - self.assertEqual(expected, actual) + assert expected == actual def test_pack_packable_value(self): """Test packing packable value.""" hw_addr = basic_types.HWAddress('0a:d3:98:a5:30:47') expected = hw_addr.pack() actual = BinaryData(hw_addr).pack() - self.assertEqual(expected, actual) + assert expected == actual def test_unexpected_value_as_parameter(self): """Should raise ValueError if pack value is not bytes.""" data = BinaryData('Some string') - self.assertRaises(ValueError, data.pack, "can't be a string") + pytest.raises(ValueError, data.pack, "can't be a string") diff --git a/tests/unit/test_foundation/test_network_types.py b/tests/unit/test_foundation/test_network_types.py index 7cef9a939..5a493ab56 100644 --- a/tests/unit/test_foundation/test_network_types.py +++ b/tests/unit/test_foundation/test_network_types.py @@ -1,5 +1,5 @@ """Test Python-openflow network types.""" -import unittest +import pytest from pyof.foundation.basic_types import BinaryData from pyof.foundation.exceptions import UnpackException @@ -7,7 +7,7 @@ ARP, VLAN, Ethernet, GenericTLV, IPv4, IPv6) -class TestARP(unittest.TestCase): +class TestARP: """Test ARP packets, without Ethernet headers.""" def test_arp_pack(self): @@ -17,7 +17,7 @@ def test_arp_pack(self): packed = arp.pack() expected = b'\x00\x01\x08\x00\x06\x04\x00\x01\x00\x15\xaf\xd58\x98\xac' expected += b'\x10\x00\n\x00\x00\x00\x00\x00\x00\xac\x10\n\x14' - self.assertEqual(packed, expected) + assert packed == expected def test_arp_unpack(self): """Test unpack method of ARP class.""" @@ -27,14 +27,14 @@ def test_arp_unpack(self): tha='00:15:af:d5:38:98', tpa='172.16.0.10') unpacked = ARP() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected def test_unpack_invalid_htype(self): """Raise UnpackException when L2 protocol is not Ethernet.""" raw = b'\x01\x23\x08\x00\x06\x04\x00\x02\x00\x1f:>\x9a\xcf\xac\x10\n' raw += b'\x14\x00\x15\xaf\xd58\x98\xac\x10\x00\n' arp = ARP() - with self.assertRaises(UnpackException): + with pytest.raises(UnpackException): arp.unpack(raw) def test_unpack_invalid_ptype(self): @@ -42,11 +42,11 @@ def test_unpack_invalid_ptype(self): raw = b'\x00\x01\x08\x90\x06\x04\x00\x02\x00\x1f:>\x9a\xcf\xac\x10\n' raw += b'\x14\x00\x15\xaf\xd58\x98\xac\x10\x00\n' arp = ARP() - with self.assertRaises(UnpackException): + with pytest.raises(UnpackException): arp.unpack(raw) -class TestNetworkTypes(unittest.TestCase): +class TestNetworkTypes: """Reproduce bugs found.""" def test_GenTLV_value_unpack(self): @@ -55,10 +55,10 @@ def test_GenTLV_value_unpack(self): tlv = GenericTLV(value=value) tlv_unpacked = GenericTLV() tlv_unpacked.unpack(tlv.pack()) - self.assertEqual(tlv.value.value, tlv_unpacked.value.value) + assert tlv.value.value == tlv_unpacked.value.value -class TestEthernet(unittest.TestCase): +class TestEthernet: """Test Ethernet frames.""" def test_Ethernet_pack(self): @@ -68,7 +68,7 @@ def test_Ethernet_pack(self): data=b'testdata') packed = ethernet.pack() expected = b'\x00\x1f:>\x9a\xcf\x00\x15\xaf\xd58\x98\x08\x00testdata' - self.assertEqual(packed, expected) + assert packed == expected def test_Ethernet_unpack(self): """Test pack method of Ethernet class without VLAN tag.""" @@ -79,7 +79,7 @@ def test_Ethernet_unpack(self): expected.pack() unpacked = Ethernet() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected def test_Tagged_Ethernet_pack(self): """Test pack method of Ethernet class including VLAN tag.""" @@ -89,7 +89,7 @@ def test_Tagged_Ethernet_pack(self): packed = ethernet.pack() expected = b'\x00\x1f:>\x9a\xcf\x00\x15\xaf\xd58' expected += b'\x98\x81\x00\x00\xc8\x08\x00testdata' - self.assertEqual(packed, expected) + assert packed == expected def test_Tagged_Ethernet_unpack(self): """Test pack method of Ethernet class including VLAN tag.""" @@ -102,10 +102,10 @@ def test_Tagged_Ethernet_unpack(self): expected.pack() unpacked = Ethernet() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected -class TestVLAN(unittest.TestCase): +class TestVLAN: """Test VLAN headers.""" def test_VLAN_pack(self): @@ -113,7 +113,7 @@ def test_VLAN_pack(self): vlan = VLAN(pcp=3, vid=20) packed = vlan.pack() expected = b'\x81\x00`\x14' - self.assertEqual(packed, expected) + assert packed == expected def test_VLAN_unpack(self): """Test unpack method of VLAN class.""" @@ -121,17 +121,17 @@ def test_VLAN_unpack(self): expected = VLAN(pcp=5, vid=123) unpacked = VLAN() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected def test_unpack_wrong_tpid(self): """Raise UnpackException if the tpid is not VLAN_TPID.""" raw = b'\x12\x34\xa0{' vlan = VLAN() - with self.assertRaises(UnpackException): + with pytest.raises(UnpackException): vlan.unpack(raw) -class TestIPv4(unittest.TestCase): +class TestIPv4: """Test IPv4 packets.""" def test_IPv4_pack(self): @@ -142,7 +142,7 @@ def test_IPv4_pack(self): packed = packet.pack() expected = b'F(\x00 \x00\x00\x00\x00@\x11\x02' expected += b'\xc5\xc0\xa8\x00\n\xac\x10\n\x1e1000testdata' - self.assertEqual(packed, expected) + assert packed == expected def test_IPv4_unpack(self): """Test unpack of IPv4 binary packet.""" @@ -154,15 +154,15 @@ def test_IPv4_unpack(self): expected.pack() unpacked = IPv4() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected def test_IPv4_size(self): """Test Header size for IPv4 packet.""" packet = IPv4() packet.pack() - self.assertEqual(20, packet.get_size()) - self.assertEqual(20, packet.length) - self.assertEqual(20, packet.ihl * 4) + assert 20 == packet.get_size() + assert 20 == packet.length + assert 20 == packet.ihl * 4 def test_IPv4_checksum(self): """Test if the IPv4 checksum is being calculated correclty.""" @@ -170,10 +170,10 @@ def test_IPv4_checksum(self): destination="172.16.10.30", options=b'1000', data=b'testdata') packet.pack() - self.assertEqual(packet.checksum, 709) + assert packet.checksum == 709 -class TestIPv6(unittest.TestCase): +class TestIPv6: """Test IPv6 packets.""" def test_IPv6_pack(self): @@ -185,7 +185,7 @@ def test_IPv6_pack(self): expected += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' expected += b'\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' expected += b'\x00\x00\x00\x02testdata' - self.assertEqual(packed, expected) + assert packed == expected def test_IPv6_unpack(self): """Test unpack of IPv6 binary packet.""" @@ -198,4 +198,4 @@ def test_IPv6_unpack(self): expected.pack() unpacked = IPv6() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected diff --git a/tests/unit/test_struct.py b/tests/unit/test_struct.py index e6918b157..a845e4ca0 100644 --- a/tests/unit/test_struct.py +++ b/tests/unit/test_struct.py @@ -1,11 +1,11 @@ """Automate struct tests.""" -import unittest +import pytest from pyof.foundation.base import GenericMessage from tests.unit.raw_dump import RawDump -class TestStruct(unittest.TestCase): +class StructTest: """Run tests related to struct packing and unpacking. Test the lib with raw dump files from an OpenFlow switch. We assume the @@ -16,49 +16,28 @@ class TestStruct(unittest.TestCase): no parameters. To run these tests, just extends this class and call 2 methods in the - ``setUp`` method like the example. + ``setup_method`` method like the example. Example: .. code-block:: python3 - class MyTest(TestDump): - @classmethod - def setUpClass(cls): - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_barrier_reply') + class TestMine(StructTest): + def setup_method(self): + self.set_raw_dump_file('v0x01', 'ofpt_barrier_reply') # Create BarrierReply(xid=5) when needed - super().set_raw_dump_object(BarrierReply, xid=5) + selfset_raw_dump_object(BarrierReply, xid=5) # As in spec: ``OFP_ASSERT(sizeof(struct ...) == ...);`` - super().set_minimum_size(8) + self.set_minimum_size(8) To only test the minimum size and skip packing/unpacking: .. code-block:: python3 - class MyTest(TestDump): - @classmethod - def setUpClass(cls): - super().set_minimum_size(8, BarrierReply) + class TestMine(TestDump): + def setup_method(self): + self.set_minimum_size(8, BarrierReply) """ - def __init__(self, *args, **kwargs): - """Avoid that this class tests are executed. - - The tests in this class are executed through the child, so there's no - no need for them to be executed once more through the parent. - """ - super().__init__(*args, **kwargs) - # Override the run method, so it does nothing instead of running the - # tests (again). - if self.__class__ == TestStruct: - self.run = lambda *args, **kwargs: None - - _new_raw_dump = None - _new_raw_object = None - _msg_cls = None - _min_size = None - - @classmethod - def set_raw_dump_file(cls, version, basename): + def set_raw_dump_file(self, version, basename): """Set which raw dump the tests will use. Args: @@ -67,10 +46,9 @@ def set_raw_dump_file(cls, version, basename): basename (str): The raw filename without extension. E.g. ``ofpt_echo_reply``. """ - cls._new_raw_dump = lambda: RawDump(version, basename) + self._new_raw_dump = lambda: RawDump(version, basename) - @classmethod - def get_raw_dump(cls): + def get_raw_dump(self): """Return a new instance of :class:`.RawDump`. Use the parameters set in :meth:`set_raw_dump_file`. @@ -80,12 +58,14 @@ def get_raw_dump(cls): :meth:`set_raw_dump_file`. """ - if cls._new_raw_dump is None: + try: + if self._new_raw_dump is None: + raise FileNotFoundError() + except AttributeError: raise FileNotFoundError() - return cls._new_raw_dump() + return self._new_raw_dump() - @classmethod - def set_raw_dump_object(cls, msg_cls, *args, **kwargs): + def set_raw_dump_object(self, msg_self, *args, **kwargs): """Set how to create the object that is dumped in a raw file. Args: @@ -94,14 +74,13 @@ def set_raw_dump_object(cls, msg_cls, *args, **kwargs): object. Example: - ``super().__init__(BarrierReply, xid=5)`` will create + ``self.set_raw_dump_object(BarrierReply, xid=5)`` will create ``BarrierReply(xid=5)``. """ - TestStruct._msg_cls = msg_cls - cls._new_raw_object = lambda: msg_cls(*args, **kwargs) + self._msg_self = msg_self + self._new_raw_object = lambda: msg_self(*args, **kwargs) - @classmethod - def get_raw_object(cls): + def get_raw_object(self): """Create a new object of the dumped message. Use the class and parameters set in :meth:`set_raw_dump_object`. @@ -111,13 +90,12 @@ def get_raw_object(cls): :meth:`set_raw_dump_object`. """ - pyof_obj = cls._new_raw_object() + pyof_obj = self._new_raw_object() if isinstance(pyof_obj, GenericMessage): pyof_obj.update_header_length() return pyof_obj - @classmethod - def set_minimum_size(cls, size, msg_cls=None): + def set_minimum_size(self, size, msg_self=None): """Set the struct minimum size. The minimum size can be found in OF spec. For example, @@ -126,17 +104,17 @@ def set_minimum_size(cls, size, msg_cls=None): Args: size (int): The minimum size of the struct, in bytes. - msg_cls (class): The class (or function) to have its size checked. + msg_self (class): The class (or function) to have its size checked. If None, use the same class set in :meth:`set_raw_dump_object`. """ - cls._min_size = size - if msg_cls is not None: - TestStruct._msg_cls = msg_cls + self._min_size = size + if msg_self is not None: + self._msg_self = msg_self def _test_pack(self, obj, expected_bytes): """Check whether packed objects equals to dump file.""" actual_bytes = obj.pack() - self.assertSequenceEqual(expected_bytes, actual_bytes) + assert expected_bytes == actual_bytes def test_raw_dump_file(self): """Object pack should equal file; file unpack should equal object. @@ -147,7 +125,7 @@ def test_raw_dump_file(self): try: file_bytes = self.get_raw_dump().read() except FileNotFoundError: - raise self.skipTest('No raw dump file found.') + raise pytest.skip('No raw dump file found.') pyof_obj = self.get_raw_object() self._test_pack(pyof_obj, file_bytes) @@ -171,12 +149,12 @@ def _test_unpack(self, pyof_obj, bytes2unpack=None): bytes2unpack = bytes2unpack[8:unpacked.header.length.value] unpacked.unpack(bytes2unpack) - self.assertEqual(pyof_obj, unpacked) - self.assertEqual(pyof_obj.get_size(), unpacked.get_size()) + assert pyof_obj == unpacked + assert pyof_obj.get_size() == unpacked.get_size() def test_minimum_size(self): """Test struct minimum size.""" - obj = TestStruct._msg_cls() + obj = self._msg_self() if self._min_size is None: raise Exception(f'{self.__class__.__name__}._min_size is not set') - self.assertEqual(obj.get_size(), self._min_size) + assert obj.get_size() == self._min_size diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index af46fe1fd..04c9faf9c 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,39 +1,39 @@ """Automate utils tests.""" -import unittest +import pytest from pyof.utils import UnpackException, unpack, validate_packet from pyof.v0x01.symmetric.hello import Hello as Hello_v0x01 from pyof.v0x04.symmetric.hello import Hello as Hello_v0x04 -class TestUtils(unittest.TestCase): +class TestUtils: """Run tests to verify unpack independent of version.""" def test_unpack_v0x01_packet(self): """Test if the package in version v0x01 is properly unpacked.""" data = Hello_v0x01().pack() expected = unpack(data) - self.assertEqual(expected.pack(), data) + assert expected.pack() == data def test_unpack_v0x04_packet(self): """Test if the package in version v0x04 is properly unpacked.""" data = Hello_v0x04().pack() expected = unpack(data) - self.assertEqual(expected.pack(), data) + assert expected.pack() == data def test_invalid_packet_with_version_more_then_128(self): """Test validate a invalid packet with version more than 128.""" hello = Hello_v0x04() hello.header.version = 129 - self.assertRaises(UnpackException, validate_packet, hello.pack()) + pytest.raises(UnpackException, validate_packet, hello.pack()) def test_validate_packet_with_invalid_packets(self): """Test validate a invalid packet with invalid packets.""" hello = Hello_v0x04() hello.header.version = 128 - self.assertRaises(UnpackException, validate_packet, hello.pack()) + pytest.raises(UnpackException, validate_packet, hello.pack()) hello.header.version = 0 - self.assertRaises(UnpackException, validate_packet, hello.pack()) + pytest.raises(UnpackException, validate_packet, hello.pack()) diff --git a/tests/unit/v0x01/test_asynchronous/test_error_msg.py b/tests/unit/v0x01/test_asynchronous/test_error_msg.py index b6019c7a3..d8887ed9e 100644 --- a/tests/unit/v0x01/test_asynchronous/test_error_msg.py +++ b/tests/unit/v0x01/test_asynchronous/test_error_msg.py @@ -1,22 +1,20 @@ """Testing Error Message.""" from pyof.v0x01.asynchronous.error_msg import ( BadRequestCode, ErrorMsg, ErrorType, FlowModFailedCode) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestErrorMessage(TestStruct): +class TestErrorMessage(StructTest): """Test the Error Message.""" - @classmethod - def setUpClass(cls): - """Setup TestStruct.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_error_msg') - super().set_raw_dump_object(ErrorMsg, xid=12, + def setup_method(self): + """Setup StructTest.""" + self.set_raw_dump_file('v0x01', 'ofpt_error_msg') + self.set_raw_dump_object(ErrorMsg, xid=12, error_type=ErrorType.OFPET_BAD_REQUEST, code=BadRequestCode.OFPBRC_BAD_STAT, data=b'') - super().set_minimum_size(12) + self.set_minimum_size(12) def test_unpack_error_msg(self): """Test Unpack a sample ErrorMsg.""" @@ -30,4 +28,4 @@ def test_unpack_error_msg(self): actual = ErrorMsg(xid=24) actual.unpack(expected[8:]) - self.assertEqual(actual, error_msg) + assert actual == error_msg diff --git a/tests/unit/v0x01/test_asynchronous/test_flow_removed.py b/tests/unit/v0x01/test_asynchronous/test_flow_removed.py index 319a0c59d..9c64c7edf 100644 --- a/tests/unit/v0x01/test_asynchronous/test_flow_removed.py +++ b/tests/unit/v0x01/test_asynchronous/test_flow_removed.py @@ -2,15 +2,14 @@ from pyof.foundation.basic_types import HWAddress, IPAddress from pyof.v0x01.asynchronous.flow_removed import FlowRemoved, FlowRemovedReason from pyof.v0x01.common.flow_match import Match -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFlowRemoved(TestStruct): +class TestFlowRemoved(StructTest): """Test the FlowRemoved message.""" - @classmethod - def setUpClass(cls): - """Setup TestStruct.""" + def setup_method(self): + """Setup StructTest.""" reason = FlowRemovedReason.OFPRR_IDLE_TIMEOUT match = Match(in_port=80, dl_vlan=1, dl_vlan_pcp=1, dl_type=1, nw_tos=1, nw_proto=1, tp_src=80, tp_dst=80, @@ -19,11 +18,10 @@ def setUpClass(cls): nw_src=IPAddress('192.168.0.1'), nw_dst=IPAddress('192.168.0.2')) - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_flow_removed') - super().set_raw_dump_object(FlowRemoved, xid=12, + self.set_raw_dump_file('v0x01', 'ofpt_flow_removed') + self.set_raw_dump_object(FlowRemoved, xid=12, match=match, cookie=0, priority=1, reason=reason, duration_sec=4, duration_nsec=23, idle_timeout=9, packet_count=10, byte_count=4) - super().set_minimum_size(88) + self.set_minimum_size(88) diff --git a/tests/unit/v0x01/test_asynchronous/test_packet_in.py b/tests/unit/v0x01/test_asynchronous/test_packet_in.py index 2014573a4..4a1e6b1e5 100644 --- a/tests/unit/v0x01/test_asynchronous/test_packet_in.py +++ b/tests/unit/v0x01/test_asynchronous/test_packet_in.py @@ -1,19 +1,17 @@ """Packet in message tests.""" from pyof.v0x01.asynchronous.packet_in import PacketIn, PacketInReason -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestPacketIn(TestStruct): +class TestPacketIn(StructTest): """Packet in message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_packet_in') - super().set_raw_dump_object(PacketIn, xid=15, buffer_id=1, total_len=1, + self.set_raw_dump_file('v0x01', 'ofpt_packet_in') + self.set_raw_dump_object(PacketIn, xid=15, buffer_id=1, total_len=1, in_port=1, reason=PacketInReason.OFPR_ACTION) # Different from the specification, the minimum size of this class is # 18, not 20. - super().set_minimum_size(18) + self.set_minimum_size(18) diff --git a/tests/unit/v0x01/test_asynchronous/test_port_status.py b/tests/unit/v0x01/test_asynchronous/test_port_status.py index 72021b516..43d52fd63 100644 --- a/tests/unit/v0x01/test_asynchronous/test_port_status.py +++ b/tests/unit/v0x01/test_asynchronous/test_port_status.py @@ -2,19 +2,17 @@ from pyof.foundation.basic_types import HWAddress from pyof.v0x01.asynchronous.port_status import PortReason, PortStatus from pyof.v0x01.common.phy_port import PhyPort, PortFeatures, PortState -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestPortStatus(TestStruct): +class TestPortStatus(StructTest): """Test the Port Status message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_port_status') - super().set_raw_dump_object(_new_portstatus) - super().set_minimum_size(64) + self.set_raw_dump_file('v0x01', 'ofpt_port_status') + self.set_raw_dump_object(_new_portstatus) + self.set_minimum_size(64) def _new_portstatus(): diff --git a/tests/unit/v0x01/test_common/test_action.py b/tests/unit/v0x01/test_common/test_action.py index fbcdf912e..2fce1039f 100644 --- a/tests/unit/v0x01/test_common/test_action.py +++ b/tests/unit/v0x01/test_common/test_action.py @@ -3,122 +3,104 @@ ActionDLAddr, ActionEnqueue, ActionNWAddr, ActionNWTos, ActionOutput, ActionTPPort, ActionType, ActionVendorHeader, ActionVlanPCP, ActionVlanVid) from pyof.v0x01.common.phy_port import Port -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestActionOutput(TestStruct): +class TestActionOutput(StructTest): """ActionOutput message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_action_output') - super().set_raw_dump_object(ActionOutput, port=Port.OFPP_CONTROLLER, + self.set_raw_dump_file('v0x01', 'ofpt_action_output') + self.set_raw_dump_object(ActionOutput, port=Port.OFPP_CONTROLLER, max_length=8) - super().set_minimum_size(8) + self.set_minimum_size(8) -class TestActionEnqueue(TestStruct): +class TestActionEnqueue(StructTest): """ActionEnqueue message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_action_enqueue') - super().set_raw_dump_object(ActionEnqueue, port=Port.OFPP_CONTROLLER, + self.set_raw_dump_file('v0x01', 'ofpt_action_enqueue') + self.set_raw_dump_object(ActionEnqueue, port=Port.OFPP_CONTROLLER, queue_id=4) - super().set_minimum_size(16) + self.set_minimum_size(16) -class TestActionVlanVid(TestStruct): +class TestActionVlanVid(StructTest): """ActionVlanVid message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_action_vlan_vid') - super().set_raw_dump_object(ActionVlanVid, vlan_id=5) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x01', 'ofpt_action_vlan_vid') + self.set_raw_dump_object(ActionVlanVid, vlan_id=5) + self.set_minimum_size(8) -class TestActionVlanPCP(TestStruct): +class TestActionVlanPCP(StructTest): """ActionVlanPCP message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_action_vlan_pcp') - super().set_raw_dump_object(ActionVlanPCP, vlan_pcp=2) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x01', 'ofpt_action_vlan_pcp') + self.set_raw_dump_object(ActionVlanPCP, vlan_pcp=2) + self.set_minimum_size(8) -class TestActionDLAddr(TestStruct): +class TestActionDLAddr(StructTest): """ActionDLAddr message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_action_dl_addr') - super().set_raw_dump_object(ActionDLAddr, + self.set_raw_dump_file('v0x01', 'ofpt_action_dl_addr') + self.set_raw_dump_object(ActionDLAddr, action_type=ActionType.OFPAT_SET_DL_SRC, dl_addr=[12, 12, 12, 12, 12, 12]) - super().set_minimum_size(16) + self.set_minimum_size(16) -class TestActionNWAddr(TestStruct): +class TestActionNWAddr(StructTest): """ActionNWAddr message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_action_nw_addr') - super().set_raw_dump_object(ActionNWAddr, + self.set_raw_dump_file('v0x01', 'ofpt_action_nw_addr') + self.set_raw_dump_object(ActionNWAddr, action_type=ActionType.OFPAT_SET_NW_SRC, nw_addr=[12, 12, 12, 12, 12, 12]) - super().set_minimum_size(8) + self.set_minimum_size(8) -class TestActionNWTos(TestStruct): +class TestActionNWTos(StructTest): """ActionNWTos message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_action_nw_tos') - super().set_raw_dump_object(ActionNWTos, + self.set_raw_dump_file('v0x01', 'ofpt_action_nw_tos') + self.set_raw_dump_object(ActionNWTos, action_type=ActionType.OFPAT_SET_NW_SRC, nw_tos=123456) - super().set_minimum_size(8) + self.set_minimum_size(8) -class TestActionTPPort(TestStruct): +class TestActionTPPort(StructTest): """ActionTPPort message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_action_tp_port') - super().set_raw_dump_object(ActionTPPort, + self.set_raw_dump_file('v0x01', 'ofpt_action_tp_port') + self.set_raw_dump_object(ActionTPPort, action_type=ActionType.OFPAT_SET_TP_SRC, tp_port=8888) - super().set_minimum_size(8) + self.set_minimum_size(8) -class TestActionVendorHeader(TestStruct): +class TestActionVendorHeader(StructTest): """ActionVendorHeader message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_action_vendor_header') - super().set_raw_dump_object(ActionVendorHeader, length=16, vendor=1) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x01', 'ofpt_action_vendor_header') + self.set_raw_dump_object(ActionVendorHeader, length=16, vendor=1) + self.set_minimum_size(8) diff --git a/tests/unit/v0x01/test_common/test_flow_match.py b/tests/unit/v0x01/test_common/test_flow_match.py index 4c804c667..da25505e1 100644 --- a/tests/unit/v0x01/test_common/test_flow_match.py +++ b/tests/unit/v0x01/test_common/test_flow_match.py @@ -1,13 +1,13 @@ """Testing FlowMatch structure.""" -import unittest +import pytest from pyof.v0x01.common import flow_match -class TestMatch(unittest.TestCase): +class TestMatch: """Test Match structure.""" - def setUp(self): + def setup_method(self): """Basic setup for test.""" self.message = flow_match.Match() self.message.in_port = 22 @@ -25,21 +25,21 @@ def setUp(self): def test_get_size(self): """[Common/FlowMatch] - size 40.""" - self.assertEqual(self.message.get_size(), 40) + assert self.message.get_size() == 40 def test_pack_unpack(self): """[Common/FlowMatch] - packing and unpacking.""" pack = self.message.pack() unpacked = flow_match.Match() unpacked.unpack(pack) - self.assertEqual(self.message.pack(), unpacked.pack()) + assert self.message.pack() == unpacked.pack() - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_pack(self): """[Common/FlowMatch] - packing.""" pass - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_unpack(self): """[Common/FlowMatch] - unpacking.""" pass diff --git a/tests/unit/v0x01/test_common/test_header.py b/tests/unit/v0x01/test_common/test_header.py index e88b58a1b..cfab262f3 100644 --- a/tests/unit/v0x01/test_common/test_header.py +++ b/tests/unit/v0x01/test_common/test_header.py @@ -1,15 +1,16 @@ """Testing Header structure.""" import os -import unittest from unittest.mock import patch +import pytest + from pyof.v0x01.common.header import Header, Type -class TestHeader(unittest.TestCase): +class TestHeader: """Test the message Header.""" - def setUp(self): + def setup_method(self): """Setup the TestHeader Class instantiating a HELLO header.""" self.message = Header() self.message.message_type = Type.OFPT_HELLO @@ -18,18 +19,18 @@ def setUp(self): def test_size(self): """[Common/Header] - size 8.""" - self.assertEqual(self.message.get_size(), 8) + assert self.message.get_size() == 8 - @unittest.expectedFailure + @pytest.mark.xfail def test_pack_empty(self): """[Common/Header] - packing empty header.""" - self.assertRaises(TypeError, + pytest.raises(TypeError, Header().pack()) def test_pack(self): """[Common/Header] - packing Hello.""" packed_header = b'\x01\x00\x00\x00\x00\x00\x00\x01' - self.assertEqual(self.message.pack(), packed_header) + assert self.message.pack() == packed_header def test_unpack(self): """[Common/Header] - unpacking Hello.""" @@ -38,9 +39,9 @@ def test_unpack(self): f = open(filename, 'rb') self.message.unpack(f.read(8)) - self.assertEqual(self.message.length, 8) - self.assertEqual(self.message.xid, 1) - self.assertEqual(self.message.message_type, Type.OFPT_HELLO) - self.assertEqual(self.message.version, 1) + assert self.message.length == 8 + assert self.message.xid == 1 + assert self.message.message_type == Type.OFPT_HELLO + assert self.message.version == 1 f.close() diff --git a/tests/unit/v0x01/test_common/test_phy_port.py b/tests/unit/v0x01/test_common/test_phy_port.py index dfdfe2818..58262527d 100644 --- a/tests/unit/v0x01/test_common/test_phy_port.py +++ b/tests/unit/v0x01/test_common/test_phy_port.py @@ -1,17 +1,15 @@ """Testing PhyPort structure.""" import os -from unittest import TestCase from pyof.foundation.basic_types import HWAddress from pyof.foundation.constants import OFP_MAX_PORT_NAME_LEN -from pyof.v0x01.common.phy_port import ( - PhyPort, PortConfig, PortFeatures, PortState) +from pyof.v0x01.common.phy_port import PhyPort, PortFeatures, PortState -class TestPhyPort(TestCase): +class TestPhyPort: """Test PhyPort.""" - def setUp(self): + def setup_method(self): """Basic setup for test.""" self.message = PhyPort() self.message.port_no = 1 @@ -23,14 +21,14 @@ def setUp(self): def test_get_size(self): """[Common/PhyPort] - size 48.""" - self.assertEqual(self.message.get_size(), 48) + assert self.message.get_size() == 48 def test_pack(self): """[Common/PhyPort] - packing.""" data = b'\x00\x01\x9a\xda\x11\x8a\xf4\x0cs1-eth1\x00\x00\x00\x00\x00' data += 15 * b'\x00' data += b'\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' - self.assertEqual(self.message.pack(), data) + assert self.message.pack() == data def test_unpack(self): """[Common/PhyPort] - unpacking.""" @@ -40,11 +38,11 @@ def test_unpack(self): f.seek(16, 1) self.message.unpack(f.read(48)) - self.assertEqual(self.message.port_no, 1) - self.assertEqual(self.message.hw_addr, '9a:da:11:8a:f4:0c') - self.assertEqual(self.message.name, 's1-eth1') - self.assertEqual(self.message.state, PortState.OFPPS_STP_LISTEN) - self.assertEqual(self.message.curr, (PortFeatures.OFPPF_10GB_FD | - PortFeatures.OFPPF_COPPER)) + assert self.message.port_no == 1 + assert self.message.hw_addr == '9a:da:11:8a:f4:0c' + assert self.message.name == 's1-eth1' + assert self.message.state == PortState.OFPPS_STP_LISTEN + assert self.message.curr == (PortFeatures.OFPPF_10GB_FD | + PortFeatures.OFPPF_COPPER) f.close() diff --git a/tests/unit/v0x01/test_common/test_queue.py b/tests/unit/v0x01/test_common/test_queue.py index d8c184d62..ad61b9998 100644 --- a/tests/unit/v0x01/test_common/test_queue.py +++ b/tests/unit/v0x01/test_common/test_queue.py @@ -1,13 +1,13 @@ """Testing Queue structure.""" -import unittest +import pytest from pyof.v0x01.common import queue -class TestQueuePropHeader(unittest.TestCase): +class TestQueuePropHeader: """Test QueuePropHeader.""" - def setUp(self): + def setup_method(self): """Basic setup for test.""" self.message = queue.QueuePropHeader() self.message.queue_property = queue.QueueProperties.OFPQT_MIN_RATE @@ -15,23 +15,23 @@ def setUp(self): def test_get_size(self): """[Common/QueuePropHeader] - size 8.""" - self.assertEqual(self.message.get_size(), 8) + assert self.message.get_size() == 8 - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_pack(self): """[Common/QueuePropHeader] - packing.""" pass - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_unpack(self): """[Common/QueuePropHeader] - unpacking.""" pass -class TestPacketQueue(unittest.TestCase): +class TestPacketQueue: """TestPacketQueue.""" - def setUp(self): + def setup_method(self): """Basic setup for test.""" self.message = queue.PacketQueue() self.message.queue_id = 1 @@ -39,37 +39,37 @@ def setUp(self): def test_get_size(self): """[Common/PacketQueue] - size 8.""" - self.assertEqual(self.message.get_size(), 8) + assert self.message.get_size() == 8 - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_pack(self): """[Common/PacketQueue] - packing.""" pass - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_unpack(self): """[Common/PacketQueue] - unpacking.""" pass -class TestQueuePropMinRate(unittest.TestCase): +class TestQueuePropMinRate: """Test QueuePropMinRate.""" - def setUp(self): + def setup_method(self): """Basic setup for test.""" self.message = queue.QueuePropMinRate() self.message.rate = 1000 def test_get_size(self): """[Common/PropMinRate] - size 16.""" - self.assertEqual(self.message.get_size(), 16) + assert self.message.get_size() == 16 - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_pack(self): """[Common/PropMinRate] - packing.""" pass - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_unpack(self): """[Common/PropMinRate] - unpacking.""" pass diff --git a/tests/unit/v0x01/test_controller2switch/test_aggregate_stats_reply.py b/tests/unit/v0x01/test_controller2switch/test_aggregate_stats_reply.py index 9094b734f..3a5855aef 100644 --- a/tests/unit/v0x01/test_controller2switch/test_aggregate_stats_reply.py +++ b/tests/unit/v0x01/test_controller2switch/test_aggregate_stats_reply.py @@ -1,20 +1,18 @@ """Test for AggregateStatsReply message.""" from pyof.v0x01.controller2switch.common import AggregateStatsReply, StatsType from pyof.v0x01.controller2switch.stats_reply import StatsReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestAggregateStatsReply(TestStruct): +class TestAggregateStatsReply(StructTest): """Test for AggregateStatsReply message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/AggregateStatsReply] - size 24.""" aggregate_stats_reply = AggregateStatsReply(packet_count=5, byte_count=1, flow_count=8) - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_aggregate_stats_reply') - super().set_raw_dump_object(StatsReply, xid=17, + self.set_raw_dump_file('v0x01', 'ofpt_aggregate_stats_reply') + self.set_raw_dump_object(StatsReply, xid=17, body_type=StatsType.OFPST_AGGREGATE, flags=0, body=aggregate_stats_reply) - super().set_minimum_size(12) + self.set_minimum_size(12) diff --git a/tests/unit/v0x01/test_controller2switch/test_aggregate_stats_request.py b/tests/unit/v0x01/test_controller2switch/test_aggregate_stats_request.py index 9be8e0d8e..33c8a58fb 100644 --- a/tests/unit/v0x01/test_controller2switch/test_aggregate_stats_request.py +++ b/tests/unit/v0x01/test_controller2switch/test_aggregate_stats_request.py @@ -4,24 +4,22 @@ from pyof.v0x01.controller2switch.common import ( AggregateStatsRequest, StatsType) from pyof.v0x01.controller2switch.stats_request import StatsRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestAggregateStatsRequest(TestStruct): +class TestAggregateStatsRequest(StructTest): """Test class for TestAggregateStatsRequest.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/AggregateStatsRequest] - size 44.""" request = AggregateStatsRequest(table_id=1, out_port=Port.OFPP_NONE, match=_get_match()) - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_aggregate_request') - super().set_raw_dump_object(StatsRequest, xid=17, + self.set_raw_dump_file('v0x01', 'ofpt_aggregate_request') + self.set_raw_dump_object(StatsRequest, xid=17, body_type=StatsType.OFPST_AGGREGATE, flags=0, body=request) - super().set_minimum_size(12) + self.set_minimum_size(12) def _get_match(): diff --git a/tests/unit/v0x01/test_controller2switch/test_barrier_reply.py b/tests/unit/v0x01/test_controller2switch/test_barrier_reply.py index 481cf059a..d2b204a53 100644 --- a/tests/unit/v0x01/test_controller2switch/test_barrier_reply.py +++ b/tests/unit/v0x01/test_controller2switch/test_barrier_reply.py @@ -1,15 +1,13 @@ """Barrier reply message tests.""" from pyof.v0x01.controller2switch.barrier_reply import BarrierReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestBarrierReply(TestStruct): +class TestBarrierReply(StructTest): """Barrier reply message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_barrier_reply') - super().set_raw_dump_object(BarrierReply, xid=5) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x01', 'ofpt_barrier_reply') + self.set_raw_dump_object(BarrierReply, xid=5) + self.set_minimum_size(8) diff --git a/tests/unit/v0x01/test_controller2switch/test_barrier_request.py b/tests/unit/v0x01/test_controller2switch/test_barrier_request.py index af6f45ab4..a04c329aa 100644 --- a/tests/unit/v0x01/test_controller2switch/test_barrier_request.py +++ b/tests/unit/v0x01/test_controller2switch/test_barrier_request.py @@ -1,15 +1,13 @@ """Barrier request message tests.""" from pyof.v0x01.controller2switch.barrier_request import BarrierRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestBarrierRequest(TestStruct): +class TestBarrierRequest(StructTest): """Barrier reply message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_barrier_request') - super().set_raw_dump_object(BarrierRequest, xid=5) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x01', 'ofpt_barrier_request') + self.set_raw_dump_object(BarrierRequest, xid=5) + self.set_minimum_size(8) diff --git a/tests/unit/v0x01/test_controller2switch/test_desc_stats.py b/tests/unit/v0x01/test_controller2switch/test_desc_stats.py index 33d4ecf90..d9da3873f 100644 --- a/tests/unit/v0x01/test_controller2switch/test_desc_stats.py +++ b/tests/unit/v0x01/test_controller2switch/test_desc_stats.py @@ -2,21 +2,19 @@ from pyof.foundation.constants import DESC_STR_LEN from pyof.v0x01.controller2switch.common import DescStats, StatsType from pyof.v0x01.controller2switch.stats_reply import StatsReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestDescStats(TestStruct): +class TestDescStats(StructTest): """Test class for TestDescStats.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/DescStats] - size 1056.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_desc_stats_reply') - super().set_raw_dump_object(StatsReply, xid=14, + self.set_raw_dump_file('v0x01', 'ofpt_desc_stats_reply') + self.set_raw_dump_object(StatsReply, xid=14, body_type=StatsType.OFPST_DESC, flags=0, body=_get_desc_stats()) - super().set_minimum_size(12) + self.set_minimum_size(12) def _get_desc_stats(): diff --git a/tests/unit/v0x01/test_controller2switch/test_features_reply.py b/tests/unit/v0x01/test_controller2switch/test_features_reply.py index 9f468921c..7fd972f23 100644 --- a/tests/unit/v0x01/test_controller2switch/test_features_reply.py +++ b/tests/unit/v0x01/test_controller2switch/test_features_reply.py @@ -2,20 +2,18 @@ from pyof.foundation.basic_types import DPID, HWAddress from pyof.v0x01.common.phy_port import PhyPort, PortConfig, PortState from pyof.v0x01.controller2switch.features_reply import FeaturesReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFeaturesReply(TestStruct): +class TestFeaturesReply(StructTest): """Feature reply message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_features_reply') + self.set_raw_dump_file('v0x01', 'ofpt_features_reply') kwargs = _get_kwargs() - super().set_raw_dump_object(FeaturesReply, **kwargs) - super().set_minimum_size(32) + self.set_raw_dump_object(FeaturesReply, **kwargs) + self.set_minimum_size(32) def _get_kwargs(): diff --git a/tests/unit/v0x01/test_controller2switch/test_features_request.py b/tests/unit/v0x01/test_controller2switch/test_features_request.py index 1513eb09b..c89d39bc0 100644 --- a/tests/unit/v0x01/test_controller2switch/test_features_request.py +++ b/tests/unit/v0x01/test_controller2switch/test_features_request.py @@ -1,15 +1,13 @@ """Feature request message tests.""" from pyof.v0x01.controller2switch.features_request import FeaturesRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFeaturesRequest(TestStruct): +class TestFeaturesRequest(StructTest): """Feature request message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_features_request') - super().set_raw_dump_object(FeaturesRequest, xid=3) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x01', 'ofpt_features_request') + self.set_raw_dump_object(FeaturesRequest, xid=3) + self.set_minimum_size(8) diff --git a/tests/unit/v0x01/test_controller2switch/test_flow_mod.py b/tests/unit/v0x01/test_controller2switch/test_flow_mod.py index f08b3178b..7f7d8a85e 100644 --- a/tests/unit/v0x01/test_controller2switch/test_flow_mod.py +++ b/tests/unit/v0x01/test_controller2switch/test_flow_mod.py @@ -3,33 +3,29 @@ from pyof.v0x01.common.flow_match import Match from pyof.v0x01.common.phy_port import Port from pyof.v0x01.controller2switch.flow_mod import FlowMod, FlowModCommand -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFlowAdd(TestStruct): +class TestFlowAdd(StructTest): """Flow addition message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_flow_add') + self.set_raw_dump_file('v0x01', 'ofpt_flow_add') kwargs = _get_flowmod_kwargs(FlowModCommand.OFPFC_ADD) - super().set_raw_dump_object(FlowMod, **kwargs) - super().set_minimum_size(72) + self.set_raw_dump_object(FlowMod, **kwargs) + self.set_minimum_size(72) -class TestFlowDelete(TestStruct): +class TestFlowDelete(StructTest): """Flow deletion message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_flow_delete') + self.set_raw_dump_file('v0x01', 'ofpt_flow_delete') kwargs = _get_flowmod_kwargs(FlowModCommand.OFPFC_DELETE) - super().set_raw_dump_object(FlowMod, **kwargs) - super().set_minimum_size(72) + self.set_raw_dump_object(FlowMod, **kwargs) + self.set_minimum_size(72) def _get_flowmod_kwargs(command): diff --git a/tests/unit/v0x01/test_controller2switch/test_flow_stats.py b/tests/unit/v0x01/test_controller2switch/test_flow_stats.py index 18a9e9692..67df9f155 100644 --- a/tests/unit/v0x01/test_controller2switch/test_flow_stats.py +++ b/tests/unit/v0x01/test_controller2switch/test_flow_stats.py @@ -2,21 +2,19 @@ from pyof.v0x01.common.flow_match import Match from pyof.v0x01.controller2switch.common import FlowStats, StatsType from pyof.v0x01.controller2switch.stats_reply import StatsReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFlowStats(TestStruct): +class TestFlowStats(StructTest): """Test class for TestFlowStats.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/FlowStats] - size 88.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_flow_stats_reply') - super().set_raw_dump_object(StatsReply, xid=12, + self.set_raw_dump_file('v0x01', 'ofpt_flow_stats_reply') + self.set_raw_dump_object(StatsReply, xid=12, body_type=StatsType.OFPST_FLOW, flags=0, body=_get_flow_stats()) - super().set_minimum_size(12) + self.set_minimum_size(12) def _get_flow_stats(): diff --git a/tests/unit/v0x01/test_controller2switch/test_flow_stats_request.py b/tests/unit/v0x01/test_controller2switch/test_flow_stats_request.py index 46ecad11e..9aa3c8b50 100644 --- a/tests/unit/v0x01/test_controller2switch/test_flow_stats_request.py +++ b/tests/unit/v0x01/test_controller2switch/test_flow_stats_request.py @@ -2,21 +2,19 @@ from pyof.v0x01.common.flow_match import Match from pyof.v0x01.controller2switch.common import FlowStatsRequest, StatsType from pyof.v0x01.controller2switch.stats_request import StatsRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFlowStatsRequest(TestStruct): +class TestFlowStatsRequest(StructTest): """Test class for TestFlowStatsRequest.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/FlowStatsRequest] - size 44.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_flow_stats_request') - super().set_raw_dump_object(StatsRequest, xid=12, + self.set_raw_dump_file('v0x01', 'ofpt_flow_stats_request') + self.set_raw_dump_object(StatsRequest, xid=12, body_type=StatsType.OFPST_FLOW, flags=0, body=_get_flow_stats_request()) - super().set_minimum_size(12) + self.set_minimum_size(12) def _get_flow_stats_request(): diff --git a/tests/unit/v0x01/test_controller2switch/test_get_config_reply.py b/tests/unit/v0x01/test_controller2switch/test_get_config_reply.py index ab81813cb..9c4663259 100644 --- a/tests/unit/v0x01/test_controller2switch/test_get_config_reply.py +++ b/tests/unit/v0x01/test_controller2switch/test_get_config_reply.py @@ -1,18 +1,16 @@ """Test GetConfigReply message.""" from pyof.v0x01.controller2switch.common import ConfigFlag from pyof.v0x01.controller2switch.get_config_reply import GetConfigReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestGetConfigReply(TestStruct): +class TestGetConfigReply(StructTest): """Test class for TestGetConfigReply.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/GetConfigReply] - size 12.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_get_config_reply') - super().set_raw_dump_object(GetConfigReply, xid=13, + self.set_raw_dump_file('v0x01', 'ofpt_get_config_reply') + self.set_raw_dump_object(GetConfigReply, xid=13, flags=ConfigFlag.OFPC_FRAG_REASM, miss_send_len=1024) - super().set_minimum_size(12) + self.set_minimum_size(12) diff --git a/tests/unit/v0x01/test_controller2switch/test_get_config_request.py b/tests/unit/v0x01/test_controller2switch/test_get_config_request.py index 4ea511bcc..b95fe2baf 100644 --- a/tests/unit/v0x01/test_controller2switch/test_get_config_request.py +++ b/tests/unit/v0x01/test_controller2switch/test_get_config_request.py @@ -1,15 +1,13 @@ """Test GetConfigRequest message.""" from pyof.v0x01.controller2switch.get_config_request import GetConfigRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestGetConfigRequest(TestStruct): +class TestGetConfigRequest(StructTest): """Test class for TestGetConfigRequest.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/GetConfigRequest] - size 8.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_get_config_request') - super().set_raw_dump_object(GetConfigRequest, xid=1) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x01', 'ofpt_get_config_request') + self.set_raw_dump_object(GetConfigRequest, xid=1) + self.set_minimum_size(8) diff --git a/tests/unit/v0x01/test_controller2switch/test_packet_out.py b/tests/unit/v0x01/test_controller2switch/test_packet_out.py index 397352661..59730a52c 100644 --- a/tests/unit/v0x01/test_controller2switch/test_packet_out.py +++ b/tests/unit/v0x01/test_controller2switch/test_packet_out.py @@ -3,28 +3,23 @@ from pyof.v0x01.common.action import ActionOutput from pyof.v0x01.common.phy_port import Port from pyof.v0x01.controller2switch.packet_out import PacketOut -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest +import pytest - -class TestPacketOut(TestStruct): +class TestPacketOut(StructTest): """Packet out message tests (also those in :class:`.TestDump`). Attributes: message (PacketOut): The message configured in :meth:`setUpClass`. """ - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_packet_out') - super().set_raw_dump_object(PacketOut, xid=8, buffer_id=4294967295, + self.set_raw_dump_file('v0x01', 'ofpt_packet_out') + self.set_raw_dump_object(PacketOut, xid=8, buffer_id=4294967295, in_port=Port.OFPP_NONE, data=_get_data(), actions=_get_actions()) - super().set_minimum_size(16) - - def setUp(self): - """Run before every test.""" + self.set_minimum_size(16) self.message = self.get_raw_object() def test_valid_virtual_in_ports(self): @@ -32,7 +27,7 @@ def test_valid_virtual_in_ports(self): valid = (Port.OFPP_LOCAL, Port.OFPP_CONTROLLER, Port.OFPP_NONE) for in_port in valid: self.message.in_port = in_port - self.assertTrue(self.message.is_valid()) + assert self.message.is_valid() def test_invalid_virtual_in_ports(self): """Invalid virtual ports as defined in 1.0.1 spec.""" @@ -40,23 +35,23 @@ def test_invalid_virtual_in_ports(self): Port.OFPP_FLOOD, Port.OFPP_ALL) for in_port in invalid: self.message.in_port = in_port - self.assertFalse(self.message.is_valid()) - self.assertRaises(ValidationError, self.message.validate) + assert not self.message.is_valid() + pytest.raises(ValidationError, self.message.validate) def test_valid_physical_in_ports(self): """Physical port limits from 1.0.0 spec.""" max_valid = int(Port.OFPP_MAX.value) for in_port in (1, max_valid): self.message.in_port = in_port - self.assertTrue(self.message.is_valid()) + assert self.message.is_valid() def test_invalid_physical_in_port(self): """Physical port limits from 1.0.0 spec.""" max_valid = int(Port.OFPP_MAX.value) for in_port in (-1, 0, max_valid + 1, max_valid + 2): self.message.in_port = in_port - self.assertFalse(self.message.is_valid()) - self.assertRaises(ValidationError, self.message.validate) + assert not self.message.is_valid() + pytest.raises(ValidationError, self.message.validate) def _get_actions(): diff --git a/tests/unit/v0x01/test_controller2switch/test_port_mod.py b/tests/unit/v0x01/test_controller2switch/test_port_mod.py index c5791d160..be12b3097 100644 --- a/tests/unit/v0x01/test_controller2switch/test_port_mod.py +++ b/tests/unit/v0x01/test_controller2switch/test_port_mod.py @@ -1,20 +1,18 @@ """Test PortMod message.""" from pyof.v0x01.common.phy_port import PortConfig, PortFeatures from pyof.v0x01.controller2switch.port_mod import PortMod -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestPortMod(TestStruct): +class TestPortMod(StructTest): """Test class for PortMod.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/PortMod] - size 32.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_port_mod') - super().set_raw_dump_object(PortMod, xid=3, port_no=80, + self.set_raw_dump_file('v0x01', 'ofpt_port_mod') + self.set_raw_dump_object(PortMod, xid=3, port_no=80, hw_addr='aa:bb:cc:00:33:9f', config=PortConfig.OFPPC_PORT_DOWN, mask=PortConfig.OFPPC_NO_FWD, advertise=PortFeatures.OFPPF_FIBER) - super().set_minimum_size(32) + self.set_minimum_size(32) diff --git a/tests/unit/v0x01/test_controller2switch/test_port_stats.py b/tests/unit/v0x01/test_controller2switch/test_port_stats.py index fee0039d4..0875b29eb 100644 --- a/tests/unit/v0x01/test_controller2switch/test_port_stats.py +++ b/tests/unit/v0x01/test_controller2switch/test_port_stats.py @@ -1,21 +1,19 @@ """Test for PortStats structure.""" from pyof.v0x01.controller2switch.common import PortStats, StatsType from pyof.v0x01.controller2switch.stats_reply import StatsReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestPortStats(TestStruct): +class TestPortStats(StructTest): """Test for PortStats structure.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/PortStats] - size 104.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_port_stats') - super().set_raw_dump_object(StatsReply, xid=13, + self.set_raw_dump_file('v0x01', 'ofpt_port_stats') + self.set_raw_dump_object(StatsReply, xid=13, body_type=StatsType.OFPST_PORT, flags=0, body=_get_port_stats()) - super().set_minimum_size(12) + self.set_minimum_size(12) def _get_port_stats(): diff --git a/tests/unit/v0x01/test_controller2switch/test_port_stats_request.py b/tests/unit/v0x01/test_controller2switch/test_port_stats_request.py index 135a424ac..774f9c508 100644 --- a/tests/unit/v0x01/test_controller2switch/test_port_stats_request.py +++ b/tests/unit/v0x01/test_controller2switch/test_port_stats_request.py @@ -1,18 +1,16 @@ """Test for PortStatsRequest.""" from pyof.v0x01.controller2switch.common import PortStatsRequest, StatsType from pyof.v0x01.controller2switch.stats_request import StatsRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestPortStatsRequest(TestStruct): +class TestPortStatsRequest(StructTest): """Test for PortStatsRequest.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/PortStatsRequest] - size 8.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_port_stats_request') - super().set_raw_dump_object(StatsRequest, xid=17, + self.set_raw_dump_file('v0x01', 'ofpt_port_stats_request') + self.set_raw_dump_object(StatsRequest, xid=17, body_type=StatsType.OFPST_PORT, flags=0, body=PortStatsRequest(port_no=80)) - super().set_minimum_size(12) + self.set_minimum_size(12) diff --git a/tests/unit/v0x01/test_controller2switch/test_queue_get_config_reply.py b/tests/unit/v0x01/test_controller2switch/test_queue_get_config_reply.py index 41a3aab65..7d16a6eb4 100644 --- a/tests/unit/v0x01/test_controller2switch/test_queue_get_config_reply.py +++ b/tests/unit/v0x01/test_controller2switch/test_queue_get_config_reply.py @@ -3,21 +3,19 @@ from pyof.v0x01.common.queue import ( PacketQueue, QueueProperties, QueuePropHeader) from pyof.v0x01.controller2switch import queue_get_config_reply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestQueueGetConfigReply(TestStruct): +class TestQueueGetConfigReply(StructTest): """Test for QueueGetConfigReply message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/QueueGetConfigReply] - size 16.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_queue_get_config_reply') - super().set_raw_dump_object(queue_get_config_reply.QueueGetConfigReply, + self.set_raw_dump_file('v0x01', 'ofpt_queue_get_config_reply') + self.set_raw_dump_object(queue_get_config_reply.QueueGetConfigReply, xid=1, port=Port.OFPP_ALL, queues=_get_packet_queue()) - super().set_minimum_size(16) + self.set_minimum_size(16) def _get_packet_queue(): diff --git a/tests/unit/v0x01/test_controller2switch/test_queue_get_config_request.py b/tests/unit/v0x01/test_controller2switch/test_queue_get_config_request.py index c9f23e683..55ec3b7b8 100644 --- a/tests/unit/v0x01/test_controller2switch/test_queue_get_config_request.py +++ b/tests/unit/v0x01/test_controller2switch/test_queue_get_config_request.py @@ -1,17 +1,15 @@ """Test for QueueGetConfigRequest message.""" from pyof.v0x01.common.phy_port import Port from pyof.v0x01.controller2switch import queue_get_config_request as request -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestQueueGetConfigRequest(TestStruct): +class TestQueueGetConfigRequest(StructTest): """Test for QueueGetConfigRequest message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/QueueGetConfigRequest] - size 12.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_queue_get_config_request') - super().set_raw_dump_object(request.QueueGetConfigRequest, + self.set_raw_dump_file('v0x01', 'ofpt_queue_get_config_request') + self.set_raw_dump_object(request.QueueGetConfigRequest, xid=1, port=Port.OFPP_MAX) - super().set_minimum_size(12) + self.set_minimum_size(12) diff --git a/tests/unit/v0x01/test_controller2switch/test_queue_stats.py b/tests/unit/v0x01/test_controller2switch/test_queue_stats.py index 909d89d11..135e36351 100644 --- a/tests/unit/v0x01/test_controller2switch/test_queue_stats.py +++ b/tests/unit/v0x01/test_controller2switch/test_queue_stats.py @@ -1,21 +1,19 @@ """Test for QueueStats.""" from pyof.v0x01.controller2switch.common import QueueStats, StatsType from pyof.v0x01.controller2switch.stats_reply import StatsReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestQueueStats(TestStruct): +class TestQueueStats(StructTest): """Test for QueueStats.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/QueueStats] - size 32.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_queue_stats') - super().set_raw_dump_object(StatsReply, xid=7, + self.set_raw_dump_file('v0x01', 'ofpt_queue_stats') + self.set_raw_dump_object(StatsReply, xid=7, body_type=StatsType.OFPST_QUEUE, flags=0, body=_get_queue_stats()) - super().set_minimum_size(12) + self.set_minimum_size(12) def _get_queue_stats(): diff --git a/tests/unit/v0x01/test_controller2switch/test_queue_stats_request.py b/tests/unit/v0x01/test_controller2switch/test_queue_stats_request.py index 9fc374995..986d05624 100644 --- a/tests/unit/v0x01/test_controller2switch/test_queue_stats_request.py +++ b/tests/unit/v0x01/test_controller2switch/test_queue_stats_request.py @@ -1,20 +1,18 @@ """Test for QueueStatsRequest message.""" from pyof.v0x01.controller2switch.common import QueueStatsRequest, StatsType from pyof.v0x01.controller2switch.stats_request import StatsRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestQueueStatsRequest(TestStruct): +class TestQueueStatsRequest(StructTest): """Test for QueueStatsRequest message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/QueueStatsRequest] - size 8.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_queue_stats_request') - super().set_raw_dump_object(StatsRequest, xid=14, + self.set_raw_dump_file('v0x01', 'ofpt_queue_stats_request') + self.set_raw_dump_object(StatsRequest, xid=14, body_type=StatsType.OFPST_QUEUE, flags=0, body=QueueStatsRequest(port_no=80, queue_id=5)) - super().set_minimum_size(12) + self.set_minimum_size(12) diff --git a/tests/unit/v0x01/test_controller2switch/test_set_config.py b/tests/unit/v0x01/test_controller2switch/test_set_config.py index a3f793184..a8c8b114f 100644 --- a/tests/unit/v0x01/test_controller2switch/test_set_config.py +++ b/tests/unit/v0x01/test_controller2switch/test_set_config.py @@ -1,18 +1,16 @@ """Set Config message tests.""" from pyof.v0x01.controller2switch.common import ConfigFlag from pyof.v0x01.controller2switch.set_config import SetConfig -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestSetConfig(TestStruct): +class TestSetConfig(StructTest): """Test the Set Config message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_set_config') - super().set_raw_dump_object(SetConfig, xid=3, + self.set_raw_dump_file('v0x01', 'ofpt_set_config') + self.set_raw_dump_object(SetConfig, xid=3, flags=ConfigFlag.OFPC_FRAG_NORMAL, miss_send_len=128) - super().set_minimum_size(12) + self.set_minimum_size(12) diff --git a/tests/unit/v0x01/test_controller2switch/test_stats_reply.py b/tests/unit/v0x01/test_controller2switch/test_stats_reply.py index c51f87809..efc5ca3c5 100644 --- a/tests/unit/v0x01/test_controller2switch/test_stats_reply.py +++ b/tests/unit/v0x01/test_controller2switch/test_stats_reply.py @@ -1,18 +1,16 @@ """Test for StatsReply message.""" from pyof.v0x01.controller2switch.common import StatsType from pyof.v0x01.controller2switch.stats_reply import StatsReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestStatsReply(TestStruct): +class TestStatsReply(StructTest): """Test for StatsReply message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/StatsReply] - size 12.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_stats_reply') - super().set_raw_dump_object(StatsReply, xid=1, + self.set_raw_dump_file('v0x01', 'ofpt_stats_reply') + self.set_raw_dump_object(StatsReply, xid=1, body_type=StatsType.OFPST_FLOW, flags=0x0001, body=b'') - super().set_minimum_size(12) + self.set_minimum_size(12) diff --git a/tests/unit/v0x01/test_controller2switch/test_stats_request.py b/tests/unit/v0x01/test_controller2switch/test_stats_request.py index 15154b193..7c958051e 100644 --- a/tests/unit/v0x01/test_controller2switch/test_stats_request.py +++ b/tests/unit/v0x01/test_controller2switch/test_stats_request.py @@ -1,18 +1,16 @@ """Test for StatsRequest message.""" from pyof.v0x01.controller2switch.common import StatsType from pyof.v0x01.controller2switch.stats_request import StatsRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestStatsRequest(TestStruct): +class TestStatsRequest(StructTest): """Test for StatsRequest message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/StatsRequest] - size 12.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_stats_request') - super().set_raw_dump_object(StatsRequest, xid=1, + self.set_raw_dump_file('v0x01', 'ofpt_stats_request') + self.set_raw_dump_object(StatsRequest, xid=1, body_type=StatsType.OFPST_FLOW, flags=1, body=b'') - super().set_minimum_size(12) + self.set_minimum_size(12) diff --git a/tests/unit/v0x01/test_controller2switch/test_table_stats.py b/tests/unit/v0x01/test_controller2switch/test_table_stats.py index 618ed426d..4694d38e0 100644 --- a/tests/unit/v0x01/test_controller2switch/test_table_stats.py +++ b/tests/unit/v0x01/test_controller2switch/test_table_stats.py @@ -3,21 +3,19 @@ from pyof.v0x01.common.flow_match import FlowWildCards from pyof.v0x01.controller2switch.common import StatsType, TableStats from pyof.v0x01.controller2switch.stats_reply import StatsReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestTableStats(TestStruct): +class TestTableStats(StructTest): """Test class for TableStats.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/TableStats] - size 64.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_table_stats') - super().set_raw_dump_object(StatsReply, xid=14, + self.set_raw_dump_file('v0x01', 'ofpt_table_stats') + self.set_raw_dump_object(StatsReply, xid=14, body_type=StatsType.OFPST_TABLE, flags=0, body=_get_table_stats()) - super().set_minimum_size(12) + self.set_minimum_size(12) def _get_table_stats(): diff --git a/tests/unit/v0x01/test_controller2switch/test_vendor_stats.py b/tests/unit/v0x01/test_controller2switch/test_vendor_stats.py index ec4ce8ff1..e260c0058 100644 --- a/tests/unit/v0x01/test_controller2switch/test_vendor_stats.py +++ b/tests/unit/v0x01/test_controller2switch/test_vendor_stats.py @@ -1,24 +1,22 @@ """Test VendorStats message.""" from pyof.v0x01.controller2switch.common import StatsType, VendorStats from pyof.v0x01.controller2switch.stats_request import StatsRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestVendorStats(TestStruct): +class TestVendorStats(StructTest): """Test class for TestVendorStats. The dump and unpacked data were provided by user bug report. """ - @classmethod - def setUpClass(cls): + def setup_method(self): """[Controller2Switch/VendorStats] - size 1056.""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_vendor_stats_reply') - super().set_raw_dump_object(StatsRequest, xid=4, + self.set_raw_dump_file('v0x01', 'ofpt_vendor_stats_reply') + self.set_raw_dump_object(StatsRequest, xid=4, body_type=StatsType.OFPST_VENDOR, flags=0, body=_get_vendor_stats()) - super().set_minimum_size(12) + self.set_minimum_size(12) def _get_vendor_stats(): diff --git a/tests/unit/v0x01/test_symmetric/test_echo_reply.py b/tests/unit/v0x01/test_symmetric/test_echo_reply.py index 07cf86e48..06c1147ba 100644 --- a/tests/unit/v0x01/test_symmetric/test_echo_reply.py +++ b/tests/unit/v0x01/test_symmetric/test_echo_reply.py @@ -1,15 +1,13 @@ """Echo reply message tests.""" from pyof.v0x01.symmetric.echo_reply import EchoReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestEchoReply(TestStruct): +class TestEchoReply(StructTest): """Echo reply message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_echo_reply') - super().set_raw_dump_object(EchoReply, xid=0) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x01', 'ofpt_echo_reply') + self.set_raw_dump_object(EchoReply, xid=0) + self.set_minimum_size(8) diff --git a/tests/unit/v0x01/test_symmetric/test_echo_request.py b/tests/unit/v0x01/test_symmetric/test_echo_request.py index 2071b2ed7..28236196e 100644 --- a/tests/unit/v0x01/test_symmetric/test_echo_request.py +++ b/tests/unit/v0x01/test_symmetric/test_echo_request.py @@ -1,15 +1,13 @@ """Echo request message tests.""" from pyof.v0x01.symmetric.echo_request import EchoRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestEchoRequest(TestStruct): +class TestEchoRequest(StructTest): """Echo request message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_echo_request') - super().set_raw_dump_object(EchoRequest, xid=0) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x01', 'ofpt_echo_request') + self.set_raw_dump_object(EchoRequest, xid=0) + self.set_minimum_size(8) diff --git a/tests/unit/v0x01/test_symmetric/test_hello.py b/tests/unit/v0x01/test_symmetric/test_hello.py index c7353a74b..021ffeaff 100644 --- a/tests/unit/v0x01/test_symmetric/test_hello.py +++ b/tests/unit/v0x01/test_symmetric/test_hello.py @@ -1,15 +1,13 @@ """Hello message tests.""" from pyof.v0x01.symmetric.hello import Hello -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestHello(TestStruct): +class TestHello(StructTest): """Hello message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x01', 'ofpt_hello') - super().set_raw_dump_object(Hello, xid=1) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x01', 'ofpt_hello') + self.set_raw_dump_object(Hello, xid=1) + self.set_minimum_size(8) diff --git a/tests/unit/v0x01/test_symmetric/test_vendor_header.py b/tests/unit/v0x01/test_symmetric/test_vendor_header.py index 16a5cb855..975b71188 100644 --- a/tests/unit/v0x01/test_symmetric/test_vendor_header.py +++ b/tests/unit/v0x01/test_symmetric/test_vendor_header.py @@ -1,14 +1,14 @@ """Testing VendorHeader message.""" from pyof.v0x01.symmetric.vendor_header import VendorHeader -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest +from pyof.v0x01.symmetric.hello import Hello - -class TestVendorHeader(TestStruct): +class TestVendorHeader(StructTest): """Vendor message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): - super().set_minimum_size(8) + def setup_method(self): + self.set_raw_dump_object(Hello, xid=1) + self.set_minimum_size(8) def test_unpack(self): """Test unpack VendorHeader message.""" diff --git a/tests/unit/v0x04/test_asynchronous/test_error_msg.py b/tests/unit/v0x04/test_asynchronous/test_error_msg.py index 0fde9b240..bf1f28b80 100644 --- a/tests/unit/v0x04/test_asynchronous/test_error_msg.py +++ b/tests/unit/v0x04/test_asynchronous/test_error_msg.py @@ -2,22 +2,20 @@ from pyof.foundation.exceptions import MethodNotImplemented from pyof.v0x04.asynchronous.error_msg import ( ErrorExperimenterMsg, ErrorMsg, ErrorType, MeterModFailedCode) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestErrorMsg(TestStruct): +class TestErrorMsg(StructTest): """ErroMsg message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" error_type = ErrorType.OFPET_METER_MOD_FAILED code = MeterModFailedCode.OFPMMFC_UNKNOWN_METER - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_error') - super().set_raw_dump_object(ErrorMsg, xid=1, error_type=error_type, + self.set_raw_dump_file('v0x04', 'ofpt_error') + self.set_raw_dump_object(ErrorMsg, xid=1, error_type=error_type, code=code, data=_get_data()) - super().set_minimum_size(12) + self.set_minimum_size(12) @staticmethod def test_unpack(): diff --git a/tests/unit/v0x04/test_asynchronous/test_flow_removed.py b/tests/unit/v0x04/test_asynchronous/test_flow_removed.py index 3c9ca60bc..a355da652 100644 --- a/tests/unit/v0x04/test_asynchronous/test_flow_removed.py +++ b/tests/unit/v0x04/test_asynchronous/test_flow_removed.py @@ -2,25 +2,23 @@ from pyof.v0x04.asynchronous.flow_removed import FlowRemoved, FlowRemovedReason from pyof.v0x04.common.flow_match import ( Match, MatchType, OxmClass, OxmOfbMatchField, OxmTLV) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFlowRemovedMsg(TestStruct): +class TestFlowRemovedMsg(StructTest): """FlowRemoved message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_flow_removed') - super().set_raw_dump_object(FlowRemoved, xid=0, + self.set_raw_dump_file('v0x04', 'ofpt_flow_removed') + self.set_raw_dump_object(FlowRemoved, xid=0, cookie=0x0000000000000000, priority=1000, reason=FlowRemovedReason.OFPRR_DELETE, table_id=0, duration_sec=77, duration_nsec=559000000, idle_timeout=0, hard_timeout=0, packet_count=0, byte_count=0, match=_new_match()) - super().set_minimum_size(56) + self.set_minimum_size(56) def _new_match(): diff --git a/tests/unit/v0x04/test_asynchronous/test_packet_in.py b/tests/unit/v0x04/test_asynchronous/test_packet_in.py index f8ff56c9c..832e12d3a 100644 --- a/tests/unit/v0x04/test_asynchronous/test_packet_in.py +++ b/tests/unit/v0x04/test_asynchronous/test_packet_in.py @@ -4,35 +4,33 @@ from pyof.v0x04.common.flow_match import ( Match, MatchType, OxmClass, OxmOfbMatchField, OxmTLV) from pyof.v0x04.common.port import PortNo -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest +import pytest - -class TestPacketInRaw(TestStruct): +class TestPacketInRaw(StructTest): """Test PacketIn using a dump file.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_packet_in') - super().set_raw_dump_object(PacketIn, xid=0, buffer_id=OFP_NO_BUFFER, + self.set_raw_dump_file('v0x04', 'ofpt_packet_in') + self.set_raw_dump_object(PacketIn, xid=0, buffer_id=OFP_NO_BUFFER, total_len=90, reason=PacketInReason.OFPR_ACTION, table_id=0, cookie=0x0000000000000000, match=_new_match(), data=_get_data()) - super().set_minimum_size(34) + self.set_minimum_size(34) def test_valid_physical_in_port(self): """Physical port limits from 1.3.0 spec.""" try: msg = self.get_raw_dump().read() except FileNotFoundError: - raise self.skipTest('No raw dump file found.') + raise pytest.skip('No raw dump file found.') else: max_valid = int(PortNo.OFPP_MAX.value) - 1 msg = self.get_raw_object() if msg.in_port in (1, max_valid): - self.assertTrue(msg.is_valid()) + assert msg.is_valid() def _new_match(): diff --git a/tests/unit/v0x04/test_asynchronous/test_port_status.py b/tests/unit/v0x04/test_asynchronous/test_port_status.py index e86195651..d3f65daa3 100644 --- a/tests/unit/v0x04/test_asynchronous/test_port_status.py +++ b/tests/unit/v0x04/test_asynchronous/test_port_status.py @@ -2,19 +2,17 @@ from pyof.foundation.basic_types import HWAddress from pyof.v0x04.asynchronous.port_status import PortReason, PortStatus from pyof.v0x04.common.port import Port, PortFeatures, PortState -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestPortStatus(TestStruct): +class TestPortStatus(StructTest): """Test the Port Status message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_port_status') - super().set_raw_dump_object(_new_portstatus) - super().set_minimum_size(80) + self.set_raw_dump_file('v0x04', 'ofpt_port_status') + self.set_raw_dump_object(_new_portstatus) + self.set_minimum_size(80) def _new_portstatus(): diff --git a/tests/unit/v0x04/test_common/test_flow_match.py b/tests/unit/v0x04/test_common/test_flow_match.py index b5e8c3ad8..e11e35368 100644 --- a/tests/unit/v0x04/test_common/test_flow_match.py +++ b/tests/unit/v0x04/test_common/test_flow_match.py @@ -1,12 +1,12 @@ """Test OXM-related implementations.""" -from unittest import TestCase +import pytest from pyof.foundation.exceptions import PackException, UnpackException from pyof.v0x04.common.flow_match import ( Match, MatchType, OxmClass, OxmOfbMatchField, OxmTLV) -class TestMatch(TestCase): +class TestMatch: """Test Match class.""" tlv1 = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, @@ -25,19 +25,19 @@ def test_unpacked_pack(self): """ unpacked = Match() unpacked.unpack(self.match.pack()) - self.assertEqual(self.match, unpacked) + assert self.match == unpacked def test_pack_other_instance(self): """Test packing another Match instance by using the value argument.""" expected = self.match.pack() valued_pack = Match().pack(self.match) - self.assertEqual(expected, valued_pack) + assert expected == valued_pack -class TestOxmTLV(TestCase): +class TestOxmTLV: """Test OXM TLV pack and unpack.""" - def setUp(self): + def setup_method(self): """Instantiate an OXM TLV struct.""" self.tlv = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, oxm_field=OxmOfbMatchField.OFPXMT_OFB_IN_PHY_PORT, @@ -49,7 +49,7 @@ def test_different_class_types(self): OxmClass.OFPXMC_EXPERIMENTER): self.tlv.oxm_class = oxm_class unpacked = self._create_from_pack() - self.assertEqual(oxm_class, unpacked.oxm_class) + assert oxm_class == unpacked.oxm_class def test_different_fields(self): """Pack, unpack the result and assert the values are equal.""" @@ -57,21 +57,21 @@ def test_different_fields(self): OxmOfbMatchField.OFPXMT_OFB_IPV6_EXTHDR): self.tlv.oxm_field = oxm_field unpacked = self._create_from_pack() - self.assertEqual(oxm_field, unpacked.oxm_field) + assert oxm_field == unpacked.oxm_field def test_hasmask_bit(self): """Pack, unpack the result and assert the values are equal.""" for oxm_hasmask in True, False: self.tlv.oxm_hasmask = oxm_hasmask unpacked = self._create_from_pack() - self.assertEqual(oxm_hasmask, unpacked.oxm_hasmask) + assert oxm_hasmask == unpacked.oxm_hasmask def test_different_values(self): """Pack, unpack the result and assert the values are equal.""" for oxm_value in b'', b'abc': self.tlv.oxm_value = oxm_value unpacked = self._create_from_pack() - self.assertEqual(oxm_value, unpacked.oxm_value) + assert oxm_value == unpacked.oxm_value def _create_from_pack(self): """Return a new instance by unpacking self.tlv.pack().""" @@ -83,7 +83,7 @@ def test_pack_overflowed_field(self): """Raise PackException if field is bigger than 7 bit.""" self.tlv.oxm_class = OxmClass.OFPXMC_EXPERIMENTER self.tlv.oxm_field = 2**7 - with self.assertRaises(PackException): + with pytest.raises(PackException): self.tlv.pack() def test_pack_invalid_field(self): @@ -93,7 +93,7 @@ def test_pack_invalid_field(self): """ self.tlv.oxm_class = OxmClass.OFPXMC_OPENFLOW_BASIC self.tlv.oxm_field = 42 - with self.assertRaises(PackException): + with pytest.raises(PackException): self.tlv.pack() def test_unpack_invalid_field(self): @@ -103,7 +103,7 @@ def test_unpack_invalid_field(self): """ field42 = b'\x80\x00T\x00' tlv = OxmTLV() - with self.assertRaises(UnpackException): + with pytest.raises(UnpackException): tlv.unpack(field42) def test_max_field_value(self): @@ -112,4 +112,4 @@ def test_max_field_value(self): self.tlv.oxm_field = 127 unpacked = OxmTLV() unpacked.unpack(self.tlv.pack()) - self.assertEqual(self.tlv, unpacked) + assert self.tlv == unpacked diff --git a/tests/unit/v0x04/test_common/test_port.py b/tests/unit/v0x04/test_common/test_port.py index f2e938b38..d46a48f00 100644 --- a/tests/unit/v0x04/test_common/test_port.py +++ b/tests/unit/v0x04/test_common/test_port.py @@ -1,15 +1,13 @@ """Test of Port class from common module.""" from pyof.v0x04.common.port import Port -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestPort(TestStruct): +class TestPort(StructTest): """Port structure tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'port') - super().set_raw_dump_object(Port) - super().set_minimum_size(64) + self.set_raw_dump_file('v0x04', 'port') + self.set_raw_dump_object(Port) + self.set_minimum_size(64) diff --git a/tests/unit/v0x04/test_common/test_queue.py b/tests/unit/v0x04/test_common/test_queue.py index bb516260c..639f0e577 100644 --- a/tests/unit/v0x04/test_common/test_queue.py +++ b/tests/unit/v0x04/test_common/test_queue.py @@ -2,64 +2,54 @@ from pyof.v0x04.common.queue import ( PacketQueue, QueuePropExperimenter, QueuePropHeader, QueuePropMaxRate, QueuePropMinRate) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestPacketQueue(TestStruct): +class TestPacketQueue(StructTest): """Packet Queue structure tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'packet_queue') - super().set_raw_dump_object(PacketQueue) - super().set_minimum_size(16) + self.set_raw_dump_file('v0x04', 'packet_queue') + self.set_raw_dump_object(PacketQueue) + self.set_minimum_size(16) -class TestQueuePropExperimenter(TestStruct): +class TestQueuePropExperimenter(StructTest): """QueuePropExperimenter tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'queue_prop_experimenter') - super().set_raw_dump_object(QueuePropExperimenter) - super().set_minimum_size(16) + self.set_raw_dump_file('v0x04', 'queue_prop_experimenter') + self.set_raw_dump_object(QueuePropExperimenter) + self.set_minimum_size(16) -class TestQueuePropHeader(TestStruct): +class TestQueuePropHeader(StructTest): """QueuePropHeader structure tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'queue_prop_header') - super().set_raw_dump_object(QueuePropHeader) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x04', 'queue_prop_header') + self.set_raw_dump_object(QueuePropHeader) + self.set_minimum_size(8) -class TestQueuePropMaxRate(TestStruct): +class TestQueuePropMaxRate(StructTest): """QueuePropMaxRate structure tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'queue_prop_max_rate') - super().set_raw_dump_object(QueuePropMaxRate) - super().set_minimum_size(16) + self.set_raw_dump_file('v0x04', 'queue_prop_max_rate') + self.set_raw_dump_object(QueuePropMaxRate) + self.set_minimum_size(16) -class TestQueuePropMinRate(TestStruct): +class TestQueuePropMinRate(StructTest): """QueuePropMinRate structure tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'queue_prop_min_rate') - super().set_raw_dump_object(QueuePropMinRate) - super().set_minimum_size(16) + self.set_raw_dump_file('v0x04', 'queue_prop_min_rate') + self.set_raw_dump_object(QueuePropMinRate) + self.set_minimum_size(16) diff --git a/tests/unit/v0x04/test_controller2switch/test_aggregate_stats.py b/tests/unit/v0x04/test_controller2switch/test_aggregate_stats.py index 23dbc39b5..58e093f91 100644 --- a/tests/unit/v0x04/test_controller2switch/test_aggregate_stats.py +++ b/tests/unit/v0x04/test_controller2switch/test_aggregate_stats.py @@ -2,23 +2,21 @@ from pyof.v0x04.controller2switch.common import MultipartType from pyof.v0x04.controller2switch.multipart_reply import ( AggregateStatsReply, MultipartReply) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestAggregateStats(TestStruct): +class TestAggregateStats(StructTest): """Aggregate stats message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" mp_type = MultipartType.OFPMP_AGGREGATE - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_aggregate_stats') - super().set_raw_dump_object(MultipartReply, xid=1, + self.set_raw_dump_file('v0x04', 'ofpt_aggregate_stats') + self.set_raw_dump_object(MultipartReply, xid=1, multipart_type=mp_type, flags=0, body=_get_body()) - super().set_minimum_size(16) + self.set_minimum_size(16) def _get_body(): diff --git a/tests/unit/v0x04/test_controller2switch/test_aggregate_stats_request.py b/tests/unit/v0x04/test_controller2switch/test_aggregate_stats_request.py index 1b88942d6..241684181 100644 --- a/tests/unit/v0x04/test_controller2switch/test_aggregate_stats_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_aggregate_stats_request.py @@ -3,22 +3,20 @@ from pyof.v0x04.controller2switch.common import MultipartType from pyof.v0x04.controller2switch.multipart_request import ( AggregateStatsRequest, MultipartRequest) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestAggregateStatsRequest(TestStruct): +class TestAggregateStatsRequest(StructTest): """Aggregate stats request message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" mp_type = MultipartType.OFPMP_AGGREGATE - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_aggregate_stats_request') - super().set_raw_dump_object(MultipartRequest, xid=1, + self.set_raw_dump_file('v0x04', 'ofpt_aggregate_stats_request') + self.set_raw_dump_object(MultipartRequest, xid=1, multipart_type=mp_type, flags=0, body=_get_body()) - super().set_minimum_size(16) + self.set_minimum_size(16) def _get_body(): diff --git a/tests/unit/v0x04/test_controller2switch/test_barrier_reply.py b/tests/unit/v0x04/test_controller2switch/test_barrier_reply.py index 05a5d07d0..e9b3bf238 100644 --- a/tests/unit/v0x04/test_controller2switch/test_barrier_reply.py +++ b/tests/unit/v0x04/test_controller2switch/test_barrier_reply.py @@ -1,15 +1,13 @@ """Barrier reply message tests.""" from pyof.v0x04.controller2switch.barrier_reply import BarrierReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestBarrierReply(TestStruct): +class TestBarrierReply(StructTest): """Barrier reply message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_barrier_reply') - super().set_raw_dump_object(BarrierReply, xid=5) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x04', 'ofpt_barrier_reply') + self.set_raw_dump_object(BarrierReply, xid=5) + self.set_minimum_size(8) diff --git a/tests/unit/v0x04/test_controller2switch/test_barrier_request.py b/tests/unit/v0x04/test_controller2switch/test_barrier_request.py index 79964a50e..a8916f328 100644 --- a/tests/unit/v0x04/test_controller2switch/test_barrier_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_barrier_request.py @@ -1,15 +1,13 @@ """Barrier request message tests.""" from pyof.v0x04.controller2switch.barrier_request import BarrierRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestBarrierRequest(TestStruct): +class TestBarrierRequest(StructTest): """Barrier reply message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_barrier_request') - super().set_raw_dump_object(BarrierRequest, xid=5) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x04', 'ofpt_barrier_request') + self.set_raw_dump_object(BarrierRequest, xid=5) + self.set_minimum_size(8) diff --git a/tests/unit/v0x04/test_controller2switch/test_features_reply.py b/tests/unit/v0x04/test_controller2switch/test_features_reply.py index fc7be6459..806ae0348 100644 --- a/tests/unit/v0x04/test_controller2switch/test_features_reply.py +++ b/tests/unit/v0x04/test_controller2switch/test_features_reply.py @@ -1,20 +1,18 @@ """Echo request message tests.""" from pyof.foundation.basic_types import DPID from pyof.v0x04.controller2switch.features_reply import FeaturesReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFeaturesReply(TestStruct): +class TestFeaturesReply(StructTest): """Feature reply message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_features_reply') + self.set_raw_dump_file('v0x04', 'ofpt_features_reply') kwargs = _get_kwargs() - super().set_raw_dump_object(FeaturesReply, **kwargs) - super().set_minimum_size(32) + self.set_raw_dump_object(FeaturesReply, **kwargs) + self.set_minimum_size(32) def _get_kwargs(): diff --git a/tests/unit/v0x04/test_controller2switch/test_features_request.py b/tests/unit/v0x04/test_controller2switch/test_features_request.py index c5d27068e..0d2d201f3 100644 --- a/tests/unit/v0x04/test_controller2switch/test_features_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_features_request.py @@ -1,15 +1,13 @@ """Feature request message tests.""" from pyof.v0x04.controller2switch.features_request import FeaturesRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFeaturesRequest(TestStruct): +class TestFeaturesRequest(StructTest): """Feature request message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_features_request') - super().set_raw_dump_object(FeaturesRequest, xid=3) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x04', 'ofpt_features_request') + self.set_raw_dump_object(FeaturesRequest, xid=3) + self.set_minimum_size(8) diff --git a/tests/unit/v0x04/test_controller2switch/test_flow_mod.py b/tests/unit/v0x04/test_controller2switch/test_flow_mod.py index 0faa0db17..a00c3420c 100644 --- a/tests/unit/v0x04/test_controller2switch/test_flow_mod.py +++ b/tests/unit/v0x04/test_controller2switch/test_flow_mod.py @@ -6,21 +6,20 @@ Match, MatchType, OxmClass, OxmOfbMatchField, OxmTLV) from pyof.v0x04.common.port import PortNo from pyof.v0x04.controller2switch.flow_mod import FlowMod, FlowModCommand -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFlowMod(TestStruct): +class TestFlowMod(StructTest): """FlowMod test.""" - def test_min_size(self): - """Test struct minimum size.""" - super().set_raw_dump_file('v0x04', 'ofpt_flow_mod') - super().set_raw_dump_object(FlowMod, xid=2219910763, + def setup_method(self): + self.set_raw_dump_file('v0x04', 'ofpt_flow_mod') + self.set_raw_dump_object(FlowMod, xid=2219910763, command=FlowModCommand.OFPFC_ADD, priority=1000, match=_new_match(), instructions=_new_list_of_instructions()) - super().set_minimum_size(56) + self.set_minimum_size(56) def _new_match(): diff --git a/tests/unit/v0x04/test_controller2switch/test_flow_stats.py b/tests/unit/v0x04/test_controller2switch/test_flow_stats.py index 3f9653738..1dea54f13 100644 --- a/tests/unit/v0x04/test_controller2switch/test_flow_stats.py +++ b/tests/unit/v0x04/test_controller2switch/test_flow_stats.py @@ -8,22 +8,20 @@ from pyof.v0x04.controller2switch.common import MultipartType from pyof.v0x04.controller2switch.multipart_reply import ( FlowStats, MultipartReply) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFlowStats(TestStruct): +class TestFlowStats(StructTest): """Flow stats message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_flow_stats') - super().set_raw_dump_object(MultipartReply, xid=2898845528, + self.set_raw_dump_file('v0x04', 'ofpt_flow_stats') + self.set_raw_dump_object(MultipartReply, xid=2898845528, multipart_type=MultipartType.OFPMP_FLOW, flags=0, body=_get_body()) - super().set_minimum_size(16) + self.set_minimum_size(16) def _get_body(): diff --git a/tests/unit/v0x04/test_controller2switch/test_flow_stats_request.py b/tests/unit/v0x04/test_controller2switch/test_flow_stats_request.py index b3c8fdcda..66987fea7 100644 --- a/tests/unit/v0x04/test_controller2switch/test_flow_stats_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_flow_stats_request.py @@ -3,21 +3,19 @@ from pyof.v0x04.controller2switch.common import MultipartType from pyof.v0x04.controller2switch.multipart_request import ( FlowStatsRequest, MultipartRequest) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestFlowStatsRequest(TestStruct): +class TestFlowStatsRequest(StructTest): """Flow stats request message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_flow_stats_request') - super().set_raw_dump_object(MultipartRequest, xid=590715727, + self.set_raw_dump_file('v0x04', 'ofpt_flow_stats_request') + self.set_raw_dump_object(MultipartRequest, xid=590715727, multipart_type=MultipartType.OFPMP_FLOW, flags=0, body=_get_body()) - super().set_minimum_size(16) + self.set_minimum_size(16) def _get_body(): diff --git a/tests/unit/v0x04/test_controller2switch/test_get_async_reply.py b/tests/unit/v0x04/test_controller2switch/test_get_async_reply.py index bccd85352..2982d9466 100644 --- a/tests/unit/v0x04/test_controller2switch/test_get_async_reply.py +++ b/tests/unit/v0x04/test_controller2switch/test_get_async_reply.py @@ -1,18 +1,16 @@ """GetAsyncReply message tests.""" from pyof.v0x04.controller2switch.get_async_reply import GetAsyncReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestGetAsyncReply(TestStruct): +class TestGetAsyncReply(StructTest): """Test the GetAsyncReply message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_get_async_reply') - super().set_raw_dump_object(GetAsyncReply, xid=3, packet_in_mask1=0, + self.set_raw_dump_file('v0x04', 'ofpt_get_async_reply') + self.set_raw_dump_object(GetAsyncReply, xid=3, packet_in_mask1=0, packet_in_mask2=0, port_status_mask1=0, port_status_mask2=0, flow_removed_mask1=0, flow_removed_mask2=0) - super().set_minimum_size(32) + self.set_minimum_size(32) diff --git a/tests/unit/v0x04/test_controller2switch/test_get_async_request.py b/tests/unit/v0x04/test_controller2switch/test_get_async_request.py index 73efe1d25..2e68b6417 100644 --- a/tests/unit/v0x04/test_controller2switch/test_get_async_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_get_async_request.py @@ -1,15 +1,13 @@ """GetAsyncRequest message tests.""" from pyof.v0x04.controller2switch.get_async_request import GetAsyncRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestGetAsyncRequest(TestStruct): +class TestGetAsyncRequest(StructTest): """Test the GetAsyncRequest message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_get_async_request') - super().set_raw_dump_object(GetAsyncRequest, xid=3) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x04', 'ofpt_get_async_request') + self.set_raw_dump_object(GetAsyncRequest, xid=3) + self.set_minimum_size(8) diff --git a/tests/unit/v0x04/test_controller2switch/test_get_config_reply.py b/tests/unit/v0x04/test_controller2switch/test_get_config_reply.py index ba276bca2..67f03be74 100644 --- a/tests/unit/v0x04/test_controller2switch/test_get_config_reply.py +++ b/tests/unit/v0x04/test_controller2switch/test_get_config_reply.py @@ -1,15 +1,13 @@ """Config Reply message tests.""" from pyof.v0x04.controller2switch.get_config_reply import GetConfigReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestGetConfigReply(TestStruct): +class TestGetConfigReply(StructTest): """Config Reply message tests.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_get_config_reply') - super().set_raw_dump_object(GetConfigReply, xid=1) - super().set_minimum_size(12) + self.set_raw_dump_file('v0x04', 'ofpt_get_config_reply') + self.set_raw_dump_object(GetConfigReply, xid=1) + self.set_minimum_size(12) diff --git a/tests/unit/v0x04/test_controller2switch/test_get_config_request.py b/tests/unit/v0x04/test_controller2switch/test_get_config_request.py index 04a5eb130..4b382755a 100644 --- a/tests/unit/v0x04/test_controller2switch/test_get_config_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_get_config_request.py @@ -1,15 +1,13 @@ """Config Request message tests.""" from pyof.v0x04.controller2switch.get_config_request import GetConfigRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestGetConfigRequest(TestStruct): +class TestGetConfigRequest(StructTest): """Config Request message tests.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_get_config_request') - super().set_raw_dump_object(GetConfigRequest) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x04', 'ofpt_get_config_request') + self.set_raw_dump_object(GetConfigRequest) + self.set_minimum_size(8) diff --git a/tests/unit/v0x04/test_controller2switch/test_group_mod.py b/tests/unit/v0x04/test_controller2switch/test_group_mod.py index 90c6686de..41807ebc4 100644 --- a/tests/unit/v0x04/test_controller2switch/test_group_mod.py +++ b/tests/unit/v0x04/test_controller2switch/test_group_mod.py @@ -1,6 +1,4 @@ """group_mod tests.""" -from unittest import TestCase - from pyof.v0x04.controller2switch.group_mod import GroupMod from pyof.v0x04.common.action import ( ActionExperimenterDefault, ActionSetField, ListOfActions) @@ -10,28 +8,26 @@ from pyof.v0x04.controller2switch.group_mod import ListOfBuckets -class TestGroupMod(TestCase): +class TestGroupMod: """group_mod tests.""" def test_min_size(self): """Test minimum struct size.""" - self.assertEqual(16, GroupMod().get_size()) + assert 16 == GroupMod().get_size() -class TestBucket(TestCase): +class TestBucket: """bucket tests.""" def test_min_size(self): """Test minimum struct size.""" - self.assertEqual(16, Bucket().get_size()) + assert 16 == Bucket().get_size() -class TestListBuckets(TestCase): +class TestListBuckets: - def setUp(self): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUp() - self.oxmtlv1 = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, oxm_field=OxmOfbMatchField.OFPXMT_OFB_METADATA, oxm_hasmask=False, @@ -69,34 +65,34 @@ def test_bucket_list(self): unpacked_buckets = ListOfBuckets() unpacked_buckets.unpack(buff) - self.assertEqual(len(unpacked_buckets), 3) - self.assertEqual(unpacked_buckets[0].length, 48) - self.assertEqual(unpacked_buckets[0].weight, 1) - self.assertEqual(len(unpacked_buckets[0].actions), 2) - self.assertEqual(unpacked_buckets[0].actions[0].field.oxm_value, - self.oxmtlv1.oxm_value) - self.assertEqual(unpacked_buckets[0].actions[1].field.oxm_value, - self.oxmtlv2.oxm_value) - - self.assertEqual(unpacked_buckets[1].length, 80) - self.assertEqual(unpacked_buckets[1].weight, 2) - self.assertEqual(len(unpacked_buckets[1].actions), 4) - self.assertEqual(unpacked_buckets[1].actions[0].field.oxm_value, - self.oxmtlv1.oxm_value) - self.assertEqual(unpacked_buckets[1].actions[1].field.oxm_value, - self.oxmtlv2.oxm_value) - self.assertEqual(unpacked_buckets[1].actions[2].body, - self.action3.body) - self.assertEqual(unpacked_buckets[1].actions[3].body, - self.action4.body) - - self.assertEqual(unpacked_buckets[2].length, 48) - self.assertEqual(unpacked_buckets[2].weight, 3) - self.assertEqual(len(unpacked_buckets[2].actions), 2) - self.assertEqual(unpacked_buckets[2].actions[0].body, - self.action3.body) - self.assertEqual(unpacked_buckets[2].actions[1].body, - self.action4.body) + assert len(unpacked_buckets) == 3 + assert unpacked_buckets[0].length == 48 + assert unpacked_buckets[0].weight == 1 + assert len(unpacked_buckets[0].actions) == 2 + assert unpacked_buckets[0].actions[0].field.oxm_value == \ + self.oxmtlv1.oxm_value + assert unpacked_buckets[0].actions[1].field.oxm_value == \ + self.oxmtlv2.oxm_value + + assert unpacked_buckets[1].length == 80 + assert unpacked_buckets[1].weight == 2 + assert len(unpacked_buckets[1].actions) == 4 + assert unpacked_buckets[1].actions[0].field.oxm_value == \ + self.oxmtlv1.oxm_value + assert unpacked_buckets[1].actions[1].field.oxm_value == \ + self.oxmtlv2.oxm_value + assert unpacked_buckets[1].actions[2].body == \ + self.action3.body + assert unpacked_buckets[1].actions[3].body == \ + self.action4.body + + assert unpacked_buckets[2].length == 48 + assert unpacked_buckets[2].weight == 3 + assert len(unpacked_buckets[2].actions) == 2 + assert unpacked_buckets[2].actions[0].body == \ + self.action3.body + assert unpacked_buckets[2].actions[1].body == \ + self.action4.body def test_buckets_one_item(self): @@ -112,14 +108,14 @@ def test_buckets_one_item(self): unpacked_buckets = ListOfBuckets() unpacked_buckets.unpack(buff) - self.assertEqual(len(unpacked_buckets), 1) - self.assertEqual(unpacked_buckets[0].length, 48) - self.assertEqual(unpacked_buckets[0].weight, 1) - self.assertEqual(len(unpacked_buckets[0].actions), 2) - self.assertEqual(unpacked_buckets[0].actions[0].field.oxm_value, - self.oxmtlv1.oxm_value) - self.assertEqual(unpacked_buckets[0].actions[1].field.oxm_value, - self.oxmtlv2.oxm_value) + assert len(unpacked_buckets) == 1 + assert unpacked_buckets[0].length == 48 + assert unpacked_buckets[0].weight == 1 + assert len(unpacked_buckets[0].actions) == 2 + assert unpacked_buckets[0].actions[0].field.oxm_value == \ + self.oxmtlv1.oxm_value + assert unpacked_buckets[0].actions[1].field.oxm_value == \ + self.oxmtlv2.oxm_value def test_buckets_no_action(self): @@ -135,9 +131,9 @@ def test_buckets_no_action(self): unpacked_buckets = ListOfBuckets() unpacked_buckets.unpack(buff) - self.assertEqual(len(unpacked_buckets), 1) - self.assertEqual(unpacked_buckets[0].length, 48) - self.assertEqual(unpacked_buckets[0].weight, 1) - self.assertEqual(len(unpacked_buckets[0].actions), 1) - self.assertEqual(unpacked_buckets[0].actions[0].field.oxm_value, - self.oxmtlv1.oxm_value) + assert len(unpacked_buckets) == 1 + assert unpacked_buckets[0].length == 48 + assert unpacked_buckets[0].weight == 1 + assert len(unpacked_buckets[0].actions) == 1 + assert unpacked_buckets[0].actions[0].field.oxm_value == \ + self.oxmtlv1.oxm_value diff --git a/tests/unit/v0x04/test_controller2switch/test_group_stats.py b/tests/unit/v0x04/test_controller2switch/test_group_stats.py index f6961eaaa..502bbb72e 100644 --- a/tests/unit/v0x04/test_controller2switch/test_group_stats.py +++ b/tests/unit/v0x04/test_controller2switch/test_group_stats.py @@ -9,22 +9,20 @@ BucketCounter, ListOfBucketCounter, MultipartType) from pyof.v0x04.controller2switch.multipart_reply import ( GroupStats, MultipartReply) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestGroupStats(TestStruct): +class TestGroupStats(StructTest): """Group stats message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_group_stats') - super().set_raw_dump_object(MultipartReply, xid=1, + self.set_raw_dump_file('v0x04', 'ofpt_group_stats') + self.set_raw_dump_object(MultipartReply, xid=1, multipart_type=MultipartType.OFPMP_GROUP, flags=0, body=_get_body()) - super().set_minimum_size(16) + self.set_minimum_size(16) def _get_body(): diff --git a/tests/unit/v0x04/test_controller2switch/test_group_stats_request.py b/tests/unit/v0x04/test_controller2switch/test_group_stats_request.py index 2779a35a2..cc92dc0c7 100644 --- a/tests/unit/v0x04/test_controller2switch/test_group_stats_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_group_stats_request.py @@ -2,21 +2,19 @@ from pyof.v0x04.controller2switch.common import MultipartType from pyof.v0x04.controller2switch.multipart_request import ( GroupStatsRequest, MultipartRequest) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestGroupStatsRequest(TestStruct): +class TestGroupStatsRequest(StructTest): """Group stats request message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_group_stats_request') - super().set_raw_dump_object(MultipartRequest, xid=1, + self.set_raw_dump_file('v0x04', 'ofpt_group_stats_request') + self.set_raw_dump_object(MultipartRequest, xid=1, multipart_type=MultipartType.OFPMP_GROUP, flags=0, body=_get_body()) - super().set_minimum_size(16) + self.set_minimum_size(16) def _get_body(): diff --git a/tests/unit/v0x04/test_controller2switch/test_meter_mod.py b/tests/unit/v0x04/test_controller2switch/test_meter_mod.py index e3dc24f51..17243bf61 100644 --- a/tests/unit/v0x04/test_controller2switch/test_meter_mod.py +++ b/tests/unit/v0x04/test_controller2switch/test_meter_mod.py @@ -1,46 +1,44 @@ """MeterMod tests.""" -from unittest import TestCase - from pyof.v0x04.controller2switch.meter_mod import ( MeterBandDrop, MeterBandDscpRemark, MeterBandExperimenter, MeterBandHeader, MeterMod) -class TestMeterMod(TestCase): +class TestMeterMod: """MeterMod test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(16, MeterMod().get_size()) + assert 16 == MeterMod().get_size() -class TestMeterBandHeader(TestCase): +class TestMeterBandHeader: """MeterBandHeader test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(12, MeterBandHeader().get_size()) + assert 12 == MeterBandHeader().get_size() -class TestMeterBandDrop(TestCase): +class TestMeterBandDrop: """MeterBandDrop test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(16, MeterBandDrop().get_size()) + assert 16 == MeterBandDrop().get_size() -class TestMeterBandDscpRemark(TestCase): +class TestMeterBandDscpRemark: """MeterBandDscpRemark test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(16, MeterBandDscpRemark().get_size()) + assert 16 == MeterBandDscpRemark().get_size() -class TestMeterBandExperimenter(TestCase): +class TestMeterBandExperimenter: """MeterBandExperimenter test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(16, MeterBandExperimenter().get_size()) + assert 16 == MeterBandExperimenter().get_size() diff --git a/tests/unit/v0x04/test_controller2switch/test_meter_multipart_request.py b/tests/unit/v0x04/test_controller2switch/test_meter_multipart_request.py index 497a3365b..53f3857c7 100644 --- a/tests/unit/v0x04/test_controller2switch/test_meter_multipart_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_meter_multipart_request.py @@ -1,22 +1,20 @@ """MultipartRequest message test.""" from pyof.v0x04.controller2switch.multipart_request import ( MeterMultipartRequest, MultipartRequest, MultipartType) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestMeterMultipartRequest(TestStruct): +class TestMeterMultipartRequest(StructTest): """Test the MultipartRequest message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" mp_type = MultipartType.OFPMP_METER - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_meter_multipart_request') - super().set_raw_dump_object(MultipartRequest, xid=1, + self.set_raw_dump_file('v0x04', 'ofpt_meter_multipart_request') + self.set_raw_dump_object(MultipartRequest, xid=1, multipart_type=mp_type, flags=0, body=_get_body()) - super().set_minimum_size(16) + self.set_minimum_size(16) def _get_body(): diff --git a/tests/unit/v0x04/test_controller2switch/test_multipart_reply.py b/tests/unit/v0x04/test_controller2switch/test_multipart_reply.py index f1421136f..41abfc908 100644 --- a/tests/unit/v0x04/test_controller2switch/test_multipart_reply.py +++ b/tests/unit/v0x04/test_controller2switch/test_multipart_reply.py @@ -3,21 +3,19 @@ from pyof.v0x04.controller2switch.common import MultipartType from pyof.v0x04.controller2switch.multipart_reply import ( Desc, MultipartReply, MultipartReplyFlags) -from tests.unit.v0x04.test_struct import TestStruct +from tests.unit.v0x04.test_struct import StructTest -class TestMultipartReply(TestStruct): +class TestMultipartReply(StructTest): """Test MultipartReply.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_message(MultipartReply, xid=16, + self.set_message(MultipartReply, xid=16, multipart_type=MultipartType.OFPMP_METER_CONFIG, flags=MultipartReplyFlags.OFPMPF_REPLY_MORE, body=b'') - super().set_minimum_size(16) + self.set_minimum_size(16) @staticmethod def get_attributes(multipart_type=MultipartType.OFPMP_DESC, diff --git a/tests/unit/v0x04/test_controller2switch/test_multipart_request.py b/tests/unit/v0x04/test_controller2switch/test_multipart_request.py index 76f061223..7f31f7734 100644 --- a/tests/unit/v0x04/test_controller2switch/test_multipart_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_multipart_request.py @@ -2,22 +2,19 @@ from pyof.v0x04.controller2switch.multipart_request import ( MultipartRequest, MultipartRequestFlags, MultipartType, PortStatsRequest, TableFeatures) -from tests.unit.v0x04.test_struct import TestStruct +from tests.unit.v0x04.test_struct import StructTest -class TestMultipartRequest(TestStruct): +class TestMultipartRequest(StructTest): """Test the MultipartRequest message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - - super().set_message(MultipartRequest, xid=16, + self.set_message(MultipartRequest, xid=16, multipart_type=MultipartType.OFPMP_TABLE_FEATURES, flags=MultipartRequestFlags.OFPMPF_REQ_MORE, body=b'') - super().set_minimum_size(16) + self.set_minimum_size(16) @staticmethod def get_attributes(multipart_type=MultipartType.OFPMP_DESC, diff --git a/tests/unit/v0x04/test_controller2switch/test_packet_out.py b/tests/unit/v0x04/test_controller2switch/test_packet_out.py index 6bb3dc33e..0c4e7730f 100644 --- a/tests/unit/v0x04/test_controller2switch/test_packet_out.py +++ b/tests/unit/v0x04/test_controller2switch/test_packet_out.py @@ -4,12 +4,12 @@ from pyof.v0x04.common.constants import OFP_NO_BUFFER from pyof.v0x04.common.port import PortNo from pyof.v0x04.controller2switch.packet_out import PacketOut -from tests.unit.test_struct import TestStruct - +from tests.unit.test_struct import StructTest +import pytest NO_RAW = 'No raw dump file found.' -class TestPacketOut(TestStruct): +class TestPacketOut(StructTest): """Packet out message tests (also those in :class:`.TestDump`). Attributes: @@ -17,26 +17,26 @@ class TestPacketOut(TestStruct): """ - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_packet_out') - super().set_raw_dump_object(PacketOut, xid=2544740805, + self.set_raw_dump_file('v0x04', 'ofpt_packet_out') + self.set_raw_dump_object(PacketOut, xid=2544740805, buffer_id=OFP_NO_BUFFER, in_port=PortNo.OFPP_CONTROLLER, actions=_get_actions(), data=_get_data()) - super().set_minimum_size(24) + self.set_minimum_size(24) - def test_valid_virtual_in_ports(self): + @pytest.mark.parametrize("port", + [ + PortNo.OFPP_LOCAL, + PortNo.OFPP_CONTROLLER, + PortNo.OFPP_ANY + ] + ) + def test_valid_virtual_in_ports(self, port): """Valid virtual ports as defined in 1.3.0 spec.""" - virtual_ports = (PortNo.OFPP_LOCAL, PortNo.OFPP_CONTROLLER, - PortNo.OFPP_ANY) - for port in virtual_ports: - with self.subTest(port=port): - msg = PacketOut(in_port=port) - self.assertTrue(msg.is_valid(), - f'{port.name} should be a valid in_port') + msg = PacketOut(in_port=port) + assert msg.is_valid(), f'{port.name} should be a valid in_port' def test_invalid_virtual_in_ports(self): """Invalid virtual ports as defined in 1.3.0 spec.""" @@ -50,8 +50,8 @@ def test_invalid_virtual_in_ports(self): msg = self.get_raw_object() for in_port in invalid: msg.in_port = in_port - self.assertFalse(msg.is_valid()) - self.assertRaises(ValidationError, msg.validate) + assert not msg.is_valid() + pytest.raises(ValidationError, msg.validate) def test_valid_physical_in_ports(self): """Physical port limits from 1.3.0 spec.""" @@ -64,7 +64,7 @@ def test_valid_physical_in_ports(self): msg = self.get_raw_object() for in_port in (1, max_valid): msg.in_port = in_port - self.assertTrue(msg.is_valid()) + assert msg.is_valid() def _get_actions(): diff --git a/tests/unit/v0x04/test_controller2switch/test_port_desc.py b/tests/unit/v0x04/test_controller2switch/test_port_desc.py index ffda99e6b..c6205d2ee 100644 --- a/tests/unit/v0x04/test_controller2switch/test_port_desc.py +++ b/tests/unit/v0x04/test_controller2switch/test_port_desc.py @@ -5,23 +5,21 @@ ListOfPorts, Port, PortConfig, PortFeatures, PortNo, PortState) from pyof.v0x04.controller2switch.common import MultipartType from pyof.v0x04.controller2switch.multipart_reply import MultipartReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestPortDesc(TestStruct): +class TestPortDesc(StructTest): """Test PortDesc.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" mp_type = MultipartType.OFPMP_PORT_DESC - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_port_desc') - super().set_raw_dump_object(MultipartReply, xid=1917225664, + self.set_raw_dump_file('v0x04', 'ofpt_port_desc') + self.set_raw_dump_object(MultipartReply, xid=1917225664, multipart_type=mp_type, flags=0, body=_get_body()) - super().set_minimum_size(16) + self.set_minimum_size(16) def _get_body(): diff --git a/tests/unit/v0x04/test_controller2switch/test_port_mod.py b/tests/unit/v0x04/test_controller2switch/test_port_mod.py index e898b94a6..1e2f29f07 100644 --- a/tests/unit/v0x04/test_controller2switch/test_port_mod.py +++ b/tests/unit/v0x04/test_controller2switch/test_port_mod.py @@ -1,12 +1,10 @@ """PortMod tests.""" -import unittest - from pyof.v0x04.controller2switch.port_mod import PortMod -class TestPortMod(unittest.TestCase): +class TestPortMod: """PortMod test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(40, PortMod().get_size()) + assert 40 == PortMod().get_size() diff --git a/tests/unit/v0x04/test_controller2switch/test_port_stats.py b/tests/unit/v0x04/test_controller2switch/test_port_stats.py index ebf0608c9..71fad4377 100644 --- a/tests/unit/v0x04/test_controller2switch/test_port_stats.py +++ b/tests/unit/v0x04/test_controller2switch/test_port_stats.py @@ -1,15 +1,13 @@ """Config Port Stats message tests.""" from pyof.v0x04.controller2switch.multipart_reply import PortStats -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestPortStats(TestStruct): +class TestPortStats(StructTest): """Config Port Stats message tests.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_port_stats') - super().set_raw_dump_object(PortStats) - super().set_minimum_size(112) + self.set_raw_dump_file('v0x04', 'ofpt_port_stats') + self.set_raw_dump_object(PortStats) + self.set_minimum_size(112) diff --git a/tests/unit/v0x04/test_controller2switch/test_port_stats_request.py b/tests/unit/v0x04/test_controller2switch/test_port_stats_request.py index aa28a5059..eefcd9580 100644 --- a/tests/unit/v0x04/test_controller2switch/test_port_stats_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_port_stats_request.py @@ -1,15 +1,13 @@ """Config Port Stats Request message tests.""" from pyof.v0x04.controller2switch.multipart_request import PortStatsRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestPortStatsRequest(TestStruct): +class TestPortStatsRequest(StructTest): """Config Port Stats Request message tests.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_port_stats_request') - super().set_raw_dump_object(PortStatsRequest) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x04', 'ofpt_port_stats_request') + self.set_raw_dump_object(PortStatsRequest) + self.set_minimum_size(8) diff --git a/tests/unit/v0x04/test_controller2switch/test_queue_get_config_reply.py b/tests/unit/v0x04/test_controller2switch/test_queue_get_config_reply.py index 833f9e512..fd66e74bf 100644 --- a/tests/unit/v0x04/test_controller2switch/test_queue_get_config_reply.py +++ b/tests/unit/v0x04/test_controller2switch/test_queue_get_config_reply.py @@ -2,20 +2,18 @@ from pyof.v0x04.common.queue import ListOfQueues, PacketQueue from pyof.v0x04.controller2switch.queue_get_config_reply import ( QueueGetConfigReply) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestQueueGetConfigReply(TestStruct): +class TestQueueGetConfigReply(StructTest): """Test the QueueGetConfigReply message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_queue_get_config_reply') - super().set_raw_dump_object(QueueGetConfigReply, xid=1, port=1, + self.set_raw_dump_file('v0x04', 'ofpt_queue_get_config_reply') + self.set_raw_dump_object(QueueGetConfigReply, xid=1, port=1, queues=_new_list_of_queues()) - super().set_minimum_size(16) + self.set_minimum_size(16) def _new_list_of_queues(): diff --git a/tests/unit/v0x04/test_controller2switch/test_queue_get_config_request.py b/tests/unit/v0x04/test_controller2switch/test_queue_get_config_request.py index ee37b2031..a9f8961fa 100644 --- a/tests/unit/v0x04/test_controller2switch/test_queue_get_config_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_queue_get_config_request.py @@ -1,16 +1,14 @@ """Testing QueueGetConfigRequest message.""" from pyof.v0x04.controller2switch.queue_get_config_request import ( QueueGetConfigRequest) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestQueueGetConfigRequest(TestStruct): +class TestQueueGetConfigRequest(StructTest): """Test the QueueGetConfigRequest message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_queue_get_config_request') - super().set_raw_dump_object(QueueGetConfigRequest, xid=1, port=1) - super().set_minimum_size(16) + self.set_raw_dump_file('v0x04', 'ofpt_queue_get_config_request') + self.set_raw_dump_object(QueueGetConfigRequest, xid=1, port=1) + self.set_minimum_size(16) diff --git a/tests/unit/v0x04/test_controller2switch/test_queue_stats.py b/tests/unit/v0x04/test_controller2switch/test_queue_stats.py index 69739ad2e..8aadd6469 100644 --- a/tests/unit/v0x04/test_controller2switch/test_queue_stats.py +++ b/tests/unit/v0x04/test_controller2switch/test_queue_stats.py @@ -1,15 +1,13 @@ """Stats queue message.""" from pyof.v0x04.controller2switch.multipart_reply import QueueStats -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestQueueStats(TestStruct): +class TestQueueStats(StructTest): """Stats queue message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_queue_stats') - super().set_raw_dump_object(QueueStats) - super().set_minimum_size(40) + self.set_raw_dump_file('v0x04', 'ofpt_queue_stats') + self.set_raw_dump_object(QueueStats) + self.set_minimum_size(40) diff --git a/tests/unit/v0x04/test_controller2switch/test_queue_stats_request.py b/tests/unit/v0x04/test_controller2switch/test_queue_stats_request.py index 0a558476c..2ecdc4cc4 100644 --- a/tests/unit/v0x04/test_controller2switch/test_queue_stats_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_queue_stats_request.py @@ -1,15 +1,13 @@ """Queue Stat Request message.""" from pyof.v0x04.controller2switch.multipart_request import QueueStatsRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestQueueStatsRequest(TestStruct): +class TestQueueStatsRequest(StructTest): """Queue Stat Request message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_queue_stats_request') - super().set_raw_dump_object(QueueStatsRequest) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x04', 'ofpt_queue_stats_request') + self.set_raw_dump_object(QueueStatsRequest) + self.set_minimum_size(8) diff --git a/tests/unit/v0x04/test_controller2switch/test_role_reply.py b/tests/unit/v0x04/test_controller2switch/test_role_reply.py index 2c0e103b2..4a94a57be 100644 --- a/tests/unit/v0x04/test_controller2switch/test_role_reply.py +++ b/tests/unit/v0x04/test_controller2switch/test_role_reply.py @@ -1,16 +1,14 @@ """RoleReply message tests.""" from pyof.v0x04.controller2switch.role_reply import RoleReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestRoleReply(TestStruct): +class TestRoleReply(StructTest): """Test the RoleReply message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_role_reply') - super().set_raw_dump_object(RoleReply, xid=3, role=0, + self.set_raw_dump_file('v0x04', 'ofpt_role_reply') + self.set_raw_dump_object(RoleReply, xid=3, role=0, generation_id=0) - super().set_minimum_size(24) + self.set_minimum_size(24) diff --git a/tests/unit/v0x04/test_controller2switch/test_role_request.py b/tests/unit/v0x04/test_controller2switch/test_role_request.py index 14b292503..e116234f2 100644 --- a/tests/unit/v0x04/test_controller2switch/test_role_request.py +++ b/tests/unit/v0x04/test_controller2switch/test_role_request.py @@ -1,16 +1,14 @@ """RoleRequest message tests.""" from pyof.v0x04.controller2switch.role_request import RoleRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestRoleRequest(TestStruct): +class TestRoleRequest(StructTest): """Test the RoleRequest message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_role_request') - super().set_raw_dump_object(RoleRequest, xid=3, role=0, + self.set_raw_dump_file('v0x04', 'ofpt_role_request') + self.set_raw_dump_object(RoleRequest, xid=3, role=0, generation_id=0) - super().set_minimum_size(24) + self.set_minimum_size(24) diff --git a/tests/unit/v0x04/test_controller2switch/test_set_async.py b/tests/unit/v0x04/test_controller2switch/test_set_async.py index 84a52b51a..9399f8a7d 100644 --- a/tests/unit/v0x04/test_controller2switch/test_set_async.py +++ b/tests/unit/v0x04/test_controller2switch/test_set_async.py @@ -1,18 +1,16 @@ """SetAsync message tests.""" from pyof.v0x04.controller2switch.set_async import SetAsync -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestSetAsync(TestStruct): +class TestSetAsync(StructTest): """Test the SetAsync message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_set_async') - super().set_raw_dump_object(SetAsync, xid=3, packet_in_mask1=0, + self.set_raw_dump_file('v0x04', 'ofpt_set_async') + self.set_raw_dump_object(SetAsync, xid=3, packet_in_mask1=0, packet_in_mask2=0, port_status_mask1=0, port_status_mask2=0, flow_removed_mask1=0, flow_removed_mask2=0) - super().set_minimum_size(32) + self.set_minimum_size(32) diff --git a/tests/unit/v0x04/test_controller2switch/test_set_config.py b/tests/unit/v0x04/test_controller2switch/test_set_config.py index 72f8b46b6..e3e9282a9 100644 --- a/tests/unit/v0x04/test_controller2switch/test_set_config.py +++ b/tests/unit/v0x04/test_controller2switch/test_set_config.py @@ -2,19 +2,17 @@ from pyof.v0x04.common.action import ControllerMaxLen from pyof.v0x04.controller2switch.common import ConfigFlag from pyof.v0x04.controller2switch.set_config import SetConfig -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestSetConfig(TestStruct): +class TestSetConfig(StructTest): """Test the Set Config message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" buffer = ControllerMaxLen.OFPCML_NO_BUFFER - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_set_config') - super().set_raw_dump_object(SetConfig, xid=1201346349, + self.set_raw_dump_file('v0x04', 'ofpt_set_config') + self.set_raw_dump_object(SetConfig, xid=1201346349, flags=ConfigFlag.OFPC_FRAG_NORMAL, miss_send_len=buffer) - super().set_minimum_size(12) + self.set_minimum_size(12) diff --git a/tests/unit/v0x04/test_controller2switch/test_table_mod.py b/tests/unit/v0x04/test_controller2switch/test_table_mod.py index 34d7ac329..de7264821 100644 --- a/tests/unit/v0x04/test_controller2switch/test_table_mod.py +++ b/tests/unit/v0x04/test_controller2switch/test_table_mod.py @@ -1,17 +1,15 @@ """Table flow modification tests.""" -import unittest - from pyof.v0x04.common.header import Type from pyof.v0x04.controller2switch.table_mod import TableMod -class TestTableMod(unittest.TestCase): +class TestTableMod: """TableMod test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(16, TableMod().get_size()) + assert 16 == TableMod().get_size() def test_header_type(self): """Test header type.""" - self.assertEqual(Type.OFPT_TABLE_MOD, TableMod().header.message_type) + assert Type.OFPT_TABLE_MOD == TableMod().header.message_type diff --git a/tests/unit/v0x04/test_controller2switch/test_table_stats.py b/tests/unit/v0x04/test_controller2switch/test_table_stats.py index 3c6049c90..19e6f106d 100644 --- a/tests/unit/v0x04/test_controller2switch/test_table_stats.py +++ b/tests/unit/v0x04/test_controller2switch/test_table_stats.py @@ -1,15 +1,13 @@ """Table stats message.""" from pyof.v0x04.controller2switch.multipart_reply import TableStats -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestTableStats(TestStruct): +class TestTableStats(StructTest): """Table stats message.""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_table_stats') - super().set_raw_dump_object(TableStats) - super().set_minimum_size(24) + self.set_raw_dump_file('v0x04', 'ofpt_table_stats') + self.set_raw_dump_object(TableStats) + self.set_minimum_size(24) diff --git a/tests/unit/v0x04/test_struct.py b/tests/unit/v0x04/test_struct.py index 8aebdefe5..a43f53b32 100644 --- a/tests/unit/v0x04/test_struct.py +++ b/tests/unit/v0x04/test_struct.py @@ -1,11 +1,9 @@ """Automate struct tests.""" -import unittest - from pyof.v0x04.common.header import Header from pyof.v0x04.common.utils import new_message_from_header -class TestStruct(unittest.TestCase): +class StructTest: """Run tests related to struct packing and unpacking. Test the lib with raw dump files from an OpenFlow switch. We assume the @@ -16,48 +14,39 @@ class TestStruct(unittest.TestCase): no parameters. To run these tests, just extends this class and call 2 methods in the - ``setUp`` method like the example. + ``setup_method`` method like the example. Example: .. code-block:: python3 - class MyTest(TestStruct): - @classmethod - def setUpClass(cls): - super().setUpClass() + class TestMine(StructTest): + def setup_method(self): # Create BarrierReply(xid=5) - super().set_message(BarrierReply, xid=5) + self.set_message(BarrierReply, xid=5) # As in spec: ``OFP_ASSERT(sizeof(struct ...) == ...);`` - super().set_minimum_size(8) + self.set_minimum_size(8) To only test the minimum size and skip packing/unpacking: .. code-block:: python3 - class MyTest(TestStruct): - @classmethod - def setUpClass(cls): - super().set_message(BarrierReply) - super().set_minimum_size(8) + class TestMine(StructTest): + def setup_method(self): + self.set_message(BarrierReply) + self.set_minimum_size(8) """ - def __init__(self, *args, **kwargs): - """The constructor will avoid that this class tests are executed. + def setup_method(self): + """This parent class will not perform tests because its name does not + start with "Test". The tests in this class are executed through the child, so there's no no need for them to be executed once more through the parent. """ - super().__init__(*args, **kwargs) - # Override the run method, so it does nothing instead of running the - # tests (again). - if self.__class__ == TestStruct: - self.run = lambda *args, **kwargs: None - - _msg_cls = None - _msg_params = None - _min_size = None - - @classmethod - def set_message(cls, msg_cls, *args, **kwargs): + self._msg_self = None + self._msg_params = None + self._min_size = None + + def set_message(self, msg_self, *args, **kwargs): """Set how to create the message object. Args: @@ -65,14 +54,13 @@ def set_message(cls, msg_cls, *args, **kwargs): parameters to instantiate an object. Example: - ``super().__init__(BarrierReply, xid=5)`` will create + ``self.set_message(BarrierReply, xid=5)`` will create ``BarrierReply(xid=5)``. """ - TestStruct._msg_cls = msg_cls - cls._msg_params = (args, kwargs) + self._msg_self = msg_self + self._msg_params = (args, kwargs) - @classmethod - def set_minimum_size(cls, size): + def set_minimum_size(self, size): """Set the struct minimum size (from spec). The minimum size can be found in OF spec. For example, @@ -82,11 +70,11 @@ def set_minimum_size(cls, size): Args: size (int): The minimum size of the struct, in bytes. """ - cls._min_size = size + self._min_size = size def test_pack_unpack(self): """Pack the message, unpack and check whether they are the same.""" - if self._msg_cls: + if self._msg_self: args, kwargs = self._msg_params self._test_pack_unpack(*args, **kwargs) @@ -96,7 +84,7 @@ def _test_pack_unpack(self, *args, **kwargs): Call this method multiple times if you want to test more than one object. """ - obj = self._msg_cls(*args, **kwargs) + obj = self._msg_self(*args, **kwargs) packed = obj.pack() header = Header() header_size = header.get_size() @@ -104,11 +92,11 @@ def _test_pack_unpack(self, *args, **kwargs): unpacked = new_message_from_header(header) unpacked.unpack(packed[header_size:]) - self.assertEqual(packed, unpacked.pack()) + assert packed == unpacked.pack() def test_minimum_size(self): """Test struct minimum size.""" if self._min_size is None: raise self.skipTest('minimum size was not set.') - obj = TestStruct._msg_cls() - self.assertEqual(obj.get_size(), self._min_size) + obj = self._msg_self() + assert obj.get_size() == self._min_size diff --git a/tests/unit/v0x04/test_symmetric/test_echo_reply.py b/tests/unit/v0x04/test_symmetric/test_echo_reply.py index 09ddc0cd2..241952f4a 100644 --- a/tests/unit/v0x04/test_symmetric/test_echo_reply.py +++ b/tests/unit/v0x04/test_symmetric/test_echo_reply.py @@ -1,15 +1,13 @@ """Echo reply message tests.""" from pyof.v0x04.symmetric.echo_reply import EchoReply -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestEchoReply(TestStruct): +class TestEchoReply(StructTest): """Echo reply message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_echo_reply') - super().set_raw_dump_object(EchoReply, xid=0) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x04', 'ofpt_echo_reply') + self.set_raw_dump_object(EchoReply, xid=0) + self.set_minimum_size(8) diff --git a/tests/unit/v0x04/test_symmetric/test_echo_request.py b/tests/unit/v0x04/test_symmetric/test_echo_request.py index 4dd93efff..549938a92 100644 --- a/tests/unit/v0x04/test_symmetric/test_echo_request.py +++ b/tests/unit/v0x04/test_symmetric/test_echo_request.py @@ -1,15 +1,13 @@ """Echo request message tests.""" from pyof.v0x04.symmetric.echo_request import EchoRequest -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestEchoRequest(TestStruct): +class TestEchoRequest(StructTest): """Echo request message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_echo_request') - super().set_raw_dump_object(EchoRequest, xid=0) - super().set_minimum_size(8) + self.set_raw_dump_file('v0x04', 'ofpt_echo_request') + self.set_raw_dump_object(EchoRequest, xid=0) + self.set_minimum_size(8) diff --git a/tests/unit/v0x04/test_symmetric/test_hello.py b/tests/unit/v0x04/test_symmetric/test_hello.py index 85c63ffab..33fb8aa53 100644 --- a/tests/unit/v0x04/test_symmetric/test_hello.py +++ b/tests/unit/v0x04/test_symmetric/test_hello.py @@ -1,20 +1,18 @@ """Hello message tests.""" from pyof.v0x04.symmetric.hello import ( Hello, HelloElemHeader, HelloElemType, ListOfHelloElements) -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestHello(TestStruct): +class TestHello(StructTest): """Hello message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_hello') - super().set_raw_dump_object(Hello, xid=62, + self.set_raw_dump_file('v0x04', 'ofpt_hello') + self.set_raw_dump_object(Hello, xid=62, elements=_new_list_of_elements()) - super().set_minimum_size(8) + self.set_minimum_size(8) def _new_list_of_elements(): diff --git a/tests/unit/v0x04/test_symmetric/test_vendor_header.py b/tests/unit/v0x04/test_symmetric/test_vendor_header.py index 15be9a93b..87603c208 100644 --- a/tests/unit/v0x04/test_symmetric/test_vendor_header.py +++ b/tests/unit/v0x04/test_symmetric/test_vendor_header.py @@ -1,16 +1,14 @@ """Experimenter message tests.""" from pyof.v0x04.symmetric.experimenter import ExperimenterHeader -from tests.unit.test_struct import TestStruct +from tests.unit.test_struct import StructTest -class TestExperimenter(TestStruct): +class TestExperimenter(StructTest): """Experimenter message tests (also those in :class:`.TestDump`).""" - @classmethod - def setUpClass(cls): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUpClass() - super().set_raw_dump_file('v0x04', 'ofpt_experimenter') - super().set_raw_dump_object(ExperimenterHeader, xid=1, experimenter=1, + self.set_raw_dump_file('v0x04', 'ofpt_experimenter') + self.set_raw_dump_object(ExperimenterHeader, xid=1, experimenter=1, exp_type=0) - super().set_minimum_size(16) + self.set_minimum_size(16)