diff --git a/of_core/v0x04/action.py b/of_core/v0x04/action.py index 1e3c3a3..0237c31 100644 --- a/of_core/v0x04/action.py +++ b/of_core/v0x04/action.py @@ -1,35 +1,48 @@ -from napps.kytos.of_core.flow import ActionBase +"""Of_core.v0x04.action""" + +# pylint: disable=unused-argument + from napps.amlight.noviflow.pyof.v0x04.action import ( - NoviActionSetBfdData as OFNoviActionSetBfdData, - NoviActionPushInt as OFNoviActionPushInt, NoviActionAddIntMetadata as OFNoviActionAddIntMetadata, +) +from napps.amlight.noviflow.pyof.v0x04.action import ( NoviActionPopInt as OFNoviActionPopInt, - NoviActionSendReport as OFNoviActionSendReport ) +from napps.amlight.noviflow.pyof.v0x04.action import ( + NoviActionPushInt as OFNoviActionPushInt, +) +from napps.amlight.noviflow.pyof.v0x04.action import ( + NoviActionSendReport as OFNoviActionSendReport, +) +from napps.amlight.noviflow.pyof.v0x04.action import ( + NoviActionSetBfdData as OFNoviActionSetBfdData, +) +from napps.kytos.of_core.flow import ActionBase class NoviActionSetBfdData(ActionBase): """Action with an BFD fields.""" - def __init__(self, port_no, my_disc, interval, multiplier, - keep_alive_timeout): + def __init__(self, port_no, my_disc, interval, multiplier, keep_alive_timeout): """Require BFD fields.""" self.port_no = port_no self.my_disc = my_disc self.interval = interval self.multiplier = multiplier self.keep_alive_timeout = keep_alive_timeout - self.action_type = 'set_bfd' + self.action_type = "set_bfd" @classmethod def from_of_action(cls, of_action): """Return a high-level NoviActionSetBfdData instance from pyof.""" - return cls(port_no=of_action.port_no.value, - my_disc=of_action.my_disc.value, - interval=of_action.interval.value, - multiplier=of_action.multiplier.value, - keep_alive_timeout=of_action.keep_alive_timeout.value) + return cls( + port_no=of_action.port_no.value, + my_disc=of_action.my_disc.value, + interval=of_action.interval.value, + multiplier=of_action.multiplier.value, + keep_alive_timeout=of_action.keep_alive_timeout.value, + ) def as_of_action(self): """Return a pyof NoviActionSetBfdData instance.""" @@ -38,15 +51,15 @@ def as_of_action(self): my_disc=self.my_disc, interval=self.interval, multiplier=self.multiplier, - keep_alive_timeout=self.keep_alive_timeout + keep_alive_timeout=self.keep_alive_timeout, ) class NoviActionPushInt(ActionBase): """Action to push INT.""" - def __init__(self, *args): - self.action_type = 'push_int' + def __init__(self): + self.action_type = "push_int" @classmethod def from_of_action(cls, of_action): @@ -62,7 +75,7 @@ class NoviActionAddIntMetadata(ActionBase): """Action to add INT metadata.""" def __init__(self, *args): - self.action_type = 'add_int_metadata' + self.action_type = "add_int_metadata" @classmethod def from_of_action(cls, of_action): @@ -78,7 +91,7 @@ class NoviActionPopInt(ActionBase): """Action to pop INT.""" def __init__(self, *args): - self.action_type = 'pop_int' + self.action_type = "pop_int" @classmethod def from_of_action(cls, of_action): @@ -94,7 +107,7 @@ class NoviActionSendReport(ActionBase): """Action to send INT report.""" def __init__(self, *args): - self.action_type = 'send_report' + self.action_type = "send_report" @classmethod def from_of_action(cls, of_action): diff --git a/pyof/v0x04/action.py b/pyof/v0x04/action.py index a0b37eb..3e6d7b3 100644 --- a/pyof/v0x04/action.py +++ b/pyof/v0x04/action.py @@ -5,7 +5,8 @@ """ from enum import IntEnum -from pyof.foundation.basic_types import Pad, UBInt32, UBInt16, UBInt8 + +from pyof.foundation.basic_types import Pad, UBInt8, UBInt16, UBInt32 from pyof.v0x04.common.action import ActionExperimenter @@ -35,31 +36,32 @@ class ActionExperimenterNoviflow(ActionExperimenter): reserved = UBInt8() novi_action_type = UBInt16(enum_ref=NoviActionType) - _allowed_ids = (0xff000002,) + _allowed_ids = (0xFF000002,) _allowed_novi = () - def __init__(self, length=16, customer=0xff, reserved=0, - novi_action_type=None): - super().__init__(length=length, experimenter=0xff000002) + def __init__(self, length=16, customer=0xFF, reserved=0, novi_action_type=None): + super().__init__(length=length, experimenter=0xFF000002) self.customer = customer self.reserved = reserved self.novi_action_type = novi_action_type @classmethod def get_allowed_novi(cls): + """Get allowed novi""" return cls._allowed_novi @classmethod def get_subclass(cls, buff, offset): + """Get subclass""" novi_action = UBInt16(enum_ref=NoviActionType) - novi_action.unpack(buff, offset=offset+2) + novi_action.unpack(buff, offset=offset + 2) for novi_cls in ActionExperimenterNoviflow.__subclasses__(): if novi_action.value in novi_cls.get_allowed_novi(): return novi_cls return cls -class NoviActionSetBfdData(ActionExperimenterNoviflow): +class NoviActionSetBfdData(ActionExperimenterNoviflow): # LOOK AT ME """Set the BFD data.""" port_no = UBInt32() @@ -71,13 +73,19 @@ class NoviActionSetBfdData(ActionExperimenterNoviflow): _allowed_novi = (NoviActionType.NOVI_ACTION_SET_BFD_DATA,) - def __init__(self, port_no=None, my_disc=None, interval=None, - multiplier=None, keep_alive_timeout=None): + def __init__( + self, + port_no=None, + my_disc=None, + interval=None, + multiplier=None, + keep_alive_timeout=None, + ): super().__init__( length=32, - customer=0xff, + customer=0xFF, reserved=0, - novi_action_type=NoviActionType.NOVI_ACTION_SET_BFD_DATA + novi_action_type=NoviActionType.NOVI_ACTION_SET_BFD_DATA, ) self.port_no = port_no self.my_disc = my_disc @@ -92,13 +100,13 @@ class NoviActionPushInt(ActionExperimenterNoviflow): pad = Pad(4) _allowed_novi = (NoviActionType.NOVI_ACTION_PUSH_INT,) - + def __init__(self): super().__init__( - customer=0xff, + customer=0xFF, reserved=0, - novi_action_type=NoviActionType.NOVI_ACTION_PUSH_INT - ) + novi_action_type=NoviActionType.NOVI_ACTION_PUSH_INT, + ) class NoviActionAddIntMetadata(ActionExperimenterNoviflow): @@ -107,40 +115,40 @@ class NoviActionAddIntMetadata(ActionExperimenterNoviflow): pad = Pad(4) _allowed_novi = (NoviActionType.NOVI_ACTION_ADD_INT_METADATA,) - + def __init__(self): super().__init__( - customer=0xff, + customer=0xFF, reserved=0, - novi_action_type=NoviActionType.NOVI_ACTION_ADD_INT_METADATA + novi_action_type=NoviActionType.NOVI_ACTION_ADD_INT_METADATA, ) class NoviActionPopInt(ActionExperimenterNoviflow): """Pop INT action.""" - + pad = Pad(4) _allowed_novi = (NoviActionType.NOVI_ACTION_POP_INT,) - + def __init__(self): super().__init__( - customer=0xff, + customer=0xFF, reserved=0, - novi_action_type=NoviActionType.NOVI_ACTION_POP_INT + novi_action_type=NoviActionType.NOVI_ACTION_POP_INT, ) class NoviActionSendReport(ActionExperimenterNoviflow): """Pop INT action.""" - + pad = Pad(4) _allowed_novi = (NoviActionType.NOVI_ACTION_SEND_REPORT,) - + def __init__(self): super().__init__( - customer=0xff, + customer=0xFF, reserved=0, - novi_action_type=NoviActionType.NOVI_ACTION_SEND_REPORT + novi_action_type=NoviActionType.NOVI_ACTION_SEND_REPORT, ) diff --git a/settings.py b/settings.py index 4410efc..e69de29 100644 --- a/settings.py +++ b/settings.py @@ -1 +0,0 @@ -"""Module with the Constants used in the amlight/noviflow.""" diff --git a/setup.py b/setup.py index 33b116e..3d9478f 100644 --- a/setup.py +++ b/setup.py @@ -86,7 +86,8 @@ class Test(TestCommand): def run(self): """Run tests.""" - cmd = f"python3 -m pytest tests/ {self.get_args()}" + cmd = "python3 -m pytest tests/ --cov-report term-missing" + cmd += f" {self.get_args()}" try: check_call(cmd, shell=True) except CalledProcessError as exc: @@ -114,8 +115,14 @@ class TestCoverage(Test): def run(self): """Run unittest quietly and display coverage report.""" - cmd = f"python3 -m pytest --cov=. tests/ {self.get_args()}" - call(cmd, shell=True) + cmd = "python3 -m pytest --cov=. tests/ --cov-report term-missing" + cmd += f" {self.get_args()}" + try: + check_call(cmd, shell=True) + except CalledProcessError as exc: + print(exc) + print("Coverage tests failed. Fix the errors above and try again.") + sys.exit(-1) class Linter(SimpleCommand): @@ -126,7 +133,12 @@ class Linter(SimpleCommand): def run(self): """Run yala.""" print("Yala is running. It may take several seconds...") - check_call("yala *.py", shell=True) + try: + check_call("yala *.py tests of_core pyof", shell=True) + print("No linter error found.") + except CalledProcessError: + print("Linter check failed. Fix the error(s) above and try again.") + sys.exit(-1) class KytosInstall: diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 5cedf94..72d2d75 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -1,8 +1,10 @@ """Test Main methods.""" -import pytest +# pylint: disable=attribute-defined-outside-init from unittest.mock import MagicMock, patch +import pytest + from napps.amlight.noviflow.of_core.v0x04.action import ( NoviActionAddIntMetadata, NoviActionPopInt, @@ -26,17 +28,17 @@ NoviActionSetBfdData as OFNoviActionSetBfdData, ) from napps.amlight.noviflow.pyof.v0x04.action import NoviActionType as NType -from napps.amlight.noviflow.pyof.v0x04.action import NoviActionType from napps.kytos.of_core.v0x04.flow import Flow as Flow04 -from pyof.foundation.basic_types import UBInt8, UBInt32 - from kytos.lib.helpers import get_controller_mock, get_switch_mock +from pyof.foundation.basic_types import UBInt8, UBInt32 + class TestMain: """Tests for the Main class.""" def setup_method(self): + """Setup method""" patch("kytos.core.helpers.run_on_thread", lambda x: x).start() # pylint: disable=import-outside-toplevel from napps.amlight.noviflow.main import Main @@ -107,6 +109,8 @@ def test_execute_register_actions(self) -> None: ), ], ) + + # pylint: disable=no-member def test_create_noviactions(self, payload, expected_class): """Test creating NoviAction classes from a Flow04.""" flow = Flow04.from_dict(payload, self.mock_switch) @@ -175,25 +179,25 @@ def test__eq__success_with_different_flows(self): [ ( OFNoviActionPopInt, - NoviActionType.NOVI_ACTION_POP_INT, + NType.NOVI_ACTION_POP_INT, b"\xff\xff\x00\x10\xff\x00\x00\x02\xff\x00\x00\x0e\x00\x00\x00\x00", NoviActionPopInt, ), ( OFNoviActionPushInt, - NoviActionType.NOVI_ACTION_PUSH_INT, + NType.NOVI_ACTION_PUSH_INT, b"\xff\xff\x00\x10\xff\x00\x00\x02\xff\x00\x00\x0c\x00\x00\x00\x00", NoviActionPushInt, ), ( OFNoviActionAddIntMetadata, - NoviActionType.NOVI_ACTION_ADD_INT_METADATA, + NType.NOVI_ACTION_ADD_INT_METADATA, b"\xff\xff\x00\x10\xff\x00\x00\x02\xff\x00\x00\x0d\x00\x00\x00\x00", NoviActionAddIntMetadata, ), ( OFNoviActionSendReport, - NoviActionType.NOVI_ACTION_SEND_REPORT, + NType.NOVI_ACTION_SEND_REPORT, b"\xff\xff\x00\x10\xff\x00\x00\x02\xff\x00\x00\x0f\x00\x00\x00\x00", NoviActionSendReport, ), @@ -247,7 +251,9 @@ def test_noviaction_set_bfd_data(self): assert action.keep_alive_timeout == 15 packed = action.pack() - expected = b"\xff\xff\x00 \xff\x00\x00\x02\xff\x00\x00\x04\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x05\x03\x0f\x00\x00\x00\x00\x00\x00" + expected = b"\xff\xff\x00 \xff\x00\x00\x02\xff\x00\x00\x04\x00\x00" + expected += b"\x00\x02\x00\x00\x00\x01\x00\x00\x00\x05\x03\x0f\x00" + expected += b"\x00\x00\x00\x00\x00" assert packed == expected assert len(packed) == 32 @@ -255,11 +261,8 @@ def test_noviaction_set_bfd_data(self): unpacked.unpack(packed) assert unpacked.customer == 0xFF assert unpacked.reserved == 0 - assert ( - unpacked.novi_action_type.value - == NoviActionType.NOVI_ACTION_SET_BFD_DATA.value - ) - assert unpacked.port_no == port_no + assert unpacked.novi_action_type.value == NType.NOVI_ACTION_SET_BFD_DATA.value + assert unpacked.port_no == port_no # None = ... assert unpacked.my_disc == my_disc assert unpacked.interval == interval assert unpacked.multiplier == multiplier @@ -278,8 +281,7 @@ def test_noviaction_set_bfd_data(self): assert as_of_action.customer == 0xFF assert as_of_action.reserved == 0 assert ( - as_of_action.novi_action_type.value - == NoviActionType.NOVI_ACTION_SET_BFD_DATA.value + as_of_action.novi_action_type.value == NType.NOVI_ACTION_SET_BFD_DATA.value ) assert as_of_action.port_no == port_no assert as_of_action.my_disc == my_disc