pkg5testenv.py revision 3158
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
3158N/A# Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
1715N/A
3143N/Afrom __future__ import print_function
1715N/Aimport os
1715N/Aimport sys
1715N/Aimport platform
1715N/Aimport tempfile
1715N/A
3049N/Adef setup_environment(path_to_proto, debug=False, system_test=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.
3049N/A
3049N/A If 'system_test' is True, tests will run on live system.
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:
3143N/A print("Unable to determine appropriate proto area location.")
3143N/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:
3049N/A pkg_path = os.environ["ROOT"]
1715N/A else:
3049N/A if system_test:
3049N/A pkg_path = "/"
3049N/A else:
3158N/A pkg_path = "{0}/{1}/root_{2}".format(
3158N/A cmddir, path_to_proto, proc)
3049N/A
3158N/A proto_area = "{0}/{1}/root_{2}".format(cmddir, path_to_proto, proc)
1715N/A
1715N/A # Clean up relative ../../, etc. out of path to proto
3049N/A pkg_path = os.path.realpath(pkg_path)
1715N/A proto_area = os.path.realpath(proto_area)
1715N/A
3049N/A pkgs = os.path.join(pkg_path, "usr/lib/python2.6/vendor-packages")
3049N/A bins = os.path.join(pkg_path, "usr/bin")
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 #
1715N/A # Tell package manager where its application data files live.
1715N/A #
3049N/A os.environ["PACKAGE_MANAGER_ROOT"] = pkg_path
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
2758N/A pkg5unittest.g_test_dir = cmddir
3049N/A pkg5unittest.g_pkg_path = pkg_path
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()