7185843 pkg makes too many calls to be.beList
authorBrock Pytlik <brock.pytlik@oracle.com>
Fri, 27 Jul 2012 14:40:53 -0700
changeset 2750 9982917e7fee
parent 2749 9d664b5d7896
child 2751 9a410ee15675
7185843 pkg makes too many calls to be.beList 7185847 workaround for 7043482 can be removed
src/modules/client/api.py
src/modules/client/bootenv.py
src/tests/api/t_bootenv.py
--- a/src/modules/client/api.py	Tue Jul 24 15:38:25 2012 -0700
+++ b/src/modules/client/api.py	Fri Jul 27 14:40:53 2012 -0700
@@ -4770,7 +4770,13 @@
         def log_operation_start(self, name):
                 """Marks the start of an operation to be recorded in image
                 history."""
-                be_name, be_uuid = bootenv.BootEnv.get_be_name(self._img.root)
+                # If an operation is going to be discarded, then don't take the
+                # performance hit of actually getting the BE info.
+                if name in history.DISCARDED_OPERATIONS:
+                        be_name, be_uuid = None, None
+                else:
+                        be_name, be_uuid = bootenv.BootEnv.get_be_name(
+                            self._img.root)
                 self._img.history.log_operation_start(name,
                     be_name=be_name, be_uuid=be_uuid)
 
--- a/src/modules/client/bootenv.py	Tue Jul 24 15:38:25 2012 -0700
+++ b/src/modules/client/bootenv.py	Fri Jul 27 14:40:53 2012 -0700
@@ -20,7 +20,7 @@
 # CDDL HEADER END
 #
 
-# Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
 
 import errno
 import os
@@ -285,32 +285,17 @@
 
         @staticmethod
         def get_be_list(raise_error=False):
+                # This check enables the test suite to run much more quickly.
+                # It is necessary because pkg5unittest (eventually) imports this
+                # module before the environment is sanitized.
+                if "PKG_NO_LIVE_ROOT" in os.environ:
+                        return BootEnvNull.get_be_list()
                 # Check for the old beList() API since pkg(1) can be
                 # back published and live on a system without the 
                 # latest libbe.
                 rc = 0
 
                 beVals = be.beList()
-                # XXX temporary workaround for ON bug #7043482 (needed for
-                # successful test suite runs on b166-b167).
-                if portable.util.get_canonical_os_name() == "sunos":
-                        for entry in os.listdir("/proc/self/path"):
-                                try:
-                                        int(entry)
-                                except ValueError:
-                                        # Only interested in file descriptors.
-                                        continue
-
-                                fpath = os.path.join("/proc/self/path", entry)
-                                try:
-                                        if os.readlink(fpath) == \
-                                            "/etc/dev/cro_db":
-                                                os.close(int(entry))
-                                except OSError, e:
-                                        if e.errno not in (errno.ENOENT,
-                                            errno.EBADFD):
-                                                raise
-
                 if isinstance(beVals[0], int):
                         rc, beList = beVals
                 else:
@@ -676,6 +661,7 @@
 
                 self.destroy_snapshot()
 
+
 class BootEnvNull(object):
 
         """BootEnvNull is a class that gets used when libbe doesn't exist."""
@@ -722,7 +708,7 @@
 
         @staticmethod
         def get_be_list():
-                pass
+                return []
 
         @staticmethod
         def get_be_name(path):
--- a/src/tests/api/t_bootenv.py	Tue Jul 24 15:38:25 2012 -0700
+++ b/src/tests/api/t_bootenv.py	Fri Jul 27 14:40:53 2012 -0700
@@ -20,10 +20,7 @@
 # CDDL HEADER END
 #
 
-#
-# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
-# Use is subject to license terms.
-#
+# Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
 
 import testutils
 if __name__ == "__main__":
@@ -39,14 +36,56 @@
                 
         def test_api_consistency(self):
                 """Make sure every public method in BootEnv exists in
-                BootEnvNull.
+                BootEnvNull and the other way around.
                 """
-                nullm = dir(bootenv.BootEnvNull)
-                for m in dir(bootenv.BootEnv):
-                        if m.startswith("_"):
-                                continue
-                        self.assert_(m in nullm,
-                            "missing method %s in BootEnvNull" % m)
-            
+
+                nullm = set(d for d in dir(bootenv.BootEnvNull)
+                    if not d.startswith("_"))
+                bem = set(d for d in dir(bootenv.BootEnv)
+                    if not d.startswith("_"))
+
+                be_missing = nullm - bem
+                null_missing = bem - nullm
+
+                estr = ""
+                if be_missing:
+                        estr += "The following methods were missing from " \
+                            "BootEnv:\n" + \
+                            "\n".join("\t%s" % s for s in be_missing)
+                if null_missing:
+                        estr += "The following methods were missing from " \
+                            "BootEnvNull:\n" + \
+                            "\n".join("\t%s" % s for s in null_missing)
+                self.assert_(not estr, estr)
+
+        def test_bootenv(self):
+                """All other test suite tests test the BootEnv class with,
+                PKG_NO_LIVE_ROOT set in the environment, see the run() and
+                env_santize(..) methods in Pkg5TestSuite.  That environment
+                variable means that BootEnv.get_be_list will always return an
+                empty list.  While we want to generally avoid touching the live
+                image root at all, making an exception here seems the best
+                reasonable way to test some of the non-modifying BootEnv code.
+                To that end, this test clears the relevant environment variable.
+                """
+
+                del os.environ["PKG_NO_LIVE_ROOT"]
+                self.assert_(bootenv.BootEnv.libbe_exists())
+                self.assert_(bootenv.BootEnv.check_verify())
+                self.assert_(isinstance(
+                    bootenv.BootEnv.get_be_list(raise_error=False), list))
+                self.assert_(isinstance(
+                    bootenv.BootEnv.get_be_list(raise_error=True), list))
+
+                bootenv.BootEnv.get_be_name("/")
+                self.assert_(
+                    isinstance(bootenv.BootEnv.get_uuid_be_dic(), dict))
+                bootenv.BootEnv.get_activated_be_name()
+                bootenv.BootEnv.get_active_be_name()
+                # This assumes that a1b2c3d4e5f6g7h8i9j0 is highly unlikely to
+                # be an existing BE on the system.
+                bootenv.BootEnv.check_be_name("a1b2c3d4e5f6g7h8i9j0")
+
+
 if __name__ == "__main__":
         unittest.main()