|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +import re |
| 14 | +import shlex |
| 15 | +import subprocess |
| 16 | +import testtools |
| 17 | + |
| 18 | +import six |
| 19 | + |
| 20 | +from functional.common import exceptions |
| 21 | + |
| 22 | + |
| 23 | +def execute(cmd, action, flags='', params='', fail_ok=False, |
| 24 | + merge_stderr=False): |
| 25 | + """Executes specified command for the given action.""" |
| 26 | + cmd = ' '.join([cmd, flags, action, params]) |
| 27 | + cmd = shlex.split(cmd.encode('utf-8')) |
| 28 | + result = '' |
| 29 | + result_err = '' |
| 30 | + stdout = subprocess.PIPE |
| 31 | + stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE |
| 32 | + proc = subprocess.Popen(cmd, stdout=stdout, stderr=stderr) |
| 33 | + result, result_err = proc.communicate() |
| 34 | + if not fail_ok and proc.returncode != 0: |
| 35 | + raise exceptions.CommandFailed(proc.returncode, cmd, result, |
| 36 | + result_err) |
| 37 | + return result |
| 38 | + |
| 39 | + |
| 40 | +class TestCase(testtools.TestCase): |
| 41 | + |
| 42 | + delimiter_line = re.compile('^\+\-[\+\-]+\-\+$') |
| 43 | + |
| 44 | + def openstack(self, action, flags='', params='', fail_ok=False): |
| 45 | + """Executes openstackclient command for the given action.""" |
| 46 | + return execute('openstack', action, flags, params, fail_ok) |
| 47 | + |
| 48 | + def assert_table_structure(self, items, field_names): |
| 49 | + """Verify that all items have keys listed in field_names.""" |
| 50 | + for item in items: |
| 51 | + for field in field_names: |
| 52 | + self.assertIn(field, item) |
| 53 | + |
| 54 | + def assert_show_fields(self, items, field_names): |
| 55 | + """Verify that all items have keys listed in field_names.""" |
| 56 | + for item in items: |
| 57 | + for key in six.iterkeys(item): |
| 58 | + self.assertIn(key, field_names) |
| 59 | + |
| 60 | + def parse_show(self, raw_output): |
| 61 | + """Return list of dicts with item values parsed from cli output.""" |
| 62 | + |
| 63 | + items = [] |
| 64 | + table_ = self.table(raw_output) |
| 65 | + for row in table_['values']: |
| 66 | + item = {} |
| 67 | + item[row[0]] = row[1] |
| 68 | + items.append(item) |
| 69 | + return items |
| 70 | + |
| 71 | + def parse_listing(self, raw_output): |
| 72 | + """Return list of dicts with basic item parsed from cli output.""" |
| 73 | + |
| 74 | + items = [] |
| 75 | + table_ = self.table(raw_output) |
| 76 | + for row in table_['values']: |
| 77 | + item = {} |
| 78 | + for col_idx, col_key in enumerate(table_['headers']): |
| 79 | + item[col_key] = row[col_idx] |
| 80 | + items.append(item) |
| 81 | + return items |
| 82 | + |
| 83 | + def table(self, output_lines): |
| 84 | + """Parse single table from cli output. |
| 85 | +
|
| 86 | + Return dict with list of column names in 'headers' key and |
| 87 | + rows in 'values' key. |
| 88 | + """ |
| 89 | + table_ = {'headers': [], 'values': []} |
| 90 | + columns = None |
| 91 | + |
| 92 | + if not isinstance(output_lines, list): |
| 93 | + output_lines = output_lines.split('\n') |
| 94 | + |
| 95 | + if not output_lines[-1]: |
| 96 | + # skip last line if empty (just newline at the end) |
| 97 | + output_lines = output_lines[:-1] |
| 98 | + |
| 99 | + for line in output_lines: |
| 100 | + if self.delimiter_line.match(line): |
| 101 | + columns = self._table_columns(line) |
| 102 | + continue |
| 103 | + if '|' not in line: |
| 104 | + continue |
| 105 | + row = [] |
| 106 | + for col in columns: |
| 107 | + row.append(line[col[0]:col[1]].strip()) |
| 108 | + if table_['headers']: |
| 109 | + table_['values'].append(row) |
| 110 | + else: |
| 111 | + table_['headers'] = row |
| 112 | + |
| 113 | + return table_ |
| 114 | + |
| 115 | + def _table_columns(self, first_table_row): |
| 116 | + """Find column ranges in output line. |
| 117 | +
|
| 118 | + Return list of tuples (start,end) for each column |
| 119 | + detected by plus (+) characters in delimiter line. |
| 120 | + """ |
| 121 | + positions = [] |
| 122 | + start = 1 # there is '+' at 0 |
| 123 | + while start < len(first_table_row): |
| 124 | + end = first_table_row.find('+', start) |
| 125 | + if end == -1: |
| 126 | + break |
| 127 | + positions.append((start, end)) |
| 128 | + start = end + 1 |
| 129 | + return positions |
0 commit comments