Skip to content

Commit

Permalink
isolate test environment with docker-compose (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop committed Jan 4, 2019
1 parent 94a6a70 commit ec6db61
Show file tree
Hide file tree
Showing 22 changed files with 247 additions and 303 deletions.
31 changes: 10 additions & 21 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
sudo: required
dist: xenial
services:
- docker
addons:
apt_packages:
- pandoc
language: python
python:
- 3.6
matrix:
include:
- python: 3.6
env: TOXENV=py36
- python: 3.6
env: TOXENV=sphinx
- python: 3.6
env: TOXENV=black
- python: 3.6
env: TOXENV=pylama
- python: 3.6
env: TOXENV=mypy
- python: 3.6
env: TOXENV=nbval
install:
- pip install tox tox-travis coveralls
- env: PYTHON=3.6 TESTS=pytest
- env: PYTHON=3.7 TESTS=pytest
- env: PYTHON=3.6 TESTS=sphinx
- env: PYTHON=3.6 TESTS=black
- env: PYTHON=3.6 TESTS=pylama
- env: PYTHON=3.6 TESTS=mypy
- env: PYTHON=3.6 TESTS=nbval
script:
- tox
- make start_dev_env
- make $TESTS
deploy:
provider: pypi
user: dbarroso
Expand Down
36 changes: 30 additions & 6 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,46 @@ Before you make any significant code changes to the core it's recommended that y
Setting up your environment
---------------------------

In order to run tests locally you need to have `Docker <https://docs.docker.com/install/>`_ and `Pandoc <https://pandoc.org/installing.html>`_ installed. Docker is used to test the Nornir plugins and Pandoc is required for building the documentation provided by `Sphinx <http://www.sphinx-doc.org/>`_. After those are installed you can go ahead and install the needed Python dependencies.
In order to run tests locally you need to have `Docker <https://docs.docker.com/install/>`_ and `docker-compose <https://docs.docker.com/compose/>`_ installed.

Starting development environment
--------------------------------

You need some services to run the tests. Those are managed with ``docker-compose``. In order to start them you can execute:

.. code-block:: bash
make start_dev_env
You can then stop it with:

.. code-block:: bash
pip install -r requirements-dev.txt
make stop_dev_env
Running tests
-------------

While the automated tests will be triggered when you submit a new pull request it can still save you time to run the tests locally first.
While the automated tests will be triggered when you submit a new pull request it can still save you time to run the tests locally first.

.. code-block:: bash
make tests
That will run the entire test suite, if you want to target some specific test you can do:

.. code-block:: bash
make build_test_container && make nbval
To run only ``nbval`` environment or:


.. code-block:: bash
make tests
make build_test_container && make pytest ARGS="tests/plugins/tasks/networking/test_tcp_ping.py"
The test above will run the tests against the Nornir code and documentation.
To run a specific test.


Coding style
Expand All @@ -91,6 +115,6 @@ Nornir uses `Black <https://github.com/ambv/black>`_, the the uncompromising Pyt

.. code-block:: bash
make format
black .
The Black GitHub repo has information about how you can integrate Black in your editor.
18 changes: 18 additions & 0 deletions Dockerfile.tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ARG PYTHON

FROM python:${PYTHON}-stretch

RUN apt-get update && apt-get install -y pandoc

ADD docs/requirements.txt /tmp/requirements-docs.txt
RUN pip install -r /tmp/requirements-docs.txt

ADD requirements.txt /tmp/
ADD requirements-dev.txt /tmp/
RUN pip install -r /tmp/requirements-dev.txt

ADD . /nornir

RUN pip install -e /nornir

WORKDIR "/nornir"
72 changes: 66 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@
ifeq ($(PYTHON), )
override PYTHON=3.6
endif

.PHONY: format
format:
black .
DOCKER_COMPOSE_FILE=docker-compose.yaml
DOCKER_COMPOSE=PYTHON=${PYTHON} docker-compose -f ${DOCKER_COMPOSE_FILE}

.PHONY: tests
tests:
tox
.PHONY: start_dev_env
start_dev_env:
${DOCKER_COMPOSE} \
up -d \
dev1.group_1 dev2.group_1 dev3.group_2 dev4.group_2 dev5.no_group httpbin

.PHONY: stop_dev_env
stop_dev_env:
${DOCKER_COMPOSE} \
down

.PHONY: build_test_container
build_test_container:
${DOCKER_COMPOSE} \
build nornir

.PHONY: enter-container
enter-container:
${DOCKER_COMPOSE} \
run nornir bash

.PHONY: pytest
pytest:
${DOCKER_COMPOSE} \
run nornir py.test --cov=nornir --cov-report=term-missing -vs ${ARGS}

.PHONY: black
black:
${DOCKER_COMPOSE} \
run nornir black --check .

.PHONY: sphinx
sphinx:
# TODO REPLACE with: sphinx-build -n -E -q -N -b dummy -d docs/_build/doctrees docs asd
${DOCKER_COMPOSE} \
run nornir sphinx-build -W -b html -d docs/_build/doctrees docs docs/_build/html

.PHONY: pylama
pylama:
${DOCKER_COMPOSE} \
run nornir pylama .

.PHONY: mypy
mypy:
${DOCKER_COMPOSE} \
run nornir mypy .

.PHONY: nbval
nbval:
${DOCKER_COMPOSE} \
run nornir \
pytest --nbval \
docs/plugins \
docs/howto \
docs/tutorials/intro/initializing_nornir.ipynb \
docs/tutorials/intro/inventory.ipynb \

PHONY: tests
tests: build_test_container black sphinx pylama mypy nbval
make pytest PYTEST=3.6
make pytest PYTEST=3.7
69 changes: 69 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
version: '2.1'

services:
nornir:
build:
context: .
dockerfile: Dockerfile.tests
args:
PYTHON: ${PYTHON}
extra_hosts:
dev1.group_1: 10.21.33.101
dev2.group_1: 10.21.33.102
dev3.group_2: 10.21.33.103
dev4.group_2: 10.21.33.104
dev5.no_group: 10.21.33.105
httpbin: 10.21.33.201
networks:
net1:
ipv4_address: 10.21.33.2

dev1.group_1:
hostname: dev1.group_1
image: dbarroso/stupid_ssh_container
networks:
net1:
ipv4_address: 10.21.33.101

dev2.group_1:
hostname: dev2.group_1
image: dbarroso/stupid_ssh_container
networks:
net1:
ipv4_address: 10.21.33.102

dev3.group_2:
hostname: dev3.group_2
image: dbarroso/stupid_ssh_container
networks:
net1:
ipv4_address: 10.21.33.103

dev4.group_2:
hostname: dev4.group_2
image: dbarroso/stupid_ssh_container
networks:
net1:
ipv4_address: 10.21.33.104

dev5.no_group:
hostname: dev5.no_group
image: dbarroso/stupid_ssh_container
networks:
net1:
ipv4_address: 10.21.33.105

httpbin:
hostname: httpbin
image: bungoume/httpbin-container
networks:
net1:
ipv4_address: 10.21.33.201

networks:
net1:
driver: bridge
ipam:
config:
- subnet: 10.21.33.0/24
8 changes: 4 additions & 4 deletions docs/howto/handling_connections.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@
"output_type": "stream",
"text": [
"dev1.group_1:\r\n",
" port: 65001\r\n",
" hostname:\r\n",
" port: 22\r\n",
" hostname: dev1.group_1\r\n",
" username:\r\n",
" password: a_password\r\n",
" platform: eos\r\n",
Expand All @@ -156,8 +156,8 @@
" - group_1\r\n",
" connection_options:\r\n",
" paramiko:\r\n",
" port: 65001\r\n",
" hostname: 127.0.0.1\r\n",
" port: 22\r\n",
" hostname:\r\n",
" username: root\r\n",
" password: docker\r\n",
" platform: linux\r\n",
Expand Down
1 change: 0 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ sphinx_rtd_theme
sphinxcontrib-napoleon
jupyter
nbsphinx
-r ../requirements.txt
future
# jtextfsm
jinja2
Expand Down
5 changes: 4 additions & 1 deletion nornir/plugins/functions/text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ def _print_individual_result(
)
for attribute in attrs:
x = getattr(result, attribute, "")
if x and not isinstance(x, str):
if isinstance(x, BaseException):
# for consistency between py3.6 and py3.7
print(f"{x.__class__.__name__}{x.args}")
elif x and not isinstance(x, str):
pprint.pprint(x, indent=2)
elif x:
print(x)
Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pytest-cov
pylama
flake8-import-order
requests-mock
tox
black==18.6b4
mypy
-r requirements.txt

0 comments on commit ec6db61

Please sign in to comment.