Skip to content

Commit

Permalink
Replace unittest with pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
Alopalao committed Feb 7, 2024
1 parent 5cf78d3 commit cf91c08
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 91 deletions.
24 changes: 11 additions & 13 deletions tests/unit/v0x01/test_common/test_phy_port.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."""
Expand All @@ -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()
28 changes: 14 additions & 14 deletions tests/unit/v0x04/test_common/test_flow_match.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -49,29 +49,29 @@ 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."""
for oxm_field in (OxmOfbMatchField.OFPXMT_OFB_IN_PORT,
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()."""
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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
100 changes: 48 additions & 52 deletions tests/unit/v0x04/test_controller2switch/test_group_mod.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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):

Expand All @@ -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):

Expand All @@ -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
22 changes: 10 additions & 12 deletions tests/unit/v0x04/test_controller2switch/test_meter_mod.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit cf91c08

Please sign in to comment.