test_bluez_bluetooth.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.bluez_bluetooth as bluez
  9. import salt.utils.validate.net
  10. from salt.exceptions import CommandExecutionError
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import MagicMock, patch
  14. from tests.support.unit import TestCase
  15. class MockBluetooth(object):
  16. """
  17. Mock class for bluetooth
  18. """
  19. def __init__(self):
  20. pass
  21. @staticmethod
  22. def discover_devices(lookup_names):
  23. """
  24. Mock method to return all Discoverable devices
  25. """
  26. return [["a", "b", "c"], ["d", "e", "f"]]
  27. class BluezTestCase(TestCase, LoaderModuleMockMixin):
  28. """
  29. Test cases for salt.modules.bluez
  30. """
  31. def setup_loader_modules(self):
  32. return {bluez: {"bluetooth": MockBluetooth()}}
  33. def test_version(self):
  34. """
  35. Test if return bluetooth version
  36. """
  37. mock = MagicMock(return_value="5.7")
  38. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  39. self.assertDictEqual(
  40. bluez.version(),
  41. {"PyBluez": "<= 0.18 (Unknown, but installed)", "Bluez": "5.7"},
  42. )
  43. def test_address_(self):
  44. """
  45. Test of getting address of bluetooth adapter
  46. """
  47. mock = MagicMock(return_value="hci : hci0")
  48. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  49. self.assertDictEqual(
  50. bluez.address_(),
  51. {"hci ": {"device": "hci ", "path": "/sys/class/bluetooth/hci "}},
  52. )
  53. def test_power(self):
  54. """
  55. Test of getting address of bluetooth adapter
  56. """
  57. mock = MagicMock(return_value={})
  58. with patch.object(bluez, "address_", mock):
  59. self.assertRaises(CommandExecutionError, bluez.power, "hci0", "on")
  60. mock = MagicMock(return_value={"hci0": {"device": "hci0", "power": "on"}})
  61. with patch.object(bluez, "address_", mock):
  62. mock = MagicMock(return_value="")
  63. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  64. self.assertTrue(bluez.power("hci0", "on"))
  65. mock = MagicMock(return_value={"hci0": {"device": "hci0", "power": "on"}})
  66. with patch.object(bluez, "address_", mock):
  67. mock = MagicMock(return_value="")
  68. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  69. self.assertFalse(bluez.power("hci0", "off"))
  70. def test_discoverable(self):
  71. """
  72. Test of enabling bluetooth device
  73. """
  74. mock = MagicMock(
  75. side_effect=[
  76. {},
  77. {"hci0": {"device": "hci0", "power": "on"}},
  78. {"hci0": {"device": "hci0", "power": "on"}},
  79. ]
  80. )
  81. with patch.object(bluez, "address_", mock):
  82. self.assertRaises(CommandExecutionError, bluez.discoverable, "hci0")
  83. mock = MagicMock(return_value="UP RUNNING ISCAN")
  84. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  85. self.assertTrue(bluez.discoverable("hci0"))
  86. mock = MagicMock(return_value="")
  87. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  88. self.assertFalse(bluez.discoverable("hci0"))
  89. def test_noscan(self):
  90. """
  91. Test of turning off of scanning modes
  92. """
  93. mock = MagicMock(
  94. side_effect=[
  95. {},
  96. {"hci0": {"device": "hci0", "power": "on"}},
  97. {"hci0": {"device": "hci0", "power": "on"}},
  98. ]
  99. )
  100. with patch.object(bluez, "address_", mock):
  101. self.assertRaises(CommandExecutionError, bluez.noscan, "hci0")
  102. mock = MagicMock(return_value="SCAN")
  103. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  104. self.assertFalse(bluez.noscan("hci0"))
  105. mock = MagicMock(return_value="")
  106. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  107. self.assertTrue(bluez.noscan("hci0"))
  108. def test_scan(self):
  109. """
  110. Test of scanning of bluetooth devices
  111. """
  112. self.assertListEqual(bluez.scan(), [{"a": "b"}, {"d": "e"}])
  113. def test_block(self):
  114. """
  115. Test of blocking specific bluetooth device
  116. """
  117. mock = MagicMock(side_effect=[False, True])
  118. with patch.object(salt.utils.validate.net, "mac", mock):
  119. self.assertRaises(CommandExecutionError, bluez.block, "DE:AD:BE:EF:CA:ZE")
  120. mock = MagicMock(return_value="")
  121. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  122. self.assertIsNone(bluez.block("DE:AD:BE:EF:CA:FE"))
  123. def test_unblock(self):
  124. """
  125. Test to unblock specific bluetooth device
  126. """
  127. mock = MagicMock(side_effect=[False, True])
  128. with patch.object(salt.utils.validate.net, "mac", mock):
  129. self.assertRaises(CommandExecutionError, bluez.block, "DE:AD:BE:EF:CA:ZE")
  130. mock = MagicMock(return_value="")
  131. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  132. self.assertIsNone(bluez.unblock("DE:AD:BE:EF:CA:FE"))
  133. def test_pair(self):
  134. """
  135. Test to pair bluetooth adapter with a device
  136. """
  137. mock = MagicMock(side_effect=[False, True, True])
  138. with patch.object(salt.utils.validate.net, "mac", mock):
  139. self.assertRaises(
  140. CommandExecutionError, bluez.pair, "DE:AD:BE:EF:CA:FE", "1234"
  141. )
  142. self.assertRaises(
  143. CommandExecutionError, bluez.pair, "DE:AD:BE:EF:CA:FE", "abcd"
  144. )
  145. mock = MagicMock(return_value={"device": "hci0"})
  146. with patch.object(bluez, "address_", mock):
  147. mock = MagicMock(return_value="Ok")
  148. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  149. self.assertListEqual(
  150. bluez.pair("DE:AD:BE:EF:CA:FE", "1234"), ["Ok"]
  151. )
  152. def test_unpair(self):
  153. """
  154. Test to unpair bluetooth adaptor with a device
  155. """
  156. mock = MagicMock(side_effect=[False, True])
  157. with patch.object(salt.utils.validate.net, "mac", mock):
  158. self.assertRaises(CommandExecutionError, bluez.unpair, "DE:AD:BE:EF:CA:FE")
  159. mock = MagicMock(return_value="Ok")
  160. with patch.dict(bluez.__salt__, {"cmd.run": mock}):
  161. self.assertListEqual(bluez.unpair("DE:AD:BE:EF:CA:FE"), ["Ok"])
  162. def test_start(self):
  163. """
  164. Test to start bluetooth service
  165. """
  166. mock = MagicMock(return_value="Ok")
  167. with patch.dict(bluez.__salt__, {"service.start": mock}):
  168. self.assertEqual(bluez.start(), "Ok")
  169. def test_stop(self):
  170. """
  171. Test to stop bluetooth service
  172. """
  173. mock = MagicMock(return_value="Ok")
  174. with patch.dict(bluez.__salt__, {"service.stop": mock}):
  175. self.assertEqual(bluez.stop(), "Ok")