KVMHypervisor is 2200 lines long and mostly untested. This patch starts
an effort to add tests for this class.

Signed-off-by: Aaron Karper <[email protected]>
---
 test/py/ganeti.hypervisor.hv_kvm_unittest.py | 61 ++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/test/py/ganeti.hypervisor.hv_kvm_unittest.py 
b/test/py/ganeti.hypervisor.hv_kvm_unittest.py
index c831461..3596d07 100755
--- a/test/py/ganeti.hypervisor.hv_kvm_unittest.py
+++ b/test/py/ganeti.hypervisor.hv_kvm_unittest.py
@@ -50,8 +50,11 @@ from ganeti.hypervisor import hv_kvm
 import ganeti.hypervisor.hv_kvm.netdev as netdev
 import ganeti.hypervisor.hv_kvm.monitor as monitor
 
+import mock
 import testutils
 
+from testutils.config_mock import ConfigMock
+
 
 class QmpStub(threading.Thread):
   """Stub for a QMP endpoint for a KVM instance
@@ -501,5 +504,63 @@ class TestGetRuntimeInfo(unittest.TestCase):
     self.assertTrue(devinfo.pci==5)
 
 
+class TestKvmRuntime(testutils.GanetiTestCase):
+  """The _ExecuteKvmRuntime is at the core of all KVM operations."""
+
+  def setUp(self):
+    super(TestKvmRuntime, self).setUp()
+    kvm_class = 'ganeti.hypervisor.hv_kvm.KVMHypervisor'
+    self.MockOut('qmp', mock.patch('ganeti.hypervisor.hv_kvm.QmpConnection'))
+    self.MockOut('run_cmd', mock.patch('ganeti.utils.RunCmd'))
+    self.MockOut('ensure_dirs', mock.patch('ganeti.utils.EnsureDirs'))
+    self.MockOut('write_file', mock.patch('ganeti.utils.WriteFile'))
+    self.MockOut(mock.patch(kvm_class + '.ValidateParameters'))
+    self.MockOut(mock.patch('ganeti.hypervisor.hv_kvm.OpenTap',
+                            return_value=('test_nic', [])))
+    self.MockOut(mock.patch(kvm_class + '._ConfigureNIC'))
+    self.MockOut('pid_alive', mock.patch(kvm_class + '._InstancePidAlive',
+                                         return_value=('file', -1, False)))
+    self.MockOut(mock.patch(kvm_class + '._ExecuteCpuAffinity'))
+    self.MockOut(mock.patch(kvm_class + '._CallMonitorCommand'))
+
+    self.cfg = ConfigMock()
+    params = constants.HVC_DEFAULTS[constants.HT_KVM].copy()
+    beparams = constants.BEC_DEFAULTS.copy()
+    self.instance = self.cfg.AddNewInstance(name='name.example.com',
+                                            hypervisor='kvm',
+                                            hvparams=params,
+                                            beparams=beparams)
+
+  def testDirectoriesCreated(self):
+    hypervisor = hv_kvm.KVMHypervisor()
+    self.mocks['ensure_dirs'].assert_called_with([
+        ('/var/run/ganeti/kvm-hypervisor', 0775),
+        ('/var/run/ganeti/kvm-hypervisor/pid', 0775),
+        ('/var/run/ganeti/kvm-hypervisor/uid', 0775),
+        ('/var/run/ganeti/kvm-hypervisor/ctrl', 0775),
+        ('/var/run/ganeti/kvm-hypervisor/conf', 0775),
+        ('/var/run/ganeti/kvm-hypervisor/nic', 0775),
+        ('/var/run/ganeti/kvm-hypervisor/chroot', 0775),
+        ('/var/run/ganeti/kvm-hypervisor/chroot-quarantine', 0775),
+        ('/var/run/ganeti/kvm-hypervisor/keymap', 0775)])
+
+  def testStartInstance(self):
+    hypervisor = hv_kvm.KVMHypervisor()
+    def RunCmd(cmd, **kwargs):
+      if '--help' in cmd:
+        return mock.Mock(
+            failed=False, output=testutils.ReadTestData("kvm_1.1.2_help.txt"))
+      if '-S' in cmd:
+        self.mocks['pid_alive'].return_value = ('file', -1, True)
+        return mock.Mock(failed=False)
+      elif '-M' in cmd:
+        return mock.Mock(failed=False, output='')
+      elif '-device' in cmd:
+        return mock.Mock(failed=False, output='name "virtio-blk-pci"')
+      else:
+        raise errors.ProgrammerError('Unexpected command: %s' % cmd)
+    self.mocks['run_cmd'].side_effect = RunCmd
+    hypervisor.StartInstance(self.instance, [], False)
+
 if __name__ == "__main__":
   testutils.GanetiTestProgram()
-- 
2.1.0.rc2.206.gedb03e5

Reply via email to