Move func that parse table-like outputs

This patch moves nova_stdout_parser into helpers to be able to re-use it
for another commands.

Change-Id: Id98194487efd8808931a6f3b6b1b05864d4cdb5f
This commit is contained in:
Ilya Kharin 2016-09-22 01:02:05 +03:00
parent ecbbec0248
commit 75c9cdb464
5 changed files with 69 additions and 71 deletions

View File

@ -14,15 +14,15 @@ from cliff import command as cmd
from fuelclient import objects
from octane.util import env as env_util
from octane.util import helpers
from octane.util import node as node_util
from octane.util import nova
def clean_services_for_node(controller, node):
services_stdout = node_util.run_with_openrc(
["nova", "service-list", "--host", node.data['hostname']],
controller)
services = nova.nova_stdout_parser(services_stdout)
services = helpers.parse_table_output(services_stdout)
for service in services:
node_util.run_with_openrc(
["nova", "service-delete", service['Id']], controller,

View File

@ -118,3 +118,49 @@ def test_get_parameters(mocker, source, parameters_to_get, parameters):
result = helpers.get_parameters(mock_fp, parameters_to_get)
mock_iter.assert_called_once_with(mock_fp)
assert result == parameters
@pytest.mark.parametrize("cmd_output, expected_result", [
(
"""+--------------------------------------+--------------------+
| ID | Name |
+--------------------------------------+--------------------+
| 85cfb077-3397-405e-ae61-dfce35d3073a | test_boot_volume_2 |
+--------------------------------------+--------------------+""",
[
{
"ID": "85cfb077-3397-405e-ae61-dfce35d3073a",
"Name": "test_boot_volume_2",
}
]
),
(
"""
+------+-------------+
| ID | Name |
+------+-------------+
| id_1 | test_name_1 |
| id_2 | test_name_2 |
+------+-------------+
""",
[
{
"ID": "id_1",
"Name": "test_name_1",
},
{
"ID": "id_2",
"Name": "test_name_2",
}
]
),
(
"""+--------------------------------------+--------------------+
| ID | Name |
+--------------------------------------+--------------------+
+--------------------------------------+--------------------+""",
[]
),
])
def test_parse_table_output(cmd_output, expected_result):
assert expected_result == helpers.parse_table_output(cmd_output)

View File

@ -70,52 +70,6 @@ def test_is_there_nova_instances_exists_in_status(
"--limit", "1", "--minimal"], controller)
@pytest.mark.parametrize("cmd_output, expected_result", [
(
"""+--------------------------------------+--------------------+
| ID | Name |
+--------------------------------------+--------------------+
| 85cfb077-3397-405e-ae61-dfce35d3073a | test_boot_volume_2 |
+--------------------------------------+--------------------+""",
[
{
"ID": "85cfb077-3397-405e-ae61-dfce35d3073a",
"Name": "test_boot_volume_2",
}
]
),
(
"""
+------+-------------+
| ID | Name |
+------+-------------+
| id_1 | test_name_1 |
| id_2 | test_name_2 |
+------+-------------+
""",
[
{
"ID": "id_1",
"Name": "test_name_1",
},
{
"ID": "id_2",
"Name": "test_name_2",
}
]
),
(
"""+--------------------------------------+--------------------+
| ID | Name |
+--------------------------------------+--------------------+
+--------------------------------------+--------------------+""",
[]
),
])
def test_nova_stdout_parser(cmd_output, expected_result):
assert expected_result == nova.nova_stdout_parser(cmd_output)
@pytest.mark.parametrize("node_fqdn", ["fqdn"])
@pytest.mark.parametrize("state", ["ACTIVE", "MIGRATING"])
@pytest.mark.parametrize("delay", [100])

View File

@ -70,3 +70,20 @@ def normalized_cliff_show_json(data):
if isinstance(data, list):
return {i['Field']: i['Value'] for i in data}
return data
def parse_table_output(output):
"""Parse table-like outputs of commands."""
results = []
headers = None
for line in output.splitlines():
line = line.strip()
if not line or line[0] == '+':
continue
cols = line.strip("|").split("|")
cols = [c.strip() for c in cols]
if headers is None:
headers = cols
else:
results.append(dict(zip(headers, cols)))
return results

View File

@ -14,6 +14,7 @@ import logging
import time
from octane import magic_consts
from octane.util import helpers
from octane.util import node as node_util
LOG = logging.getLogger(__name__)
@ -30,32 +31,12 @@ class WaiterException(Exception):
super(Exception, self).__init__(msg)
def nova_stdout_parser(cmd_stdout):
"""Parse nova cmd stdout
Return list of dicts ther keys are the header of the cmd out table.
"""
results = []
headers = None
for line in cmd_stdout.splitlines():
line = line.strip()
if not line or line[0] == '+':
continue
cols = line.strip("|").split("|")
cols = [c.strip() for c in cols]
if headers is None:
headers = cols
else:
results.append(dict(zip(headers, cols)))
return results
def do_nova_instances_exist(controller, node_fqdn, status=None):
cmd = ['nova', 'list', '--host', node_fqdn, '--limit', '1', '--minimal']
if status:
cmd += ['--status', status]
result = node_util.run_with_openrc(cmd, controller)
return bool(nova_stdout_parser(result))
return bool(helpers.parse_table_output(result))
def waiting_for_status_completed(controller, node_fqdn, status,
@ -76,7 +57,7 @@ def get_compute_lists(controller):
"""return tuple of lists enabled and disabled computes"""
service_stdout = node_util.run_with_openrc(
["nova", "service-list", "--binary", "nova-compute"], controller)
parsed_service_list = nova_stdout_parser(service_stdout)
parsed_service_list = helpers.parse_table_output(service_stdout)
enabled_computes = []
disabled_computes = []
for service in parsed_service_list:
@ -95,7 +76,7 @@ def get_active_instances(controller, node_fqdn):
"--status", "ACTIVE",
"--minimal"],
controller)
instances = nova_stdout_parser(instances_stdout)
instances = helpers.parse_table_output(instances_stdout)
return [i["ID"] for i in instances]