Skip to content

Commit

Permalink
Integration tests (nautobot#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
FragmentedPacket authored Aug 14, 2021
1 parent af5f7b3 commit e389272
Show file tree
Hide file tree
Showing 187 changed files with 3,866 additions and 14,133 deletions.
5 changes: 5 additions & 0 deletions .bandit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
skips: []
# No need to check for security issues in the test scripts!
exclude_dirs:
- "./tests/"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ docs/_build/*
.python-version
pb.*.yml
.DS_Store
.env

# https://github.com/ansible/ansible/issues/68499
# ansible_collections/
Expand Down
83 changes: 51 additions & 32 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,72 @@ services:
- docker

os: linux
dist: bionic
dist: focal

stages:
- test
- integration

env:
global:
- COLLECTION_NAMESPACE: "networktocode"
- COLLECTION_NAME: "nautobot"
- COLLECTION_VERSION: "2.0.1"
- COLLECTION_VERSION: "3.0.0"
- COMPOSE_PROJECT_NAME: "nautobot_ansible"

python:
- 3.6
- 3.7
- 3.8
- 3.9

install:
- pip install -U pip
- pip install invoke
- invoke start

script:
- invoke unit

jobs:
fast_finish: true
include:
- name: "Python 3.6 - Nautobot - Latest PyPi Ansible"
- name: "Python 3.6 - Nautobot 1.0.X - ansible-base 2.10.X"
python: 3.6
stage: integration
env:
- INVOKE_NAUTOBOT_ANSIBLE_PYTHON_VER=3.6
- INVOKE_NAUTOBOT_ANSIBLE_NAUTOBOT_VER=1.0.3
script:
- invoke integration
- name: "Python 3.7 - Nautobot 1.1.X - Latest Ansible Core"
python: 3.9
stage: integration
env:
- PYTHON_VER=3.6
install:
- pip install -U pip
- INVOKE_NAUTOBOT_ANSIBLE_PYTHON_VER=3.7
- INVOKE_NAUTOBOT_ANSIBLE_NAUTOBOT_VER=1.1.1
before_script:
- pip uninstall -y virtualenv
- sudo apt update && sudo apt install python3-virtualenv
script:
- curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
- export PATH=$HOME/.poetry/bin:$PATH
- poetry config virtualenvs.create false && poetry install

before_script:
- mkdir -p ~/ansible_collections/$COLLECTION_NAMESPACE
- ansible-galaxy collection install community.general -p /home/travis/.ansible/collections
- ansible-galaxy collection build .
- ansible-galaxy collection install $COLLECTION_NAMESPACE-$COLLECTION_NAME*.tar.gz -p /home/travis/.ansible/collections

# Run all further tests from within the installed directory
# Required to resolve imports of other collections
- cd /home/travis/.ansible/collections/ansible_collections/$COLLECTION_NAMESPACE/$COLLECTION_NAME

script:
# Check python syntax
- black . --check --diff

# Sanity tests
# --requirements - install pip packages as necessary
# Skip pep8 as we use black instead
- ansible-test sanity -v --requirements --python $PYTHON_VER --skip-test pep8 plugins/

# Unit tests, with code coverage
- ansible-test units -v --coverage --python $PYTHON_VER
# Report code coverage
- ansible-test coverage report --all --omit "tests/*,hacking/*,docs/*" --show-missing
- poetry config virtualenvs.create false
- poetry remove ansible-base
- poetry add ansible-core
- invoke integration
# - name: "Python 3.8 - Nautobot 1.1.X - Latest Ansible Core"
# python: 3.8
# env:
# - INVOKE_NAUTOBOT_ANSIBLE_PYTHON_VER=3.8
# - INVOKE_NAUTOBOT_ANSIBLE_NAUTOBOT_VER=develop
# script:
# - poetry install ansible-core
# - invoke integration

deploy:
provider: script
skip_cleanup: true
cleanup: true
script: ansible-galaxy collection publish $TRAVIS_BUILD_DIR/$COLLECTION_NAMESPACE-$COLLECTION_NAME-$COLLECTION_VERSION.tar.gz --api-key="$GALAXY_API_TOKEN"
on:
tags: true
118 changes: 118 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#############
# Dependencies
#
# This base stage just installs the dependencies required for production
# without any development deps.
ARG PYTHON_VER=3.8
FROM python:${PYTHON_VER} AS base

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -yqq && apt-get install -yqq shellcheck && apt-get clean

WORKDIR /usr/src/app

# Update pip to latest
RUN python -m pip install -U pip

# Install poetry for dep management
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
ENV PATH="$PATH:/root/.poetry/bin"
RUN poetry config virtualenvs.create false

# Install project manifest
COPY pyproject.toml .

# Install poetry.lock from which to build
COPY poetry.lock .

# Install all dependencies
RUN poetry install

# Allow for flexible Python versions, for broader testing
ARG PYTHON_VER
ENV PYTHON_VERSION=${PYTHON_VER}

# Set a custom collection path for all ansible commands
# Note: This only allows for one path, not colon-separated, because we use it
# elsewhere
ARG ANSIBLE_COLLECTIONS_PATH=/usr/share/ansible/collections
ENV ANSIBLE_COLLECTIONS_PATH=${ANSIBLE_COLLECTIONS_PATH}

# For Module unit tests as we use some testing features avaiable in this collection
RUN ansible-galaxy collection install community.general

# Copy in the application source and everything not explicitly banned by
# .dockerignore
COPY . .

#########
# Linting
#
# Runs all necessary linting and code checks
FROM base AS lint


# We should look into pylint/flake8, etc. in the future
# RUN echo 'Running Flake8' && \
# flake8 . && \
RUN echo 'Running Black' && \
black --check --diff . && \
# Removed and running Pylint in unit tests after project has been created
# echo 'Running Pylint' && \
# find . -name '*.py' | xargs pylint && \
echo 'Running Bandit' && \
bandit --recursive ./ --configfile .bandit.yml


############
# Unit Tests
#
# This test stage runs true unit tests (no outside services) at build time, as
# well as enforcing codestyle and performing fast syntax checks. It is built
# into an image with docker-compose for running the full test suite.
FROM base AS unittests

ARG PYTHON_VER=3.8
ENV PYTHON_VERSION=${PYTHON_VER}

# Allows for custom command line arguments to be passed to ansible-test (like -vvv)
ARG ANSIBLE_SANITY_ARGS
ENV ANSIBLE_SANITY_ARGS=${ANSIBLE_SANITY_ARGS}
ARG ANSIBLE_UNIT_ARGS
ENV ANSIBLE_UNIT_ARGS=${ANSIBLE_UNIT_ARGS}

# Ansible sanity and unit tests
#
# Runs the sanity and unit tests inside the container build context to isolate
# thsoe tests from all runtime influences

# Build Collection to run ansible-tests against
RUN ansible-galaxy collection build --output-path ./dist/ .

# Install built library
RUN ansible-galaxy collection install ./dist/networktocode*.tar.gz

# Switch to the collection path for tests
WORKDIR ${ANSIBLE_COLLECTIONS_PATH}/ansible_collections/networktocode/nautobot

# Run sanity tests
RUN ansible-test sanity $ANSIBLE_SANITY_ARGS \
--requirements \
--skip-test pep8 \
--python ${PYTHON_VER} \
plugins/

# Run unit tests
RUN ansible-test units $ANSIBLE_UNIT_ARGS --coverage --python $PYTHON_VERSION

############
# Integration Tests
FROM unittests AS integration

ARG ANSIBLE_INTEGRATION_ARGS
ENV ANSIBLE_INTEGRATION_ARGS=${ANSIBLE_INTEGRATION_ARGS}

# Integration test entrypoint
ENTRYPOINT ${ANSIBLE_COLLECTIONS_PATH}/ansible_collections/networktocode/nautobot/tests/integration/entrypoint.sh

CMD ["--help"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ To keep the code simple, we only officially support the two latest releases of N

We have a new docs site live that can be found [here](https://nautobot-ansible.readthedocs.io/en/latest/).

> This is a fork of the netbox.netbox Ansible Galaxy collection found at [https://github.com/netbox-community/ansible_modules](https://github.com/netbox-community/ansible_modules) in February, 2021
> This is a fork of the netbox.netbox Ansible Galaxy collection found at [https://github.com/netbox-community/ansible_modules](https://github.com/netbox-community/ansible_modules) in February, 2021
31 changes: 31 additions & 0 deletions development/dev.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
NAUTOBOT_CREATE_SUPERUSER="true"
NAUTOBOT_SUPERUSER_NAME="admin"
NAUTOBOT_SUPERUSER_PASSWORD="admin"
NAUTOBOT_SUPERUSER_API_TOKEN="0123456789abcdef0123456789abcdef01234567"

NAUTOBOT_ALLOWED_HOSTS=*
NAUTOBOT_CHANGELOG_RETENTION=0
NAUTOBOT_CONFIG=/opt/nautobot/nautobot_config.py
NAUTOBOT_DB_HOST=postgres
NAUTOBOT_DB_NAME=nautobot
NAUTOBOT_DB_PASSWORD=decinablesprewad
NAUTOBOT_DB_USER=nautobot
NAUTOBOT_DB_TIMEOUT=300
NAUTOBOT_DB_ENGINE=django.db.backends.postgresql
NAUTOBOT_MAX_PAGE_SIZE=0
NAUTOBOT_NAPALM_TIMEOUT=5
NAUTOBOT_REDIS_HOST=redis
NAUTOBOT_REDIS_PASSWORD=decinablesprewad
NAUTOBOT_REDIS_PORT=6379
# Uncomment REDIS_SSL if using SSL
# NAUTOBOT_REDIS_SSL=True
NAUTOBOT_SECRET_KEY=0123456789abcdef0123456789abcdef01234567

# Needed for Postgres should match the values for Nautobot above
PGPASSWORD=decinablesprewad
POSTGRES_DB=nautobot
POSTGRES_PASSWORD=decinablesprewad
POSTGRES_USER=nautobot

# Needed for Redis should match the values for Nautobot above
REDIS_PASSWORD=decinablesprewad
32 changes: 32 additions & 0 deletions development/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: "3"
services:
nautobot:
image: "ghcr.io/nautobot/nautobot:${NAUTOBOT_VER}-py${PYTHON_VER}"
command: "nautobot-server runserver 0.0.0.0:8000"
ports:
- "8000:8000"
depends_on:
- "postgres"
- "redis"
env_file:
- "./dev.env"
tty: true
worker:
image: "ghcr.io/nautobot/nautobot:${NAUTOBOT_VER}-py${PYTHON_VER}"
entrypoint: "nautobot-server rqworker"
depends_on:
- "nautobot"
env_file:
- "./dev.env"
tty: true
postgres:
image: "postgres:13"
env_file:
- "./dev.env"
redis:
image: "redis:6-alpine"
command:
- "sh"
- "-c" # this is to evaluate the $REDIS_PASSWORD from the env
- "redis-server --appendonly yes --requirepass $$REDIS_PASSWORD" ## $$ because of docker-compose
env_file: "./dev.env"
38 changes: 38 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
version: "3.4"
x-args: &args
PYTHON_VER: ${PYTHON_VER:-3.8}
# Build block with context and target default
x-build: &build
args: *args
context: .
target: unittests
x-service: &service
environment:
ANSIBLE_INTEGRATION_ARGS: ${ANSIBLE_INTEGRATION_ARGS:-}
ANSIBLE_SANITY_ARGS: ${ANSIBLE_SANITY_ARGS:-}
ANSIBLE_UNIT_ARGS: ${ANSIBLE_UNIT_ARGS:-}

services:
unit:
<<: *service
build:
<<: *build

lint:
<<: *service
build:
<<: *build
target: lint

integration:
<<: *service
build:
<<: *build
target: integration

# Attach these services to the Nautobot network that gets spun up from invoke start
networks:
default:
external:
name: nautobot_ansible_default
3 changes: 2 additions & 1 deletion docs/getting_started/contributing/modules/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ Contributing Modules to the Ansible Collection

Module Architecture <architecture>
Creating a New Ansible Module <new_module>
Adding New Module Options <update_module>
Adding New Module Options <update_module>
Running Tests Locally <testing_locally>
Loading

0 comments on commit e389272

Please sign in to comment.