From 07be216a0d45e368a0c2019d43bc8dd904a8cf1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sat, 20 May 2017 13:05:54 +0200 Subject: [PATCH] tests: add run-tests script, plug it into travis Also, replace subproces.call with a mockup, as notify-send is not available on travis. --- .coveragerc | 3 +++ .gitignore | 2 ++ .travis.yml | 22 +++++++++++++++++++++- ci/requirements.txt | 6 ++++++ qubesagent/test_firewall.py | 17 +++++++++++++++++ run-tests | 13 +++++++++++++ test-packages/qubesdb.py | 0 7 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 .coveragerc create mode 100644 ci/requirements.txt create mode 100755 run-tests create mode 100644 test-packages/qubesdb.py diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..bcaddf3 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,3 @@ +[run] +source = qubesagent +omit = qubesagent/test* diff --git a/.gitignore b/.gitignore index 557a3f0..cc81005 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ deb/* *.pyo *~ *.o +.coverage +*.egg-info diff --git a/.travis.yml b/.travis.yml index f644ced..e465bb5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ sudo: required dist: trusty -language: generic +language: python +python: '3.5' install: git clone https://github.com/QubesOS/qubes-builder ~/qubes-builder script: ~/qubes-builder/scripts/travis-build env: @@ -8,3 +9,22 @@ env: - DISTS_VM=fc24 USE_QUBES_REPO_VERSION=3.2 USE_QUBES_REPO_TESTING=1 - DISTS_VM=jessie USE_QUBES_REPO_VERSION=3.2 USE_QUBES_REPO_TESTING=1 - DISTS_VM=stretch USE_QUBES_REPO_VERSION=3.2 USE_QUBES_REPO_TESTING=1 + +jobs: + include: + - python: '3.5' + install: pip install --quiet -r ci/requirements.txt + env: TESTS_ONLY=1 + script: + - ./run-tests + after_success: + - codecov + - stage: deploy + python: '3.5' + env: DIST_DOM0=fc25 TESTS_ONLY= + script: ~/qubes-builder/scripts/travis-deploy + + +branches: + except: + - /.*_.*/ diff --git a/ci/requirements.txt b/ci/requirements.txt new file mode 100644 index 0000000..b5abd8d --- /dev/null +++ b/ci/requirements.txt @@ -0,0 +1,6 @@ +# WARNING: those requirements are used only for travis-ci.org +# they SHOULD NOT be used under normal conditions; use system package manager +docutils +pylint +codecov +python-daemon diff --git a/qubesagent/test_firewall.py b/qubesagent/test_firewall.py index b6222c6..f373122 100644 --- a/qubesagent/test_firewall.py +++ b/qubesagent/test_firewall.py @@ -1,6 +1,7 @@ import logging import operator from unittest import TestCase +from unittest.mock import patch import qubesagent.firewall @@ -150,6 +151,11 @@ class TestIptablesWorker(TestCase): def setUp(self): super(TestIptablesWorker, self).setUp() self.obj = IptablesWorker() + self.subprocess_patch = patch('subprocess.call') + self.subprocess_mock = self.subprocess_patch.start() + + def tearDown(self): + self.subprocess_patch.stop() def test_000_chain_for_addr(self): self.assertEqual( @@ -296,6 +302,11 @@ class TestNftablesWorker(TestCase): def setUp(self): super(TestNftablesWorker, self).setUp() self.obj = NftablesWorker() + self.subprocess_patch = patch('subprocess.call') + self.subprocess_mock = self.subprocess_patch.start() + + def tearDown(self): + self.subprocess_patch.stop() def test_000_chain_for_addr(self): self.assertEqual( @@ -465,6 +476,12 @@ class TestFirewallWorker(TestCase): self.obj.qdb.entries[ '/qubes-firewall/{}/{}'.format(addr, key)] = value + self.subprocess_patch = patch('subprocess.call') + self.subprocess_mock = self.subprocess_patch.start() + + def tearDown(self): + self.subprocess_patch.stop() + def test_read_rules(self): expected_rules1 = [ {'proto': 'tcp', 'dstports': '80-80', 'action': 'drop'}, diff --git a/run-tests b/run-tests new file mode 100755 index 0000000..00e7497 --- /dev/null +++ b/run-tests @@ -0,0 +1,13 @@ +#!/bin/sh + +: "${PYTHON:=python3}" +: "${ROOTDIR:=.}" +: "${TESTPYTHONPATH:=$ROOTDIR/test-packages}" + +PYTHONPATH="${TESTPYTHONPATH}:${PYTHONPATH}" +export PYTHONPATH + +[ -r version ] || ln -s ${ROOTDIR}/version ./ +[ -r setup.py ] || ln -s ${ROOTDIR}/setup.py ./ +"${PYTHON}" ./setup.py egg_info --egg-base "${TESTPYTHONPATH}" +"${PYTHON}" -m coverage run -m unittest discover -p '*.py' -v "$@" diff --git a/test-packages/qubesdb.py b/test-packages/qubesdb.py new file mode 100644 index 0000000..e69de29