test_adb.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt testing libs
  5. from tests.support.unit import skipIf, TestCase
  6. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, Mock
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. # Salt libs
  9. import salt.beacons.adb as adb
  10. @skipIf(NO_MOCK, NO_MOCK_REASON)
  11. class ADBBeaconTestCase(TestCase, LoaderModuleMockMixin):
  12. '''
  13. Test case for salt.beacons.adb
  14. '''
  15. def setup_loader_modules(self):
  16. return {
  17. adb: {
  18. 'last_state': {},
  19. 'last_state_extra': {'no_devices': False}
  20. }
  21. }
  22. def test_no_adb_command(self):
  23. with patch('salt.utils.path.which') as mock:
  24. mock.return_value = None
  25. ret = adb.__virtual__()
  26. mock.assert_called_once_with('adb')
  27. self.assertFalse(ret)
  28. def test_with_adb_command(self):
  29. with patch('salt.utils.path.which') as mock:
  30. mock.return_value = '/usr/bin/adb'
  31. ret = adb.__virtual__()
  32. mock.assert_called_once_with('adb')
  33. self.assertEqual(ret, 'adb')
  34. def test_non_list_config(self):
  35. config = {}
  36. ret = adb.validate(config)
  37. self.assertEqual(ret, (False, 'Configuration for adb beacon must'
  38. ' be a list.'))
  39. def test_empty_config(self):
  40. config = [{}]
  41. ret = adb.validate(config)
  42. self.assertEqual(ret, (False, 'Configuration for adb beacon must'
  43. ' include a states array.'))
  44. def test_invalid_states(self):
  45. config = [{'states': ['Random', 'Failings']}]
  46. ret = adb.validate(config)
  47. self.assertEqual(ret, (False, 'Need a one of the following'
  48. ' adb states: offline, bootloader,'
  49. ' device, host, recovery, no'
  50. ' permissions, sideload,'
  51. ' unauthorized, unknown, missing'))
  52. def test_device_state(self):
  53. config = [{'states': ['device']}]
  54. mock = Mock(return_value='List of devices attached\nHTC\tdevice',)
  55. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  56. ret = adb.validate(config)
  57. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  58. ret = adb.beacon(config)
  59. self.assertEqual(ret, [{'device': 'HTC',
  60. 'state': 'device',
  61. 'tag': 'device'}])
  62. def test_device_state_change(self):
  63. config = [{'states': ['offline']}]
  64. out = [
  65. 'List of devices attached\nHTC\tdevice',
  66. 'List of devices attached\nHTC\toffline'
  67. ]
  68. mock = Mock(side_effect=out)
  69. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  70. ret = adb.validate(config)
  71. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  72. ret = adb.beacon(config)
  73. self.assertEqual(ret, [])
  74. ret = adb.beacon(config)
  75. self.assertEqual(ret, [{'device': 'HTC',
  76. 'state': 'offline',
  77. 'tag': 'offline'}])
  78. def test_multiple_devices(self):
  79. config = [{'states': ['offline', 'device']}]
  80. out = [
  81. 'List of devices attached\nHTC\tdevice',
  82. 'List of devices attached\nHTC\toffline\nNexus\tdevice'
  83. ]
  84. mock = Mock(side_effect=out)
  85. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  86. ret = adb.validate(config)
  87. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  88. ret = adb.beacon(config)
  89. self.assertEqual(ret, [{'device': 'HTC',
  90. 'state': 'device',
  91. 'tag': 'device'}])
  92. ret = adb.beacon(config)
  93. self.assertEqual(ret, [
  94. {'device': 'HTC', 'state': 'offline', 'tag': 'offline'},
  95. {'device': 'Nexus', 'state': 'device', 'tag': 'device'}
  96. ])
  97. def test_no_devices_with_different_states(self):
  98. config = [{'states': ['offline'], 'no_devices_event': True}]
  99. mock = Mock(return_value='List of devices attached\nHTC\tdevice')
  100. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  101. ret = adb.validate(config)
  102. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  103. ret = adb.beacon(config)
  104. self.assertEqual(ret, [])
  105. def test_no_devices_no_repeat(self):
  106. config = [{'states': ['offline', 'device'], 'no_devices_event': True}]
  107. out = [
  108. 'List of devices attached\nHTC\tdevice',
  109. 'List of devices attached',
  110. 'List of devices attached'
  111. ]
  112. mock = Mock(side_effect=out)
  113. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  114. ret = adb.validate(config)
  115. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  116. ret = adb.beacon(config)
  117. self.assertEqual(ret, [{'device': 'HTC',
  118. 'state': 'device',
  119. 'tag': 'device'}])
  120. ret = adb.beacon(config)
  121. self.assertEqual(ret, [{'tag': 'no_devices'}])
  122. ret = adb.beacon(config)
  123. self.assertEqual(ret, [])
  124. def test_no_devices(self):
  125. config = [{'states': ['offline', 'device'], 'no_devices_event': True}]
  126. out = [
  127. 'List of devices attached',
  128. 'List of devices attached'
  129. ]
  130. mock = Mock(side_effect=out)
  131. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  132. ret = adb.validate(config)
  133. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  134. ret = adb.beacon(config)
  135. self.assertEqual(ret, [{'tag': 'no_devices'}])
  136. ret = adb.beacon(config)
  137. self.assertEqual(ret, [])
  138. def test_device_missing(self):
  139. config = [{'states': ['device', 'missing']}]
  140. out = [
  141. 'List of devices attached\nHTC\tdevice',
  142. 'List of devices attached',
  143. 'List of devices attached\nHTC\tdevice',
  144. 'List of devices attached\nHTC\tdevice'
  145. ]
  146. mock = Mock(side_effect=out)
  147. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  148. ret = adb.validate(config)
  149. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  150. ret = adb.beacon(config)
  151. self.assertEqual(ret, [{'device': 'HTC',
  152. 'state': 'device',
  153. 'tag': 'device'}])
  154. ret = adb.beacon(config)
  155. self.assertEqual(ret, [{'device': 'HTC',
  156. 'state': 'missing',
  157. 'tag': 'missing'}])
  158. ret = adb.beacon(config)
  159. self.assertEqual(ret, [{'device': 'HTC',
  160. 'state': 'device',
  161. 'tag': 'device'}])
  162. ret = adb.beacon(config)
  163. self.assertEqual(ret, [])
  164. def test_with_startup(self):
  165. config = [{'states': ['device']}]
  166. mock = Mock(return_value='* daemon started successfully *\nList of devices attached\nHTC\tdevice',)
  167. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  168. ret = adb.validate(config)
  169. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  170. ret = adb.beacon(config)
  171. self.assertEqual(ret, [{'device': 'HTC',
  172. 'state': 'device',
  173. 'tag': 'device'}])
  174. def test_with_user(self):
  175. config = [{'states': ['device'], 'user': 'fred'}]
  176. mock = Mock(return_value='* daemon started successfully *\nList of devices attached\nHTC\tdevice')
  177. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  178. ret = adb.validate(config)
  179. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  180. ret = adb.beacon(config)
  181. mock.assert_called_once_with('adb devices', runas='fred')
  182. self.assertEqual(ret, [{'device': 'HTC',
  183. 'state': 'device',
  184. 'tag': 'device'}])
  185. def test_device_low_battery(self):
  186. config = [{'states': ['device'], 'battery_low': 30}]
  187. out = [
  188. 'List of devices attached\nHTC\tdevice',
  189. '25',
  190. ]
  191. mock = Mock(side_effect=out)
  192. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  193. ret = adb.validate(config)
  194. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  195. ret = adb.beacon(config)
  196. self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
  197. {'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
  198. def test_device_no_repeat(self):
  199. config = [{'states': ['device'], 'battery_low': 30}]
  200. out = [
  201. 'List of devices attached\nHTC\tdevice',
  202. '25',
  203. 'List of devices attached\nHTC\tdevice',
  204. '25'
  205. ]
  206. mock = Mock(side_effect=out)
  207. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  208. ret = adb.validate(config)
  209. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  210. ret = adb.beacon(config)
  211. self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
  212. {'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
  213. ret = adb.beacon(config)
  214. self.assertEqual(ret, [])
  215. def test_device_no_repeat_capacity_increase(self):
  216. config = [{'states': ['device'], 'battery_low': 75}]
  217. out = [
  218. 'List of devices attached\nHTC\tdevice',
  219. '25',
  220. 'List of devices attached\nHTC\tdevice',
  221. '30'
  222. ]
  223. mock = Mock(side_effect=out)
  224. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  225. ret = adb.validate(config)
  226. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  227. ret = adb.beacon(config)
  228. self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
  229. {'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
  230. ret = adb.beacon(config)
  231. self.assertEqual(ret, [])
  232. def test_device_no_repeat_with_not_found_state(self):
  233. config = [{'states': ['offline'], 'battery_low': 30}]
  234. out = [
  235. 'List of devices attached\nHTC\tdevice',
  236. '25',
  237. 'List of devices attached\nHTC\tdevice',
  238. '25'
  239. ]
  240. mock = Mock(side_effect=out)
  241. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  242. ret = adb.validate(config)
  243. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  244. ret = adb.beacon(config)
  245. self.assertEqual(ret, [{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
  246. ret = adb.beacon(config)
  247. self.assertEqual(ret, [])
  248. def test_device_battery_charged(self):
  249. config = [{'states': ['device'], 'battery_low': 30}]
  250. out = [
  251. 'List of devices attached\nHTC\tdevice',
  252. '100',
  253. ]
  254. mock = Mock(side_effect=out)
  255. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  256. ret = adb.validate(config)
  257. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  258. ret = adb.beacon(config)
  259. self.assertEqual(ret, [{'device': 'HTC',
  260. 'state': 'device',
  261. 'tag': 'device'}])
  262. def test_device_low_battery_equal(self):
  263. config = [{'states': ['device'], 'battery_low': 25}]
  264. out = [
  265. 'List of devices attached\nHTC\tdevice',
  266. '25',
  267. ]
  268. mock = Mock(side_effect=out)
  269. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  270. ret = adb.validate(config)
  271. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  272. ret = adb.beacon(config)
  273. self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
  274. {'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
  275. def test_device_battery_not_found(self):
  276. config = [{'states': ['device'], 'battery_low': 25}]
  277. out = [
  278. 'List of devices attached\nHTC\tdevice',
  279. '/system/bin/sh: cat: /sys/class/power_supply/*/capacity: No such file or directory',
  280. ]
  281. mock = Mock(side_effect=out)
  282. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  283. ret = adb.validate(config)
  284. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  285. ret = adb.beacon(config)
  286. self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
  287. def test_device_repeat_multi(self):
  288. config = [{'states': ['offline'], 'battery_low': 35}]
  289. out = [
  290. 'List of devices attached\nHTC\tdevice',
  291. '25',
  292. 'List of devices attached\nHTC\tdevice',
  293. '40',
  294. 'List of devices attached\nHTC\tdevice',
  295. '25',
  296. 'List of devices attached\nHTC\tdevice',
  297. '80'
  298. ]
  299. mock = Mock(side_effect=out)
  300. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  301. ret = adb.validate(config)
  302. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  303. ret = adb.beacon(config)
  304. self.assertEqual(ret, [{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
  305. ret = adb.beacon(config)
  306. self.assertEqual(ret, [])
  307. ret = adb.beacon(config)
  308. self.assertEqual(ret, [{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
  309. ret = adb.beacon(config)
  310. self.assertEqual(ret, [])
  311. def test_weird_batteries(self):
  312. config = [{'states': ['device'], 'battery_low': 25}]
  313. out = [
  314. 'List of devices attached\nHTC\tdevice',
  315. '-9000',
  316. ]
  317. mock = Mock(side_effect=out)
  318. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  319. ret = adb.validate(config)
  320. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  321. ret = adb.beacon(config)
  322. self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
  323. def test_multiple_batteries(self):
  324. config = [{'states': ['device'], 'battery_low': 30}]
  325. out = [
  326. 'List of devices attached\nHTC\tdevice',
  327. '25\n40',
  328. ]
  329. mock = Mock(side_effect=out)
  330. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  331. ret = adb.validate(config)
  332. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  333. ret = adb.beacon(config)
  334. self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
  335. {'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
  336. def test_multiple_low_batteries(self):
  337. config = [{'states': ['device'], 'battery_low': 30}]
  338. out = [
  339. 'List of devices attached\nHTC\tdevice',
  340. '25\n14',
  341. ]
  342. mock = Mock(side_effect=out)
  343. with patch.dict(adb.__salt__, {'cmd.run': mock}):
  344. ret = adb.validate(config)
  345. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  346. ret = adb.beacon(config)
  347. self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
  348. {'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])