summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_all.py13
-rw-r--r--tests/test_apt_cache.py69
-rw-r--r--tests/test_aptsources.py3
-rw-r--r--tests/test_auth.py67
-rw-r--r--tests/test_debfile.py4
-rw-r--r--tests/test_debfile_multiarch.py10
-rw-r--r--tests/test_lp659438.py6
-rw-r--r--tests/test_progress.py2
-rw-r--r--tests/test_tagfile.py4
9 files changed, 148 insertions, 30 deletions
diff --git a/tests/test_all.py b/tests/test_all.py
index 80526d76..25774617 100644
--- a/tests/test_all.py
+++ b/tests/test_all.py
@@ -40,17 +40,22 @@ def get_library_dir():
plat_specifier)
return os.path.abspath(library_dir)
+class MyTestRunner(unittest.runner.TextTestRunner):
+ def __init__(self, *args, **kwargs):
+ kwargs["stream"] = sys.stdout
+ super(MyTestRunner, self).__init__(*args, **kwargs)
+
if __name__ == '__main__':
if not os.access("/etc/apt/sources.list", os.R_OK):
- sys.stderr.write("[tests] Skipping because sources.list is not readable\n")
+ sys.stdout.write("[tests] Skipping because sources.list is not readable\n")
sys.exit(0)
- sys.stderr.write("[tests] Running on %s\n" % sys.version.replace("\n", ""))
+ sys.stdout.write("[tests] Running on %s\n" % sys.version.replace("\n", ""))
dirname = os.path.dirname(__file__)
if dirname:
os.chdir(dirname)
library_dir = get_library_dir()
- sys.stderr.write("Using library_dir: '%s'" % library_dir)
+ sys.stdout.write("Using library_dir: '%s'" % library_dir)
if library_dir:
sys.path.insert(0, os.path.abspath(library_dir))
@@ -58,4 +63,4 @@ if __name__ == '__main__':
if path.endswith('.py') and os.path.isfile(path):
exec('from %s import *' % path[:-3])
- unittest.main()
+ unittest.main(testRunner=MyTestRunner)
diff --git a/tests/test_apt_cache.py b/tests/test_apt_cache.py
index 448aed75..21dfeb98 100644
--- a/tests/test_apt_cache.py
+++ b/tests/test_apt_cache.py
@@ -7,29 +7,49 @@
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.
"""Unit tests for verifying the correctness of check_dep, etc in apt_pkg."""
+
+import glob
+import logging
import os
+import shutil
+import sys
import tempfile
import unittest
+if sys.version_info[0] == 2 and sys.version_info[1] == 6:
+ from unittest2 import TestCase
+else:
+ from unittest import TestCase
+
+
from test_all import get_library_dir
-import sys
-sys.path.insert(0, get_library_dir())
+libdir = get_library_dir()
+if libdir:
+ sys.path.insert(0, libdir)
import apt
import apt_pkg
-import shutil
-import glob
-import logging
+
def if_sources_list_is_readable(f):
def wrapper(*args, **kwargs):
if os.access("/etc/apt/sources.list", os.R_OK):
f(*args, **kwargs)
else:
- logging.warn("skipping '%s' because sources.list is not readable" % f)
+ logging.warning("skipping '%s' because sources.list is not readable" % f)
return wrapper
-class TestAptCache(unittest.TestCase):
+
+def get_open_file_descriptors():
+ try:
+ fds = os.listdir("/proc/self/fd")
+ except OSError:
+ logging.warning("failed to list /proc/self/fd")
+ return set([])
+ return set(map(int, fds))
+
+
+class TestAptCache(TestCase):
""" test the apt cache """
def setUp(self):
@@ -39,6 +59,8 @@ class TestAptCache(unittest.TestCase):
self._cnf = {}
for item in apt_pkg.config.keys():
self._cnf[item] = apt_pkg.config.find(item)
+ apt_pkg.config.clear("APT::Update::Post-Invoke")
+ apt_pkg.config.clear("APT::Update::Post-Invoke-Success")
def tearDown(self):
for item in self._cnf:
@@ -68,14 +90,41 @@ class TestAptCache(unittest.TestCase):
self.assertEqual(r['Package'], pkg.shortname)
self.assertTrue('Version' in r)
self.assertTrue(len(r['Description']) > 0)
- self.assertTrue(str(r).startswith('Package: %s\n' % pkg.shortname))
+ self.assertTrue(
+ str(r).startswith('Package: %s\n' % pkg.shortname))
+
+ @if_sources_list_is_readable
+ def test_cache_close_leak_fd(self):
+ fds_before_open = get_open_file_descriptors()
+ cache = apt.Cache()
+ opened_fd = get_open_file_descriptors().difference(fds_before_open)
+ cache.close()
+ fds_after_close = get_open_file_descriptors()
+ unclosed_fd = opened_fd.intersection(fds_after_close)
+ self.assertEqual(fds_before_open, fds_after_close)
+ self.assertEqual(unclosed_fd, set())
+
+ def test_cache_open_twice_leaks_fds(self):
+ cache = apt.Cache()
+ fds_before_open = get_open_file_descriptors()
+ cache.open()
+ fds_after_open_twice = get_open_file_descriptors()
+ self.assertEqual(fds_before_open, fds_after_open_twice)
+
+ @if_sources_list_is_readable
+ def test_cache_close_download_fails(self):
+ cache = apt.Cache()
+ self.assertEqual(cache.required_download, 0)
+ cache.close()
+ with self.assertRaises(apt.cache.CacheClosedException):
+ cache.required_download
def test_get_provided_packages(self):
apt.apt_pkg.config.set("Apt::architecture", "i386")
cache = apt.Cache(rootdir="./data/test-provides/")
cache.open()
if len(cache) == 0:
- logging.warn("skipping test_get_provided_packages, cache empty?!?")
+ logging.warning("skipping test_get_provided_packages, cache empty?!?")
return
# a true virtual pkg
l = cache.get_providing_packages("mail-transport-agent")
@@ -88,7 +137,7 @@ class TestAptCache(unittest.TestCase):
# create highlevel cache and get the lowlevel one from it
highlevel_cache = apt.Cache(rootdir="./data/test-provides")
if len(highlevel_cache) == 0:
- logging.warn("skipping test_log_level_pkg_provides, cache empty?!?")
+ logging.warning("skipping test_log_level_pkg_provides, cache empty?!?")
return
# low level cache provides list of the pkg
cache = highlevel_cache._cache
diff --git a/tests/test_aptsources.py b/tests/test_aptsources.py
index 41cfabb3..75dd91c1 100644
--- a/tests/test_aptsources.py
+++ b/tests/test_aptsources.py
@@ -164,7 +164,8 @@ class TestAptSources(unittest.TestCase):
from subprocess import Popen, PIPE
target = "./data/aptsources/sources.list.enable_comps"
line = "deb http://archive.ubuntu.com/ubuntu lucid main\n"
- open(target, "w").write(line)
+ with open(target, "w") as target_file:
+ target_file.write(line)
apt_pkg.config.set("Dir::Etc::sourcelist", target)
sources = aptsources.sourceslist.SourcesList(True, self.templates)
distro = aptsources.distro.get_distro(id="Ubuntu")
diff --git a/tests/test_auth.py b/tests/test_auth.py
index 2b524d28..bc353427 100644
--- a/tests/test_auth.py
+++ b/tests/test_auth.py
@@ -1,5 +1,10 @@
#!/usr/bin/env python
+from __future__ import print_function
+
+import contextlib
+import errno
+import itertools
import os
import shutil
import sys
@@ -152,6 +157,21 @@ class TestAuthKeys(TestCase):
for item in cnf:
apt_pkg.config.set(item, cnf[item])
+ @contextlib.contextmanager
+ def _discard_stderr(self):
+ stderr_fd = sys.stderr.fileno()
+ stderr_save = os.dup(stderr_fd)
+ try:
+ devnull = os.open('/dev/null', os.O_WRONLY)
+ try:
+ os.dup2(devnull, stderr_fd)
+ yield
+ finally:
+ os.close(devnull)
+ finally:
+ os.dup2(stderr_save, stderr_fd)
+ os.close(stderr_save)
+
def testAddAndExportKey(self):
"""Add an example key."""
apt.auth.add_key(WHEEZY_KEY)
@@ -202,20 +222,22 @@ class TestAuthKeys(TestCase):
self._start_keyserver()
self.addCleanup(self._stop_keyserver)
with self.assertRaises(apt.auth.AptKeyError) as cm:
- apt.auth.add_key_from_keyserver(
- "0101010178F7FE5C3E65D8AF8B48AD6246925553",
- "hkp://localhost:19191")
+ with self._discard_stderr():
+ apt.auth.add_key_from_keyserver(
+ "0101010178F7FE5C3E65D8AF8B48AD6246925553",
+ "hkp://localhost:%d" % self.keyserver_port)
self.assertTrue(
- str(cm.exception).startswith("Fingerprints do not match"))
+ str(cm.exception).startswith("Fingerprints do not match"), cm.exception)
def testAddKeyFromServer(self):
"""Install a GnuPG key from a remote server."""
self._start_keyserver()
self.addCleanup(self._stop_keyserver)
- apt.auth.add_key_from_keyserver(
- "0xa1bD8E9D78F7FE5C3E65D8AF8B48AD6246925553",
- "hkp://localhost:19191")
+ with self._discard_stderr():
+ apt.auth.add_key_from_keyserver(
+ "0xa1bD8E9D78F7FE5C3E65D8AF8B48AD6246925553",
+ "hkp://localhost:%d" % self.keyserver_port)
ret = apt.auth.list_keys()
self.assertEqual(len(ret), 1)
@@ -228,6 +250,8 @@ class TestAuthKeys(TestCase):
def _start_keyserver(self):
"""Start a fake keyserver on http://localhost:19191
+ If port 19191 is unavailable, try successive ports until one is.
+ Store the port actually in use in self.keyserver_port.
Thanks pitti.
"""
dir = tempfile.mkdtemp()
@@ -236,15 +260,39 @@ class TestAuthKeys(TestCase):
with open(os.path.join(dir, "pks", "lookup"), "w") as key_file:
key_file.write(WHEEZY_KEY)
+ keyserver_pipe = os.pipe()
self.keyserver_pid = os.fork()
if self.keyserver_pid == 0:
+ os.close(keyserver_pipe[0])
# quiesce server log
os.dup2(os.open('/dev/null', os.O_WRONLY), sys.stderr.fileno())
os.chdir(dir)
- httpd = HTTPServer(('localhost', 19191), HTTPRequestHandler)
+ for port in itertools.count(19191):
+ try:
+ httpd = HTTPServer(('localhost', port), HTTPRequestHandler)
+ break
+ except IOError as e:
+ if e.errno != errno.EADDRINUSE:
+ raise
+ keyserver_write = os.fdopen(keyserver_pipe[1], 'w')
+ print(port, file=keyserver_write)
+ keyserver_write.close()
httpd.serve_forever()
os._exit(0)
+ os.close(keyserver_pipe[1])
+ keyserver_read = os.fdopen(keyserver_pipe[0])
+ self.keyserver_port = int(keyserver_read.readline())
+ keyserver_read.close()
+
+ # temporarily disable proxy, as gnupg does not get along with that
+ # (LP #789049)
+ self.orig_proxy = os.environ.get('http_proxy')
+ try:
+ del os.environ['http_proxy']
+ except KeyError:
+ pass
+
# wait a bit until server is ready
time.sleep(0.5)
@@ -255,6 +303,9 @@ class TestAuthKeys(TestCase):
os.kill(self.keyserver_pid, 15)
os.wait()
+ # restore proxy
+ if self.orig_proxy is not None:
+ os.environ['http_proxy'] = self.orig_proxy
if __name__ == "__main__":
unittest.main()
diff --git a/tests/test_debfile.py b/tests/test_debfile.py
index 04a6b65a..75c46966 100644
--- a/tests/test_debfile.py
+++ b/tests/test_debfile.py
@@ -13,7 +13,9 @@ import unittest
from test_all import get_library_dir
import sys
-sys.path.insert(0, get_library_dir())
+libdir = get_library_dir()
+if libdir:
+ sys.path.insert(0, libdir)
import apt_pkg
import apt.debfile
diff --git a/tests/test_debfile_multiarch.py b/tests/test_debfile_multiarch.py
index 7c02a32a..bbf62016 100644
--- a/tests/test_debfile_multiarch.py
+++ b/tests/test_debfile_multiarch.py
@@ -13,7 +13,9 @@ import unittest
from test_all import get_library_dir
import sys
-sys.path.insert(0, get_library_dir())
+libdir = get_library_dir()
+if libdir:
+ sys.path.insert(0, libdir)
import apt
import apt_pkg
import apt.debfile
@@ -23,7 +25,8 @@ class TestDebfileMultiarch(unittest.TestCase):
def test_multiarch_deb_check(self):
if apt_pkg.get_architectures() != ["amd64", "i386"]:
- logging.warn("skipping test because running on a non-multiarch system")
+ # TODO: use unittest.skip
+ #logging.warning("skipping test because running on a non-multiarch system")
return
deb = apt.debfile.DebPackage(
"./data/test_debs/multiarch-test1_i386.deb")
@@ -37,7 +40,8 @@ class TestDebfileMultiarch(unittest.TestCase):
# use "lib3ds-1-3" as a test to see if non-multiach lib conflicts work
canary = "lib3ds-1-3"
if not canary in cache:
- logging.warn("skipping test because %s is missing" % canary)
+ # TODO: use unittest.skip
+ #logging.warning("skipping test because %s is missing" % canary)
return
cache[canary].mark_install()
deb = apt.debfile.DebPackage(
diff --git a/tests/test_lp659438.py b/tests/test_lp659438.py
index d3bdd910..b9a837b4 100644
--- a/tests/test_lp659438.py
+++ b/tests/test_lp659438.py
@@ -39,6 +39,8 @@ class RegressionTestCase(unittest.TestCase):
def setUp(self):
apt_pkg.init_config()
+ apt_pkg.config.clear("APT::Update::Post-Invoke")
+ apt_pkg.config.clear("APT::Update::Post-Invoke-Success")
self.chroot_path = chroot_path = tempfile.mkdtemp()
# Create a damaged status file
self.cache = apt.cache.Cache(rootdir=chroot_path)
@@ -48,8 +50,8 @@ class RegressionTestCase(unittest.TestCase):
Status: install reinstreq half-installed
Priority: optional
Section: admin
-Architecture: all
-Version: 3.6.9+build1+nobinonly-0ubuntu1""")
+Version: 3.6.9+build1+nobinonly-0ubuntu1
+Architecture: all""")
sources_list_path = apt_pkg.config.find_file("Dir::Etc::sourcelist")
repo_path = os.path.abspath("./data/test-repo")
with open(sources_list_path, "w") as sources_list:
diff --git a/tests/test_progress.py b/tests/test_progress.py
index 3b6285d6..b7bba02f 100644
--- a/tests/test_progress.py
+++ b/tests/test_progress.py
@@ -37,6 +37,8 @@ class TestProgress(unittest.TestCase):
with open("fetch_sources.list","w") as fobj:
fobj.write(deb_line)
apt_pkg.config.set("Dir::Etc::sourcelist", "fetch_sources.list")
+ apt_pkg.config.clear("APT::Update::Post-Invoke")
+ apt_pkg.config.clear("APT::Update::Post-Invoke-Success")
def test_acquire_progress(self):
progress = TestAcquireProgress()
diff --git a/tests/test_tagfile.py b/tests/test_tagfile.py
index 33197e6a..f26f851b 100644
--- a/tests/test_tagfile.py
+++ b/tests/test_tagfile.py
@@ -21,7 +21,9 @@ import tempfile
import unittest
from test_all import get_library_dir
-sys.path.insert(0, get_library_dir())
+libdir = get_library_dir()
+if libdir:
+ sys.path.insert(0, libdir)
import apt_pkg