Test portdbapi/egencache cache-formats behavior.
authorZac Medico <zmedico@gentoo.org>
Sat, 8 Sep 2012 04:07:45 +0000 (21:07 -0700)
committerZac Medico <zmedico@gentoo.org>
Sat, 8 Sep 2012 04:07:45 +0000 (21:07 -0700)
pym/portage/tests/dbapi/test_portdb_cache.py [new file with mode: 0644]

diff --git a/pym/portage/tests/dbapi/test_portdb_cache.py b/pym/portage/tests/dbapi/test_portdb_cache.py
new file mode 100644 (file)
index 0000000..e6d2ba7
--- /dev/null
@@ -0,0 +1,178 @@
+# Copyright 2012 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+import subprocess
+import sys
+import textwrap
+
+import portage
+from portage import os
+from portage import _unicode_decode
+from portage.const import (BASH_BINARY, PORTAGE_BIN_PATH,
+       PORTAGE_PYM_PATH, USER_CONFIG_PATH)
+from portage.tests import TestCase
+from portage.tests.resolver.ResolverPlayground import ResolverPlayground
+from portage.util import ensure_dirs
+
+class PortdbCacheTestCase(TestCase):
+
+       def testPortdbCache(self):
+               debug = False
+
+               ebuilds = {
+                       "dev-libs/A-1": {},
+                       "dev-libs/A-2": {},
+                       "sys-apps/B-1": {},
+                       "sys-apps/B-2": {},
+               }
+
+               playground = ResolverPlayground(ebuilds=ebuilds, debug=debug)
+               settings = playground.settings
+               eprefix = settings["EPREFIX"]
+               portdir = settings["PORTDIR"]
+               user_config_dir = os.path.join(eprefix, USER_CONFIG_PATH)
+               metadata_dir = os.path.join(portdir, "metadata")
+               md5_cache_dir = os.path.join(metadata_dir, "md5-cache")
+               pms_cache_dir = os.path.join(metadata_dir, "cache")
+               layout_conf_path = os.path.join(metadata_dir, "layout.conf")
+
+               portage_python = portage._python_interpreter
+               egencache_cmd = (portage_python, "-Wd",
+                       os.path.join(PORTAGE_BIN_PATH, "egencache"))
+               python_cmd = (portage_python, "-Wd", "-c")
+
+               test_commands = (
+                       (lambda: not os.path.exists(pms_cache_dir),),
+                       (lambda: not os.path.exists(md5_cache_dir),),
+                       python_cmd + (textwrap.dedent("""
+                               import os, sys, portage
+                               if portage.portdb.porttree_root in portage.portdb._pregen_auxdb:
+                                       sys.exit(1)
+                       """),),
+
+                       egencache_cmd + ("--update",),
+                       (lambda: os.path.exists(pms_cache_dir),),
+                       (lambda: not os.path.exists(md5_cache_dir),),
+                       python_cmd + (textwrap.dedent("""
+                               import os, sys, portage
+                               if portage.portdb.porttree_root not in portage.portdb._pregen_auxdb:
+                                       sys.exit(1)
+                       """),),
+                       python_cmd + (textwrap.dedent("""
+                               import os, sys, portage
+                               from portage.cache.metadata import database as pms_database
+                               if not isinstance(portage.portdb._pregen_auxdb[portage.portdb.porttree_root], pms_database):
+                                       sys.exit(1)
+                       """),),
+
+                       (BASH_BINARY, "-c", "echo %s > %s" %
+                               tuple(map(portage._shell_quote,
+                               ("cache-formats = md5-dict pms", layout_conf_path,)))),
+                       egencache_cmd + ("--update",),
+                       (lambda: os.path.exists(md5_cache_dir),),
+                       python_cmd + (textwrap.dedent("""
+                               import os, sys, portage
+                               if portage.portdb.porttree_root not in portage.portdb._pregen_auxdb:
+                                       sys.exit(1)
+                       """),),
+                       python_cmd + (textwrap.dedent("""
+                               import os, sys, portage
+                               from portage.cache.flat_hash import md5_database
+                               if not isinstance(portage.portdb._pregen_auxdb[portage.portdb.porttree_root], md5_database):
+                                       sys.exit(1)
+                       """),),
+
+                       (BASH_BINARY, "-c", "echo %s > %s" %
+                               tuple(map(portage._shell_quote,
+                               ("cache-formats = pms md5-dict", layout_conf_path,)))),
+                       python_cmd + (textwrap.dedent("""
+                               import os, sys, portage
+                               if portage.portdb.porttree_root not in portage.portdb._pregen_auxdb:
+                                       sys.exit(1)
+                       """),),
+                       python_cmd + (textwrap.dedent("""
+                               import os, sys, portage
+                               from portage.cache.metadata import database as pms_database
+                               if not isinstance(portage.portdb._pregen_auxdb[portage.portdb.porttree_root], pms_database):
+                                       sys.exit(1)
+                       """),),
+               )
+
+               features = []
+               if not portage.process.sandbox_capable or \
+                       os.environ.get("SANDBOX_ON") == "1":
+                       features.append("-sandbox")
+
+               make_conf = (
+                       "FEATURES=\"%s\"\n" % (" ".join(features),),
+                       "PORTDIR=\"%s\"\n" % (portdir,),
+                       "PORTAGE_GRPNAME=\"%s\"\n" % (os.environ["PORTAGE_GRPNAME"],),
+                       "PORTAGE_USERNAME=\"%s\"\n" % (os.environ["PORTAGE_USERNAME"],),
+               )
+
+               pythonpath =  os.environ.get("PYTHONPATH")
+               if pythonpath is not None and not pythonpath.strip():
+                       pythonpath = None
+               if pythonpath is not None and \
+                       pythonpath.split(":")[0] == PORTAGE_PYM_PATH:
+                       pass
+               else:
+                       if pythonpath is None:
+                               pythonpath = ""
+                       else:
+                               pythonpath = ":" + pythonpath
+                       pythonpath = PORTAGE_PYM_PATH + pythonpath
+
+               env = {
+                       "PATH" : os.environ.get("PATH", ""),
+                       "PORTAGE_OVERRIDE_EPREFIX" : eprefix,
+                       "PORTAGE_PYTHON" : portage_python,
+                       "PYTHONPATH" : pythonpath,
+               }
+
+               if "__PORTAGE_TEST_HARDLINK_LOCKS" in os.environ:
+                       env["__PORTAGE_TEST_HARDLINK_LOCKS"] = \
+                               os.environ["__PORTAGE_TEST_HARDLINK_LOCKS"]
+
+               dirs = [user_config_dir]
+
+               try:
+                       for d in dirs:
+                               ensure_dirs(d)
+                       with open(os.path.join(user_config_dir, "make.conf"), 'w') as f:
+                               for line in make_conf:
+                                       f.write(line)
+
+                       if debug:
+                               # The subprocess inherits both stdout and stderr, for
+                               # debugging purposes.
+                               stdout = None
+                       else:
+                               # The subprocess inherits stderr so that any warnings
+                               # triggered by python -Wd will be visible.
+                               stdout = subprocess.PIPE
+
+                       for i, args in enumerate(test_commands):
+
+                               if hasattr(args[0], '__call__'):
+                                       self.assertTrue(args[0](),
+                                               "callable at index %s failed" % (i,))
+                                       continue
+
+                               proc = subprocess.Popen(args,
+                                       env=env, stdout=stdout)
+
+                               if debug:
+                                       proc.wait()
+                               else:
+                                       output = proc.stdout.readlines()
+                                       proc.wait()
+                                       proc.stdout.close()
+                                       if proc.returncode != os.EX_OK:
+                                               for line in output:
+                                                       sys.stderr.write(_unicode_decode(line))
+
+                               self.assertEqual(os.EX_OK, proc.returncode,
+                                       "command failed with args %s" % (args,))
+               finally:
+                       playground.cleanup()