From e360582c4a58c7f54cfc692c176619219dbacad0 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 29 Oct 2014 20:44:44 +0100 Subject: [PATCH] add apt unittest --- lib/ansible/modules/packaging/os/apt.py | 8 +++-- lib/ansible/modules/tests/__init__.py | 0 lib/ansible/modules/tests/test_apt.py | 42 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 lib/ansible/modules/tests/__init__.py create mode 100644 lib/ansible/modules/tests/test_apt.py diff --git a/lib/ansible/modules/packaging/os/apt.py b/lib/ansible/modules/packaging/os/apt.py index def129caa00..459aaaa97a9 100644 --- a/lib/ansible/modules/packaging/os/apt.py +++ b/lib/ansible/modules/packaging/os/apt.py @@ -230,10 +230,10 @@ def expand_dpkg_options(dpkg_options_compressed): def expand_pkgspec_from_fnmatches(m, pkgspec, cache): new_pkgspec = [] for name_or_fnmatch_or_version in pkgspec: - pkgname_or_fnmatch_pattern = name_or_fnmatch_or_version.split("=")[0] + pkgname_or_fnmatch_pattern, version = package_split(name_or_fnmatch_or_version) # note that any of these chars is not allowed in a (debian) pkgname if [c for c in pkgname_or_fnmatch_pattern if c in "*?[]!"]: - if "=" in pkgname_or_fnmatch_pattern: + if version: m.fail_json(msg="pkgname wildcard and version can not be mixed") # handle multiarch pkgnames, the idea is that "apt*" should # only select native packages. But "apt*:i386" should still work @@ -568,4 +568,6 @@ def main(): # import module snippets from ansible.module_utils.basic import * -main() +# FIXME: if __name__ == "__main__": ? +if "ANSIBLE_IN_HAPPY_UNITTEST_LAND" not in os.environ: + main() diff --git a/lib/ansible/modules/tests/__init__.py b/lib/ansible/modules/tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/lib/ansible/modules/tests/test_apt.py b/lib/ansible/modules/tests/test_apt.py new file mode 100644 index 00000000000..10e3583801f --- /dev/null +++ b/lib/ansible/modules/tests/test_apt.py @@ -0,0 +1,42 @@ +import collections +import mock +import os +import unittest + +# FIXME: this is not super elegant +os.environ["ANSIBLE_IN_HAPPY_UNITTEST_LAND"] = "1" +from packaging.apt import ( + expand_pkgspec_from_fnmatches, +) + + +class AptExpandPkgspecTestCase(unittest.TestCase): + + def setUp(self): + FakePackage = collections.namedtuple("Package", ("name",)) + self.fake_cache = [ FakePackage("apt"), + FakePackage("apt-utils"), + ] + + def test_trivil(self): + foo = ["apt"] + self.assertEqual( + expand_pkgspec_from_fnmatches(None, foo, self.fake_cache), foo) + + def test_bug_28(self): + foo = ["apt=1.0*"] + self.assertEqual( + expand_pkgspec_from_fnmatches(None, foo, self.fake_cache), foo) + + def test_pkgname_wildcard_version_wildcard_fails(self): + foo = ["apt*=1.0*"] + m_mock = mock.Mock() + expand_pkgspec_from_fnmatches(m_mock, foo, self.fake_cache) + self.assertTrue(m_mock.fail_json.called) + + def test_pkgname_expands(self): + foo = ["apt*"] + m_mock = mock.Mock() + self.assertEqual( + expand_pkgspec_from_fnmatches(m_mock, foo, self.fake_cache), + ["apt", "apt-utils"])