1
0

test_adb.py 16 KB

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