Skip to content

Commit

Permalink
updates (#11)
Browse files Browse the repository at this point in the history
* add gh actions

* pyupgrade, isort, black

* flake8
  • Loading branch information
grizz authored Mar 1, 2021
1 parent 8e51427 commit 6154a6c
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 33 deletions.
84 changes: 84 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: tests

on: [push, pull_request]

jobs:

linting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip
restore-keys: ${{ runner.os }}-pip
- name: Install Poetry
uses: snok/[email protected]
with:
virtualenvs-create: true
virtualenvs-in-project: true
# virtualenvs-path: ~/.venv
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v2
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
# install dependencies if cache does not exist
- name: Check cache and install dependencies
run: poetry install
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
- name: Run linters
run: |
source .venv/bin/activate
flake8 .
black . --check
isort .
test:
needs: linting
strategy:
fail-fast: true
matrix:
os: [ "ubuntu-latest", "macos-latest" ]
python-version: [ "3.6", "3.7", "3.8", "3.9" ]
runs-on: ${{ matrix.os }}
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/[email protected]
with:
virtualenvs-create: true
virtualenvs-in-project: true
# This approach appears to be cachign the repo source as well (e.g,
# the library code that tests are supposed to test)
#
# FIXME
#
#- name: Load cached venv
# id: cached-poetry-dependencies
# uses: actions/cache@v2
# with:
# path: .venv
# key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
# install dependencies if cache does not exist
- name: Check cache and install dependencies
run: poetry install
- name: Run tests
run: |
source .venv/bin/activate
poetry run pytest tests/ --cov="confu" --cov-report=xml --cov-report=term-missing
poetry run coverage report
# upload coverage stats
- name: Upload coverage
uses: codecov/codecov-action@v1
with:
file: ./coverage.xml
fail_ci_if_error: true
21 changes: 11 additions & 10 deletions src/netom/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

import ipaddress
import os
from pkg_resources import get_distribution

import confu
from confu import schema
import tmpl
from confu import schema
from pkg_resources import get_distribution

# TODO move out of this namespace
from .models import BgpNeighbor
from .exception import NetomValidationError

# TODO move out of this namespace
from .models import BgpNeighbor

__version__ = get_distribution("netom").version

Expand All @@ -30,7 +29,7 @@ def ip_version(value):
return ipaddress.ip_address(value).version


class Render(object):
class Render:
"""
Renders data to defined type.
"""
Expand All @@ -47,17 +46,19 @@ def __init__(self, model_version, model_type):
self.type = model_type

# FIXME use pkg resources
search_path = os.path.join(os.getcwd(), "src/netom/templates/", self.version, self.type) #, "bgp")
search_path = os.path.join(
os.getcwd(), "src/netom/templates/", self.version, self.type
) # , "bgp")
self.engine = tmpl.get_engine("jinja2")(search_path=search_path)
self.engine.engine.filters["make_variable_name"] = make_variable_name
self.engine.engine.filters["ip_version"] = ip_version
# self.engine.search_path = os.path.dirname(search_path)

def _render(self, filename, data, fobj):
#engine.engine.undefined = IgnoreUndefined
# engine.engine.undefined = IgnoreUndefined
output = self.engine._render(src=filename, env=data)
fobj.write(output)
#ctx.tmpl.update(engine=engine)
# ctx.tmpl.update(engine=engine)

def bgp_neighbors(self, data, fobj, validate=True):
"""
Expand All @@ -68,7 +69,7 @@ def bgp_neighbors(self, data, fobj, validate=True):
# collate by group
for each in data["neighbors"]:
if validate:
validate_data(BgpNeighbor(), each)
validate(BgpNeighbor(), each)
groups.setdefault(each["peer_group"], []).append(each)

groups = dict(peer_groups=groups)
Expand Down
1 change: 0 additions & 1 deletion src/netom/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sys


if __name__ == "__main__":
from .cli import main

Expand Down
12 changes: 9 additions & 3 deletions src/netom/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import argparse
import io
import sys
Expand All @@ -7,13 +6,17 @@

import netom


def main(argv=None):
if argv is None:
argv = sys.argv[1:]

parser = argparse.ArgumentParser(description="netom")
parser.add_argument("--version", action="version",
version="{}s version {}".format("%(prog)", netom.__version__))
parser.add_argument(
"--version",
action="version",
version="{}s version {}".format("%(prog)", netom.__version__),
)
# parser.add_argument("--output-format", help="output format (yaml, json, text)")

parser.add_argument("command")
Expand All @@ -31,6 +34,9 @@ def main(argv=None):
model_version = argd["model_version"]
model_type = argd["model_type"]

if command != "render":
raise Exception("only render is supported")

render = netom.Render(model_version, model_type)
data = munge.load_datafile(argd["data_file"])

Expand Down
2 changes: 1 addition & 1 deletion src/netom/exception.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

class NetomException(Exception):
"""Base exception used by this module."""


class NetomValidationError(NetomException):
"""A validation error occurred."""
23 changes: 16 additions & 7 deletions src/netom/models.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@

import confu
from confu import schema


class BgpNeighbor(schema.Schema):
"""
Defines the BGP Neighbor model.
"""

name = schema.Str("name", help="name of the session")
description = schema.Str("description", help="description of the session", default=None)
description = schema.Str(
"description", help="description of the session", default=None
)
enabled = schema.Bool("enabled", help="enabled", default=True)
neighbor_address = schema.IpAddress("neighbor_address", help="neighbor IP address")
peer_as = schema.Int("peer_as", help="neighbor AS number")
peer_group = schema.Str("peer_group", help="peer group")
peer_type = schema.Str("peer_type", help="peer type (internal or external)", default="external")
max_prefixes = schema.Int("max_prefixes", help="maximum number of prefixes to accept")
peer_type = schema.Str(
"peer_type", help="peer type (internal or external)", default="external"
)
max_prefixes = schema.Int(
"max_prefixes", help="maximum number of prefixes to accept"
)
import_policy = schema.Str("import_policy", help="Import policy to apply")
export_policy = schema.Str("export_policy", help="Export policy to apply")
auth_password = schema.Str("auth_password", help="MD5 session password", default=None)
local_address = schema.IpAddress("local_address", help="local IP address", default=None)
auth_password = schema.Str(
"auth_password", help="MD5 session password", default=None
)
local_address = schema.IpAddress(
"local_address", help="local IP address", default=None
)
local_as = schema.Int("peer_as", help="neighbor AS number", default=0)
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import os

import pytest
import pytest


@pytest.fixture
Expand Down
7 changes: 2 additions & 5 deletions tests/test_bgp_neighbor.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@

from confu.schema import validate
import pytest

import netom
from netom import exception
from netom.models import BgpNeighbor


INVALID0 = dict(
neighbor_address="not an ip",
)
)


VALID0 = dict(
Expand All @@ -22,7 +19,7 @@
enabled=True,
import_policy="peer-in",
export_policy="peer-out",
)
)


# FIXME
Expand Down
6 changes: 2 additions & 4 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

import pytest

import netom


def test_init():
pass
assert netom.Render("test", "12")

0 comments on commit 6154a6c

Please sign in to comment.