salt/tests/unit/beacons/test_adb_beacon.py

382 lines
13 KiB
Python
Raw Normal View History

# coding: utf-8
# Python libs
from __future__ import absolute_import
# Salt testing libs
from tests.support.unit import skipIf, TestCase
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, Mock
2017-02-19 15:35:30 +00:00
from tests.support.mixins import LoaderModuleMockMixin
# Salt libs
2017-02-19 15:35:30 +00:00
import salt.beacons.adb as adb
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-02-19 15:35:30 +00:00
class ADBBeaconTestCase(TestCase, LoaderModuleMockMixin):
'''
Test case for salt.beacons.adb
'''
2017-02-19 15:35:30 +00:00
def setup_loader_modules(self):
return {
adb: {
'last_state': {},
'last_state_extra': {'no_devices': False}
}
}
def test_no_adb_command(self):
2015-11-30 19:19:13 +00:00
with patch('salt.utils.which') as mock:
mock.return_value = None
ret = adb.__virtual__()
2015-11-30 19:19:13 +00:00
mock.assert_called_once_with('adb')
self.assertFalse(ret)
def test_with_adb_command(self):
2015-11-30 19:19:13 +00:00
with patch('salt.utils.which') as mock:
mock.return_value = '/usr/bin/adb'
ret = adb.__virtual__()
2015-11-30 19:19:13 +00:00
mock.assert_called_once_with('adb')
self.assertEqual(ret, 'adb')
def test_non_dict_config(self):
config = []
log_mock = Mock()
2017-03-31 12:42:52 +00:00
with patch.object(adb, 'log', log_mock):
2017-03-31 12:42:52 +00:00
ret = adb.beacon(config)
2017-03-31 12:42:52 +00:00
self.assertEqual(ret, [])
log_mock.info.assert_called_once_with('Configuration for adb beacon must be a dict.')
def test_empty_config(self):
config = {}
log_mock = Mock()
2017-03-31 12:42:52 +00:00
with patch.object(adb, 'log', log_mock):
ret = adb.beacon(config)
2017-03-31 12:42:52 +00:00
self.assertEqual(ret, [])
log_mock.info.assert_called_once_with('Configuration for adb beacon must include a states array.')
def test_invalid_states(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['Random', 'Failings']}
log_mock = Mock()
2017-03-31 12:42:52 +00:00
with patch.object(adb, 'log', log_mock):
2017-03-31 12:42:52 +00:00
ret = adb.beacon(config)
2017-03-31 12:42:52 +00:00
self.assertEqual(ret, [])
log_mock.info.assert_called_once_with('Need a one of the following adb states:'
' offline, bootloader, device, host, recovery, '
'no permissions, sideload, unauthorized, unknown, missing')
def test_device_state(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device']}
2015-11-30 19:19:13 +00:00
mock = Mock(return_value='List of devices attached\nHTC\tdevice',)
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
def test_device_state_change(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['offline']}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'List of devices attached\nHTC\toffline'
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [])
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
self.assertEqual(ret, [{'device': 'HTC', 'state': 'offline', 'tag': 'offline'}])
def test_multiple_devices(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['offline', 'device']}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'List of devices attached\nHTC\toffline\nNexus\tdevice'
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
ret = adb.beacon(config)
self.assertEqual(ret, [
2015-11-30 19:19:13 +00:00
{'device': 'HTC', 'state': 'offline', 'tag': 'offline'},
{'device': 'Nexus', 'state': 'device', 'tag': 'device'}
])
def test_no_devices_with_different_states(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['offline'], 'no_devices_event': True}
2015-11-30 19:19:13 +00:00
mock = Mock(return_value='List of devices attached\nHTC\tdevice')
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [])
def test_no_devices_no_repeat(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['offline', 'device'], 'no_devices_event': True}
out = [
'List of devices attached\nHTC\tdevice',
'List of devices attached',
'List of devices attached'
]
mock = Mock(side_effect=out)
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
ret = adb.beacon(config)
self.assertEqual(ret, [{'tag': 'no_devices'}])
ret = adb.beacon(config)
self.assertEqual(ret, [])
def test_no_devices(self):
config = {'states': ['offline', 'device'], 'no_devices_event': True}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached',
'List of devices attached'
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
self.assertEqual(ret, [{'tag': 'no_devices'}])
ret = adb.beacon(config)
self.assertEqual(ret, [])
def test_device_missing(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device', 'missing']}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'List of devices attached',
'List of devices attached\nHTC\tdevice',
'List of devices attached\nHTC\tdevice'
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
self.assertEqual(ret, [{'device': 'HTC', 'state': 'missing', 'tag': 'missing'}])
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
ret = adb.beacon(config)
self.assertEqual(ret, [])
def test_with_startup(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device']}
2015-11-30 19:19:13 +00:00
mock = Mock(return_value='* daemon started successfully *\nList of devices attached\nHTC\tdevice',)
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
def test_with_user(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device'], 'user': 'fred'}
2017-02-19 15:35:30 +00:00
mock = Mock(return_value='* daemon started successfully *\nList of devices attached\nHTC\tdevice')
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
mock.assert_called_once_with('adb devices', runas='fred')
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
def test_device_low_battery(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device'], 'battery_low': 30}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'25',
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
2015-11-30 19:19:13 +00:00
{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
def test_device_no_repeat(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device'], 'battery_low': 30}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'25',
'List of devices attached\nHTC\tdevice',
'25'
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
2015-11-30 19:19:13 +00:00
{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
ret = adb.beacon(config)
self.assertEqual(ret, [])
def test_device_no_repeat_capacity_increase(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device'], 'battery_low': 75}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'25',
'List of devices attached\nHTC\tdevice',
'30'
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
2015-11-30 19:19:13 +00:00
{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
ret = adb.beacon(config)
self.assertEqual(ret, [])
def test_device_no_repeat_with_not_found_state(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['offline'], 'battery_low': 30}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'25',
'List of devices attached\nHTC\tdevice',
'25'
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
self.assertEqual(ret, [{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
ret = adb.beacon(config)
self.assertEqual(ret, [])
def test_device_battery_charged(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device'], 'battery_low': 30}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'100',
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
def test_device_low_battery_equal(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device'], 'battery_low': 25}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'25',
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
2015-11-30 19:19:13 +00:00
{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
def test_device_battery_not_found(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device'], 'battery_low': 25}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'/system/bin/sh: cat: /sys/class/power_supply/*/capacity: No such file or directory',
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
def test_device_repeat_multi(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['offline'], 'battery_low': 35}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'25',
'List of devices attached\nHTC\tdevice',
'40',
'List of devices attached\nHTC\tdevice',
'25',
'List of devices attached\nHTC\tdevice',
'80'
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
self.assertEqual(ret, [{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
ret = adb.beacon(config)
self.assertEqual(ret, [])
ret = adb.beacon(config)
2015-11-30 19:19:13 +00:00
self.assertEqual(ret, [{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
ret = adb.beacon(config)
self.assertEqual(ret, [])
def test_weird_batteries(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device'], 'battery_low': 25}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'-9000',
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
def test_multiple_batteries(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device'], 'battery_low': 30}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'25\n40',
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
2015-11-30 19:19:13 +00:00
{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
def test_multiple_low_batteries(self):
2015-11-30 19:19:13 +00:00
config = {'states': ['device'], 'battery_low': 30}
out = [
2015-11-30 19:19:13 +00:00
'List of devices attached\nHTC\tdevice',
'25\n14',
]
mock = Mock(side_effect=out)
2015-11-30 19:19:13 +00:00
with patch.dict(adb.__salt__, {'cmd.run': mock}):
ret = adb.beacon(config)
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
2015-11-30 19:19:13 +00:00
{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])