This patch adds a few unit tests for the GetInstanceInfo method of
hv_lxc.

Signed-off-by: Yuto KAWAMURA(kawamuray) <[email protected]>
---
 test/py/ganeti.hypervisor.hv_lxc_unittest.py | 35 ++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/test/py/ganeti.hypervisor.hv_lxc_unittest.py 
b/test/py/ganeti.hypervisor.hv_lxc_unittest.py
index d005fe1..e41354d 100755
--- a/test/py/ganeti.hypervisor.hv_lxc_unittest.py
+++ b/test/py/ganeti.hypervisor.hv_lxc_unittest.py
@@ -28,13 +28,30 @@ from ganeti import objects
 from ganeti import hypervisor
 from ganeti import utils
 
+from ganeti.hypervisor import hv_base
 from ganeti.hypervisor import hv_lxc
 from ganeti.hypervisor.hv_lxc import LXCHypervisor
 
+import mock
+import shutil
+import tempfile
 import testutils
 from testutils import patch_object
 
 
+def setUpModule():
+  # Creating instance of LXCHypervisor will fail by permission issue of
+  # instance directories
+  global temp_dir
+  temp_dir = tempfile.mkdtemp()
+  LXCHypervisor._ROOT_DIR = utils.PathJoin(temp_dir, "root")
+  LXCHypervisor._LOG_DIR = utils.PathJoin(temp_dir, "log")
+
+
+def tearDownModule():
+  shutil.rmtree(temp_dir)
+
+
 def RunResultOk(stdout):
   return utils.RunResult(0, None, stdout, "", [], None, None)
 
@@ -66,5 +83,23 @@ class TestLXCIsInstanceAlive(unittest.TestCase):
     self.assertFalse(LXCHypervisor._IsInstanceAlive("inst2"))
 
 
+class TestLXCHypervisorGetInstanceInfo(unittest.TestCase):
+  def setUp(self):
+    self.hv = LXCHypervisor()
+    self.hv._GetCgroupCpuList = mock.Mock(return_value=[1, 3])
+    self.hv._GetCgroupMemoryLimit = mock.Mock(return_value=128*(1024**2))
+
+  @patch_object(LXCHypervisor, "_IsInstanceAlive")
+  def testRunningInstance(self, isalive_mock):
+    isalive_mock.return_value = True
+    self.assertEqual(self.hv.GetInstanceInfo("inst1"),
+                     ("inst1", 0, 128, 2, hv_base.HvInstanceState.RUNNING, 0))
+
+  @patch_object(LXCHypervisor, "_IsInstanceAlive")
+  def testInactiveOrNonexistentInstance(self, isalive_mock):
+    isalive_mock.return_value = False
+    self.assertIsNone(self.hv.GetInstanceInfo("inst1"))
+
+
 if __name__ == "__main__":
   testutils.GanetiTestProgram()
-- 
1.8.5.5

Reply via email to