1""" 2 :codeauthor: Rupesh Tare <rupesht@saltstack.com> 3""" 4 5 6import os 7 8import salt.modules.daemontools as daemontools 9from salt.exceptions import CommandExecutionError 10from tests.support.mixins import LoaderModuleMockMixin 11from tests.support.mock import MagicMock, patch 12from tests.support.unit import TestCase 13 14 15class DaemontoolsTestCase(TestCase, LoaderModuleMockMixin): 16 """ 17 Test cases for salt.modules.daemontools 18 """ 19 20 def setup_loader_modules(self): 21 return {daemontools: {}} 22 23 def test_start(self): 24 """ 25 Test for Starts service via daemontools 26 """ 27 mock = MagicMock(return_value=None) 28 with patch.dict(daemontools.__salt__, {"file.remove": mock}): 29 mock = MagicMock(return_value="") 30 with patch.object(daemontools, "_service_path", mock): 31 mock = MagicMock(return_value=False) 32 with patch.dict(daemontools.__salt__, {"cmd.retcode": mock}): 33 self.assertTrue(daemontools.start("name")) 34 35 def test_stop(self): 36 """ 37 Test for Stops service via daemontools 38 """ 39 mock = MagicMock(return_value=None) 40 with patch.dict(daemontools.__salt__, {"file.touch": mock}): 41 mock = MagicMock(return_value="") 42 with patch.object(daemontools, "_service_path", mock): 43 mock = MagicMock(return_value=False) 44 with patch.dict(daemontools.__salt__, {"cmd.retcode": mock}): 45 self.assertTrue(daemontools.stop("name")) 46 47 def test_term(self): 48 """ 49 Test for Send a TERM to service via daemontools 50 """ 51 mock = MagicMock(return_value="") 52 with patch.object(daemontools, "_service_path", mock): 53 mock = MagicMock(return_value=False) 54 with patch.dict(daemontools.__salt__, {"cmd.retcode": mock}): 55 self.assertTrue(daemontools.term("name")) 56 57 def test_reload_(self): 58 """ 59 Test for Wrapper for term() 60 """ 61 mock = MagicMock(return_value=None) 62 with patch.object(daemontools, "term", mock): 63 self.assertEqual(daemontools.reload_("name"), None) 64 65 def test_restart(self): 66 """ 67 Test for Restart service via daemontools. This will stop/start service 68 """ 69 mock = MagicMock(return_value=False) 70 with patch.object(daemontools, "stop", mock): 71 self.assertEqual(daemontools.restart("name"), "restart False") 72 73 def test_full_restart(self): 74 """ 75 Test for Calls daemontools.restart() function 76 """ 77 mock = MagicMock(return_value=None) 78 with patch.object(daemontools, "restart", mock): 79 self.assertEqual(daemontools.restart("name"), None) 80 81 def test_status(self): 82 """ 83 Test for Return the status for a service via 84 daemontools, return pid if running 85 """ 86 with patch("re.search", MagicMock(return_value=1)): 87 mock = MagicMock(return_value="") 88 with patch.object(daemontools, "_service_path", mock): 89 mock = MagicMock(return_value="name") 90 with patch.dict(daemontools.__salt__, {"cmd.run_stdout": mock}): 91 self.assertEqual(daemontools.status("name"), "") 92 93 def test_available(self): 94 """ 95 Test for Returns ``True`` if the specified service 96 is available, otherwise returns``False``. 97 """ 98 mock = MagicMock(return_value=[]) 99 with patch.object(daemontools, "get_all", mock): 100 self.assertFalse(daemontools.available("name")) 101 102 def test_missing(self): 103 """ 104 Test for The inverse of daemontools.available. 105 """ 106 mock = MagicMock(return_value=[]) 107 with patch.object(daemontools, "get_all", mock): 108 self.assertTrue(daemontools.missing("name")) 109 110 def test_get_all(self): 111 """ 112 Test for Return a list of all available services 113 """ 114 self.assertRaises(CommandExecutionError, daemontools.get_all) 115 116 with patch.object(daemontools, "SERVICE_DIR", "A"): 117 mock = MagicMock(return_value="A") 118 with patch.object(os, "listdir", mock): 119 self.assertEqual(daemontools.get_all(), ["A"]) 120