summaryrefslogtreecommitdiff
path: root/heatclient/tests/unit/test_format_utils.py
blob: 4f149fba3a5b236c6e5c45808eed02926bfdbf4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#   Licensed under the Apache License, Version 2.0 (the "License"); you may
#   not use this file except in compliance with the License. You may obtain
#   a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.
#
#   Copyright 2015 IBM Corp.

import io
import json
import yaml

from heatclient.common import format_utils
from heatclient.tests.unit.osc import utils


columns = ['col1', 'col2', 'col3']
data = ['abcde', ['fg', 'hi', 'jk'], {'lmnop': 'qrstu'}]


class ShowJson(format_utils.JsonFormat):
    def take_action(self, parsed_args):
        return columns, data


class ShowYaml(format_utils.YamlFormat):
    def take_action(self, parsed_args):
        return columns, data


class ShowShell(format_utils.ShellFormat):
    def take_action(self, parsed_args):
        return columns, data


class ShowValue(format_utils.ValueFormat):
    def take_action(self, parsed_args):
        return columns, data


class TestFormats(utils.TestCommand):

    def test_json_format(self):
        self.cmd = ShowJson(self.app, None)
        parsed_args = self.check_parser(self.cmd, [], [])
        expected = json.dumps(dict(zip(columns, data)), indent=2)

        self.cmd.run(parsed_args)

        self.assertEqual(expected, self.app.stdout.make_string().rstrip())

    def test_yaml_format(self):
        self.cmd = ShowYaml(self.app, None)
        parsed_args = self.check_parser(self.cmd, [], [])
        expected = yaml.safe_dump(dict(zip(columns, data)),
                                  default_flow_style=False)

        self.cmd.run(parsed_args)

        self.assertEqual(expected, self.app.stdout.make_string())

    def test_shell_format(self):
        self.cmd = ShowShell(self.app, None)
        parsed_args = self.check_parser(self.cmd, [], [])
        expected = '''\
col1="abcde"
col2="['fg', 'hi', 'jk']"
col3="{'lmnop': 'qrstu'}"
'''

        self.cmd.run(parsed_args)

        self.assertEqual(expected, self.app.stdout.make_string())

    def test_value_format(self):
        self.cmd = ShowValue(self.app, None)
        parsed_args = self.check_parser(self.cmd, [], [])
        expected = '''\
abcde
['fg', 'hi', 'jk']
{'lmnop': 'qrstu'}
'''

        self.cmd.run(parsed_args)

        self.assertEqual(expected, self.app.stdout.make_string())

    def test_indent_and_truncate(self):
        self.assertIsNone(format_utils.indent_and_truncate(None))
        self.assertIsNone(format_utils.indent_and_truncate(None,
                                                           truncate=True))
        self.assertEqual(
            '',
            format_utils.indent_and_truncate(''))
        self.assertEqual(
            'one',
            format_utils.indent_and_truncate('one'))
        self.assertIsNone(format_utils.indent_and_truncate(None, spaces=2))
        self.assertEqual(
            '',
            format_utils.indent_and_truncate('', spaces=2))
        self.assertEqual(
            '  one',
            format_utils.indent_and_truncate('one', spaces=2))
        self.assertEqual(
            'one\ntwo\nthree\nfour\nfive',
            format_utils.indent_and_truncate('one\ntwo\nthree\nfour\nfive'))
        self.assertEqual(
            'three\nfour\nfive',
            format_utils.indent_and_truncate(
                'one\ntwo\nthree\nfour\nfive',
                truncate=True,
                truncate_limit=3))
        self.assertEqual(
            '  and so on\n  three\n  four\n  five\n  truncated',
            format_utils.indent_and_truncate(
                'one\ntwo\nthree\nfour\nfive',
                spaces=2,
                truncate=True,
                truncate_limit=3,
                truncate_prefix='and so on',
                truncate_postfix='truncated'))

    def test_print_software_deployment_output(self):
        out = io.StringIO()
        format_utils.print_software_deployment_output(
            {'deploy_stdout': ''}, out=out, name='deploy_stdout')
        self.assertEqual(
            '  deploy_stdout: |\n\n',
            out.getvalue())
        ov = {'deploy_stdout': '', 'deploy_stderr': '1\n2\n3\n4\n5\n6\n7\n8\n9'
                                                    '\n10\n11',
              'deploy_status_code': 0}
        out = io.StringIO()
        format_utils.print_software_deployment_output(ov, out=out,
                                                      name='deploy_stderr')
        self.assertEqual(
            u'''\
  deploy_stderr: |
    ...
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    (truncated, view all with --long)
''', out.getvalue())
        out = io.StringIO()
        format_utils.print_software_deployment_output(ov, out=out,
                                                      name='deploy_stderr',
                                                      long=True)
        self.assertEqual(
            u'''\
  deploy_stderr: |
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
''', out.getvalue())