Skip to content

Commit 04d6a02

Browse files
committed
🐛 fix: add run_async behaviour for device
1 parent 8f8d563 commit 04d6a02

File tree

6 files changed

+48
-7
lines changed

6 files changed

+48
-7
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ sdk_test/package_info.json
1515
config.json
1616
venv/
1717
htmlcov/
18+
ignore
19+
.vscode

rapyuta_io/clients/device.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,9 @@ def execute_command(self, command, retry_limit=0):
568568
if response.status_code == requests.codes.BAD_REQUEST:
569569
raise ParameterMissingException(get_error(response.text))
570570
execution_result = get_api_response_data(response)
571-
if not command.bg:
572-
return execution_result[self.uuid]
571+
if not command.run_async:
572+
return execution_result
573+
573574
jid = execution_result.get('jid')
574575
if not jid:
575576
raise ValueError("Job ID not found in the response")

rapyuta_io/clients/device_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def execute_command(
178178

179179
execution_result = get_api_response_data(response)
180180

181-
if not command.bg:
181+
if not command.run_async:
182182
return execution_result
183183

184184
jid = execution_result.get('jid')

rapyuta_io/clients/model.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Command(ObjDict):
5252
:ivar shell: Represents the shell where it is going to execute
5353
:ivar env: List of environment variables.
5454
:ivar bg: Boolean value specifying whether the execution runs on the background or not
55+
:ivar run_async: Boolean value to specify to run command in async mode i.e., to run command and return its jid (not depending on completion of the command).
5556
:ivar runas: Run the command as a specific user
5657
:ivar cmd: Command to execute on the device
5758
:ivar pwd: Present working directory
@@ -61,14 +62,15 @@ class Command(ObjDict):
6162
6263
"""
6364

64-
def __init__(self, cmd, shell=None, env=None, bg=False, runas=None, pwd=None, cwd=None, timeout=300):
65+
def __init__(self, cmd, shell=None, env=None, bg=False, run_async=False, runas=None, pwd=None, cwd=None, timeout=300):
6566
super(ObjDict, self).__init__()
6667
if env is None:
6768
env = dict()
6869
self.cmd = cmd
6970
self.shell = shell
7071
self.env = env
7172
self.bg = bg
73+
self.run_async = run_async
7274
self.runas = runas
7375
self.cwd = pwd
7476
if cwd is not None:
@@ -83,6 +85,8 @@ def validate(self):
8385
raise InvalidCommandException("Invalid shell")
8486
if self.bg is not None and not isinstance(self.bg, bool):
8587
raise InvalidCommandException("Invalid background option")
88+
if self.run_async is not None and not isinstance(self.run_async, bool):
89+
raise InvalidCommandException("Invalid run asynchronous option")
8690
if self.runas is not None and not isinstance(self.runas, six.string_types):
8791
raise InvalidCommandException("Invalid runas option")
8892
if self.cwd is not None and not isinstance(self.cwd, six.string_types):

tests/device_test.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from rapyuta_io.utils import ObjDict, InvalidCommandException
2222
from tests.utils.client import get_client, headers
2323
from tests.utils.device_respones import DEVICE_LIST, DEVICE_INFO, DEVICE_LIST_EMPTY, \
24-
DEVICE_NOT_FOUND, EXECUTE_COMMAND_BAD_REQUEST, EXECUTE_COMMAND_OK, DELETE_DEVICE_BAD_REQUEST, \
24+
DEVICE_NOT_FOUND, EXECUTE_COMMAND_BAD_REQUEST, EXECUTE_COMMAND_OK, EXECUTE_COMMAND_OK_BG, DELETE_DEVICE_BAD_REQUEST, \
2525
DELETE_DEVICE_OK, UPDATE_DEVICE_BAD_REQUEST, UPDATE_DEVICE_OK, DEVICE_SELECTION, APPLY_PARAMETERS_SUCCESS_RESPONSE, \
2626
CREATE_DIRECT_LINK_SUCCESS_RESPONSE, CREATE_DOCKERCOMPOSE_DEVICE_SUCCESS, GET_DOCKERCOMPOSE_DEVICE_SUCCESS, \
2727
CREATE_PREINSTALLED_DEVICE_SUCCESS, GET_PREINSTALLED_DEVICE_SUCCESS, UPGRADE_DOCKERCOMPOSE_DEVICE_SUCCESS, \
@@ -276,8 +276,31 @@ def test_execute_command_ok(self, mock_execute, get_device_response):
276276
command.cwd = ''
277277
result = device.execute_command(command)
278278
self.assertEqual(mock_execute.call_count, 2)
279-
expected = 'Linux rapyuta 4.9.80-v7+ #1098 SMP Fri Mar 9 19:11:42 GMT2018 armv7l ' \
280-
'armv7l armv7l GNU/Linux'
279+
expected = {'test_device_id':'Linux rapyuta 4.9.80-v7+ #1098 SMP Fri Mar 9 19:11:42 GMT2018 armv7l ' \
280+
'armv7l armv7l GNU/Linux'}
281+
self.assertEqual(result, expected)
282+
283+
@patch('requests.Response', spec=Response)
284+
@patch('rapyuta_io.utils.rest_client.RestClient.execute')
285+
def test_execute_command_ok_background(self, mock_execute, get_device_response):
286+
get_device_response.text = DEVICE_INFO
287+
get_device_response.status_code = requests.codes.OK
288+
execute_command_response = get_device_response()
289+
execute_command_response.text = EXECUTE_COMMAND_OK_BG
290+
execute_command_response.status_code = requests.codes.OK
291+
mock_execute.side_effect = [get_device_response, execute_command_response]
292+
mock_execute.return_value = get_device_response
293+
client = get_client()
294+
device = client.get_device('test_device_id')
295+
self.assertIsInstance(device, Device, 'Object should be an instance of class Device')
296+
command = Command('uname -a')
297+
command.shell = '/bin/bash'
298+
command.bg = True
299+
command.runas = 'root'
300+
command.cwd = ''
301+
result = device.execute_command(command)
302+
self.assertEqual(mock_execute.call_count, 2)
303+
expected = {'test_device_id':'SUCCESS'}
281304
self.assertEqual(result, expected)
282305

283306
def test_execute_command_invalid_parameters(self):
@@ -300,6 +323,12 @@ def test_execute_command_invalid_parameters(self):
300323
'value': 'true',
301324
'correct_value': False
302325
},
326+
{
327+
'response': 'Invalid run asynchronous option',
328+
'key': 'run_async',
329+
'value': 'true',
330+
'correct_value': False
331+
},
303332
{
304333
'response': 'Invalid runas option',
305334
'key': 'runas',

tests/utils/device_respones.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,11 @@
523523
"data": { "test_device_id": "Linux rapyuta 4.9.80-v7+ #1098 SMP Fri Mar 9\
524524
19:11:42 GMT2018 armv7l armv7l armv7l GNU/Linux" } } } '''
525525

526+
EXECUTE_COMMAND_OK_BG = ''' {
527+
"status": "success",
528+
"response": {
529+
"data": { "test_device_id": "SUCCESS" } } } '''
530+
526531
EXECUTE_COMMAND_BAD_REQUEST = ''' {
527532
"status": "error",
528533
"response": { "data": {}, "error": "device_ids parameter missing" } } '''

0 commit comments

Comments
 (0)