From bfc30ec0c8afc0a2d8299d1d55d343fd32d51894 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Fri, 13 Feb 2009 15:35:05 +0000
Subject: [PATCH] Some small improvements to the fake hypervisor
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch modifies the fake hypervisor to substract the memory β€œused”
by β€œrunning” instances from the free memory, so the actual node
information changes based on the running instances.

Also some style changes and fixes are added.

Reviewed-by: ultrotter
---
 lib/hypervisor/hv_fake.py | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/lib/hypervisor/hv_fake.py b/lib/hypervisor/hv_fake.py
index 1211cbd95..7bd9f7732 100644
--- a/lib/hypervisor/hv_fake.py
+++ b/lib/hypervisor/hv_fake.py
@@ -65,11 +65,11 @@ class FakeHypervisor(hv_base.BaseHypervisor):
     if not os.path.exists(file_name):
       return None
     try:
-      fh = file(file_name, "r")
+      fh = open(file_name, "r")
       try:
         inst_id = fh.readline().strip()
-        memory = fh.readline().strip()
-        vcpus = fh.readline().strip()
+        memory = utils.TryConvert(int, fh.readline().strip())
+        vcpus = utils.TryConvert(fh.readline().strip())
         stat = "---b-"
         times = "0"
         return (instance_name, inst_id, memory, vcpus, stat, times)
@@ -88,15 +88,16 @@ class FakeHypervisor(hv_base.BaseHypervisor):
     data = []
     for file_name in os.listdir(self._ROOT_DIR):
       try:
-        fh = file(self._ROOT_DIR+"/"+file_name, "r")
+        fh = open(self._ROOT_DIR+"/"+file_name, "r")
         inst_id = "-1"
-        memory = "0"
+        memory = 0
+        vcpus = 1
         stat = "-----"
         times = "-1"
         try:
           inst_id = fh.readline().strip()
-          memory = fh.readline().strip()
-          vcpus = fh.readline().strip()
+          memory = utils.TryConvert(int, fh.readline().strip())
+          vcpus = utils.TryConvert(int, fh.readline().strip())
           stat = "---b-"
           times = "0"
         finally:
@@ -189,6 +190,11 @@ class FakeHypervisor(hv_base.BaseHypervisor):
           result['memory_dom0'] = int(val.split()[0])/1024
     result['memory_free'] = sum_free
 
+    # substract running instances
+    all_instances = self.GetAllInstancesInfo()
+    result['memory_free'] -= min(result['memory_free'],
+                                 sum([row[2] for row in all_instances]))
+
     cpu_total = 0
     try:
       fh = open("/proc/cpuinfo")
-- 
GitLab