File: test_utils.py

package info (click to toggle)
python-zunclient 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,052 kB
  • sloc: python: 10,127; sh: 110; makefile: 25
file content (263 lines) | stat: -rw-r--r-- 8,962 bytes parent folder | download | duplicates (4)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#
# Copyright 2013 OpenStack LLC.
# All Rights Reserved.
#
#    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.

import collections

from zunclient.common import cliutils
from zunclient.common import utils
from zunclient import exceptions as exc
from zunclient.tests.unit import utils as test_utils


class CommonFiltersTest(test_utils.BaseTestCase):
    def test_limit(self):
        result = utils.common_filters(limit=42)
        self.assertEqual(['limit=42'], result)

    def test_limit_0(self):
        result = utils.common_filters(limit=0)
        self.assertEqual(['limit=0'], result)

    def test_limit_negative_number(self):
        result = utils.common_filters(limit=-2)
        self.assertEqual(['limit=-2'], result)

    def test_other(self):
        for key in ('marker', 'sort_key', 'sort_dir'):
            result = utils.common_filters(**{key: 'test'})
            self.assertEqual(['%s=test' % key], result)


class SplitAndDeserializeTest(test_utils.BaseTestCase):

    def test_split_and_deserialize(self):
        ret = utils.split_and_deserialize('str=foo')
        self.assertEqual(('str', 'foo'), ret)

        ret = utils.split_and_deserialize('int=1')
        self.assertEqual(('int', 1), ret)

        ret = utils.split_and_deserialize('bool=false')
        self.assertEqual(('bool', False), ret)

        ret = utils.split_and_deserialize('list=[1, "foo", 2]')
        self.assertEqual(('list', [1, "foo", 2]), ret)

        ret = utils.split_and_deserialize('dict={"foo": 1}')
        self.assertEqual(('dict', {"foo": 1}), ret)

        ret = utils.split_and_deserialize('str_int="1"')
        self.assertEqual(('str_int', "1"), ret)

    def test_split_and_deserialize_fail(self):
        self.assertRaises(exc.CommandError,
                          utils.split_and_deserialize, 'foo:bar')


class ArgsArrayToPatchTest(test_utils.BaseTestCase):

    def test_args_array_to_patch(self):
        my_args = {
            'attributes': ['str=foo', 'int=1', 'bool=true',
                           'list=[1, 2, 3]', 'dict={"foo": "bar"}'],
        }
        patch = utils.args_array_to_patch(my_args['attributes'])
        self.assertEqual([{'str': 'foo'},
                          {'int': 1},
                          {'bool': True},
                          {'list': [1, 2, 3]},
                          {'dict': {"foo": "bar"}}], patch)


class FormatArgsTest(test_utils.BaseTestCase):

    def test_format_args_none(self):
        self.assertEqual({}, utils.format_args(None))

    def test_format_args(self):
        li = utils.format_args([
            'K1=V1,K2=V2,'
            'K3=V3,K4=V4,'
            'K5=V5'])
        self.assertEqual({'K1': 'V1',
                          'K2': 'V2',
                          'K3': 'V3',
                          'K4': 'V4',
                          'K5': 'V5'
                          }, li)

    def test_format_args_semicolon(self):
        li = utils.format_args([
            'K1=V1;K2=V2;'
            'K3=V3;K4=V4;'
            'K5=V5'])
        self.assertEqual({'K1': 'V1',
                          'K2': 'V2',
                          'K3': 'V3',
                          'K4': 'V4',
                          'K5': 'V5'
                          }, li)

    def test_format_args_mix_commas_semicolon(self):
        li = utils.format_args([
            'K1=V1,K2=V2,'
            'K3=V3;K4=V4,'
            'K5=V5'])
        self.assertEqual({'K1': 'V1',
                          'K2': 'V2',
                          'K3': 'V3',
                          'K4': 'V4',
                          'K5': 'V5'
                          }, li)

    def test_format_args_split(self):
        li = utils.format_args([
            'K1=V1,'
            'K2=V22222222222222222222222222222'
            '222222222222222222222222222,'
            'K3=3.3.3.3'])
        self.assertEqual({'K1': 'V1',
                          'K2': 'V22222222222222222222222222222'
                          '222222222222222222222222222',
                          'K3': '3.3.3.3'}, li)

    def test_format_args_multiple(self):
        li = utils.format_args([
            'K1=V1',
            'K2=V22222222222222222222222222222'
            '222222222222222222222222222',
            'K3=3.3.3.3'])
        self.assertEqual({'K1': 'V1',
                          'K2': 'V22222222222222222222222222222'
                          '222222222222222222222222222',
                          'K3': '3.3.3.3'}, li)

    def test_format_args_multiple_colon_values(self):
        li = utils.format_args([
            'K1=V1',
            'K2=V2,V22,V222,V2222',
            'K3=3.3.3.3'])
        self.assertEqual({'K1': 'V1',
                          'K2': 'V2,V22,V222,V2222',
                          'K3': '3.3.3.3'}, li)

    def test_format_args_parse_comma_false(self):
        li = utils.format_args(
            ['K1=V1,K2=2.2.2.2,K=V'],
            parse_comma=False)
        self.assertEqual({'K1': 'V1,K2=2.2.2.2,K=V'}, li)

    def test_format_args_multiple_values_per_args(self):
        li = utils.format_args([
            'K1=V1',
            'K1=V2'])
        self.assertIn('K1', li)
        self.assertIn('V1', li['K1'])
        self.assertIn('V2', li['K1'])

    def test_format_args_bad_arg(self):
        args = ['K1=V1,K22.2.2.2']
        ex = self.assertRaises(exc.CommandError,
                               utils.format_args, args)
        self.assertEqual('arguments must be a list of KEY=VALUE '
                         'not K22.2.2.2', str(ex))

    def test_format_multiple_bad_args(self):
        args = ['K1=V1', 'K22.2.2.2']
        ex = self.assertRaises(exc.CommandError,
                               utils.format_args, args)
        self.assertEqual('arguments must be a list of KEY=VALUE '
                         'not K22.2.2.2', str(ex))


class CliUtilsTest(test_utils.BaseTestCase):

    def test_keys_and_vals_to_strs(self):
        dict_in = {'a': '1',
                   'b': {
                       'x': 1,
                       'y': '2',
                       'z': '3'},
                   'c': 7}

        dict_exp = collections.OrderedDict([
            ('a', '1'),
            ('b', collections.OrderedDict([
                ('x', 1),
                ('y', '2'),
                ('z', '3')])),
            ('c', 7)])

        dict_out = cliutils.keys_and_vals_to_strs(dict_in)
        dict_act = collections.OrderedDict([
            ('a', dict_out['a']),
            ('b', collections.OrderedDict(sorted(dict_out['b'].items()))),
            ('c', dict_out['c'])])

        self.assertEqual(str(dict_exp), str(dict_act))


class ParseNetsTest(test_utils.BaseTestCase):

    def test_no_nets(self):
        nets = []
        result = utils.parse_nets(nets)
        self.assertEqual([], result)

    def test_nets_with_network(self):
        nets = [' network = 1234567 , v4-fixed-ip = 172.17.0.3 ']
        result = utils.parse_nets(nets)
        self.assertEqual([{'network': '1234567', 'v4-fixed-ip': '172.17.0.3'}],
                         result)

    def test_nets_with_port(self):
        nets = ['port=1234567, v6-fixed-ip=2001:db8::2']
        result = utils.parse_nets(nets)
        self.assertEqual([{'port': '1234567', 'v6-fixed-ip': '2001:db8::2'}],
                         result)

    def test_nets_with_only_ip(self):
        nets = ['v4-fixed-ip = 172.17.0.3']
        self.assertRaises(exc.CommandError,
                          utils.parse_nets, nets)

    def test_nets_with_both_network_port(self):
        nets = ['port=1234567, network=2345678, v4-fixed-ip=172.17.0.3']
        self.assertRaises(exc.CommandError,
                          utils.parse_nets, nets)

    def test_nets_with_invalid_ip(self):
        nets = ['network=1234567, v4-fixed-ip=23.555.567,789']
        self.assertRaises(exc.CommandError,
                          utils.parse_nets, nets)


class ParseCommandTest(test_utils.BaseTestCase):

    def test_no_command(self):
        command = []
        result = utils.parse_command(command)
        self.assertEqual('', result)

    def test_command_ls(self):
        command = ['ls', '-al']
        result = utils.parse_command(command)
        self.assertEqual('"ls" "-al"', result)

    def test_command_echo_hello(self):
        command = ['sh', '-c', 'echo hello']
        result = utils.parse_command(command)
        self.assertEqual('"sh" "-c" "echo hello"', result)