diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/context.py | 13 | ||||
-rw-r--r-- | tests/test_debiandistro.py | 100 | ||||
-rw-r--r-- | tests/test_debianpkg.py | 29 | ||||
-rw-r--r-- | tests/test_distro.py | 40 | ||||
-rw-r--r-- | tests/test_pkg.py | 102 | ||||
-rw-r--r-- | tests/test_process.py | 21 | ||||
-rw-r--r-- | tests/test_redhatdistro.py | 58 | ||||
-rw-r--r-- | tests/test_rpmpkg.py | 28 |
8 files changed, 380 insertions, 11 deletions
diff --git a/tests/context.py b/tests/context.py index d859c2f..5878771 100644 --- a/tests/context.py +++ b/tests/context.py @@ -1,5 +1,18 @@ # this context.py should be included by all tests # idea from http://kennethreitz.com/repository-structure-and-python.html +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 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 shutil diff --git a/tests/test_debiandistro.py b/tests/test_debiandistro.py new file mode 100644 index 0000000..c01efd8 --- /dev/null +++ b/tests/test_debiandistro.py @@ -0,0 +1,100 @@ +# vim: set fileencoding=utf-8 : +# (C) 2014 Guido Günther <agx@sigxcpu.org> +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 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/>. +"""Test L{whatmaps.process} config""" + +import unittest +from mock import patch + +try: + import apt_pkg + have_apt_pkg=True +except ImportError: + have_apt_pkg=False + +from whatmaps.debiandistro import DebianDistro +from whatmaps.debianpkg import DebianPkg + +class TestDebianDistro(unittest.TestCase): + def test_vars(self): + """Check Debian distro vars""" + self.assertEqual(DebianDistro.id, 'Debian') + self.assertIsNotNone(DebianDistro._pkg_services) + self.assertIsNotNone(DebianDistro._pkg_service_blacklist) + self.assertIsNotNone(DebianDistro.service_blacklist) + self.assertEqual(DebianDistro.restart_service_cmd('aservice'), + ['invoke-rc.d', 'aservice', 'restart']) + self.assertTrue(DebianDistro.has_apt()) + + def test_pkg_by_file(self): + with patch('subprocess.Popen') as mock: + PopenMock = mock.return_value + PopenMock.returncode = 0 + PopenMock.communicate.return_value = ['apackage'] + + pkg = DebianDistro.pkg_by_file('afile') + self.assertIsInstance(pkg, DebianPkg) + self.assertEqual(pkg.name, 'apackage') + PopenMock.communicate.assert_called_once_with() + mock.assert_called_once_with(['dpkg-query', '-S', 'afile'], + stderr=-1, stdout=-1) + + def test_pkg_by_file_failure(self): + """Test if None is returned on subcommand erros""" + with patch('subprocess.Popen') as mock: + PopenMock = mock.return_value + PopenMock.returncode = 1 + PopenMock.communicate.return_value = ['apackage'] + + pkg = DebianDistro.pkg_by_file('afile') + self.assertIsNone(pkg) + PopenMock.communicate.assert_called_once_with() + mock.assert_called_once_with(['dpkg-query', '-S', 'afile'], + stderr=-1, stdout=-1) + + def test_read_apt_pipeline(self): + """Test our interaction with the apt pipeline""" + class AptPipelineMock(object): + def __init__(self): + self.iter = self.lines() + + def lines(self): + for line in ['VERSION 2', 'Whatmaps::Enable-Restart=1', '\n']: + yield line + + def readlines(self): + return ['pkg1 0.0 c 1.0 **CONFIGURE**', + 'pkg2 - c 1.0 **CONFIGURE**', + ''] + + def readline(self): + return next(self.iter) + + with patch('sys.stdin', new_callable=AptPipelineMock): + pkgs = DebianDistro.read_apt_pipeline() + self.assertEqual(len(pkgs), 1) + self.assertIn('pkg1', pkgs) + self.assertTrue(pkgs['pkg1'].name, 'pkg1') + + @patch('apt_pkg.init') + @patch('apt_pkg.Acquire') + @unittest.skipUnless(have_apt_pkg, "apt_pkg not installed") + def test_filter_security_updates(self, apt_pkg_acquire, apt_pkg_init): + pkgs = {'pkg1': DebianPkg('pkg1'), + 'pkg2': DebianPkg('pkg2'), + } + with patch('apt_pkg.Cache') as mock: + DebianDistro.filter_security_updates(pkgs) + apt_pkg_init.assert_called_once_with() + apt_pkg_acquire.assert_called_once_with() diff --git a/tests/test_debianpkg.py b/tests/test_debianpkg.py new file mode 100644 index 0000000..72ce8fb --- /dev/null +++ b/tests/test_debianpkg.py @@ -0,0 +1,29 @@ +# vim: set fileencoding=utf-8 : +# (C) 2014 Guido Günther <agx@sigxcpu.org> +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 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/>. +"""Test L{whatmaps.process} config""" + +import unittest + +from mock import patch + +from whatmaps.debianpkg import DebianPkg + +class TestDebianPkg(unittest.TestCase): + def test_services(self): + with patch('whatmaps.pkg.Pkg._get_contents') as mock: + mock.return_value = ['/etc/init.d/aservice', '/usr/bin/afile'] + p = DebianPkg('doesnotmatter') + self.assertEqual(p.services, ['aservice']) + diff --git a/tests/test_distro.py b/tests/test_distro.py new file mode 100644 index 0000000..e22da68 --- /dev/null +++ b/tests/test_distro.py @@ -0,0 +1,40 @@ +# vim: set fileencoding=utf-8 : +# (C) 2014 Guido Günther <agx@sigxcpu.org> +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 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/>. +"""Test L{whatmaps.process} config""" + +import unittest + +from whatmaps.distro import Distro + +from . import context + +class Pkg(object): + name = 'doesnotmatter' + +class TestDistro(unittest.TestCase): + def test_abstract(self): + """Check abstract method signatures""" + # Variables + self.assertEqual(Distro.service_blacklist, set()) + self.assertIsNone(Distro.id) + # Pure virtual methods + self.assertRaises(Distro.pkg, None, None, NotImplementedError) + self.assertRaises(Distro.pkg_by_file, None, NotImplementedError) + self.assertRaises(Distro.restart_service_cmd, None, NotImplementedError) + self.assertRaises(Distro.restart_service, None, NotImplementedError) + # Lookup methods + self.assertEqual(Distro.pkg_services(Pkg), []) + self.assertEqual(Distro.pkg_service_blacklist(Pkg), []) + self.assertFalse(Distro.has_apt()) diff --git a/tests/test_pkg.py b/tests/test_pkg.py new file mode 100644 index 0000000..8006b19 --- /dev/null +++ b/tests/test_pkg.py @@ -0,0 +1,102 @@ +# vim: set fileencoding=utf-8 : +# (C) 2014 Guido Günther <agx@sigxcpu.org> +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 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/>. +"""Test L{whatmaps.process} config""" + +import unittest +from mock import patch + +from whatmaps.pkg import Pkg, PkgError + +from . import context + +class TestPkg(unittest.TestCase): + def setUp(self): + self.tmpdir = context.new_tmpdir(__name__) + + def test_abstract(self): + """Check abstract method signatures""" + self.assertIsNone(Pkg.type) + self.assertIsNone(Pkg.services) + + def test_repr(self): + p = Pkg('apckage') + self.assertEqual(str(p), "<None Pkg object name:'apckage'>") + + def test_list_contents(self): + with patch('subprocess.Popen') as mock: + p = Pkg('doesnotmatter') + p._list_contents = '/does/not/matter' + PopenMock = mock.return_value + PopenMock.communicate.return_value = [ + '/package/content', + '/more/package/content', + ] + PopenMock.returncode = 0 + result = p._get_contents() + self.assertIn('/package/content', result) + self.assertNotIn('/more/package/content', result) + + # We want to check that we don't invoke Popen on + # a second call so let it fail + PopenMock.returncode = 1 + + result = p._get_contents() + self.assertIn('/package/content', result) + self.assertNotIn('/more/package/content', result) + + def test_shared_objects(self): + """Test that we properly match shared objects""" + with patch('subprocess.Popen') as mock: + p = Pkg('doesnotmatter') + p._list_contents = '/does/not/matter' + PopenMock = mock.return_value + PopenMock.communicate.return_value = ['\n'.join([ + '/lib/foo.so.1', + '/lib/bar.so', + '/not/a/shared/object', + '/not/a/shared/object.soeither', + ])] + PopenMock.returncode = 0 + result = p.shared_objects + self.assertIn('/lib/foo.so.1', result) + self.assertIn('/lib/bar.so', result) + self.assertNotIn('/not/a/shred/object', result) + self.assertNotIn('/not/a/shred/object.soeither', result) + + # We want to check that we don't invoke Popen on + # a second call so let it fail. + PopenMock.returncode = 1 + result = p._get_contents() + self.assertIn('/lib/foo.so.1', result) + self.assertNotIn('/not/a/shred/object', result) + + def test_shared_object_error(self): + """Test that we raise PkgError""" + with patch('subprocess.Popen') as mock: + p = Pkg('doesnotmatter') + p._list_contents = '/does/not/matter' + PopenMock = mock.return_value + PopenMock.communicate.return_value = [''] + PopenMock.returncode = 1 + try: + p.shared_objects + self.fail("PkgError exception not raised") + except PkgError: + pass + except Exception as e: + self.fail("Raised '%s is not PkgError" % e) + + def tearDown(self): + context.teardown() diff --git a/tests/test_process.py b/tests/test_process.py index da47ebd..91f5431 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -1,18 +1,17 @@ # vim: set fileencoding=utf-8 : # (C) 2014 Guido Günther <agx@sigxcpu.org> -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. # -# 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. +# 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, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. """Test L{whatmaps.process} config""" import os diff --git a/tests/test_redhatdistro.py b/tests/test_redhatdistro.py new file mode 100644 index 0000000..438adf8 --- /dev/null +++ b/tests/test_redhatdistro.py @@ -0,0 +1,58 @@ +# vim: set fileencoding=utf-8 : +# (C) 2014 Guido Günther <agx@sigxcpu.org> +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 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/>. +"""Test L{whatmaps.process} config""" + +import unittest +from mock import patch + +from whatmaps.redhatdistro import RedHatDistro +from whatmaps.rpmpkg import RpmPkg + +class TestRedHatDistro(unittest.TestCase): + def test_vars(self): + """Check RedHat distro vars""" + self.assertEqual(RedHatDistro.id, None) + self.assertIsNotNone(RedHatDistro._pkg_services) + self.assertIsNotNone(RedHatDistro._pkg_service_blacklist) + self.assertIsNotNone(RedHatDistro.service_blacklist) + self.assertEqual(RedHatDistro.restart_service_cmd('aservice'), + ['service', 'aservice', 'restart']) + self.assertFalse(RedHatDistro.has_apt()) + + def test_pkg_by_file(self): + with patch('subprocess.Popen') as mock: + PopenMock = mock.return_value + PopenMock.returncode = 0 + PopenMock.communicate.return_value = ['apackage'] + + pkg = RedHatDistro.pkg_by_file('afile') + self.assertIsInstance(pkg, RpmPkg) + self.assertEqual(pkg.name, 'apackage') + PopenMock.communicate.assert_called_once_with() + mock.assert_called_once_with(['rpm', '-qf', 'afile'], + stderr=-1, stdout=-1) + + def test_pkg_by_file_failure(self): + """Test if None is returned on subcommand erros""" + with patch('subprocess.Popen') as mock: + PopenMock = mock.return_value + PopenMock.returncode = 1 + PopenMock.communicate.return_value = ['apackage'] + + pkg = RedHatDistro.pkg_by_file('afile') + self.assertIsNone(pkg) + PopenMock.communicate.assert_called_once_with() + mock.assert_called_once_with(['rpm', '-qf', 'afile'], + stderr=-1, stdout=-1) diff --git a/tests/test_rpmpkg.py b/tests/test_rpmpkg.py new file mode 100644 index 0000000..064f059 --- /dev/null +++ b/tests/test_rpmpkg.py @@ -0,0 +1,28 @@ +# vim: set fileencoding=utf-8 : +# (C) 2014 Guido Günther <agx@sigxcpu.org> +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 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/>. +"""Test L{whatmaps.process} config""" + +import unittest + +from mock import patch + +from whatmaps.rpmpkg import RpmPkg + +class TestRpmPkg(unittest.TestCase): + def test_services(self): + with patch('whatmaps.pkg.Pkg._get_contents') as mock: + mock.return_value = ['/etc/rc.d/init.d/aservice', '/usr/bin/afile'] + p = RpmPkg('doesnotmatter') + self.assertEqual(p.services, ['aservice']) |