-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from QualiSystems/feature/borismod_40_save_vnic
Feature/borismod 40 save vnic connect #40
- Loading branch information
Showing
12 changed files
with
145 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,42 @@ | ||
from re import search | ||
import jsonpickle | ||
|
||
COMMAND_RESULT_PREFIX = "command_json_result=" | ||
COMMAND_RESULT_POSTFIX = "=command_json_result_end" | ||
|
||
|
||
def get_result_from_command_output(output): | ||
match = search(COMMAND_RESULT_PREFIX + '(?P<result>.*)' + COMMAND_RESULT_POSTFIX, output) | ||
""" | ||
Extracts result from output | ||
:param output: Console output as returned from command execution | ||
:return: Deserialized object or deserialized JSON into dictionary | ||
""" | ||
match = _extract_result_from_output(output) | ||
if not match: | ||
return None | ||
return match.group('result') | ||
json = match.group('result') | ||
return jsonpickle.decode(json) | ||
|
||
|
||
def set_command_result(result, unpicklable=False): | ||
""" | ||
Serializes output as JSON and writes it to console output wrapped with special prefix and suffix | ||
:param result: Result to return | ||
:param unpicklable: If True adds JSON can be deserialized as real object. | ||
When False will be deserialized as dictionary | ||
""" | ||
json = jsonpickle.encode(result, unpicklable=unpicklable) | ||
result_for_output = COMMAND_RESULT_PREFIX + str(json) + COMMAND_RESULT_POSTFIX | ||
print result_for_output | ||
return result_for_output | ||
|
||
|
||
def set_command_result(output): | ||
print COMMAND_RESULT_PREFIX + str(output) + COMMAND_RESULT_POSTFIX | ||
def transfer_command_result(output): | ||
match = _extract_result_from_output(output) | ||
if match: | ||
print COMMAND_RESULT_PREFIX + match.group('result') + COMMAND_RESULT_POSTFIX | ||
|
||
|
||
def _extract_result_from_output(output): | ||
match = search(COMMAND_RESULT_PREFIX + '(?P<result>.*)' + COMMAND_RESULT_POSTFIX, output) | ||
return match |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class ConnectionResult(object): | ||
def __init__(self, mac_address, vm_uuid, network_name): | ||
self.mac_address = mac_address | ||
self.vm_uuid = vm_uuid | ||
self.network_name = network_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,50 @@ | ||
from unittest import TestCase | ||
from common.utilites.command_result import get_result_from_command_output | ||
from common.utilites.command_result import get_result_from_command_output, set_command_result | ||
from models.ConnectionResult import ConnectionResult | ||
|
||
|
||
class TestCommandResult(TestCase): | ||
|
||
def test_command_result_with_result(self): | ||
result = get_result_from_command_output('command_json_result=MY RESULT=command_json_result_end') | ||
self.assertEqual(result, 'MY RESULT') | ||
def test_get_result_from_command_output_with_result(self): | ||
result = get_result_from_command_output('command_json_result={"result":"MY RESULT"}=command_json_result_end') | ||
self.assertEqual(result["result"], 'MY RESULT') | ||
|
||
def test_command_result_empty(self): | ||
result = get_result_from_command_output('') | ||
self.assertEqual(result, None) | ||
|
||
def test_get_result_from_command_output_with_result_unpickable_true(self): | ||
|
||
connection_result = ConnectionResult(mac_address='AA', vm_uuid='BB', network_name='CC') | ||
output_result = set_command_result(result=connection_result, unpicklable=True) | ||
result = get_result_from_command_output(output_result) | ||
|
||
self.assertEqual(result.mac_address, 'AA') | ||
self.assertEqual(result.vm_uuid, 'BB') | ||
self.assertEqual(result.network_name, 'CC') | ||
|
||
def test_get_result_from_command_output_with_result_unpickable_false(self): | ||
|
||
connection_result = ConnectionResult(mac_address='AA', vm_uuid='BB', network_name='CC') | ||
output_result = set_command_result(result=[connection_result], unpicklable=False) | ||
results = get_result_from_command_output(output_result) | ||
|
||
self.assertEqual(results[0]['mac_address'], 'AA') | ||
self.assertEqual(results[0]['vm_uuid'], 'BB') | ||
self.assertEqual(results[0]['network_name'], 'CC') | ||
|
||
def test_get_result_from_command_output_with_result(self): | ||
|
||
output_result = 'command_json_result=[{"py/object": "models.ConnectionResult.ConnectionResult", "vm_uuid": "422258ab-47e9-d57c-3741-6832a432bc3a", "network_name": "QualiSB/anetwork", "mac_address": "00:50:56:a2:23:76"}]=command_json_result_end' | ||
results = get_result_from_command_output(output_result) | ||
|
||
self.assertEqual(results[0].mac_address, '00:50:56:a2:23:76') | ||
|
||
def test_get_result_from_command_output_with_result_unpickable_false(self): | ||
|
||
output_result = 'command_json_result=[{"vm_uuid": "422258c6-15d0-0646-d5e7-f2cb411eee94", "network_name": "QualiSB/anetwork", "mac_address": "00:50:56:a2:6c:04"}]=command_json_result_end' | ||
results = get_result_from_command_output(output_result) | ||
|
||
for result in results: | ||
v = dict(result) | ||
self.assertEqual(result['mac_address'], '00:50:56:a2:6c:04') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import unittest | ||
from common.utilites.common_utils import get_object_as_string | ||
|
||
|
||
class TestCommonUtils(unittest.TestCase): | ||
def test_get_object_as_string_list(self): | ||
|
||
result = get_object_as_string(['value']) | ||
|
||
self.assertEqual(result, 'value') | ||
|
||
def test_get_object_as_string_string(self): | ||
|
||
result = get_object_as_string('value') | ||
|
||
self.assertEqual(result, 'value') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters