123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- # 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
- from tests.support.mixins import LoaderModuleMockMixin
- # Salt libs
- import salt.beacons.adb as adb
- @skipIf(NO_MOCK, NO_MOCK_REASON)
- class ADBBeaconTestCase(TestCase, LoaderModuleMockMixin):
- '''
- Test case for salt.beacons.adb
- '''
- def setup_loader_modules(self):
- return {
- adb: {
- 'last_state': {},
- 'last_state_extra': {'no_devices': False}
- }
- }
- def test_no_adb_command(self):
- with patch('salt.utils.path.which') as mock:
- mock.return_value = None
- ret = adb.__virtual__()
- mock.assert_called_once_with('adb')
- self.assertFalse(ret)
- def test_with_adb_command(self):
- with patch('salt.utils.path.which') as mock:
- mock.return_value = '/usr/bin/adb'
- ret = adb.__virtual__()
- mock.assert_called_once_with('adb')
- self.assertEqual(ret, 'adb')
- def test_non_list_config(self):
- config = {}
- ret = adb.validate(config)
- self.assertEqual(ret, (False, 'Configuration for adb beacon must'
- ' be a list.'))
- def test_empty_config(self):
- config = [{}]
- ret = adb.validate(config)
- self.assertEqual(ret, (False, 'Configuration for adb beacon must'
- ' include a states array.'))
- def test_invalid_states(self):
- config = [{'states': ['Random', 'Failings']}]
- ret = adb.validate(config)
- self.assertEqual(ret, (False, 'Need a one of the following'
- ' adb states: offline, bootloader,'
- ' device, host, recovery, no'
- ' permissions, sideload,'
- ' unauthorized, unknown, missing'))
- def test_device_state(self):
- config = [{'states': ['device']}]
- mock = Mock(return_value='List of devices attached\nHTC\tdevice',)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC',
- 'state': 'device',
- 'tag': 'device'}])
- def test_device_state_change(self):
- config = [{'states': ['offline']}]
- out = [
- 'List of devices attached\nHTC\tdevice',
- 'List of devices attached\nHTC\toffline'
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [])
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC',
- 'state': 'offline',
- 'tag': 'offline'}])
- def test_multiple_devices(self):
- config = [{'states': ['offline', 'device']}]
- out = [
- 'List of devices attached\nHTC\tdevice',
- 'List of devices attached\nHTC\toffline\nNexus\tdevice'
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC',
- 'state': 'device',
- 'tag': 'device'}])
- ret = adb.beacon(config)
- self.assertEqual(ret, [
- {'device': 'HTC', 'state': 'offline', 'tag': 'offline'},
- {'device': 'Nexus', 'state': 'device', 'tag': 'device'}
- ])
- def test_no_devices_with_different_states(self):
- config = [{'states': ['offline'], 'no_devices_event': True}]
- mock = Mock(return_value='List of devices attached\nHTC\tdevice')
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [])
- def test_no_devices_no_repeat(self):
- 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.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- 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 = [
- 'List of devices attached',
- 'List of devices attached'
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'tag': 'no_devices'}])
- ret = adb.beacon(config)
- self.assertEqual(ret, [])
- def test_device_missing(self):
- config = [{'states': ['device', 'missing']}]
- out = [
- '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)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC',
- 'state': 'device',
- 'tag': 'device'}])
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC',
- 'state': 'missing',
- 'tag': 'missing'}])
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC',
- 'state': 'device',
- 'tag': 'device'}])
- ret = adb.beacon(config)
- self.assertEqual(ret, [])
- def test_with_startup(self):
- config = [{'states': ['device']}]
- mock = Mock(return_value='* daemon started successfully *\nList of devices attached\nHTC\tdevice',)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC',
- 'state': 'device',
- 'tag': 'device'}])
- def test_with_user(self):
- config = [{'states': ['device'], 'user': 'fred'}]
- mock = Mock(return_value='* daemon started successfully *\nList of devices attached\nHTC\tdevice')
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- mock.assert_called_once_with('adb devices', runas='fred')
- self.assertEqual(ret, [{'device': 'HTC',
- 'state': 'device',
- 'tag': 'device'}])
- def test_device_low_battery(self):
- config = [{'states': ['device'], 'battery_low': 30}]
- out = [
- 'List of devices attached\nHTC\tdevice',
- '25',
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
- {'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
- def test_device_no_repeat(self):
- config = [{'states': ['device'], 'battery_low': 30}]
- out = [
- 'List of devices attached\nHTC\tdevice',
- '25',
- 'List of devices attached\nHTC\tdevice',
- '25'
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
- {'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
- ret = adb.beacon(config)
- self.assertEqual(ret, [])
- def test_device_no_repeat_capacity_increase(self):
- config = [{'states': ['device'], 'battery_low': 75}]
- out = [
- 'List of devices attached\nHTC\tdevice',
- '25',
- 'List of devices attached\nHTC\tdevice',
- '30'
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
- {'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):
- config = [{'states': ['offline'], 'battery_low': 30}]
- out = [
- 'List of devices attached\nHTC\tdevice',
- '25',
- 'List of devices attached\nHTC\tdevice',
- '25'
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
- ret = adb.beacon(config)
- self.assertEqual(ret, [])
- def test_device_battery_charged(self):
- config = [{'states': ['device'], 'battery_low': 30}]
- out = [
- 'List of devices attached\nHTC\tdevice',
- '100',
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC',
- 'state': 'device',
- 'tag': 'device'}])
- def test_device_low_battery_equal(self):
- config = [{'states': ['device'], 'battery_low': 25}]
- out = [
- 'List of devices attached\nHTC\tdevice',
- '25',
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
- {'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
- def test_device_battery_not_found(self):
- config = [{'states': ['device'], 'battery_low': 25}]
- out = [
- '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)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
- def test_device_repeat_multi(self):
- config = [{'states': ['offline'], 'battery_low': 35}]
- out = [
- '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)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
- ret = adb.beacon(config)
- self.assertEqual(ret, [])
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
- ret = adb.beacon(config)
- self.assertEqual(ret, [])
- def test_weird_batteries(self):
- config = [{'states': ['device'], 'battery_low': 25}]
- out = [
- 'List of devices attached\nHTC\tdevice',
- '-9000',
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
- def test_multiple_batteries(self):
- config = [{'states': ['device'], 'battery_low': 30}]
- out = [
- 'List of devices attached\nHTC\tdevice',
- '25\n40',
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
- {'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
- def test_multiple_low_batteries(self):
- config = [{'states': ['device'], 'battery_low': 30}]
- out = [
- 'List of devices attached\nHTC\tdevice',
- '25\n14',
- ]
- mock = Mock(side_effect=out)
- with patch.dict(adb.__salt__, {'cmd.run': mock}):
- ret = adb.validate(config)
- self.assertEqual(ret, (True, 'Valid beacon configuration'))
- ret = adb.beacon(config)
- self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'},
- {'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
|