summaryrefslogtreecommitdiff
path: root/cinderclient/tests/unit/test_shell.py
blob: 48086897d8fbb05a5fe00bda218fbfd2fdc233de (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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# 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 argparse
import io
import json
import re
import sys
from unittest import mock

import ddt
import fixtures
import keystoneauth1.exceptions as ks_exc
from keystoneauth1.exceptions import DiscoveryFailure
from keystoneauth1.identity.generic.password import Password as ks_password
from keystoneauth1 import session
import requests_mock
from testtools import matchers

import cinderclient
from cinderclient import api_versions
from cinderclient.contrib import noauth
from cinderclient import exceptions
from cinderclient import shell
from cinderclient.tests.unit import fake_actions_module
from cinderclient.tests.unit.fixture_data import keystone_client
from cinderclient.tests.unit import utils
from cinderclient.tests.unit.v3 import fakes


@ddt.ddt
class ShellTest(utils.TestCase):

    FAKE_ENV = {
        'OS_USERNAME': 'username',
        'OS_PASSWORD': 'password',
        'OS_PROJECT_NAME': 'tenant_name',
        'OS_AUTH_URL': 'http://no.where/v2.0',
    }

    # Patch os.environ to avoid required auth info.
    def make_env(self, exclude=None, include=None):
        env = dict((k, v) for k, v in self.FAKE_ENV.items() if k != exclude)
        env.update(include or {})
        self.useFixture(fixtures.MonkeyPatch('os.environ', env))

    def setUp(self):
        super(ShellTest, self).setUp()
        for var in self.FAKE_ENV:
            self.useFixture(fixtures.EnvironmentVariable(var,
                                                         self.FAKE_ENV[var]))

        self.mock_completion()

    def shell(self, argstr):
        orig = sys.stdout
        try:
            sys.stdout = io.StringIO()
            _shell = shell.OpenStackCinderShell()
            _shell.main(argstr.split())
        except SystemExit:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            self.assertEqual(0, exc_value.code)
        finally:
            out = sys.stdout.getvalue()
            sys.stdout.close()
            sys.stdout = orig

        return out

    def test_default_auth_env(self):
        _shell = shell.OpenStackCinderShell()
        args, __ = _shell.get_base_parser().parse_known_args([])
        self.assertEqual('', args.os_auth_type)

    def test_auth_type_env(self):
        self.make_env(exclude='OS_PASSWORD',
                      include={'OS_AUTH_SYSTEM': 'non existent auth',
                               'OS_AUTH_TYPE': 'noauth'})
        _shell = shell.OpenStackCinderShell()
        args, __ = _shell.get_base_parser().parse_known_args([])
        self.assertEqual('noauth', args.os_auth_type)

    def test_auth_system_env(self):
        self.make_env(exclude='OS_PASSWORD',
                      include={'OS_AUTH_SYSTEM': 'noauth'})
        _shell = shell.OpenStackCinderShell()
        args, __ = _shell.get_base_parser().parse_known_args([])
        self.assertEqual('noauth', args.os_auth_type)

    @mock.patch.object(cinderclient.shell.OpenStackCinderShell,
                       '_get_keystone_session')
    @mock.patch.object(cinderclient.client.SessionClient, 'authenticate',
                       side_effect=RuntimeError())
    def test_password_auth_type(self, mock_authenticate,
                                mock_get_session):
        self.make_env(include={'OS_AUTH_TYPE': 'password'})
        _shell = shell.OpenStackCinderShell()

        # We crash the command after Client instantiation because this test
        # focuses only keystoneauth1 indentity cli opts parsing.
        self.assertRaises(RuntimeError, _shell.main, ['list'])
        self.assertIsInstance(_shell.cs.client.session.auth,
                              ks_password)

    def test_help_unknown_command(self):
        self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo')

    def test_help(self):
        # Some expected help output, including microversioned commands
        required = [
            r'.*?^usage: ',
            r'.*?^\s+create\s+Creates a volume.',
            r'.*?^\s+summary\s+Get volumes summary.',
            r'.*?^Run "cinder help SUBCOMMAND" for help on a subcommand.',
        ]
        help_text = self.shell('help')
        for r in required:
            self.assertThat(help_text,
                            matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))

    def test_help_on_subcommand(self):
        required = [
            r'.*?^usage: cinder list',
            r'.*?^Lists all volumes.',
        ]
        help_text = self.shell('help list')
        for r in required:
            self.assertThat(help_text,
                            matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))

    def test_help_on_subcommand_mv(self):
        required = [
            r'.*?^usage: cinder summary',
            r'.*?^Get volumes summary.',
        ]
        help_text = self.shell('help summary')
        for r in required:
            self.assertThat(help_text,
                            matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))

    def test_help_arg_no_subcommand(self):
        required = [
            r'.*?^usage: ',
            r'.*?^\s+create\s+Creates a volume.',
            r'.*?^\s+summary\s+Get volumes summary.',
            r'.*?^Run "cinder help SUBCOMMAND" for help on a subcommand.',
        ]
        help_text = self.shell('--os-volume-api-version 3.40')
        for r in required:
            self.assertThat(help_text,
                            matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))

    @ddt.data('backup-create --help', '--help backup-create')
    def test_dash_dash_help_on_subcommand(self, cmd):
        required = ['.*?^Creates a volume backup.']
        help_text = self.shell(cmd)

        for r in required:
            self.assertThat(help_text,
                            matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))

    def register_keystone_auth_fixture(self, mocker, url):
        mocker.register_uri('GET', url,
                            text=keystone_client.keystone_request_callback)

    @requests_mock.Mocker()
    def test_version_discovery(self, mocker):
        _shell = shell.OpenStackCinderShell()
        sess = session.Session()

        os_auth_url = "https://wrongdiscoveryresponse.discovery.com:35357/v2.0"
        self.register_keystone_auth_fixture(mocker, os_auth_url)

        self.assertRaises(DiscoveryFailure,
                          _shell._discover_auth_versions,
                          sess,
                          auth_url=os_auth_url)

        os_auth_url = "https://DiscoveryNotSupported.discovery.com:35357/v2.0"
        self.register_keystone_auth_fixture(mocker, os_auth_url)
        v2_url, v3_url = _shell._discover_auth_versions(sess,
                                                        auth_url=os_auth_url)
        self.assertEqual(os_auth_url, v2_url, "Expected v2 url")
        self.assertIsNone(v3_url, "Expected no v3 url")

        os_auth_url = "https://DiscoveryNotSupported.discovery.com:35357/v3.0"
        self.register_keystone_auth_fixture(mocker, os_auth_url)
        v2_url, v3_url = _shell._discover_auth_versions(sess,
                                                        auth_url=os_auth_url)
        self.assertEqual(os_auth_url, v3_url, "Expected v3 url")
        self.assertIsNone(v2_url, "Expected no v2 url")

    @requests_mock.Mocker()
    def list_volumes_on_service(self, count, mocker):
        os_auth_url = "http://multiple.service.names/v2.0"
        mocker.register_uri('POST', os_auth_url + "/tokens",
                            text=keystone_client.keystone_request_callback)
        # microversion support requires us to make a versions request
        # to the endpoint to see exactly what is supported by the server
        mocker.register_uri('GET',
                            "http://cinder%i.api.com/"
                            % count, text=json.dumps(fakes.fake_request_get()))
        mocker.register_uri('GET',
                            "http://cinder%i.api.com/v3/volumes/detail"
                            % count, text='{"volumes": []}')
        self.make_env(include={'OS_AUTH_URL': os_auth_url,
                               'CINDER_SERVICE_NAME': 'cinder%i' % count})
        _shell = shell.OpenStackCinderShell()
        _shell.main(['list'])

    def test_duplicate_filters(self):
        _shell = shell.OpenStackCinderShell()
        self.assertRaises(exceptions.CommandError,
                          _shell.main,
                          ['list', '--name', 'abc', '--filters', 'name=xyz'])

    def test_cinder_service_name(self):
        # Failing with 'No mock address' means we are not
        # choosing the correct endpoint
        for count in range(1, 4):
            self.list_volumes_on_service(count)

    @mock.patch('keystoneauth1.identity.v2.Password')
    @mock.patch('keystoneauth1.adapter.Adapter.get_token',
                side_effect=ks_exc.ConnectFailure())
    @mock.patch('keystoneauth1.discover.Discover',
                side_effect=ks_exc.ConnectFailure())
    @mock.patch('sys.stdin', side_effect=mock.Mock)
    @mock.patch('getpass.getpass', return_value='password')
    def test_password_prompted(self, mock_getpass, mock_stdin, mock_discover,
                               mock_token, mock_password):
        self.make_env(exclude='OS_PASSWORD')
        _shell = shell.OpenStackCinderShell()
        self.assertRaises(ks_exc.ConnectFailure, _shell.main, ['list'])
        mock_getpass.assert_called_with('OS Password: ')
        # Verify that Password() is called with value of param 'password'
        # equal to mock_getpass.return_value.
        mock_password.assert_called_with(
            self.FAKE_ENV['OS_AUTH_URL'],
            password=mock_getpass.return_value,
            tenant_id='',
            tenant_name=self.FAKE_ENV['OS_PROJECT_NAME'],
            username=self.FAKE_ENV['OS_USERNAME'])

    @mock.patch('cinderclient.api_versions.discover_version',
                return_value=api_versions.APIVersion("3.0"))
    @requests_mock.Mocker()
    def test_noauth_plugin(self, mock_disco, mocker):
        # just to prove i'm not crazy about the mock parameter ordering
        self.assertTrue(requests_mock.mocker.Mocker, type(mocker))

        os_volume_url = "http://example.com/volumes/v3"
        mocker.register_uri('GET',
                            "%s/volumes/detail"
                            % os_volume_url, text='{"volumes": []}')
        _shell = shell.OpenStackCinderShell()
        args = ['--os-endpoint', os_volume_url,
                '--os-auth-type', 'noauth', '--os-user-id',
                'admin', '--os-project-id', 'admin', 'list']
        _shell.main(args)
        self.assertIsInstance(_shell.cs.client.session.auth,
                              noauth.CinderNoAuthPlugin)

    @mock.patch.object(cinderclient.client.HTTPClient, 'authenticate',
                       side_effect=exceptions.Unauthorized('No'))
    # Easiest way to make cinderclient use httpclient is a None session
    @mock.patch.object(cinderclient.shell.OpenStackCinderShell,
                       '_get_keystone_session', return_value=None)
    def test_http_client_insecure(self, mock_authenticate, mock_session):
        self.make_env(include={'CINDERCLIENT_INSECURE': True})

        _shell = shell.OpenStackCinderShell()

        # This "fails" but instantiates the client.
        self.assertRaises(exceptions.CommandError, _shell.main, ['list'])

        self.assertEqual(False, _shell.cs.client.verify_cert)

    @mock.patch.object(cinderclient.client.SessionClient, 'authenticate',
                       side_effect=exceptions.Unauthorized('No'))
    def test_session_client_debug_logger(self, mock_session):
        _shell = shell.OpenStackCinderShell()
        # This "fails" but instantiates the client.
        self.assertRaises(exceptions.CommandError, _shell.main,
                          ['--debug', 'list'])
        # In case of SessionClient when --debug switch is specified
        # 'keystoneauth' logger should be initialized.
        self.assertEqual('keystoneauth', _shell.cs.client.logger.name)

    @mock.patch('keystoneauth1.session.Session.__init__',
                side_effect=RuntimeError())
    def test_http_client_with_cert(self, mock_session):
        _shell = shell.OpenStackCinderShell()

        # We crash the command after Session instantiation because this test
        # focuses only on arguments provided to Session.__init__
        args = '--os-cert', 'minnie', 'list'
        self.assertRaises(RuntimeError, _shell.main, args)
        mock_session.assert_called_once_with(cert='minnie', verify=mock.ANY)

    @mock.patch('keystoneauth1.session.Session.__init__',
                side_effect=RuntimeError())
    def test_http_client_with_cert_and_key(self, mock_session):
        _shell = shell.OpenStackCinderShell()

        # We crash the command after Session instantiation because this test
        # focuses only on arguments provided to Session.__init__
        args = '--os-cert', 'minnie', '--os-key', 'mickey', 'list'
        self.assertRaises(RuntimeError, _shell.main, args)
        mock_session.assert_called_once_with(cert=('minnie', 'mickey'),
                                             verify=mock.ANY)


class CinderClientArgumentParserTest(utils.TestCase):

    def setUp(self):
        super(CinderClientArgumentParserTest, self).setUp()

        self.mock_completion()

    def test_ambiguity_solved_for_one_visible_argument(self):
        parser = shell.CinderClientArgumentParser(add_help=False)
        parser.add_argument('--test-parameter',
                            dest='visible_param',
                            action='store_true')
        parser.add_argument('--test_parameter',
                            dest='hidden_param',
                            action='store_true',
                            help=argparse.SUPPRESS)

        opts = parser.parse_args(['--test'])

        # visible argument must be set
        self.assertTrue(opts.visible_param)
        self.assertFalse(opts.hidden_param)

    def test_raise_ambiguity_error_two_visible_argument(self):
        parser = shell.CinderClientArgumentParser(add_help=False)
        parser.add_argument('--test-parameter',
                            dest="visible_param1",
                            action='store_true')
        parser.add_argument('--test_parameter',
                            dest="visible_param2",
                            action='store_true')

        self.assertRaises(SystemExit, parser.parse_args, ['--test'])

    def test_raise_ambiguity_error_two_hidden_argument(self):
        parser = shell.CinderClientArgumentParser(add_help=False)
        parser.add_argument('--test-parameter',
                            dest="hidden_param1",
                            action='store_true',
                            help=argparse.SUPPRESS)
        parser.add_argument('--test_parameter',
                            dest="hidden_param2",
                            action='store_true',
                            help=argparse.SUPPRESS)

        self.assertRaises(SystemExit, parser.parse_args, ['--test'])


class TestLoadVersionedActions(utils.TestCase):
    def setUp(self):
        super(TestLoadVersionedActions, self).setUp()

        self.mock_completion()

    def test_load_versioned_actions_v3_0(self):
        parser = cinderclient.shell.CinderClientArgumentParser()
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        shell._find_actions(subparsers, fake_actions_module,
                            api_versions.APIVersion("3.0"), False, [])
        self.assertIn('fake-action', shell.subcommands.keys())
        self.assertEqual(
            "fake_action 3.0 to 3.1",
            shell.subcommands['fake-action'].get_default('func')())

    def test_load_versioned_actions_v3_2(self):
        parser = cinderclient.shell.CinderClientArgumentParser()
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        shell._find_actions(subparsers, fake_actions_module,
                            api_versions.APIVersion("3.2"), False, [])
        self.assertIn('fake-action', shell.subcommands.keys())
        self.assertEqual(
            "fake_action 3.2 to 3.3",
            shell.subcommands['fake-action'].get_default('func')())

        self.assertIn('fake-action2', shell.subcommands.keys())
        self.assertEqual(
            "fake_action2",
            shell.subcommands['fake-action2'].get_default('func')())

    def test_load_versioned_actions_not_in_version_range(self):
        parser = cinderclient.shell.CinderClientArgumentParser()
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        shell._find_actions(subparsers, fake_actions_module,
                            api_versions.APIVersion('3.10000'), False, [])
        self.assertNotIn('fake-action', shell.subcommands.keys())
        self.assertIn('fake-action2', shell.subcommands.keys())

    def test_load_versioned_actions_unsupported_input(self):
        parser = cinderclient.shell.CinderClientArgumentParser()
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        self.assertRaises(exceptions.UnsupportedAttribute,
                          shell._find_actions, subparsers, fake_actions_module,
                          api_versions.APIVersion('3.6'), False,
                          ['another-fake-action', '--foo'])

    def test_load_versioned_actions_with_help(self):
        parser = cinderclient.shell.CinderClientArgumentParser()
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        with mock.patch.object(subparsers, 'add_parser') as mock_add_parser:
            shell._find_actions(subparsers, fake_actions_module,
                                api_versions.APIVersion("3.1"), True, [])
            self.assertIn('fake-action', shell.subcommands.keys())
            expected_help = ("help message (Supported by API versions "
                             "%(start)s - %(end)s)") % {
                'start': '3.0', 'end': '3.3'}
            expected_desc = ("help message\n\n    "
                            "This will not show up in help message\n    ")
            mock_add_parser.assert_any_call(
                'fake-action',
                help=expected_help,
                description=expected_desc,
                add_help=False,
                formatter_class=cinderclient.shell.OpenStackHelpFormatter)

    def test_load_versioned_actions_with_help_on_latest(self):
        parser = cinderclient.shell.CinderClientArgumentParser()
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        with mock.patch.object(subparsers, 'add_parser') as mock_add_parser:
            shell._find_actions(subparsers, fake_actions_module,
                                api_versions.APIVersion("3.latest"), True, [])
            self.assertIn('another-fake-action', shell.subcommands.keys())
            expected_help = (" (Supported by API versions %(start)s - "
                             "%(end)s)%(hint)s") % {
                'start': '3.6', 'end': '3.latest',
                'hint': cinderclient.shell.HINT_HELP_MSG}
            mock_add_parser.assert_any_call(
                'another-fake-action',
                help=expected_help,
                description='',
                add_help=False,
                formatter_class=cinderclient.shell.OpenStackHelpFormatter)

    @mock.patch.object(cinderclient.shell.CinderClientArgumentParser,
                       'add_argument')
    def test_load_versioned_actions_with_args(self, mock_add_arg):
        parser = cinderclient.shell.CinderClientArgumentParser(add_help=False)
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        shell._find_actions(subparsers, fake_actions_module,
                            api_versions.APIVersion("3.1"), False, [])
        self.assertIn('fake-action2', shell.subcommands.keys())
        mock_add_arg.assert_has_calls([
            mock.call('-h', '--help', action='help', help='==SUPPRESS=='),
            mock.call('--foo')])

    @mock.patch.object(cinderclient.shell.CinderClientArgumentParser,
                       'add_argument')
    def test_load_versioned_actions_with_args2(self, mock_add_arg):
        parser = cinderclient.shell.CinderClientArgumentParser(add_help=False)
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        shell._find_actions(subparsers, fake_actions_module,
                            api_versions.APIVersion("3.4"), False, [])
        self.assertIn('fake-action2', shell.subcommands.keys())
        mock_add_arg.assert_has_calls([
            mock.call('-h', '--help', action='help', help='==SUPPRESS=='),
            mock.call('--bar', help="bar help")])

    @mock.patch.object(cinderclient.shell.CinderClientArgumentParser,
                       'add_argument')
    def test_load_versioned_actions_with_args_not_in_version_range(
            self, mock_add_arg):
        parser = cinderclient.shell.CinderClientArgumentParser(add_help=False)
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        shell._find_actions(subparsers, fake_actions_module,
                            api_versions.APIVersion("3.10000"), False, [])
        self.assertIn('fake-action2', shell.subcommands.keys())
        mock_add_arg.assert_has_calls([
            mock.call('-h', '--help', action='help', help='==SUPPRESS==')])

    @mock.patch.object(cinderclient.shell.CinderClientArgumentParser,
                       'add_argument')
    def test_load_versioned_actions_with_args_and_help(self, mock_add_arg):
        parser = cinderclient.shell.CinderClientArgumentParser(add_help=False)
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        shell._find_actions(subparsers, fake_actions_module,
                            api_versions.APIVersion("3.4"), True, [])
        mock_add_arg.assert_has_calls([
            mock.call('-h', '--help', action='help', help='==SUPPRESS=='),
            mock.call('--bar',
                      help="bar help (Supported by API versions"
                           " 3.3 - 3.4)")])

    @mock.patch.object(cinderclient.shell.CinderClientArgumentParser,
                       'add_argument')
    def test_load_actions_with_versioned_args_v36(self, mock_add_arg):
        parser = cinderclient.shell.CinderClientArgumentParser(add_help=False)
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        shell._find_actions(subparsers, fake_actions_module,
                            api_versions.APIVersion("3.6"), False, [])
        self.assertIn(mock.call('--foo', help="first foo"),
                      mock_add_arg.call_args_list)
        self.assertNotIn(mock.call('--foo', help="second foo"),
                         mock_add_arg.call_args_list)

    @mock.patch.object(cinderclient.shell.CinderClientArgumentParser,
                       'add_argument')
    def test_load_actions_with_versioned_args_v39(self, mock_add_arg):
        parser = cinderclient.shell.CinderClientArgumentParser(add_help=False)
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        shell = cinderclient.shell.OpenStackCinderShell()
        shell.subcommands = {}
        shell._find_actions(subparsers, fake_actions_module,
                            api_versions.APIVersion("3.9"), False, [])
        self.assertNotIn(mock.call('--foo', help="first foo"),
                         mock_add_arg.call_args_list)
        self.assertIn(mock.call('--foo', help="second foo"),
                      mock_add_arg.call_args_list)


class ShellUtilsTest(utils.TestCase):

    @mock.patch.object(cinderclient.utils, 'print_dict')
    def test_print_volume_image(self, mock_print_dict):
        response = {'os-volume_upload_image': {'name': 'myimg1'}}
        image_resp_tuple = (202, response)
        cinderclient.shell_utils.print_volume_image(image_resp_tuple)

        response = {'os-volume_upload_image':
                    {'name': 'myimg2',
                     'volume_type': None}}
        image_resp_tuple = (202, response)
        cinderclient.shell_utils.print_volume_image(image_resp_tuple)

        response = {'os-volume_upload_image':
                    {'name': 'myimg3',
                     'volume_type': {'id': '1234', 'name': 'sometype'}}}
        image_resp_tuple = (202, response)
        cinderclient.shell_utils.print_volume_image(image_resp_tuple)

        mock_print_dict.assert_has_calls(
            (mock.call({'name': 'myimg1'}),
             mock.call({'name': 'myimg2',
                        'volume_type': None}),
             mock.call({'name': 'myimg3',
                        'volume_type': 'sometype'})))