← Back to team overview

nagios-charmers team mailing list archive

[Merge] ~xavpaice/charm-nagios:makefile-20.08 into charm-nagios:master

 

Xav Paice has proposed merging ~xavpaice/charm-nagios:makefile-20.08 into charm-nagios:master.

Commit message:
Imported standard Makefile and tox.ini and fixed up tests

Requested reviews:
  Nagios Charm developers (nagios-charmers)

For more details, see:
https://code.launchpad.net/~xavpaice/charm-nagios/+git/nagios-charm/+merge/388625
-- 
Your team Nagios Charm developers is requested to review the proposed merge of ~xavpaice/charm-nagios:makefile-20.08 into charm-nagios:master.
diff --git a/.gitignore b/.gitignore
index ceabe0d..6f1f367 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,13 +1,41 @@
-*.pyc
-*.swp
-*~
+# Juju files
+.unit-state.db
+.go-cookies
+
+layers/*
+interfaces/*
+
+# Byte-compiled / optimized / DLL files
 __pycache__/
-data/*.pem
-data/*.key
-data/*.crt
-data/*.csr
-.tox/
-.idea/
-reports/
+*.py[cod]
+*$py.class
+
+# Tests files and dir
+.pytest_cache/
 .coverage
+.tox
+report/
+htmlcov/
+
+# Log files
+*.log
+
+# pycharm
+.idea/
+
+# vi
+.*.swp
+
+# version data
 repo-info
+version
+
+# Python builds
+deb_dist/
+dist/
+
+# Snaps
+*.snap
+
+# Builds
+.build/
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 49a4a5c..7e1e684 100644
--- a/Makefile
+++ b/Makefile
@@ -1,60 +1,75 @@
-#!/usr/bin/make
 PYTHON := /usr/bin/python3
-export PYTHONPATH := hooks
-PROJECTPATH = $(dir $(realpath $(MAKEFILE_LIST)))
-METADATA_FILE = "metadata.yaml"
-CHARM_NAME = $(shell cat ${PROJECTPATH}/${METADATA_FILE} | grep -E '^name:' | awk '{print $$2}')
+
+PROJECTPATH=$(dir $(realpath $(MAKEFILE_LIST)))
 ifndef CHARM_BUILD_DIR
-    CHARM_BUILD_DIR := /tmp/charm-builds
-    $(warning Warning CHARM_BUILD_DIR was not set, defaulting to $(CHARM_BUILD_DIR))
+	CHARM_BUILD_DIR=${PROJECTPATH}.build
 endif
+METADATA_FILE="metadata.yaml"
+CHARM_NAME=$(shell cat ${PROJECTPATH}/${METADATA_FILE} | grep -E '^name:' | awk '{print $$2}')
 
-default:
-	echo Nothing to do
+help:
+	@echo "This project supports the following targets"
+	@echo ""
+	@echo " make help - show this text"
+	@echo " make clean - remove unneeded files"
+	@echo " make submodules - make sure that the submodules are up-to-date"
+	@echo " make submodules-update - update submodules to latest changes on remote branch"
+	@echo " make build - build the charm"
+	@echo " make release - run clean and build targets"
+	@echo " make lint - run flake8 and black --check"
+	@echo " make black - run black and reformat files"
+	@echo " make proof - run charm proof"
+	@echo " make unittests - run the tests defined in the unittest subdirectory"
+	@echo " make functional - run the tests defined in the functional subdirectory"
+	@echo " make test - run lint, proof, unittests and functional targets"
+	@echo ""
 
-test: lint proof unittest functional
-	@echo "Testing charm $(CHARM_NAME)"
+clean:
+	@echo "Cleaning files"
+	@git clean -ffXd -e '!.idea'
+	@echo "Cleaning existing build"
+	@rm -rf ${CHARM_BUILD_DIR}/${CHARM_NAME}
 
-lint:
-	@echo "Running flake8"
-	@tox -e lint
+submodules:
+	@echo "Cloning submodules"
+	@git submodule update --init --recursive
+
+submodules-update:
+	@echo "Pulling latest updates for submodules"
+	@git submodule update --init --recursive --remote --merge
 
 build:
-	@echo "Building charm to base directory $(CHARM_BUILD_DIR)/$(CHARM_NAME)"
-	@-git describe --tags > ./repo-info
-	@mkdir -p $(CHARM_BUILD_DIR)/$(CHARM_NAME)
-	@cp -r * $(CHARM_BUILD_DIR)/$(CHARM_NAME)
+	@echo "Building charm to base directory ${CHARM_BUILD_DIR}/${CHARM_NAME}"
+	@-git rev-parse --abbrev-ref HEAD > ./repo-info
+	@-git describe --always > ./version
+	@mkdir -p ${CHARM_BUILD_DIR}/${CHARM_NAME}
+	@cp -a ./* ${CHARM_BUILD_DIR}/${CHARM_NAME}
 
-# Primitive test runner. Someone please fix this.
-functional: build
-	@echo Executing functional tests in $(CHARM_BUILD_DIR)
-	@CHARM_BUILD_DIR=$(CHARM_BUILD_DIR) \
-     PYTEST_KEEP_MODEL=$(PYTEST_KEEP_MODEL) \
-	 PYTEST_CLOUD_NAME=$(PYTEST_CLOUD_NAME) \
-	 PYTEST_CLOUD_REGION=$(PYTEST_CLOUD_REGION) \
-	 tox -e functional
-
-unittest:
-	@echo "Running unit tests"
-	@tox -e unit
+release: clean build
+	@echo "Charm is built at ${CHARM_BUILD_DIR}/${CHARM_NAME}"
 
-proof:
-	@echo "Running charm proof"
-	@charm proof
+lint:
+	@echo "Running lint checks"
+	@tox -e lint
 
-clean:
-	@echo "Cleaning files"
-	@if [ -d .tox ] ; then rm -r .tox ; fi
-	@if [ -d .pytest_cache ] ; then rm -r .pytest_cache ; fi
-	@find . | grep -E "\(__pycache__|\.pyc|\.pyo$$\)" | xargs rm -rf
-	@rm -rf $(CHARM_BUILD_DIR)/$(CHARM_NAME)/*
+black:
+	@echo "Reformat files with black"
+	@tox -e black
 
+proof:
+	@echo "Running charm proof"
+	@-charm proof
 
-bin/charm_helpers_sync.py:
-	@mkdir -p bin
-	@curl -o bin/charm_helpers_sync.py https://raw.githubusercontent.com/juju/charm-helpers/master/tools/charm_helpers_sync/charm_helpers_sync.py
+unittests:
+	@echo "Running unit tests"
+	@tox -e unit
 
-sync: bin/charm_helpers_sync.py
-	@$(PYTHON) bin/charm_helpers_sync.py -c charm-helpers.yaml
+functional: build
+	@echo "Executing functional tests in ${CHARM_BUILD_DIR}"
+	@CHARM_BUILD_DIR=${CHARM_BUILD_DIR} tox -e func
 
+test: lint proof unittests functional
+	@echo "Charm ${CHARM_NAME} has been tested"
 
+# The targets below don't depend on a file
+.PHONY: help submodules submodules-update clean build release lint black proof unittests functional test
diff --git a/tests/unit/requirements.txt b/tests/unit/requirements.txt
index df4d54b..efef55d 100644
--- a/tests/unit/requirements.txt
+++ b/tests/unit/requirements.txt
@@ -1,3 +1,4 @@
 pytest
 pytest-cov
-pyyaml
\ No newline at end of file
+pyyaml
+pynag
diff --git a/tox.ini b/tox.ini
index 34f9248..a98700a 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,50 +1,100 @@
 [tox]
 skipsdist=True
-envlist = lint, unit, functional
 skip_missing_interpreters = True
+envlist = lint, unit, func
 
 [testenv]
 basepython = python3
 setenv =
-  PYTHONPATH = .
-
-[testenv:unit]
-commands =
-  pytest -v --ignore {toxinidir}/tests/functional \
-	   --cov=hooks \
-	   --cov-report=term \
-	   --cov-report=annotate:reports/annotated \
-	   --cov-report=html:reports/html
-deps = -r{toxinidir}/tests/unit/requirements.txt
-       -r{toxinidir}/requirements.txt
-
-
-[coverage:run]
-omit = hooks/charmhelpers/*
-
-[testenv:functional]
+  PYTHONPATH = {toxinidir}:{toxinidir}/lib/:{toxinidir}/hooks/
 passenv =
   HOME
-  CHARM_BUILD_DIR
   PATH
+  CHARM_BUILD_DIR
   PYTEST_KEEP_MODEL
   PYTEST_CLOUD_NAME
   PYTEST_CLOUD_REGION
   PYTEST_MODEL
-commands = pytest -v --ignore {toxinidir}/tests/unit
-deps = -r{toxinidir}/tests/functional/requirements.txt
-       -r{toxinidir}/requirements.txt
+  MODEL_SETTINGS
+  HTTP_PROXY
+  HTTPS_PROXY
+  NO_PROXY
+  SNAP_HTTP_PROXY
+  SNAP_HTTPS_PROXY
 
 [testenv:lint]
-commands = flake8
-deps = flake8
+commands =
+    flake8
+# TODO    black --check --exclude "/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod|bin)/" .
+deps =
+    black
+    flake8
+    flake8-docstrings
+    flake8-import-order
+    pep8-naming
+    flake8-colors
 
 [flake8]
 exclude =
     .git,
     __pycache__,
     .tox,
-    hooks/charmhelpers
+    charmhelpers,
+    mod,
+    .build,
     bin
-max-line-length = 120
+ignore =  # TODO remove most of these
+    D100,
+    D101,
+    D102,
+    D103,
+    D104,
+    D107,
+    D202,
+    D205,
+    D208,
+    D210,
+    D400,
+    D401,
+    I100,
+    I101,
+    I201,
+    E201,
+    E202,
+    E231,
+    E121,
+    E126,
+    E131,
+    D201,
+    E302,
+    E501,
+    N806,
+    N816,
+    W504
+
+
+max-line-length = 88
 max-complexity = 10
+
+[testenv:black]
+commands =
+    black --exclude "/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod|bin)/" .
+deps =
+    black
+
+[testenv:unit]
+commands =
+  pytest -v --ignore {toxinidir}/tests/functional \
+           --cov=lib \
+           --cov=reactive \
+           --cov=actions \
+           --cov=hooks \
+           --cov=src \
+           --cov-report=term \
+           --cov-report=annotate:report/annotated \
+           --cov-report=html:report/html
+deps = -r{toxinidir}/tests/unit/requirements.txt
+
+[testenv:func]
+commands = pytest -v --ignore {toxinidir}/tests/unit
+deps = -r{toxinidir}/tests/functional/requirements.txt

Follow ups