1
0

test_vmctl.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import
  4. # Import Salt Libs
  5. import salt.modules.vmctl as vmctl
  6. # Import Salt Testing Libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. from tests.support.unit import TestCase
  10. class VmctlTestCase(TestCase, LoaderModuleMockMixin):
  11. """
  12. test modules.vmctl functions
  13. """
  14. def setup_loader_modules(self):
  15. return {vmctl: {}}
  16. def test_create_disk(self):
  17. """
  18. Tests creating a new disk image.
  19. """
  20. ret = {}
  21. ret["stdout"] = "vmctl: imagefile created"
  22. ret["stderr"] = ""
  23. ret["retcode"] = 0
  24. mock_cmd = MagicMock(return_value=ret)
  25. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  26. self.assertTrue(vmctl.create_disk("/path/to/disk.img", "1G"))
  27. def test_load(self):
  28. """
  29. Tests loading a configuration file.
  30. """
  31. ret = {}
  32. ret["retcode"] = 0
  33. mock_cmd = MagicMock(return_value=ret)
  34. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  35. self.assertTrue(vmctl.load("/etc/vm.switches.conf"))
  36. def test_reload(self):
  37. """
  38. Tests reloading the configuration.
  39. """
  40. ret = {}
  41. ret["retcode"] = 0
  42. mock_cmd = MagicMock(return_value=ret)
  43. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  44. self.assertTrue(vmctl.reload())
  45. def test_reset(self):
  46. """
  47. Tests resetting VMM.
  48. """
  49. ret = {}
  50. ret["retcode"] = 0
  51. mock_cmd = MagicMock(return_value=ret)
  52. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  53. res = vmctl.reset()
  54. mock_cmd.assert_called_once_with(
  55. ["vmctl", "reset"], output_loglevel="trace", python_shell=False
  56. )
  57. self.assertTrue(res)
  58. def test_reset_vms(self):
  59. """
  60. Tests resetting VMs.
  61. """
  62. ret = {}
  63. ret["retcode"] = 0
  64. mock_cmd = MagicMock(return_value=ret)
  65. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  66. res = vmctl.reset(vms=True)
  67. mock_cmd.assert_called_once_with(
  68. ["vmctl", "reset", "vms"], output_loglevel="trace", python_shell=False
  69. )
  70. self.assertTrue(res)
  71. def test_reset_switches(self):
  72. """
  73. Tests resetting switches.
  74. """
  75. ret = {}
  76. ret["retcode"] = 0
  77. mock_cmd = MagicMock(return_value=ret)
  78. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  79. res = vmctl.reset(switches=True)
  80. mock_cmd.assert_called_once_with(
  81. ["vmctl", "reset", "switches"],
  82. output_loglevel="trace",
  83. python_shell=False,
  84. )
  85. self.assertTrue(res)
  86. def test_reset_all(self):
  87. """
  88. Tests resetting all.
  89. """
  90. ret = {}
  91. ret["retcode"] = 0
  92. mock_cmd = MagicMock(return_value=ret)
  93. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  94. res = vmctl.reset(all=True)
  95. mock_cmd.assert_called_once_with(
  96. ["vmctl", "reset", "all"], output_loglevel="trace", python_shell=False
  97. )
  98. self.assertTrue(res)
  99. def test_start_existing_vm(self):
  100. """
  101. Tests starting a VM that is already defined.
  102. """
  103. ret = {}
  104. ret["stderr"] = "vmctl: started vm 4 successfully, tty /dev/ttyp4"
  105. ret["retcode"] = 0
  106. mock_cmd = MagicMock(return_value=ret)
  107. expected = {"changes": True, "console": "/dev/ttyp4"}
  108. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  109. self.assertDictEqual(expected, vmctl.start("4"))
  110. def test_start_new_vm(self):
  111. """
  112. Tests starting a new VM.
  113. """
  114. ret = {}
  115. ret["stderr"] = "vmctl: started vm 4 successfully, tty /dev/ttyp4"
  116. ret["retcode"] = 0
  117. mock_cmd = MagicMock(return_value=ret)
  118. mock_status = MagicMock(return_value={})
  119. expected = {"changes": True, "console": "/dev/ttyp4"}
  120. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  121. with patch("salt.modules.vmctl.status", mock_status):
  122. res = vmctl.start("web1", bootpath="/bsd.rd", nics=2, disk="/disk.img")
  123. mock_cmd.assert_called_once_with(
  124. [
  125. "vmctl",
  126. "start",
  127. "web1",
  128. "-i 2",
  129. "-b",
  130. "/bsd.rd",
  131. "-d",
  132. "/disk.img",
  133. ],
  134. output_loglevel="trace",
  135. python_shell=False,
  136. )
  137. self.assertDictEqual(expected, res)
  138. def test_status(self):
  139. """
  140. Tests getting status for all VMs.
  141. """
  142. ret = {}
  143. ret["stdout"] = (
  144. " ID PID VCPUS MAXMEM CURMEM TTY OWNER NAME\n"
  145. " 1 123 1 2.9G 150M ttyp5 john web1 - stopping\n"
  146. " 2 456 1 512M 301M ttyp4 paul web2\n"
  147. " 3 - 1 512M - - george web3\n"
  148. )
  149. ret["retcode"] = 0
  150. mock_cmd = MagicMock(return_value=ret)
  151. expected = {
  152. "web1": {
  153. "curmem": "150M",
  154. "id": "1",
  155. "maxmem": "2.9G",
  156. "owner": "john",
  157. "pid": "123",
  158. "state": "stopping",
  159. "tty": "ttyp5",
  160. "vcpus": "1",
  161. },
  162. "web2": {
  163. "curmem": "301M",
  164. "id": "2",
  165. "maxmem": "512M",
  166. "owner": "paul",
  167. "pid": "456",
  168. "state": "running",
  169. "tty": "ttyp4",
  170. "vcpus": "1",
  171. },
  172. "web3": {
  173. "curmem": "-",
  174. "id": "3",
  175. "maxmem": "512M",
  176. "owner": "george",
  177. "pid": "-",
  178. "state": "stopped",
  179. "tty": "-",
  180. "vcpus": "1",
  181. },
  182. }
  183. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  184. self.assertEqual(expected, vmctl.status())
  185. def test_status_single(self):
  186. """
  187. Tests getting status for a single VM.
  188. """
  189. ret = {}
  190. ret["stdout"] = (
  191. " ID PID VCPUS MAXMEM CURMEM TTY OWNER NAME\n"
  192. " 1 123 1 2.9G 150M ttyp5 ringo web4\n"
  193. " 2 - 1 512M - - george web3\n"
  194. )
  195. ret["retcode"] = 0
  196. mock_cmd = MagicMock(return_value=ret)
  197. expected = {
  198. "web4": {
  199. "curmem": "150M",
  200. "id": "1",
  201. "maxmem": "2.9G",
  202. "owner": "ringo",
  203. "pid": "123",
  204. "state": "running",
  205. "tty": "ttyp5",
  206. "vcpus": "1",
  207. },
  208. }
  209. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  210. self.assertEqual(expected, vmctl.status("web4"))
  211. def test_stop_when_running(self):
  212. """
  213. Tests stopping a VM that is running.
  214. """
  215. ret = {}
  216. ret["stdout"] = ""
  217. ret["stderr"] = "vmctl: sent request to terminate vm 14"
  218. ret["retcode"] = 0
  219. mock_cmd = MagicMock(return_value=ret)
  220. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  221. res = vmctl.stop("web1")
  222. mock_cmd.assert_called_once_with(
  223. ["vmctl", "stop", "web1"], output_loglevel="trace", python_shell=False
  224. )
  225. self.assertTrue(res["changes"])
  226. def test_stop_when_stopped(self):
  227. """
  228. Tests stopping a VM that is already stopped/stopping.
  229. """
  230. ret = {}
  231. ret["stdout"] = ""
  232. ret["stderr"] = "vmctl: terminate vm command failed: Invalid argument"
  233. ret["retcode"] = 0
  234. mock_cmd = MagicMock(return_value=ret)
  235. with patch.dict(vmctl.__salt__, {"cmd.run_all": mock_cmd}):
  236. res = vmctl.stop("web1")
  237. mock_cmd.assert_called_once_with(
  238. ["vmctl", "stop", "web1"], output_loglevel="trace", python_shell=False
  239. )
  240. self.assertFalse(res["changes"])