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

Updated test dependencies #182

Merged
merged 6 commits into from
Mar 28, 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
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ file.
[2023.2.0] - 2024-02-16
***********************

Changed
=======
- Updated python environment installation from 3.9 to 3.11
- Updated test dependencies

[2023.2.0] - 2024-02-16
***********************

Changed
=======

Expand Down
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ To install this NApp, first, make sure to have the same venv activated as you ha

$ git clone https://github.com/kytos-ng/flow_manager.git
$ cd flow_manager
$ python setup.py develop
$ python3 -m pip install --editable .

To install the kytos environment, please follow our
`development environment setup <https://github.com/kytos-ng/documentation/blob/master/tutorials/napps/development_environment_setup.rst>`_.

Requirements
============
Expand Down
1 change: 1 addition & 0 deletions barrier_request.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""kytos/flow_manager barrier_request."""

from pyof.v0x04.controller2switch.barrier_request import BarrierRequest as BReq13


Expand Down
5 changes: 3 additions & 2 deletions controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""FlowController."""

# pylint: disable=unnecessary-lambda,invalid-name,relative-beyond-top-level
import os
from collections import defaultdict
Expand Down Expand Up @@ -84,7 +85,7 @@ def upsert_flows(self, match_ids: List[str], flow_dicts: List[dict]) -> dict:
**{"_id": match_id, "updated_at": utc_now},
}
)
payload = model.dict(exclude={"inserted_at"}, exclude_none=True)
payload = model.model_dump(exclude={"inserted_at"}, exclude_none=True)
ops.append(
UpdateOne(
{"_id": match_id},
Expand Down Expand Up @@ -185,7 +186,7 @@ def upsert_flow_check(self, dpid: str, state="active") -> Optional[dict]:
updated = self.db.flow_checks.find_one_and_update(
{"_id": dpid},
{
"$set": model.dict(exclude={"inserted_at"}),
"$set": model.model_dump(exclude={"inserted_at"}),
"$setOnInsert": {"inserted_at": utc_now},
},
return_document=ReturnDocument.AFTER,
Expand Down
137 changes: 68 additions & 69 deletions db/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""DB models."""

# pylint: disable=unused-argument,invalid-name,unused-argument
# pylint: disable=no-self-argument,no-name-in-module

Expand All @@ -9,7 +10,7 @@
from typing import List, Optional, Union

from bson.decimal128 import Decimal128
from pydantic import BaseModel, Field, root_validator, validator
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator


class FlowEntryState(Enum):
Expand All @@ -24,12 +25,12 @@ class DocumentBaseModel(BaseModel):
"""DocumentBaseModel."""

id: str = Field(None, alias="_id")
inserted_at: Optional[datetime]
updated_at: Optional[datetime]
inserted_at: Optional[datetime] = None
updated_at: Optional[datetime] = None

def dict(self, **kwargs) -> dict:
def model_dump(self, **kwargs) -> dict:
"""Model to dict."""
values = super().dict(**kwargs)
values = super().model_dump(**kwargs)
if "id" in values and values["id"]:
values["_id"] = values["id"]
if "exclude" in kwargs and "_id" in kwargs["exclude"]:
Expand All @@ -40,54 +41,55 @@ def dict(self, **kwargs) -> dict:
class FlowCheckDoc(DocumentBaseModel):
"""FlowCheckDoc."""

state = "active"
state: str = Field(default="active")
viniarck marked this conversation as resolved.
Show resolved Hide resolved


class MatchSubDoc(BaseModel):
"""Match DB SubDocument Model."""

in_port: Optional[int]
dl_src: Optional[str]
dl_dst: Optional[str]
dl_type: Optional[int]
dl_vlan: Optional[Union[int, str]]
dl_vlan_pcp: Optional[int]
nw_src: Optional[str]
nw_dst: Optional[str]
nw_proto: Optional[int]
tp_src: Optional[int]
tp_dst: Optional[int]
in_phy_port: Optional[int]
ip_dscp: Optional[int]
ip_ecn: Optional[int]
udp_src: Optional[int]
udp_dst: Optional[int]
sctp_src: Optional[int]
sctp_dst: Optional[int]
icmpv4_type: Optional[int]
icmpv4_code: Optional[int]
arp_op: Optional[int]
arp_spa: Optional[str]
arp_tpa: Optional[str]
arp_sha: Optional[str]
arp_tha: Optional[str]
ipv6_src: Optional[str]
ipv6_dst: Optional[str]
ipv6_flabel: Optional[int]
icmpv6_type: Optional[int]
icmpv6_code: Optional[int]
nd_tar: Optional[int]
nd_sll: Optional[int]
nd_tll: Optional[int]
mpls_lab: Optional[int]
mpls_tc: Optional[int]
mpls_bos: Optional[int]
pbb_isid: Optional[int]
v6_hdr: Optional[int]
metadata: Optional[int]
tun_id: Optional[int]

@validator("dl_vlan")
in_port: Optional[int] = None
dl_src: Optional[str] = None
dl_dst: Optional[str] = None
dl_type: Optional[int] = None
dl_vlan: Optional[Union[int, str]] = None
dl_vlan_pcp: Optional[int] = None
nw_src: Optional[str] = None
nw_dst: Optional[str] = None
nw_proto: Optional[int] = None
tp_src: Optional[int] = None
tp_dst: Optional[int] = None
in_phy_port: Optional[int] = None
ip_dscp: Optional[int] = None
ip_ecn: Optional[int] = None
udp_src: Optional[int] = None
udp_dst: Optional[int] = None
sctp_src: Optional[int] = None
sctp_dst: Optional[int] = None
icmpv4_type: Optional[int] = None
icmpv4_code: Optional[int] = None
arp_op: Optional[int] = None
arp_spa: Optional[str] = None
arp_tpa: Optional[str] = None
arp_sha: Optional[str] = None
arp_tha: Optional[str] = None
ipv6_src: Optional[str] = None
ipv6_dst: Optional[str] = None
ipv6_flabel: Optional[int] = None
icmpv6_type: Optional[int] = None
icmpv6_code: Optional[int] = None
nd_tar: Optional[int] = None
nd_sll: Optional[int] = None
nd_tll: Optional[int] = None
mpls_lab: Optional[int] = None
mpls_tc: Optional[int] = None
mpls_bos: Optional[int] = None
pbb_isid: Optional[int] = None
v6_hdr: Optional[int] = None
metadata: Optional[int] = None
tun_id: Optional[int] = None

@field_validator("dl_vlan")
@classmethod
def vlan_with_mask(cls, v):
"""Validate vlan format"""
try:
Expand All @@ -105,37 +107,34 @@ def vlan_with_mask(cls, v):
class FlowSubDoc(BaseModel):
"""Flow DB SubDocument Model."""

table_id = 0
owner: Optional[str]
table_group = "base"
priority = 0x8000
table_id: int = 0
owner: Optional[str] = None
table_group: str = "base"
priority: int = 0x8000
cookie: Decimal128 = Decimal128("0")
idle_timeout = 0
hard_timeout = 0
match: Optional[MatchSubDoc]
actions: Optional[List[dict]]
instructions: Optional[List[dict]]

class Config:
"""Config."""

arbitrary_types_allowed = True

@validator("cookie", pre=True)
idle_timeout: int = 0
hard_timeout: int = 0
match: Optional[MatchSubDoc] = None
actions: Optional[List[dict]] = None
instructions: Optional[List[dict]] = None
model_config = ConfigDict(arbitrary_types_allowed=True)

@field_validator("cookie", mode="before")
@classmethod
def preset_cookie(cls, v, values, **kwargs) -> Decimal128:
"""Preset cookie."""
if isinstance(v, (int, str)):
return Decimal128(Decimal(v))
return v

@root_validator()
def validate_actions_intructions(cls, values) -> dict:
@model_validator(mode="after")
def validate_actions_intructions(self) -> dict:
"""Validate that actions and intructions are mutually exclusive"""
if values.get("actions") is not None and values.get("instructions") is not None:
if self.actions is not None and self.instructions is not None:
raise ValueError(
'Cannot have both "actions" and "instructions" at the same time'
)
return values
return self


class FlowDoc(DocumentBaseModel):
Expand All @@ -144,4 +143,4 @@ class FlowDoc(DocumentBaseModel):
switch: str
flow_id: str
flow: FlowSubDoc
state = FlowEntryState.PENDING.value
state: str = FlowEntryState.PENDING.value
12 changes: 7 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,13 @@ def delete_matched_flows(self, flow_dicts, switches: dict) -> None:
continue
if match_flow(
flow_dict["flow"],
switches[dpid].connection.protocol.version
if dpid in switches
and switches[dpid].connection
and switches[dpid].connection.protocol
else 0x04,
(
switches[dpid].connection.protocol.version
if dpid in switches
and switches[dpid].connection
and switches[dpid].connection.protocol
else 0x04
),
stored_flow["flow"],
):
stored_flow["state"] = FlowEntryState.DELETED.value
Expand Down
1 change: 1 addition & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Settings from flow_manager NApp."""

FLOWS_DICT_MAX_SIZE = 10000
ENABLE_CONSISTENCY_CHECK = True
ENABLE_BARRIER_REQUEST = True
Expand Down
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ class Linter(SimpleCommand):
def run(self):
"""Run yala."""
print("Yala is running. It may take several seconds...")
check_call("yala *.py controllers db tests", shell=True)
try:
check_call("yala *.py controllers db tests", 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
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests from flow_manager napp."""

import os
import sys
from pathlib import Path
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module to test the main napp file."""

from unittest.mock import Mock, MagicMock

from kytos.core import Controller
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_db_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""TestDbModels."""

import pytest
from napps.kytos.flow_manager.db.models import FlowDoc
from pydantic import ValidationError
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_flow_controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module to test FlowController."""

# pylint: disable=invalid-name,relative-beyond-top-level

from datetime import datetime
Expand Down
Loading