123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- """
- integration tests for mac_service
- """
- import plistlib
- import salt.utils.files
- from tests.support.case import ModuleCase
- from tests.support.helpers import (
- destructiveTest,
- runs_on,
- skip_if_binaries_missing,
- skip_if_not_root,
- slowTest,
- )
- @skip_if_not_root
- @runs_on(kernel="Darwin")
- @skip_if_binaries_missing("launchctl", "plutil")
- class MacServiceModuleTest(ModuleCase):
- """
- Validate the mac_service module
- """
- SERVICE_NAME = "com.salt.integration.test"
- SERVICE_PATH = "/Library/LaunchDaemons/com.salt.integration.test.plist"
- def setUp(self):
- """
- setup our test launch service.
- """
- service_data = {
- "KeepAlive": True,
- "Label": self.SERVICE_NAME,
- "ProgramArguments": ["/bin/sleep", "1000"],
- "RunAtLoad": True,
- }
- with salt.utils.files.fopen(self.SERVICE_PATH, "wb") as fp:
- plistlib.dump(service_data, fp)
- self.run_function("service.enable", [self.SERVICE_NAME])
- self.run_function("service.start", [self.SERVICE_NAME])
- def tearDown(self):
- """
- stop and remove our test service.
- """
- self.run_function("service.stop", [self.SERVICE_NAME])
- salt.utils.files.safe_rm(self.SERVICE_PATH)
- @slowTest
- def test_show(self):
- """
- Test service.show
- """
- # Existing Service
- service_info = self.run_function("service.show", [self.SERVICE_NAME])
- self.assertIsInstance(service_info, dict)
- self.assertEqual(service_info["plist"]["Label"], self.SERVICE_NAME)
- # Missing Service
- self.assertIn(
- "Service not found", self.run_function("service.show", ["spongebob"])
- )
- @slowTest
- def test_launchctl(self):
- """
- Test service.launchctl
- """
- # Expected Functionality
- self.assertTrue(
- self.run_function("service.launchctl", ["error", "bootstrap", 64])
- )
- self.assertEqual(
- self.run_function(
- "service.launchctl", ["error", "bootstrap", 64], return_stdout=True
- ),
- "64: unknown error code",
- )
- # Raise an error
- self.assertIn(
- "Failed to error service",
- self.run_function("service.launchctl", ["error", "bootstrap"]),
- )
- @slowTest
- def test_list(self):
- """
- Test service.list
- """
- # Expected Functionality
- self.assertIn("PID", self.run_function("service.list"))
- self.assertIn("{", self.run_function("service.list", [self.SERVICE_NAME]))
- # Service not found
- self.assertIn(
- "Service not found", self.run_function("service.list", ["spongebob"])
- )
- @destructiveTest
- @slowTest
- def test_enable(self):
- """
- Test service.enable
- """
- self.assertTrue(self.run_function("service.enable", [self.SERVICE_NAME]))
- self.assertIn(
- "Service not found", self.run_function("service.enable", ["spongebob"])
- )
- @destructiveTest
- @slowTest
- def test_disable(self):
- """
- Test service.disable
- """
- self.assertTrue(self.run_function("service.disable", [self.SERVICE_NAME]))
- self.assertIn(
- "Service not found", self.run_function("service.disable", ["spongebob"])
- )
- @destructiveTest
- @slowTest
- def test_start(self):
- """
- Test service.start
- Test service.stop
- Test service.status
- """
- self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME]))
- self.assertIn(
- "Service not found", self.run_function("service.start", ["spongebob"])
- )
- @destructiveTest
- @slowTest
- def test_stop(self):
- """
- Test service.stop
- """
- self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME]))
- self.assertIn(
- "Service not found", self.run_function("service.stop", ["spongebob"])
- )
- @destructiveTest
- @slowTest
- def test_status(self):
- """
- Test service.status
- """
- # A running service
- self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME]))
- self.assertTrue(self.run_function("service.status", [self.SERVICE_NAME]))
- # A stopped service
- self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME]))
- self.assertFalse(self.run_function("service.status", [self.SERVICE_NAME]))
- # Service not found
- self.assertFalse(self.run_function("service.status", ["spongebob"]))
- @slowTest
- def test_available(self):
- """
- Test service.available
- """
- self.assertTrue(self.run_function("service.available", [self.SERVICE_NAME]))
- self.assertFalse(self.run_function("service.available", ["spongebob"]))
- @slowTest
- def test_missing(self):
- """
- Test service.missing
- """
- self.assertFalse(self.run_function("service.missing", [self.SERVICE_NAME]))
- self.assertTrue(self.run_function("service.missing", ["spongebob"]))
- @destructiveTest
- @slowTest
- def test_enabled(self):
- """
- Test service.enabled
- """
- self.assertTrue(self.run_function("service.enabled", [self.SERVICE_NAME]))
- self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME]))
- self.assertTrue(self.run_function("service.enabled", [self.SERVICE_NAME]))
- self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME]))
- self.assertTrue(self.run_function("service.enabled", ["spongebob"]))
- @destructiveTest
- @slowTest
- def test_disabled(self):
- """
- Test service.disabled
- """
- self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME]))
- self.assertFalse(self.run_function("service.disabled", [self.SERVICE_NAME]))
- self.assertTrue(self.run_function("service.disable", [self.SERVICE_NAME]))
- self.assertTrue(self.run_function("service.disabled", [self.SERVICE_NAME]))
- self.assertTrue(self.run_function("service.enable", [self.SERVICE_NAME]))
- self.assertIn(
- "Service not found", self.run_function("service.stop", ["spongebob"])
- )
- @slowTest
- def test_get_all(self):
- """
- Test service.get_all
- """
- services = self.run_function("service.get_all")
- self.assertIsInstance(services, list)
- self.assertIn(self.SERVICE_NAME, services)
- @slowTest
- def test_get_enabled(self):
- """
- Test service.get_enabled
- """
- services = self.run_function("service.get_enabled")
- self.assertIsInstance(services, list)
- self.assertIn(self.SERVICE_NAME, services)
- @slowTest
- def test_service_laoded(self):
- """
- Test service.get_enabled
- """
- self.assertTrue(self.run_function("service.loaded", [self.SERVICE_NAME]))
|