2015-10-31 20:06:58 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
# Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# Salt testing libs
|
2017-02-27 13:58:07 +00:00
|
|
|
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
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-02-27 15:59:04 +00:00
|
|
|
# Salt libs
|
2017-02-19 15:35:30 +00:00
|
|
|
import salt.beacons.adb as adb
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 15:35:30 +00:00
|
|
|
class ADBBeaconTestCase(TestCase, LoaderModuleMockMixin):
|
2015-10-31 20:06:58 +00:00
|
|
|
'''
|
|
|
|
Test case for salt.beacons.adb
|
|
|
|
'''
|
2017-02-19 15:35:30 +00:00
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {
|
|
|
|
adb: {
|
|
|
|
'last_state': {},
|
|
|
|
'last_state_extra': {'no_devices': False}
|
|
|
|
}
|
|
|
|
}
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
def test_no_adb_command(self):
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
with patch('salt.utils.path.which') as mock:
|
2015-10-31 20:06:58 +00:00
|
|
|
mock.return_value = None
|
|
|
|
|
|
|
|
ret = adb.__virtual__()
|
|
|
|
|
2015-11-30 19:19:13 +00:00
|
|
|
mock.assert_called_once_with('adb')
|
2015-10-31 20:06:58 +00:00
|
|
|
self.assertFalse(ret)
|
|
|
|
|
|
|
|
def test_with_adb_command(self):
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
with patch('salt.utils.path.which') as mock:
|
2015-11-30 19:19:13 +00:00
|
|
|
mock.return_value = '/usr/bin/adb'
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
ret = adb.__virtual__()
|
|
|
|
|
2015-11-30 19:19:13 +00:00
|
|
|
mock.assert_called_once_with('adb')
|
2015-10-31 20:06:58 +00:00
|
|
|
self.assertEqual(ret, 'adb')
|
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
def test_non_list_config(self):
|
|
|
|
config = {}
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, (False, 'Configuration for adb beacon must'
|
|
|
|
' be a list.'))
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
def test_empty_config(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{}]
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, (False, 'Configuration for adb beacon must'
|
|
|
|
' include a states array.'))
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
def test_invalid_states(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['Random', 'Failings']}]
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, (False, 'Need a one of the following'
|
|
|
|
' adb states: offline, bootloader,'
|
|
|
|
' device, host, recovery, no'
|
|
|
|
' permissions, sideload,'
|
|
|
|
' unauthorized, unknown, missing'))
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
def test_device_state(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device']}]
|
2015-10-31 20:06:58 +00:00
|
|
|
|
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}):
|
2017-06-22 18:38:02 +00:00
|
|
|
|
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-10-31 20:06:58 +00:00
|
|
|
ret = adb.beacon(config)
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC',
|
|
|
|
'state': 'device',
|
|
|
|
'tag': 'device'}])
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
def test_device_state_change(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['offline']}]
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'List of devices attached\nHTC\toffline'
|
2015-10-31 20:06:58 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-10-31 20:06:58 +00:00
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
|
|
|
|
ret = adb.beacon(config)
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC',
|
|
|
|
'state': 'offline',
|
|
|
|
'tag': 'offline'}])
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
def test_multiple_devices(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['offline', 'device']}]
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'List of devices attached\nHTC\toffline\nNexus\tdevice'
|
2015-10-31 20:06:58 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-10-31 20:06:58 +00:00
|
|
|
ret = adb.beacon(config)
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC',
|
|
|
|
'state': 'device',
|
|
|
|
'tag': 'device'}])
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
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'}
|
2015-10-31 20:06:58 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
def test_no_devices_with_different_states(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['offline'], 'no_devices_event': True}]
|
2015-10-31 20:06:58 +00:00
|
|
|
|
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}):
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-10-31 20:06:58 +00:00
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
|
|
|
|
def test_no_devices_no_repeat(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['offline', 'device'], 'no_devices_event': True}]
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-02-13 12:33:52 +00:00
|
|
|
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}):
|
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2017-02-13 12:33:52 +00:00
|
|
|
ret = adb.beacon(config)
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC',
|
|
|
|
'state': 'device',
|
|
|
|
'tag': 'device'}])
|
2017-02-13 12:33:52 +00:00
|
|
|
|
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [{'tag': 'no_devices'}])
|
|
|
|
|
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
|
|
|
|
def test_no_devices(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['offline', 'device'], 'no_devices_event': True}]
|
2017-02-13 12:33:52 +00:00
|
|
|
|
2015-10-31 20:06:58 +00:00
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached',
|
|
|
|
'List of devices attached'
|
2015-10-31 20:06:58 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-10-31 20:06:58 +00:00
|
|
|
ret = adb.beacon(config)
|
2015-11-30 19:19:13 +00:00
|
|
|
self.assertEqual(ret, [{'tag': 'no_devices'}])
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
|
|
|
|
def test_device_missing(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device', 'missing']}]
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
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'
|
2015-10-31 20:06:58 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2015-10-31 20:06:58 +00:00
|
|
|
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-10-31 20:06:58 +00:00
|
|
|
ret = adb.beacon(config)
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC',
|
|
|
|
'state': 'device',
|
|
|
|
'tag': 'device'}])
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
ret = adb.beacon(config)
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC',
|
|
|
|
'state': 'missing',
|
|
|
|
'tag': 'missing'}])
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
ret = adb.beacon(config)
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC',
|
|
|
|
'state': 'device',
|
|
|
|
'tag': 'device'}])
|
2015-10-31 20:06:58 +00:00
|
|
|
|
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
2015-10-31 20:56:59 +00:00
|
|
|
|
|
|
|
def test_with_startup(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device']}]
|
2015-10-31 20:56:59 +00:00
|
|
|
|
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}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-10-31 20:56:59 +00:00
|
|
|
ret = adb.beacon(config)
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC',
|
|
|
|
'state': 'device',
|
|
|
|
'tag': 'device'}])
|
2015-10-31 22:09:08 +00:00
|
|
|
|
|
|
|
def test_with_user(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device'], 'user': 'fred'}]
|
2015-10-31 22:09:08 +00:00
|
|
|
|
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}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-10-31 22:09:08 +00:00
|
|
|
ret = adb.beacon(config)
|
2015-11-30 19:19:13 +00:00
|
|
|
mock.assert_called_once_with('adb devices', runas='fred')
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC',
|
|
|
|
'state': 'device',
|
|
|
|
'tag': 'device'}])
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
def test_device_low_battery(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device'], 'battery_low': 30}]
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'25',
|
2015-11-27 13:03:05 +00:00
|
|
|
]
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-11-27 13:03:05 +00:00
|
|
|
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'}])
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
def test_device_no_repeat(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device'], 'battery_low': 30}]
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'25',
|
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'25'
|
2015-11-27 13:03:05 +00:00
|
|
|
]
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-11-27 13:03:05 +00:00
|
|
|
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'}])
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
|
|
|
|
def test_device_no_repeat_capacity_increase(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device'], 'battery_low': 75}]
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'25',
|
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'30'
|
2015-11-27 13:03:05 +00:00
|
|
|
]
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-11-27 13:03:05 +00:00
|
|
|
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'}])
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
|
|
|
|
def test_device_no_repeat_with_not_found_state(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['offline'], 'battery_low': 30}]
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'25',
|
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'25'
|
2015-11-27 13:03:05 +00:00
|
|
|
]
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-11-27 13:03:05 +00:00
|
|
|
ret = adb.beacon(config)
|
2015-11-30 19:19:13 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
|
|
|
|
def test_device_battery_charged(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device'], 'battery_low': 30}]
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'100',
|
2015-11-27 13:03:05 +00:00
|
|
|
]
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-11-27 13:03:05 +00:00
|
|
|
ret = adb.beacon(config)
|
2017-06-22 18:38:02 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC',
|
|
|
|
'state': 'device',
|
|
|
|
'tag': 'device'}])
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
def test_device_low_battery_equal(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device'], 'battery_low': 25}]
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'25',
|
2015-11-27 13:03:05 +00:00
|
|
|
]
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-11-27 13:03:05 +00:00
|
|
|
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'}])
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
def test_device_battery_not_found(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device'], 'battery_low': 25}]
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
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',
|
2015-11-27 13:03:05 +00:00
|
|
|
]
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-11-27 13:03:05 +00:00
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
|
|
|
|
|
|
|
|
def test_device_repeat_multi(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['offline'], 'battery_low': 35}]
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
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'
|
2015-11-27 13:03:05 +00:00
|
|
|
]
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-11-27 13:03:05 +00:00
|
|
|
ret = adb.beacon(config)
|
2015-11-30 19:19:13 +00:00
|
|
|
self.assertEqual(ret, [{'device': 'HTC', 'battery_level': 25, 'tag': 'battery_low'}])
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
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'}])
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
|
|
|
|
def test_weird_batteries(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device'], 'battery_low': 25}]
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'-9000',
|
2015-11-27 13:03:05 +00:00
|
|
|
]
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-11-27 13:03:05 +00:00
|
|
|
ret = adb.beacon(config)
|
|
|
|
self.assertEqual(ret, [{'device': 'HTC', 'state': 'device', 'tag': 'device'}])
|
|
|
|
|
|
|
|
def test_multiple_batteries(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device'], 'battery_low': 30}]
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'25\n40',
|
2015-11-27 13:03:05 +00:00
|
|
|
]
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-11-27 13:03:05 +00:00
|
|
|
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'}])
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
def test_multiple_low_batteries(self):
|
2017-06-22 18:38:02 +00:00
|
|
|
config = [{'states': ['device'], 'battery_low': 30}]
|
2015-11-27 13:03:05 +00:00
|
|
|
|
|
|
|
out = [
|
2015-11-30 19:19:13 +00:00
|
|
|
'List of devices attached\nHTC\tdevice',
|
|
|
|
'25\n14',
|
2015-11-27 13:03:05 +00:00
|
|
|
]
|
|
|
|
mock = Mock(side_effect=out)
|
2015-11-30 19:19:13 +00:00
|
|
|
with patch.dict(adb.__salt__, {'cmd.run': mock}):
|
2017-06-22 18:38:02 +00:00
|
|
|
ret = adb.validate(config)
|
|
|
|
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
|
|
|
|
2015-11-27 13:03:05 +00:00
|
|
|
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'}])
|