KVM test: Fix subtest imports
PATCH 1/1 KVM test: Fix subtest imports
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.
--- client/tests/kvm/kvm.py | 22 +++++++++------------- 1 files changed, 9 insertions(+), 13 deletions(-)
client/tests/kvm/kvm.py +9 -13
@@ -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) +
Signature · 1 line
1.6.5.2
Quoted 9 lines · ----- "Lucas Meneghel Rodrigues
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.
Quoted 66 lines
--- client/tests/kvm/kvm.py | 22 +++++++++------------- 1 files changed, 9 insertions(+), 13 deletions(-)client/tests/kvm/kvm.py +0
-2@@ -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 specifiedtest type
client/tests/kvm/kvm.py +1
-1- type = params.get("type") + t_type = params.get("type") # Verify if we have the correspondent source file forit
-module_path= os.path.join(self.subtest_dir, '%s.py'
% type)
+module_path= os.path.join(self.subtest_dir, '%s.py'
%t_type)
if notos.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)client/tests/kvm/kvm.py +0
-4- try: - test_module = __import__(type) - except ImportError, e: - raise error.TestError("Failed to import test %s:%s" %
client/tests/kvm/kvm.py +1
-2- (type, e)) - + raise error.TestError("No %s.py test file found"%
t_type)client/tests/kvm/kvm.py +2
-0+ # Load the test module + # (KVM test dir was appended to sys.path in thecontrol file)
client/tests/kvm/kvm.py +4
-1+ __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) +Signature · 1 line
1.6.5.2