Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run lint in all folders #35

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions of_core/v0x04/action.py
Original file line number Diff line number Diff line change
@@ -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."""
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down
60 changes: 34 additions & 26 deletions pyof/v0x04/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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,
)
1 change: 0 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
"""Module with the Constants used in the amlight/noviflow."""
20 changes: 16 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to kytos issue #487

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):
Expand All @@ -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:
Expand Down
34 changes: 18 additions & 16 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
),
Expand Down Expand Up @@ -247,19 +251,18 @@ 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

unpacked = OFNoviActionSetBfdData()
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
Expand All @@ -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
Expand Down