1
0

test_service.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import salt.config
  7. import salt.loader
  8. import salt.states.service as service
  9. import salt.utils.platform
  10. from tests.support.helpers import destructiveTest, slowTest
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase, skipIf
  14. def func(name):
  15. """
  16. Mock func method
  17. """
  18. return name
  19. class ServiceTestCase(TestCase, LoaderModuleMockMixin):
  20. """
  21. Validate the service state
  22. """
  23. def setup_loader_modules(self):
  24. return {service: {}}
  25. def test_running(self):
  26. """
  27. Test to verify that the service is running
  28. """
  29. ret = [
  30. {"comment": "", "changes": {}, "name": "salt", "result": True},
  31. {
  32. "changes": {},
  33. "comment": "The service salt is already running",
  34. "name": "salt",
  35. "result": True,
  36. },
  37. {
  38. "changes": "saltstack",
  39. "comment": "The service salt is already running",
  40. "name": "salt",
  41. "result": True,
  42. },
  43. {
  44. "changes": {},
  45. "comment": "Service salt is set to start",
  46. "name": "salt",
  47. "result": None,
  48. },
  49. {
  50. "changes": "saltstack",
  51. "comment": "Started Service salt",
  52. "name": "salt",
  53. "result": True,
  54. },
  55. {
  56. "changes": {},
  57. "comment": "The service salt is already running",
  58. "name": "salt",
  59. "result": True,
  60. },
  61. {
  62. "changes": "saltstack",
  63. "comment": "Service salt failed to start",
  64. "name": "salt",
  65. "result": False,
  66. },
  67. {
  68. "changes": "saltstack",
  69. "comment": "Started Service salt\nService masking not available on this minion",
  70. "name": "salt",
  71. "result": True,
  72. "warnings": [
  73. "The 'unmask' argument is not supported by this platform/action"
  74. ],
  75. },
  76. ]
  77. tmock = MagicMock(return_value=True)
  78. fmock = MagicMock(return_value=False)
  79. vmock = MagicMock(return_value="salt")
  80. with patch.object(service, "_enabled_used_error", vmock):
  81. self.assertEqual(service.running("salt", enabled=1), "salt")
  82. with patch.object(service, "_available", fmock):
  83. self.assertDictEqual(service.running("salt"), ret[0])
  84. with patch.object(service, "_available", tmock):
  85. with patch.dict(service.__opts__, {"test": False}):
  86. with patch.dict(
  87. service.__salt__,
  88. {"service.enabled": tmock, "service.status": tmock},
  89. ):
  90. self.assertDictEqual(service.running("salt"), ret[1])
  91. mock = MagicMock(return_value={"changes": "saltstack"})
  92. with patch.dict(
  93. service.__salt__,
  94. {
  95. "service.enabled": MagicMock(side_effect=[False, True]),
  96. "service.status": tmock,
  97. },
  98. ):
  99. with patch.object(service, "_enable", mock):
  100. self.assertDictEqual(service.running("salt", True), ret[2])
  101. with patch.dict(
  102. service.__salt__,
  103. {
  104. "service.enabled": MagicMock(side_effect=[True, False]),
  105. "service.status": tmock,
  106. },
  107. ):
  108. with patch.object(service, "_disable", mock):
  109. self.assertDictEqual(service.running("salt", False), ret[2])
  110. with patch.dict(
  111. service.__salt__,
  112. {
  113. "service.status": MagicMock(side_effect=[False, True]),
  114. "service.enabled": MagicMock(side_effect=[False, True]),
  115. "service.start": MagicMock(return_value="stack"),
  116. },
  117. ):
  118. with patch.object(
  119. service,
  120. "_enable",
  121. MagicMock(return_value={"changes": "saltstack"}),
  122. ):
  123. self.assertDictEqual(service.running("salt", True), ret[4])
  124. with patch.dict(
  125. service.__salt__,
  126. {
  127. "service.status": MagicMock(side_effect=[False, True]),
  128. "service.enabled": MagicMock(side_effect=[False, True]),
  129. "service.unmask": MagicMock(side_effect=[False, True]),
  130. "service.start": MagicMock(return_value="stack"),
  131. },
  132. ):
  133. with patch.object(
  134. service,
  135. "_enable",
  136. MagicMock(return_value={"changes": "saltstack"}),
  137. ):
  138. self.assertDictEqual(
  139. service.running("salt", True, unmask=True), ret[7]
  140. )
  141. with patch.dict(service.__opts__, {"test": True}):
  142. with patch.dict(service.__salt__, {"service.status": tmock}):
  143. self.assertDictEqual(service.running("salt"), ret[5])
  144. with patch.dict(service.__salt__, {"service.status": fmock}):
  145. self.assertDictEqual(service.running("salt"), ret[3])
  146. with patch.dict(service.__opts__, {"test": False}):
  147. with patch.dict(
  148. service.__salt__,
  149. {
  150. "service.status": MagicMock(side_effect=[False, False]),
  151. "service.enabled": MagicMock(side_effecct=[True, True]),
  152. "service.start": MagicMock(return_value="stack"),
  153. },
  154. ):
  155. with patch.object(
  156. service,
  157. "_enable",
  158. MagicMock(return_value={"changes": "saltstack"}),
  159. ):
  160. self.assertDictEqual(service.running("salt", True), ret[6])
  161. def test_dead(self):
  162. """
  163. Test to ensure that the named service is dead
  164. """
  165. ret = [
  166. {"changes": {}, "comment": "", "name": "salt", "result": True},
  167. {
  168. "changes": "saltstack",
  169. "comment": "The service salt is already dead",
  170. "name": "salt",
  171. "result": True,
  172. },
  173. {
  174. "changes": {},
  175. "comment": "Service salt is set to be killed",
  176. "name": "salt",
  177. "result": None,
  178. },
  179. {
  180. "changes": "saltstack",
  181. "comment": "Service salt was killed",
  182. "name": "salt",
  183. "result": True,
  184. },
  185. {
  186. "changes": {},
  187. "comment": "Service salt failed to die",
  188. "name": "salt",
  189. "result": False,
  190. },
  191. {
  192. "changes": "saltstack",
  193. "comment": "The service salt is already dead",
  194. "name": "salt",
  195. "result": True,
  196. },
  197. ]
  198. info_mock = MagicMock(return_value={"StartType": ""})
  199. mock = MagicMock(return_value="salt")
  200. with patch.object(service, "_enabled_used_error", mock):
  201. self.assertEqual(service.dead("salt", enabled=1), "salt")
  202. tmock = MagicMock(return_value=True)
  203. fmock = MagicMock(return_value=False)
  204. with patch.object(service, "_available", fmock):
  205. self.assertDictEqual(service.dead("salt"), ret[0])
  206. with patch.object(service, "_available", tmock):
  207. mock = MagicMock(return_value={"changes": "saltstack"})
  208. with patch.dict(service.__opts__, {"test": True}):
  209. with patch.dict(
  210. service.__salt__,
  211. {
  212. "service.enabled": fmock,
  213. "service.stop": tmock,
  214. "service.status": fmock,
  215. "service.info": info_mock,
  216. },
  217. ):
  218. with patch.object(service, "_enable", mock):
  219. self.assertDictEqual(service.dead("salt", True), ret[5])
  220. with patch.dict(
  221. service.__salt__,
  222. {
  223. "service.enabled": tmock,
  224. "service.status": tmock,
  225. "service.info": info_mock,
  226. },
  227. ):
  228. self.assertDictEqual(service.dead("salt"), ret[2])
  229. with patch.dict(service.__opts__, {"test": False}):
  230. with patch.dict(
  231. service.__salt__,
  232. {
  233. "service.enabled": fmock,
  234. "service.stop": tmock,
  235. "service.status": fmock,
  236. "service.info": info_mock,
  237. },
  238. ):
  239. with patch.object(service, "_enable", mock):
  240. self.assertDictEqual(service.dead("salt", True), ret[1])
  241. with patch.dict(
  242. service.__salt__,
  243. {
  244. "service.enabled": MagicMock(side_effect=[True, True, False]),
  245. "service.status": MagicMock(side_effect=[True, False, False]),
  246. "service.stop": MagicMock(return_value="stack"),
  247. "service.info": info_mock,
  248. },
  249. ):
  250. with patch.object(
  251. service,
  252. "_enable",
  253. MagicMock(return_value={"changes": "saltstack"}),
  254. ):
  255. self.assertDictEqual(service.dead("salt", True), ret[3])
  256. # test an initd which a wrong status (True even if dead)
  257. with patch.dict(
  258. service.__salt__,
  259. {
  260. "service.enabled": MagicMock(side_effect=[False, False, False]),
  261. "service.status": MagicMock(side_effect=[True, True, True]),
  262. "service.stop": MagicMock(return_value="stack"),
  263. "service.info": info_mock,
  264. },
  265. ):
  266. with patch.object(service, "_disable", MagicMock(return_value={})):
  267. self.assertDictEqual(service.dead("salt", False), ret[4])
  268. def test_dead_with_missing_service(self):
  269. """
  270. Tests the case in which a service.dead state is executed on a state
  271. which does not exist.
  272. See https://github.com/saltstack/salt/issues/37511
  273. """
  274. name = "thisisnotarealservice"
  275. with patch.dict(
  276. service.__salt__, {"service.available": MagicMock(return_value=False)}
  277. ):
  278. ret = service.dead(name=name)
  279. self.assertDictEqual(
  280. ret,
  281. {
  282. "changes": {},
  283. "comment": "The named service {0} is not available".format(name),
  284. "result": True,
  285. "name": name,
  286. },
  287. )
  288. def test_enabled(self):
  289. """
  290. Test to verify that the service is enabled
  291. """
  292. ret = {"changes": "saltstack", "comment": "", "name": "salt", "result": True}
  293. mock = MagicMock(return_value={"changes": "saltstack"})
  294. with patch.object(service, "_enable", mock):
  295. self.assertDictEqual(service.enabled("salt"), ret)
  296. def test_disabled(self):
  297. """
  298. Test to verify that the service is disabled
  299. """
  300. ret = {"changes": "saltstack", "comment": "", "name": "salt", "result": True}
  301. mock = MagicMock(return_value={"changes": "saltstack"})
  302. with patch.object(service, "_disable", mock):
  303. self.assertDictEqual(service.disabled("salt"), ret)
  304. def test_mod_watch(self):
  305. """
  306. Test to the service watcher, called to invoke the watch command.
  307. """
  308. ret = [
  309. {
  310. "changes": {},
  311. "comment": "Service is already stopped",
  312. "name": "salt",
  313. "result": True,
  314. },
  315. {
  316. "changes": {},
  317. "comment": "Unable to trigger watch for service.stack",
  318. "name": "salt",
  319. "result": False,
  320. },
  321. {
  322. "changes": {},
  323. "comment": "Service is set to be started",
  324. "name": "salt",
  325. "result": None,
  326. },
  327. {
  328. "changes": {"salt": "salt"},
  329. "comment": "Service started",
  330. "name": "salt",
  331. "result": "salt",
  332. },
  333. ]
  334. mock = MagicMock(return_value=False)
  335. with patch.dict(service.__salt__, {"service.status": mock}):
  336. self.assertDictEqual(service.mod_watch("salt", "dead"), ret[0])
  337. with patch.dict(service.__salt__, {"service.start": func}):
  338. with patch.dict(service.__opts__, {"test": True}):
  339. self.assertDictEqual(service.mod_watch("salt", "running"), ret[2])
  340. with patch.dict(service.__opts__, {"test": False}):
  341. self.assertDictEqual(service.mod_watch("salt", "running"), ret[3])
  342. self.assertDictEqual(service.mod_watch("salt", "stack"), ret[1])
  343. @destructiveTest
  344. @skipIf(salt.utils.platform.is_darwin(), "service.running is currently failing on OSX")
  345. class ServiceTestCaseFunctional(TestCase, LoaderModuleMockMixin):
  346. """
  347. Validate the service state
  348. """
  349. def setup_loader_modules(self):
  350. self.opts = salt.config.DEFAULT_MINION_OPTS.copy()
  351. self.opts["grains"] = salt.loader.grains(self.opts)
  352. self.utils = salt.loader.utils(self.opts)
  353. self.modules = salt.loader.minion_mods(self.opts, utils=self.utils)
  354. self.service_name = "cron"
  355. cmd_name = "crontab"
  356. os_family = self.opts["grains"]["os_family"]
  357. os_release = self.opts["grains"]["osrelease"]
  358. if os_family == "RedHat":
  359. self.service_name = "crond"
  360. elif os_family == "Arch":
  361. self.service_name = "sshd"
  362. cmd_name = "systemctl"
  363. elif os_family == "MacOS":
  364. self.service_name = "org.ntp.ntpd"
  365. if int(os_release.split(".")[1]) >= 13:
  366. self.service_name = "com.openssh.sshd"
  367. elif os_family == "Windows":
  368. self.service_name = "Spooler"
  369. if os_family != "Windows" and salt.utils.path.which(cmd_name) is None:
  370. self.skipTest("{0} is not installed".format(cmd_name))
  371. return {
  372. service: {
  373. "__grains__": self.opts["grains"],
  374. "__opts__": self.opts,
  375. "__salt__": self.modules,
  376. "__utils__": self.utils,
  377. },
  378. }
  379. def setUp(self):
  380. self.pre_srv_enabled = (
  381. True
  382. if self.service_name in self.modules["service.get_enabled"]()
  383. else False
  384. )
  385. self.post_srv_disable = False
  386. if not self.pre_srv_enabled:
  387. self.modules["service.enable"](self.service_name)
  388. self.post_srv_disable = True
  389. def tearDown(self):
  390. if self.post_srv_disable:
  391. self.modules["service.disable"](self.service_name)
  392. @slowTest
  393. def test_running_with_reload(self):
  394. with patch.dict(service.__opts__, {"test": False}):
  395. service.dead(self.service_name, enable=False)
  396. result = service.running(name=self.service_name, enable=True, reload=False)
  397. expected = {
  398. "changes": {self.service_name: True},
  399. "comment": "Service {0} has been enabled, and is "
  400. "running".format(self.service_name),
  401. "name": self.service_name,
  402. "result": True,
  403. }
  404. self.assertDictEqual(result, expected)