summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--Makefile8
-rw-r--r--README.md9
-rw-r--r--sonar-project.properties21
-rw-r--r--src/tests/helper.py27
-rw-r--r--src/tests/test_vyos_update_crontab.py131
6 files changed, 199 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index ff15b4fbb..a5b100a5b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -112,3 +112,6 @@ tests/templates/*
debian/files
debian/vyos-1x
debian/vyos-1x.*
+
+# Sonar Cloud
+.scannerwork
diff --git a/Makefile b/Makefile
index 8a75a91e4..a46f4313a 100644
--- a/Makefile
+++ b/Makefile
@@ -35,3 +35,11 @@ all: interface_definitions op_mode_definitions
clean:
rm -rf $(TMPL_DIR)/*
rm -rf $(OP_TMPL_DIR)/*
+
+.PHONY: test
+test:
+ nosetests --with-xunit src --with-coverage --cover-erase --cover-xml --cover-package src/conf-mode,src/op-mode --verbose
+
+.PHONY: sonar
+sonar:
+ sonar-scanner -X -Dsonar.login=${SONAR_TOKEN}
diff --git a/README.md b/README.md
index d7d979c02..8ef89c5db 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,11 @@
# vyos-1x
+
+[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=vyos%3Avyos-1x&metric=coverage)](https://sonarcloud.io/component_measures?id=vyos%3Avyos-1x&metric=coverage)
+
VyOS 1.2.0+ configuration scripts and data
+
+# Test
+
+```
+make test
+```
diff --git a/sonar-project.properties b/sonar-project.properties
new file mode 100644
index 000000000..7eef2d976
--- /dev/null
+++ b/sonar-project.properties
@@ -0,0 +1,21 @@
+sonar.projectKey=vyos:vyos-1x
+sonar.projectName=vyos-1x
+sonar.projectVersion=1.2.0
+sonar.organization=vyos
+
+sonar.sources=src/conf-mode,src/op-mode
+sonar.language=py
+sonar.sourceEncoding=UTF-8
+
+sonar.links.homepage=https://github.com/vyos/vyos-1x
+sonar.links.ci=https://ci.vyos.net/job/vyos-1x/
+sonar.links.scm=https://github.com/vyos/vyos-1x
+sonar.links.issue=https://phabricator.vyos.net/
+
+sonar.host.url=https://sonarcloud.io
+
+sonar.python.pylint=/usr/local/bin/pylint
+sonar.python.pylint_config=.pylintrc
+sonar.python.pylint.reportPath=pylint-report.txt
+sonar.python.xunit.reportPath=nosetests.xml
+sonar.python.coverage.reportPath=coverage.xml
diff --git a/src/tests/helper.py b/src/tests/helper.py
new file mode 100644
index 000000000..a7e4f201c
--- /dev/null
+++ b/src/tests/helper.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2018 VyOS maintainers and contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#
+
+import sys
+import importlib.util
+
+
+def prepare_module(file_path='', module_name=''):
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ sys.modules[module_name] = module
diff --git a/src/tests/test_vyos_update_crontab.py b/src/tests/test_vyos_update_crontab.py
new file mode 100644
index 000000000..efeadf2f5
--- /dev/null
+++ b/src/tests/test_vyos_update_crontab.py
@@ -0,0 +1,131 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2018 VyOS maintainers and contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#
+
+import os
+import sys
+import tempfile
+import unittest
+
+from vyos import ConfigError
+import helper
+
+file_path = 'src/conf-mode/vyos-update-crontab.py'
+module_name = 'vyos_update_crontab'
+helper.prepare_module(file_path, module_name)
+import vyos_update_crontab
+
+
+class TestUpdateCrontab(unittest.TestCase):
+
+ def test_verify(self):
+ tests = [
+ {'name': 'one_task',
+ 'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
+ 'expected': None
+ },
+ {'name': 'has_interval_and_spec',
+ 'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '0 * * * *', 'executable': '/bin/ls', 'args': '-l'}],
+ 'expected': ConfigError
+ },
+ {'name': 'has_no_interval_and_spec',
+ 'tasks': [{'name': 'aaa', 'interval': '', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
+ 'expected': ConfigError
+ },
+ {'name': 'invalid_interval',
+ 'tasks': [{'name': 'aaa', 'interval': '1y', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
+ 'expected': ConfigError
+ },
+ {'name': 'invalid_interval_min',
+ 'tasks': [{'name': 'aaa', 'interval': '61m', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
+ 'expected': ConfigError
+ },
+ {'name': 'invalid_interval_hour',
+ 'tasks': [{'name': 'aaa', 'interval': '25h', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
+ 'expected': ConfigError
+ },
+ {'name': 'invalid_interval_day',
+ 'tasks': [{'name': 'aaa', 'interval': '32d', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
+ 'expected': ConfigError
+ },
+ {'name': 'no_executable',
+ 'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '', 'executable': '', 'args': ''}],
+ 'expected': ConfigError
+ },
+ {'name': 'invalid_executable',
+ 'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '', 'executable': '/bin/aaa', 'args': ''}],
+ 'expected': ConfigError
+ }
+ ]
+ for t in tests:
+ with self.subTest(msg=t['name'], tasks=t['tasks'], expected=t['expected']):
+ if t['expected'] is not None:
+ with self.assertRaises(t['expected']):
+ vyos_update_crontab.verify(t['tasks'])
+ else:
+ vyos_update_crontab.verify(t['tasks'])
+
+ def test_generate(self):
+ tests = [
+ {'name': 'zero_task',
+ 'tasks': [],
+ 'expected': []
+ },
+ {'name': 'one_task',
+ 'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
+ 'expected': [
+ '### Generated by vyos-update-crontab.py ###',
+ '*/60 * * * * root /bin/ls -l']
+ },
+ {'name': 'one_task_with_hour',
+ 'tasks': [{'name': 'aaa', 'interval': '10h', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
+ 'expected': [
+ '### Generated by vyos-update-crontab.py ###',
+ '0 */10 * * * root /bin/ls -l']
+ },
+ {'name': 'one_task_with_day',
+ 'tasks': [{'name': 'aaa', 'interval': '10d', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
+ 'expected': [
+ '### Generated by vyos-update-crontab.py ###',
+ '0 0 */10 * * root /bin/ls -l']
+ },
+ {'name': 'multiple_tasks',
+ 'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '', 'executable': '/bin/ls', 'args': '-l'},
+ {'name': 'bbb', 'interval': '', 'spec': '0 0 * * *', 'executable': '/bin/ls', 'args': '-ltr'}
+ ],
+ 'expected': [
+ '### Generated by vyos-update-crontab.py ###',
+ '*/60 * * * * root /bin/ls -l',
+ '0 0 * * * root /bin/ls -ltr']
+ }
+ ]
+ for t in tests:
+ with self.subTest(msg=t['name'], tasks=t['tasks'], expected=t['expected']):
+ vyos_update_crontab.crontab_file = tempfile.mkstemp()[1]
+ vyos_update_crontab.generate(t['tasks'])
+ if len(t['expected']) > 0:
+ self.assertTrue(os.path.isfile(vyos_update_crontab.crontab_file))
+ with open(vyos_update_crontab.crontab_file) as f:
+ actual = f.read()
+ self.assertEqual(t['expected'], actual.splitlines())
+ os.remove(vyos_update_crontab.crontab_file)
+ else:
+ self.assertFalse(os.path.isfile(vyos_update_crontab.crontab_file))
+
+
+if __name__ == "__main__":
+ unittest.main()