1
0

test_lxc.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.states.lxc as lxc
  9. import salt.utils.versions
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase
  14. class LxcTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.states.lxc
  17. """
  18. def setup_loader_modules(self):
  19. return {lxc: {}}
  20. # 'present' function tests: 1
  21. def test_present(self):
  22. """
  23. Test to verify the named container if it exist.
  24. """
  25. name = "web01"
  26. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  27. mock = MagicMock(side_effect=[False, True, True, True, True, True, True])
  28. mock_t = MagicMock(
  29. side_effect=[
  30. None,
  31. True,
  32. "frozen",
  33. "frozen",
  34. "stopped",
  35. "running",
  36. "running",
  37. ]
  38. )
  39. with patch.dict(lxc.__salt__, {"lxc.exists": mock, "lxc.state": mock_t}):
  40. comt = "Clone source 'True' does not exist"
  41. ret.update({"comment": comt})
  42. self.assertDictEqual(lxc.present(name, clone_from=True), ret)
  43. with patch.dict(lxc.__opts__, {"test": True}):
  44. comt = "Container 'web01' will be cloned from True"
  45. ret.update({"comment": comt, "result": None})
  46. self.assertDictEqual(lxc.present(name, clone_from=True), ret)
  47. comt = "Container 'web01' already exists"
  48. ret.update({"comment": comt, "result": True})
  49. self.assertDictEqual(lxc.present(name, clone_from=True), ret)
  50. comt = "Container 'web01' would be unfrozen"
  51. ret.update({"comment": comt, "result": None})
  52. self.assertDictEqual(
  53. lxc.present(name, running=True, clone_from=True), ret
  54. )
  55. comt = "Container '{0}' would be stopped".format(name)
  56. ret.update({"comment": comt, "result": None})
  57. self.assertDictEqual(
  58. lxc.present(name, running=False, clone_from=True), ret
  59. )
  60. comt = "Container 'web01' already exists and is stopped"
  61. ret.update({"comment": comt, "result": True})
  62. self.assertDictEqual(
  63. lxc.present(name, running=False, clone_from=True), ret
  64. )
  65. with patch.dict(lxc.__opts__, {"test": False}):
  66. comt = "Container 'web01' already exists"
  67. ret.update({"comment": comt, "result": True})
  68. self.assertDictEqual(lxc.present(name, clone_from=True), ret)
  69. # 'absent' function tests: 1
  70. def test_absent(self):
  71. """
  72. Test to ensure a container is not present, destroying it if present.
  73. """
  74. name = "web01"
  75. ret = {"name": name, "result": True, "comment": "", "changes": {}}
  76. mock = MagicMock(side_effect=[False, True, True])
  77. mock_des = MagicMock(return_value={"state": True})
  78. with patch.dict(lxc.__salt__, {"lxc.exists": mock, "lxc.destroy": mock_des}):
  79. comt = "Container '{0}' does not exist".format(name)
  80. ret.update({"comment": comt})
  81. self.assertDictEqual(lxc.absent(name), ret)
  82. with patch.dict(lxc.__opts__, {"test": True}):
  83. comt = "Container '{0}' would be destroyed".format(name)
  84. ret.update({"comment": comt, "result": None})
  85. self.assertDictEqual(lxc.absent(name), ret)
  86. with patch.dict(lxc.__opts__, {"test": False}):
  87. comt = "Container '{0}' was destroyed".format(name)
  88. ret.update(
  89. {"comment": comt, "result": True, "changes": {"state": True}}
  90. )
  91. self.assertDictEqual(lxc.absent(name), ret)
  92. # 'running' function tests: 1
  93. def test_running(self):
  94. """
  95. Test to ensure that a container is running.
  96. """
  97. name = "web01"
  98. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  99. mock = MagicMock(return_value={"state": {"new": "stop"}})
  100. mock_t = MagicMock(side_effect=[None, "running", "stopped", "start"])
  101. with patch.dict(
  102. lxc.__salt__, {"lxc.exists": mock, "lxc.state": mock_t, "lxc.start": mock}
  103. ):
  104. comt = "Container '{0}' does not exist".format(name)
  105. ret.update({"comment": comt})
  106. self.assertDictEqual(lxc.running(name), ret)
  107. comt = "Container 'web01' is already running"
  108. ret.update({"comment": comt, "result": True})
  109. self.assertDictEqual(lxc.running(name), ret)
  110. with patch.dict(lxc.__opts__, {"test": True}):
  111. comt = "Container 'web01' would be started"
  112. ret.update({"comment": comt, "result": None})
  113. self.assertDictEqual(lxc.running(name), ret)
  114. with patch.dict(lxc.__opts__, {"test": False}):
  115. comt = "Unable to start container 'web01'"
  116. ret.update(
  117. {
  118. "comment": comt,
  119. "result": False,
  120. "changes": {"state": {"new": "stop", "old": "start"}},
  121. }
  122. )
  123. self.assertDictEqual(lxc.running(name), ret)
  124. # 'frozen' function tests: 1
  125. def test_frozen(self):
  126. """
  127. Test to ensure that a container is frozen.
  128. """
  129. name = "web01"
  130. ret = {"name": name, "result": True, "comment": "", "changes": {}}
  131. mock = MagicMock(return_value={"state": {"new": "stop"}})
  132. mock_t = MagicMock(side_effect=["frozen", "stopped", "stopped"])
  133. with patch.dict(lxc.__salt__, {"lxc.freeze": mock, "lxc.state": mock_t}):
  134. comt = "Container '{0}' is already frozen".format(name)
  135. ret.update({"comment": comt})
  136. self.assertDictEqual(lxc.frozen(name), ret)
  137. with patch.dict(lxc.__opts__, {"test": True}):
  138. comt = "Container 'web01' would be started and frozen"
  139. ret.update({"comment": comt, "result": None})
  140. self.assertDictEqual(lxc.frozen(name), ret)
  141. with patch.dict(lxc.__opts__, {"test": False}):
  142. comt = "Unable to start and freeze container 'web01'"
  143. ret.update(
  144. {
  145. "comment": comt,
  146. "result": False,
  147. "changes": {"state": {"new": "stop", "old": "stopped"}},
  148. }
  149. )
  150. self.assertDictEqual(lxc.frozen(name), ret)
  151. # 'stopped' function tests: 1
  152. def test_stopped(self):
  153. """
  154. Test to ensure that a container is stopped.
  155. """
  156. name = "web01"
  157. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  158. mock = MagicMock(return_value={"state": {"new": "stop"}})
  159. mock_t = MagicMock(side_effect=[None, "stopped", "frozen", "frozen"])
  160. with patch.dict(lxc.__salt__, {"lxc.stop": mock, "lxc.state": mock_t}):
  161. comt = "Container '{0}' does not exist".format(name)
  162. ret.update({"comment": comt})
  163. self.assertDictEqual(lxc.stopped(name), ret)
  164. comt = "Container '{0}' is already stopped".format(name)
  165. ret.update({"comment": comt, "result": True})
  166. self.assertDictEqual(lxc.stopped(name), ret)
  167. with patch.dict(lxc.__opts__, {"test": True}):
  168. comt = "Container 'web01' would be stopped"
  169. ret.update({"comment": comt, "result": None})
  170. self.assertDictEqual(lxc.stopped(name), ret)
  171. with patch.dict(lxc.__opts__, {"test": False}):
  172. comt = "Unable to stop container 'web01'"
  173. ret.update(
  174. {
  175. "comment": comt,
  176. "result": False,
  177. "changes": {"state": {"new": "stop", "old": "frozen"}},
  178. }
  179. )
  180. self.assertDictEqual(lxc.stopped(name), ret)
  181. # 'set_pass' function tests: 1
  182. def test_set_pass(self):
  183. """
  184. Test to execute set_pass func.
  185. """
  186. comment = (
  187. "The lxc.set_pass state is no longer supported. Please see "
  188. "the LXC states documentation for further information."
  189. )
  190. ret = {"name": "web01", "comment": comment, "result": False, "changes": {}}
  191. self.assertDictEqual(lxc.set_pass("web01"), ret)
  192. # 'edited_conf' function tests: 1
  193. def test_edited_conf(self):
  194. """
  195. Test to edit LXC configuration options
  196. """
  197. name = "web01"
  198. comment = "{0} lxc.conf will be edited".format(name)
  199. ret = {"name": name, "result": True, "comment": comment, "changes": {}}
  200. with patch.object(salt.utils.versions, "warn_until", MagicMock()):
  201. with patch.dict(lxc.__opts__, {"test": True}):
  202. self.assertDictEqual(lxc.edited_conf(name), ret)
  203. with patch.dict(lxc.__opts__, {"test": False}):
  204. mock = MagicMock(return_value={})
  205. with patch.dict(lxc.__salt__, {"lxc.update_lxc_conf": mock}):
  206. self.assertDictEqual(lxc.edited_conf(name), {"name": "web01"})