Skip to content

Add tests #1

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

Merged
merged 8 commits into from
Jan 4, 2022
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
data/configs/*
data/configs/leaf*
data/configs/spine*

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM python:3.8.6

RUN apt-get update -y ; apt-get install vim -y
RUN pip install --upgrade pip
RUN pip install pytest

RUN mkdir /local

COPY . /local
WORKDIR /local

RUN pip install -r requirements.txt --use-deprecated=legacy-resolver

CMD /bin/bash
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.DEFAULT_GOAL := help

.PHONY: help
help:
@grep '^[a-zA-Z]' $(MAKEFILE_LIST) | \
sort | \
awk -F ':.*?## ' 'NF==2 {printf "\033[36m %-25s\033[0m %s\n", $$1, $$2}'

build: ## Build
docker build -t n3-guide .

test: ## Test
docker run -tv $(shell pwd):/source -w /local --env-file .env n3-guide:latest pytest -v tests/

cli: ## CLI
docker run -it -v $(shell pwd):/source -w /local --env-file .env n3-guide:latest bash
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
attrs==21.4.0
autoflake==1.4
bcrypt==3.2.0
black==21.9b0
Expand All @@ -12,6 +13,7 @@ cryptography==35.0.0
dnspython==2.1.0
future==0.18.2
idna==3.2
iniconfig==1.1.1
ipaddress==1.0.23
isort==5.10.1
Jinja2==3.0.2
Expand All @@ -29,17 +31,21 @@ nornir-napalm==0.1.2
nornir-netbox==0.3.0
nornir-utils==0.1.2
ntc-templates==2.3.2
packaging==21.3
paramiko==2.7.2
passlib==1.7.4
pathspec==0.9.0
platformdirs==2.4.0
pluggy==1.0.0
py==1.11.0
pycparser==2.20
pyeapi==0.8.4
pyflakes==2.4.0
Pygments==2.10.0
PyNaCl==1.4.0
pyparsing==2.4.7
pyserial==3.5
pytest==6.2.5
python-dotenv==0.19.2
python-netbox==0.0.21
PyYAML==5.4.1
Expand All @@ -52,6 +58,7 @@ scp==0.14.1
six==1.16.0
tenacity==8.0.1
textfsm==1.1.2
toml==0.10.2
tomli==1.2.1
transitions==0.8.10
typing-extensions==3.10.0.2
Expand Down
Empty file added src/__init__.py
Empty file.
8 changes: 6 additions & 2 deletions src/nr_backup_configs.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python

from helpers import nornir_setup
from nornir_napalm.plugins.tasks import napalm_get
from nornir_utils.plugins.functions import print_result
from nornir_utils.plugins.tasks.files import write_file

from helpers import nornir_setup

nr = nornir_setup()

BACKUP_PATH = "./data/configs"
Expand All @@ -23,4 +24,7 @@ def backup_config(task, path):
name="Backup Device configurations", path=BACKUP_PATH, task=backup_config
)

print_result(result, vars=["stdout"])
nr.close_connections()

if __name__ == "__main__":
print_result(result, vars=["stdout"])
8 changes: 6 additions & 2 deletions src/nr_print_inventory.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/usr/bin/env python

from helpers import nornir_setup
from rich import print as rprint

from helpers import nornir_setup

nr = nornir_setup()

rprint(nr.inventory.dict())
inventory = nr.inventory.dict()

if __name__ == "__main__":
rprint(inventory)
13 changes: 13 additions & 0 deletions tests/test_001_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os
import sys

sys.path.append(f"{os.path.dirname(os.path.dirname(os.path.realpath(__file__)))}/src")

from helpers import nornir_setup


def test_nr_setup():
nr = nornir_setup()
assert type(nr.inventory.defaults.username) == str
assert type(nr.inventory.defaults.password) == str
assert nr.config.inventory.plugin == "NetBoxInventory2"
18 changes: 18 additions & 0 deletions tests/test_002_backup_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
import sys

import pytest

sys.path.append(f"{os.path.dirname(os.path.dirname(os.path.realpath(__file__)))}/src")

from nr_backup_configs import result


@pytest.mark.parametrize("device", list(result.keys()))
def test_backup_configs_get_config(device):
assert not result[device][1].failed


@pytest.mark.parametrize("device", list(result.keys()))
def test_backup_configs_write_file(device):
assert not result[device][2].failed
17 changes: 17 additions & 0 deletions tests/test_003_print_inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
import sys

import pytest

sys.path.append(f"{os.path.dirname(os.path.dirname(os.path.realpath(__file__)))}/src")

from nr_print_inventory import inventory


@pytest.fixture
def inventory_keys():
return ["hosts", "groups", "defaults"]


def test_print_inventory(inventory_keys):
assert list(inventory.keys()) == inventory_keys