Skip to contentSkip to conversation

KVM-test: Fix a bug that about list slice in scan_results.py

PATCH New Yolkfull Chow 1 participant 2 messages Dec 30, 2009 KVM

lore
Share & tools
lore
#1 lore raw

PATCH 2/2 KVM-test: Move two 'remote_login' out of try block in kvm_vm.py

In reply to Yolkfull Chow

If vm.remote_login failed 'session.close()' can result in exception in finally clause. This patch fix the problem.

---
 client/tests/kvm/kvm_vm.py |   51 +++++++++++++++++++++----------------------
 1 files changed, 25 insertions(+), 26 deletions(-)
client/tests/kvm/kvm_vm.py +25 -26
index 7229b79..f746331 100755
@@ -827,40 +827,39 @@ class VM:
         """
         Get the cpu count of the VM.
         """
+        session = self.remote_login()
+        cmd = self.params.get("cpu_chk_cmd")
         try:
-            session = self.remote_login()
-            if session:
-                cmd = self.params.get("cpu_chk_cmd")
-                s, count = session.get_command_status_output(cmd)
-                if s == 0:
-                    return int(count)
+            s, count = session.get_command_status_output(cmd)
+            if s == 0:
+                return int(count)
             return None
         finally:
-            session.close()
+            if session:
+                session.close()
 
 
     def get_memory_size(self):
         """
         Get memory size of the VM.
         """
+        session = self.remote_login()
+        cmd = self.params.get("mem_chk_cmd")
         try:
-            session = self.remote_login()
-            if session:
-                cmd = self.params.get("mem_chk_cmd")
-                s, mem_str = session.get_command_status_output(cmd)
-                if s != 0:
-                    return None
-                mem = re.findall("([0-9][0-9][0-9]+)", mem_str)
-                mem_size = 0
-                for m in mem:
-                    mem_size += int(m)
-                if "GB" in mem_str:
-                    mem_size *= 1024
-                elif "MB" in mem_str:
-                    pass
-                else:
-                    mem_size /= 1024
-                return int(mem_size)
-            return None
+            s, mem_str = session.get_command_status_output(cmd)
+            if s != 0:
+                return None
+            mem = re.findall("([0-9][0-9][0-9]+)", mem_str)
+            mem_size = 0
+            for m in mem:
+                mem_size += int(m)
+            if "GB" in mem_str:
+                mem_size *= 1024
+            elif "MB" in mem_str:
+                pass
+            else:
+                mem_size /= 1024
+            return int(mem_size)
         finally:
-            session.close()
+            if session:
+                session.close()
Signature · 1 line
1.6.6
#0 lore raw

PATCH 1/2 KVM-test: Fix a bug that about list slice in scan_results.py

If 'info_list' is empty slice operation can result in traceback:

...

      info_list[-1] = parts[5]

IndexError: list assignment index out of range

---
 client/tests/kvm/scan_results.py |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
client/tests/kvm/scan_results.py +1 -0
index f7bafa9..1daff2f 100755
@@ -45,6 +45,7 @@ def parse_results(text):
         # Found a FAIL/ERROR/GOOD line -- get failure/success info
         elif (len(parts) >= 6 and parts[3].startswith("timestamp") and
               parts[4].startswith("localtime")):
+            info_list.append("")
             info_list[-1] = parts[5]
 
     return result_list
Signature · 1 line
1.6.6