| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import io |
| 16 |
import json |
| 17 |
import yaml |
| 18 |
|
| 19 |
from heatclient.common import format_utils |
| 20 |
from heatclient.tests.unit.osc import utils |
| 21 |
|
| 22 |
|
| 23 |
columns = ['col1', 'col2', 'col3'] |
| 24 |
data = ['abcde', ['fg', 'hi', 'jk'], {'lmnop': 'qrstu'}] |
| 25 |
|
| 26 |
|
| 27 |
class ShowJson(format_utils.JsonFormat): |
| 28 |
def take_action(self, parsed_args): |
| 29 |
return columns, data |
| 30 |
|
| 31 |
|
| 32 |
class ShowYaml(format_utils.YamlFormat): |
| 33 |
def take_action(self, parsed_args): |
| 34 |
return columns, data |
| 35 |
|
| 36 |
|
| 37 |
class ShowShell(format_utils.ShellFormat): |
| 38 |
def take_action(self, parsed_args): |
| 39 |
return columns, data |
| 40 |
|
| 41 |
|
| 42 |
class ShowValue(format_utils.ValueFormat): |
| 43 |
def take_action(self, parsed_args): |
| 44 |
return columns, data |
| 45 |
|
| 46 |
|
| 47 |
class TestFormats(utils.TestCommand): |
| 48 |
|
| 49 |
def test_json_format(self): |
| 50 |
self.cmd = ShowJson(self.app, None) |
| 51 |
parsed_args = self.check_parser(self.cmd, [], []) |
| 52 |
expected = json.dumps(dict(zip(columns, data)), indent=2) |
| 53 |
|
| 54 |
self.cmd.run(parsed_args) |
| 55 |
|
| 56 |
self.assertEqual(expected, self.app.stdout.make_string().rstrip()) |
| 57 |
|
| 58 |
def test_yaml_format(self): |
| 59 |
self.cmd = ShowYaml(self.app, None) |
| 60 |
parsed_args = self.check_parser(self.cmd, [], []) |
| 61 |
expected = yaml.safe_dump(dict(zip(columns, data)), |
| 62 |
default_flow_style=False) |
| 63 |
|
| 64 |
self.cmd.run(parsed_args) |
| 65 |
|
| 66 |
self.assertEqual(expected, self.app.stdout.make_string()) |
| 67 |
|
| 68 |
def test_shell_format(self): |
| 69 |
self.cmd = ShowShell(self.app, None) |
| 70 |
parsed_args = self.check_parser(self.cmd, [], []) |
| 71 |
expected = '''\ |
| 72 |
col1="abcde" |
| 73 |
col2="['fg', 'hi', 'jk']" |
| 74 |
col3="{'lmnop': 'qrstu'}" |
| 75 |
''' |
| 76 |
|
| 77 |
self.cmd.run(parsed_args) |
| 78 |
|
| 79 |
self.assertEqual(expected, self.app.stdout.make_string()) |
| 80 |
|
| 81 |
def test_value_format(self): |
| 82 |
self.cmd = ShowValue(self.app, None) |
| 83 |
parsed_args = self.check_parser(self.cmd, [], []) |
| 84 |
expected = '''\ |
| 85 |
abcde |
| 86 |
['fg', 'hi', 'jk'] |
| 87 |
{'lmnop': 'qrstu'} |
| 88 |
''' |
| 89 |
|
| 90 |
self.cmd.run(parsed_args) |
| 91 |
|
| 92 |
self.assertEqual(expected, self.app.stdout.make_string()) |
| 93 |
|
| 94 |
def test_indent_and_truncate(self): |
| 95 |
self.assertIsNone(format_utils.indent_and_truncate(None)) |
| 96 |
self.assertIsNone(format_utils.indent_and_truncate(None, |
| 97 |
truncate=True)) |
| 98 |
self.assertEqual( |
| 99 |
'', |
| 100 |
format_utils.indent_and_truncate('')) |
| 101 |
self.assertEqual( |
| 102 |
'one', |
| 103 |
format_utils.indent_and_truncate('one')) |
| 104 |
self.assertIsNone(format_utils.indent_and_truncate(None, spaces=2)) |
| 105 |
self.assertEqual( |
| 106 |
'', |
| 107 |
format_utils.indent_and_truncate('', spaces=2)) |
| 108 |
self.assertEqual( |
| 109 |
' one', |
| 110 |
format_utils.indent_and_truncate('one', spaces=2)) |
| 111 |
self.assertEqual( |
| 112 |
'one\ntwo\nthree\nfour\nfive', |
| 113 |
format_utils.indent_and_truncate('one\ntwo\nthree\nfour\nfive')) |
| 114 |
self.assertEqual( |
| 115 |
'three\nfour\nfive', |
| 116 |
format_utils.indent_and_truncate( |
| 117 |
'one\ntwo\nthree\nfour\nfive', |
| 118 |
truncate=True, |
| 119 |
truncate_limit=3)) |
| 120 |
self.assertEqual( |
| 121 |
' and so on\n three\n four\n five\n truncated', |
| 122 |
format_utils.indent_and_truncate( |
| 123 |
'one\ntwo\nthree\nfour\nfive', |
| 124 |
spaces=2, |
| 125 |
truncate=True, |
| 126 |
truncate_limit=3, |
| 127 |
truncate_prefix='and so on', |
| 128 |
truncate_postfix='truncated')) |
| 129 |
|
| 130 |
def test_print_software_deployment_output(self): |
| 131 |
out = io.StringIO() |
| 132 |
format_utils.print_software_deployment_output( |
| 133 |
{'deploy_stdout': ''}, out=out, name='deploy_stdout') |
| 134 |
self.assertEqual( |
| 135 |
' deploy_stdout: |\n\n', |
| 136 |
out.getvalue()) |
| 137 |
ov = {'deploy_stdout': '', 'deploy_stderr': '1\n2\n3\n4\n5\n6\n7\n8\n9' |
| 138 |
'\n10\n11', |
| 139 |
'deploy_status_code': 0} |
| 140 |
out = io.StringIO() |
| 141 |
format_utils.print_software_deployment_output(ov, out=out, |
| 142 |
name='deploy_stderr') |
| 143 |
self.assertEqual( |
| 144 |
'''\ |
| 145 |
deploy_stderr: | |
| 146 |
... |
| 147 |
2 |
| 148 |
3 |
| 149 |
4 |
| 150 |
5 |
| 151 |
6 |
| 152 |
7 |
| 153 |
8 |
| 154 |
9 |
| 155 |
10 |
| 156 |
11 |
| 157 |
(truncated, view all with --long) |
| 158 |
''', out.getvalue()) |
| 159 |
out = io.StringIO() |
| 160 |
format_utils.print_software_deployment_output(ov, out=out, |
| 161 |
name='deploy_stderr', |
| 162 |
long=True) |
| 163 |
self.assertEqual( |
| 164 |
'''\ |
| 165 |
deploy_stderr: | |
| 166 |
1 |
| 167 |
2 |
| 168 |
3 |
| 169 |
4 |
| 170 |
5 |
| 171 |
6 |
| 172 |
7 |
| 173 |
8 |
| 174 |
9 |
| 175 |
10 |
| 176 |
11 |
| 177 |
''', out.getvalue()) |