test_lxc.py 9.7 KB

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