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