pkg5testenv.py revision 2692
1715N/A#!/usr/bin/python
1715N/A
1715N/A# CDDL HEADER START
1715N/A#
1715N/A# The contents of this file are subject to the terms of the
1715N/A# Common Development and Distribution License (the "License").
1715N/A# You may not use this file except in compliance with the License.
1715N/A#
1715N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1715N/A# or http://www.opensolaris.org/os/licensing.
1715N/A# See the License for the specific language governing permissions
1715N/A# and limitations under the License.
1715N/A#
1715N/A# When distributing Covered Code, include this CDDL HEADER in each
1715N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1715N/A# If applicable, add the following below this CDDL HEADER, with the
1715N/A# fields enclosed by brackets "[]" replaced with your own identifying
1715N/A# information: Portions Copyright [yyyy] [name of copyright owner]
1715N/A#
1715N/A# CDDL HEADER END
1715N/A#
1715N/A
2692N/A# Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
1715N/A
1715N/Aimport os
1715N/Aimport sys
1715N/Aimport platform
1715N/Aimport tempfile
1715N/A
1971N/Adef setup_environment(path_to_proto, covdir=None, debug=False):
1715N/A """ Set up environment for doing testing.
1715N/A
1715N/A We set PYTHONPATH and PATH so that they reference the proto
1715N/A area, and clear packaging related environment variables
1715N/A (every variable prefixed with PKG_).
1715N/A
1715N/A path_to_proto should be a relative path indicating a path
1715N/A to proto area of the workspace. So, if your test case is
1715N/A three levels deep: ex. src/tests/cli/foo.py, this should be
1715N/A "../../../proto"
1715N/A
1715N/A This function looks at argv[0] to compute the ultimate
1715N/A path to the proto area; this is nice because you can then
1715N/A invoke test cases like normal commands; i.e.:
1715N/A "python cli/t_my_test_case.py" will just work.
1715N/A
1971N/A If 'covdir' is provided, coverage will be started and the
1971N/A related coverage object returned.
1715N/A """
1715N/A
1715N/A osname = platform.uname()[0].lower()
1715N/A proc = 'unknown'
1715N/A if osname == 'sunos':
1715N/A proc = platform.processor()
1715N/A elif osname == 'linux':
1715N/A proc = "linux_" + platform.machine()
1715N/A elif osname == 'windows':
1715N/A proc = osname
1715N/A elif osname == 'darwin':
1715N/A proc = osname
1715N/A elif osname == 'aix':
1715N/A proc = osname
1715N/A else:
1715N/A print "Unable to determine appropriate proto area location."
1715N/A print "This is a porting problem."
1715N/A sys.exit(1)
1715N/A
1715N/A # Figure out from where we're invoking the command
1715N/A cmddir, cmdname = os.path.split(sys.argv[0])
1715N/A cmddir = os.path.realpath(cmddir)
1715N/A
1715N/A if "ROOT" in os.environ:
1715N/A proto_area = os.environ["ROOT"]
1715N/A else:
1715N/A proto_area = "%s/%s/root_%s" % (cmddir, path_to_proto, proc)
1715N/A
1715N/A # Clean up relative ../../, etc. out of path to proto
1715N/A proto_area = os.path.realpath(proto_area)
1715N/A
1715N/A pkgs = "%s/usr/lib/python2.6/vendor-packages" % proto_area
1715N/A bins = "%s/usr/bin" % proto_area
1715N/A
1715N/A sys.path.insert(1, pkgs)
1715N/A
1715N/A #
1715N/A # Because subprocesses must also source from the proto area,
1715N/A # we need to set PYTHONPATH in the environment as well as
1715N/A # in sys.path.
1715N/A #
1715N/A if "PYTHONPATH" in os.environ:
1715N/A pypath = os.pathsep + os.environ["PYTHONPATH"]
1715N/A else:
1715N/A pypath = ""
1715N/A os.environ["PYTHONPATH"] = "." + os.pathsep + pkgs + pypath
1715N/A
1715N/A os.environ["PATH"] = bins + os.pathsep + os.environ["PATH"]
1715N/A
2692N/A # Proxy environment variables cause all kinds of problems, strip them
2692N/A # all out.
1715N/A # Use "keys"; otherwise we'll change dictionary size during iteration.
1715N/A for k in os.environ.keys():
2692N/A if k.startswith("PKG_") or k.lower().endswith("_proxy"):
1715N/A del os.environ[k]
1715N/A
1715N/A #
1971N/A # Start coverage before proceeding so that reports are accurate.
1971N/A #
1971N/A cov = None
1971N/A if covdir:
1971N/A # This must be imported here just after PYTHONPATH setup above.
1971N/A import coverage
1971N/A os.chmod(covdir, 01777)
1971N/A cov_file = "%s/pkg5" % covdir
1971N/A cov = coverage.coverage(data_file=cov_file, data_suffix=True)
1971N/A cov.start()
1971N/A
1971N/A #
1715N/A # Tell package manager where its application data files live.
1715N/A #
1715N/A os.environ["PACKAGE_MANAGER_ROOT"] = proto_area
1715N/A
1715N/A from pkg.client import global_settings
1715N/A global_settings.client_name = "pkg"
1715N/A
1715N/A import pkg5unittest
1715N/A pkg5unittest.g_proto_area = proto_area
1715N/A
1715N/A # Save off the value for tempdir when we were invoked, since the
1715N/A # suite will subsequently modify tempdir to sandbox test cases.
1715N/A pkg5unittest.g_tempdir = tempfile.gettempdir()
1971N/A
1971N/A return cov