test_at.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.at as at
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class AtTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.at
  16. """
  17. def setup_loader_modules(self):
  18. return {at: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to add a job to queue.
  23. """
  24. # input variables
  25. name = "jboss"
  26. timespec = "9:09 11/04/15"
  27. tag = "love"
  28. user = "jam"
  29. # mock for at.at module call
  30. mock_atat = {
  31. "jobs": [
  32. {
  33. "date": "2015-11-04",
  34. "job": "1476031633.a",
  35. "queue": "a",
  36. "tag": tag,
  37. "time": "09:09:00",
  38. "user": user,
  39. },
  40. ],
  41. }
  42. # normale return
  43. ret = {
  44. "name": name,
  45. "result": True,
  46. "changes": {
  47. "date": "2015-11-04",
  48. "job": "1476031633.a",
  49. "queue": "a",
  50. "tag": "love",
  51. "time": "09:09:00",
  52. "user": "jam",
  53. },
  54. "comment": "job {name} added and will run on {timespec}".format(
  55. name=name, timespec=timespec
  56. ),
  57. }
  58. # unknown user return
  59. ret_user = {}
  60. ret_user.update(ret)
  61. ret_user.update(
  62. {
  63. "result": False,
  64. "changes": {},
  65. "comment": "user {0} does not exists".format(user),
  66. }
  67. )
  68. # add a job
  69. mock = MagicMock(return_value=mock_atat)
  70. with patch.dict(at.__opts__, {"test": False}):
  71. with patch.dict(at.__salt__, {"at.at": mock}):
  72. self.assertDictEqual(at.present(name, timespec, tag), ret)
  73. # add a job with a non-existing user
  74. mock = MagicMock(return_value=False)
  75. with patch.dict(at.__opts__, {"test": False}):
  76. with patch.dict(at.__salt__, {"user.info": mock}):
  77. self.assertDictEqual(at.present(name, timespec, tag, user), ret_user)
  78. # add a job with test=True
  79. with patch.dict(at.__opts__, {"test": True}):
  80. ret_test = {}
  81. ret_test.update(ret)
  82. ret_test.update({"result": None, "changes": {}})
  83. self.assertDictEqual(at.present(name, timespec, tag, user), ret_test)
  84. # 'absent' function tests: 1
  85. def test_absent(self):
  86. """
  87. Test to remove a job from queue
  88. """
  89. # input variables
  90. name = "jboss"
  91. tag = "rose"
  92. user = "jam"
  93. # mock for at.atrm module call
  94. mock_atatrm = {
  95. "jobs": {"removed": ["1476033859.a", "1476033855.a"], "tag": None},
  96. }
  97. # mock for at.jobcheck module call
  98. mock_atjobcheck = {
  99. "jobs": [
  100. {
  101. "date": "2015-11-04",
  102. "job": "1476031633.a",
  103. "queue": "a",
  104. "tag": tag,
  105. "time": "09:09:00",
  106. "user": user,
  107. },
  108. ],
  109. }
  110. # normal return
  111. ret = {
  112. "name": name,
  113. "result": True,
  114. "changes": {"removed": ["1476033859.a", "1476033855.a"]},
  115. "comment": "removed 2 job(s)",
  116. }
  117. # remove a job with test=True
  118. with patch.dict(at.__opts__, {"test": True}):
  119. ret_test = {}
  120. ret_test.update(ret)
  121. ret_test.update(
  122. {"result": None, "changes": {}, "comment": "removed ? job(s)"}
  123. )
  124. self.assertDictEqual(at.absent(name), ret_test)
  125. # remove a job and pass limit parameter
  126. with patch.dict(at.__opts__, {"test": False}):
  127. ret_limit = {}
  128. ret_limit.update(ret)
  129. ret_limit.update(
  130. {
  131. "result": False,
  132. "changes": {},
  133. "comment": "limit parameter not supported {0}".format(name),
  134. }
  135. )
  136. self.assertDictEqual(at.absent(name, limit="all"), ret_limit)
  137. # remove all jobs (2 jobs found)
  138. mock = MagicMock(return_value=mock_atatrm)
  139. with patch.dict(at.__salt__, {"at.atrm": mock}):
  140. with patch.dict(at.__opts__, {"test": False}):
  141. self.assertDictEqual(at.absent(name), ret)
  142. # remove all jobs (0 jobs found)
  143. mock_atatrm_nojobs = {}
  144. mock_atatrm_nojobs.update(mock_atatrm)
  145. mock_atatrm_nojobs.update({"jobs": {"removed": []}})
  146. mock = MagicMock(return_value=mock_atatrm_nojobs)
  147. with patch.dict(at.__salt__, {"at.atrm": mock}):
  148. with patch.dict(at.__opts__, {"test": False}):
  149. ret_nojobs = {}
  150. ret_nojobs.update(ret)
  151. ret_nojobs.update(
  152. {"changes": {}, "comment": ret["comment"].replace("2", "0")}
  153. )
  154. self.assertDictEqual(at.absent(name), ret_nojobs)
  155. # remove all tagged jobs (1 jobs found)
  156. mock_atatrm_tag = {}
  157. mock_atatrm_tag.update(mock_atatrm)
  158. mock_atatrm_tag.update({"jobs": {"removed": ["1476031633.a"], "tag": "rose"}})
  159. mock = MagicMock(return_value=mock_atatrm_tag)
  160. with patch.dict(at.__salt__, {"at.atrm": mock}):
  161. mock = MagicMock(return_value=mock_atjobcheck)
  162. with patch.dict(at.__salt__, {"at.jobcheck": mock}):
  163. with patch.dict(at.__opts__, {"test": False}):
  164. ret_tag = {}
  165. ret_tag.update(ret)
  166. ret_tag.update(
  167. {
  168. "changes": {"removed": ["1476031633.a"]},
  169. "comment": ret["comment"].replace("2", "1"),
  170. }
  171. )
  172. self.assertDictEqual(at.absent(name, tag=tag), ret_tag)