----- "Lucas Meneghel Rodrigues" <[email protected]> wrote:
> The KVM test was breaking when trying to import the subtest
> 'autotest', as a naming clash was happening (client/bin/autotest
> was being imported instead of the autotest subtest), so to
> make naming clashes less likely, import test.[type] instead
> of just [type]. Also, as the kvm test was already inserted
> to sys.path on the control file, don't add additional cruft
> to the library search path.
>
> Signed-off-by: Lucas Meneghel Rodrigues <[email protected]>
This may turn out to be just fine, but I'm posting an alternative
patch anyway, which uses the imp module to import by filename.
It might be a little safer because it doesn't care about sys.path.
> ---
> client/tests/kvm/kvm.py | 22 +++++++++-------------
> 1 files changed, 9 insertions(+), 13 deletions(-)
>
> diff --git a/client/tests/kvm/kvm.py b/client/tests/kvm/kvm.py
> index 06ef9f5..332fa86 100644
> --- a/client/tests/kvm/kvm.py
> +++ b/client/tests/kvm/kvm.py
> @@ -22,9 +22,7 @@ class kvm(test.test):
> """
> version = 1
> def initialize(self):
> - # Make it possible to import modules from the subtest dir
> self.subtest_dir = os.path.join(self.bindir, 'tests')
> - sys.path.append(self.subtest_dir)
>
>
> def run_once(self, params):
> @@ -44,23 +42,20 @@ class kvm(test.test):
> try:
> try:
> # Get the test routine corresponding to the specified
> test type
> - type = params.get("type")
> + t_type = params.get("type")
> # Verify if we have the correspondent source file for
> it
> - module_path = os.path.join(self.subtest_dir, '%s.py'
> % type)
> + module_path = os.path.join(self.subtest_dir, '%s.py'
> % t_type)
> if not os.path.isfile(module_path):
> - raise error.TestError("No %s.py test file found"
> % type)
> - # Load the tests directory (which was turned into a
> py module)
> - try:
> - test_module = __import__(type)
> - except ImportError, e:
> - raise error.TestError("Failed to import test %s:
> %s" %
> - (type, e))
> -
> + raise error.TestError("No %s.py test file found"
> % t_type)
> + # Load the test module
> + # (KVM test dir was appended to sys.path in the
> control file)
> + __import__("tests.%s" % t_type)
> + test_module = sys.modules["tests.%s" % t_type]
> # Preprocess
> kvm_preprocessing.preprocess(self, params, env)
> kvm_utils.dump_env(env, env_filename)
> # Run the test function
> - run_func = getattr(test_module, "run_%s" % type)
> + run_func = getattr(test_module, "run_%s" % t_type)
> run_func(self, params, env)
> kvm_utils.dump_env(env, env_filename)
>
> @@ -76,3 +71,4 @@ class kvm(test.test):
> kvm_preprocessing.postprocess(self, params, env)
> logging.debug("Contents of environment: %s", str(env))
> kvm_utils.dump_env(env, env_filename)
> +
> --
> 1.6.5.2
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html