| 1 |
# Copyright 2012-2013 OpenStack Foundation |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import copy |
| 16 |
import io |
| 17 |
import os |
| 18 |
import sys |
| 19 |
from unittest import mock |
| 20 |
|
| 21 |
from cliff import commandmanager |
| 22 |
import fixtures |
| 23 |
from oslo_utils import importutils |
| 24 |
import testtools |
| 25 |
|
| 26 |
from osc_lib import shell |
| 27 |
from osc_lib.test import base |
| 28 |
|
| 29 |
DEFAULT_AUTH_URL = "http://127.0.0.1:5000/v2.0/" |
| 30 |
DEFAULT_PROJECT_ID = "xxxx-yyyy-zzzz" |
| 31 |
DEFAULT_PROJECT_NAME = "project" |
| 32 |
DEFAULT_DOMAIN_ID = "aaaa-bbbb-cccc" |
| 33 |
DEFAULT_DOMAIN_NAME = "default" |
| 34 |
DEFAULT_USER_DOMAIN_ID = "aaaa-bbbb-cccc" |
| 35 |
DEFAULT_USER_DOMAIN_NAME = "domain" |
| 36 |
DEFAULT_PROJECT_DOMAIN_ID = "aaaa-bbbb-cccc" |
| 37 |
DEFAULT_PROJECT_DOMAIN_NAME = "domain" |
| 38 |
DEFAULT_USERNAME = "username" |
| 39 |
DEFAULT_PASSWORD = "password" |
| 40 |
|
| 41 |
DEFAULT_CLOUD = "altocumulus" |
| 42 |
DEFAULT_REGION_NAME = "ZZ9_Plural_Z_Alpha" |
| 43 |
DEFAULT_TOKEN = "token" |
| 44 |
DEFAULT_SERVICE_URL = "http://127.0.0.1:8771/v3.0/" |
| 45 |
DEFAULT_AUTH_PLUGIN = "v2password" |
| 46 |
DEFAULT_INTERFACE = "internal" |
| 47 |
|
| 48 |
DEFAULT_COMPUTE_API_VERSION = "" |
| 49 |
DEFAULT_IDENTITY_API_VERSION = "" |
| 50 |
DEFAULT_IMAGE_API_VERSION = "" |
| 51 |
DEFAULT_VOLUME_API_VERSION = "" |
| 52 |
DEFAULT_NETWORK_API_VERSION = "" |
| 53 |
|
| 54 |
LIB_COMPUTE_API_VERSION = "" |
| 55 |
LIB_IDENTITY_API_VERSION = "" |
| 56 |
LIB_IMAGE_API_VERSION = "" |
| 57 |
LIB_VOLUME_API_VERSION = "" |
| 58 |
LIB_NETWORK_API_VERSION = "" |
| 59 |
|
| 60 |
CLOUD_1 = { |
| 61 |
'clouds': { |
| 62 |
'scc': { |
| 63 |
'auth': { |
| 64 |
'auth_url': DEFAULT_AUTH_URL, |
| 65 |
'project_name': DEFAULT_PROJECT_NAME, |
| 66 |
'username': 'zaphod', |
| 67 |
}, |
| 68 |
'region_name': 'occ-cloud,krikkit', |
| 69 |
'donut': 'glazed', |
| 70 |
'interface': 'admin', |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
CLOUD_2 = { |
| 76 |
'clouds': { |
| 77 |
'megacloud': { |
| 78 |
'cloud': 'megadodo', |
| 79 |
'auth': { |
| 80 |
'project_name': 'heart-o-gold', |
| 81 |
'username': 'zaphod', |
| 82 |
}, |
| 83 |
'region_name': 'occ-cloud,krikkit,occ-env', |
| 84 |
'log_file': '/tmp/test_log_file', |
| 85 |
'log_level': 'debug', |
| 86 |
'cert': 'mycert', |
| 87 |
'key': 'mickey', |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
PUBLIC_1 = { |
| 93 |
'public-clouds': { |
| 94 |
'megadodo': { |
| 95 |
'auth': { |
| 96 |
'auth_url': DEFAULT_AUTH_URL, |
| 97 |
'project_name': DEFAULT_PROJECT_NAME, |
| 98 |
}, |
| 99 |
'region_name': 'occ-public', |
| 100 |
'donut': 'cake', |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
# The option table values is a tuple of (<value>, <test-opt>, <test-env>) |
| 107 |
# where <value> is the test value to use, <test-opt> is True if this option |
| 108 |
# should be tested as a CLI option and <test-env> is True of this option |
| 109 |
# should be tested as an environment variable. |
| 110 |
|
| 111 |
# Global options that should be parsed before shell.initialize_app() is called |
| 112 |
global_options = { |
| 113 |
'--os-cloud': (DEFAULT_CLOUD, True, True), |
| 114 |
'--os-region-name': (DEFAULT_REGION_NAME, True, True), |
| 115 |
'--os-default-domain': (DEFAULT_DOMAIN_NAME, True, True), |
| 116 |
'--os-cacert': ('/dev/null', True, True), |
| 117 |
'--timing': (True, True, False), |
| 118 |
'--os-interface': (DEFAULT_INTERFACE, True, True), |
| 119 |
} |
| 120 |
if shell.osprofiler_profiler: |
| 121 |
global_options['--os-profile'] = ('SECRET_KEY', True, True) |
| 122 |
|
| 123 |
|
| 124 |
def fake_execute(shell, cmd): |
| 125 |
"""Pretend to execute shell commands.""" |
| 126 |
return shell.run(cmd.split()) |
| 127 |
|
| 128 |
|
| 129 |
def make_shell(shell_class=None): |
| 130 |
"""Create a new command shell and mock out some bits.""" |
| 131 |
if shell_class is None: |
| 132 |
shell_class = shell.OpenStackShell |
| 133 |
_shell = shell_class() |
| 134 |
_shell.command_manager = mock.Mock() |
| 135 |
# _shell.cloud = mock.Mock() |
| 136 |
|
| 137 |
return _shell |
| 138 |
|
| 139 |
|
| 140 |
def opt2attr(opt): |
| 141 |
if opt.startswith('--os-'): |
| 142 |
attr = opt[5:] |
| 143 |
elif opt.startswith('--'): |
| 144 |
attr = opt[2:] |
| 145 |
else: |
| 146 |
attr = opt |
| 147 |
return attr.lower().replace('-', '_') |
| 148 |
|
| 149 |
|
| 150 |
def opt2env(opt): |
| 151 |
return opt[2:].upper().replace('-', '_') |
| 152 |
|
| 153 |
|
| 154 |
class EnvFixture(fixtures.Fixture): |
| 155 |
"""Environment Fixture. |
| 156 |
|
| 157 |
This fixture replaces os.environ with provided env or an empty env. |
| 158 |
""" |
| 159 |
|
| 160 |
def __init__(self, env=None): |
| 161 |
self.new_env = env or {} |
| 162 |
|
| 163 |
def _setUp(self): |
| 164 |
self.orig_env, os.environ = os.environ, self.new_env |
| 165 |
self.addCleanup(self.revert) |
| 166 |
|
| 167 |
def revert(self): |
| 168 |
os.environ = self.orig_env |
| 169 |
|
| 170 |
|
| 171 |
class TestShell(base.TestCase): |
| 172 |
# Full name of the OpenStackShell class to test (cliff.app.App subclass) |
| 173 |
shell_class_name = "osc_lib.shell.OpenStackShell" |
| 174 |
|
| 175 |
def setUp(self): |
| 176 |
super().setUp() |
| 177 |
self.shell_class = importutils.import_class(self.shell_class_name) |
| 178 |
self.cmd_patch = mock.patch(self.shell_class_name + ".run_subcommand") |
| 179 |
self.cmd_save = self.cmd_patch.start() |
| 180 |
self.addCleanup(self.cmd_patch.stop) |
| 181 |
self.app = mock.Mock("Test Shell") |
| 182 |
|
| 183 |
def _assert_initialize_app_arg(self, cmd_options, default_args): |
| 184 |
"""Check the args passed to initialize_app() |
| 185 |
|
| 186 |
The argv argument to initialize_app() is the remainder from parsing |
| 187 |
global options declared in both cliff.app and |
| 188 |
osc_lib.OpenStackShell build_option_parser(). Any global |
| 189 |
options passed on the command line should not be in argv but in |
| 190 |
_shell.options. |
| 191 |
""" |
| 192 |
|
| 193 |
with mock.patch( |
| 194 |
self.shell_class_name + ".initialize_app", |
| 195 |
self.app, |
| 196 |
): |
| 197 |
_shell = make_shell(shell_class=self.shell_class) |
| 198 |
_cmd = cmd_options + " module list" |
| 199 |
fake_execute(_shell, _cmd) |
| 200 |
|
| 201 |
self.app.assert_called_with(["module", "list"]) |
| 202 |
for k in default_args.keys(): |
| 203 |
self.assertEqual( |
| 204 |
default_args[k], |
| 205 |
vars(_shell.options)[k], |
| 206 |
f"{k} does not match", |
| 207 |
) |
| 208 |
|
| 209 |
def _assert_cloud_region_arg(self, cmd_options, default_args): |
| 210 |
"""Check the args passed to OpenStackConfig.get_one() |
| 211 |
|
| 212 |
The argparse argument to get_one() is an argparse.Namespace |
| 213 |
object that contains all of the options processed to this point in |
| 214 |
initialize_app(). |
| 215 |
""" |
| 216 |
|
| 217 |
cloud = mock.Mock(name="cloudy") |
| 218 |
cloud.config = {} |
| 219 |
self.occ_get_one = mock.Mock(return_value=cloud) |
| 220 |
with mock.patch( |
| 221 |
"openstack.config.loader.OpenStackConfig.get_one", |
| 222 |
self.occ_get_one, |
| 223 |
): |
| 224 |
_shell = make_shell(shell_class=self.shell_class) |
| 225 |
_cmd = cmd_options + " module list" |
| 226 |
fake_execute(_shell, _cmd) |
| 227 |
|
| 228 |
self.app.assert_called_with(["module", "list"]) |
| 229 |
opts = self.occ_get_one.call_args[1]['argparse'] |
| 230 |
for k in default_args.keys(): |
| 231 |
self.assertEqual( |
| 232 |
default_args[k], |
| 233 |
vars(opts)[k], |
| 234 |
f"{k} does not match", |
| 235 |
) |
| 236 |
|
| 237 |
def _test_options_init_app(self, test_opts): |
| 238 |
"""Test options on the command line""" |
| 239 |
for opt in test_opts.keys(): |
| 240 |
if not test_opts[opt][1]: |
| 241 |
continue |
| 242 |
key = opt2attr(opt) |
| 243 |
if isinstance(test_opts[opt][0], str): |
| 244 |
cmd = opt + " " + test_opts[opt][0] |
| 245 |
else: |
| 246 |
cmd = opt |
| 247 |
kwargs = { |
| 248 |
key: test_opts[opt][0], |
| 249 |
} |
| 250 |
self._assert_initialize_app_arg(cmd, kwargs) |
| 251 |
|
| 252 |
def _test_env_init_app(self, test_opts): |
| 253 |
"""Test options in the environment""" |
| 254 |
for opt in test_opts.keys(): |
| 255 |
if not test_opts[opt][2]: |
| 256 |
continue |
| 257 |
key = opt2attr(opt) |
| 258 |
kwargs = { |
| 259 |
key: test_opts[opt][0], |
| 260 |
} |
| 261 |
env = { |
| 262 |
opt2env(opt): test_opts[opt][0], |
| 263 |
} |
| 264 |
os.environ = env.copy() |
| 265 |
self._assert_initialize_app_arg("", kwargs) |
| 266 |
|
| 267 |
def _test_options_get_one_cloud(self, test_opts): |
| 268 |
"""Test options sent "to openstack.config""" |
| 269 |
for opt in test_opts.keys(): |
| 270 |
if not test_opts[opt][1]: |
| 271 |
continue |
| 272 |
key = opt2attr(opt) |
| 273 |
if isinstance(test_opts[opt][0], str): |
| 274 |
cmd = opt + " " + test_opts[opt][0] |
| 275 |
else: |
| 276 |
cmd = opt |
| 277 |
kwargs = { |
| 278 |
key: test_opts[opt][0], |
| 279 |
} |
| 280 |
self._assert_cloud_region_arg(cmd, kwargs) |
| 281 |
|
| 282 |
def _test_env_get_one_cloud(self, test_opts): |
| 283 |
"""Test environment options sent "to openstack.config""" |
| 284 |
for opt in test_opts.keys(): |
| 285 |
if not test_opts[opt][2]: |
| 286 |
continue |
| 287 |
key = opt2attr(opt) |
| 288 |
kwargs = { |
| 289 |
key: test_opts[opt][0], |
| 290 |
} |
| 291 |
env = { |
| 292 |
opt2env(opt): test_opts[opt][0], |
| 293 |
} |
| 294 |
os.environ = env.copy() |
| 295 |
self._assert_cloud_region_arg("", kwargs) |
| 296 |
|
| 297 |
|
| 298 |
class TestShellInit(TestShell): |
| 299 |
"""Test the initialization of the shell""" |
| 300 |
|
| 301 |
def setUp(self): |
| 302 |
super().setUp() |
| 303 |
|
| 304 |
def test_shell_init(self): |
| 305 |
"""Test the initialization of the shell with default values""" |
| 306 |
|
| 307 |
with ( |
| 308 |
mock.patch( |
| 309 |
"cliff.app.App.__init__", return_value=None |
| 310 |
) as mock_init, |
| 311 |
mock.patch( |
| 312 |
"cliff.commandmanager.CommandManager" |
| 313 |
) as mock_command_manager, |
| 314 |
): |
| 315 |
_shell = self.shell_class() |
| 316 |
|
| 317 |
mock_command_manager.assert_called_once_with('openstack.cli') |
| 318 |
|
| 319 |
mock_init.assert_called_once_with( |
| 320 |
description=shell.__doc__.strip(), |
| 321 |
version=None, |
| 322 |
command_manager=mock_command_manager.return_value, |
| 323 |
stdin=None, |
| 324 |
stdout=None, |
| 325 |
stderr=None, |
| 326 |
interactive_app_factory=None, |
| 327 |
deferred_help=True, |
| 328 |
) |
| 329 |
|
| 330 |
# Since we mock cliff's app _shell.command_manager is not set |
| 331 |
self.assertFalse(hasattr(_shell, "command_manager")) |
| 332 |
self.assertTrue(_shell.dump_stack_trace) |
| 333 |
self.assertDictEqual({}, _shell.api_version) |
| 334 |
self.assertListEqual([], _shell.command_options) |
| 335 |
self.assertFalse(_shell.do_profile) |
| 336 |
|
| 337 |
def test_shell_init_with_args(self): |
| 338 |
"""Test the initialization of the shell with custom values""" |
| 339 |
|
| 340 |
description = "Test Shell" |
| 341 |
version = "1.0.0" |
| 342 |
command_manager = commandmanager.CommandManager('test-cli') |
| 343 |
stdin = io.StringIO() |
| 344 |
stdout = io.StringIO() |
| 345 |
stderr = io.StringIO() |
| 346 |
interactive_app_factory = mock.Mock() |
| 347 |
deferred_help = False |
| 348 |
|
| 349 |
with ( |
| 350 |
mock.patch( |
| 351 |
"cliff.app.App.__init__", return_value=None |
| 352 |
) as mock_init, |
| 353 |
mock.patch( |
| 354 |
"cliff.commandmanager.CommandManager" |
| 355 |
) as mock_command_manager, |
| 356 |
): |
| 357 |
_shell = self.shell_class( |
| 358 |
description=description, |
| 359 |
version=version, |
| 360 |
command_manager=command_manager, |
| 361 |
stdin=stdin, |
| 362 |
stdout=stdout, |
| 363 |
stderr=stderr, |
| 364 |
interactive_app_factory=interactive_app_factory, |
| 365 |
deferred_help=deferred_help, |
| 366 |
) |
| 367 |
|
| 368 |
mock_command_manager.assert_not_called() |
| 369 |
|
| 370 |
mock_init.assert_called_once_with( |
| 371 |
description=description, |
| 372 |
version=version, |
| 373 |
command_manager=command_manager, |
| 374 |
stdin=stdin, |
| 375 |
stdout=stdout, |
| 376 |
stderr=stderr, |
| 377 |
interactive_app_factory=interactive_app_factory, |
| 378 |
deferred_help=deferred_help, |
| 379 |
) |
| 380 |
|
| 381 |
# Since we mock cliff's app _shell.command_manager is not set |
| 382 |
self.assertFalse(hasattr(_shell, "command_manager")) |
| 383 |
self.assertTrue(_shell.dump_stack_trace) |
| 384 |
self.assertDictEqual({}, _shell.api_version) |
| 385 |
self.assertListEqual([], _shell.command_options) |
| 386 |
self.assertFalse(_shell.do_profile) |
| 387 |
|
| 388 |
|
| 389 |
class TestShellArgV(TestShell): |
| 390 |
"""Test the deferred help flag""" |
| 391 |
|
| 392 |
def setUp(self): |
| 393 |
super().setUp() |
| 394 |
|
| 395 |
def test_shell_argv(self): |
| 396 |
"""Test argv decoding |
| 397 |
|
| 398 |
Python 2 does nothing with argv while Python 3 decodes it into |
| 399 |
Unicode before we ever see it. We manually decode when running |
| 400 |
under Python 2 so verify that we get the right argv types. |
| 401 |
|
| 402 |
Use the argv supplied by the test runner so we get actual Python |
| 403 |
runtime behaviour; we only need to check the type of argv[0] |
| 404 |
which will alwyas be present. |
| 405 |
""" |
| 406 |
|
| 407 |
with mock.patch( |
| 408 |
"osc_lib.shell.OpenStackShell.run", |
| 409 |
self.app, |
| 410 |
): |
| 411 |
# Ensure type gets through unmolested through shell.main() |
| 412 |
argv = sys.argv |
| 413 |
shell.main(sys.argv) |
| 414 |
self.assertEqual(type(argv[0]), type(self.app.call_args[0][0][0])) |
| 415 |
|
| 416 |
# When shell.main() gets sys.argv itself it should be decoded |
| 417 |
shell.main() |
| 418 |
self.assertEqual(str, type(self.app.call_args[0][0][0])) |
| 419 |
|
| 420 |
|
| 421 |
class TestShellHelp(TestShell): |
| 422 |
"""Test the deferred help flag""" |
| 423 |
|
| 424 |
def setUp(self): |
| 425 |
super().setUp() |
| 426 |
self.useFixture(EnvFixture()) |
| 427 |
|
| 428 |
@testtools.skip("skip until bug 1444983 is resolved") |
| 429 |
def test_help_options(self): |
| 430 |
flag = "-h list server" |
| 431 |
kwargs = { |
| 432 |
"deferred_help": True, |
| 433 |
} |
| 434 |
with mock.patch(self.app_patch + ".initialize_app", self.app): |
| 435 |
_shell, _cmd = make_shell(), flag |
| 436 |
fake_execute(_shell, _cmd) |
| 437 |
|
| 438 |
self.assertEqual( |
| 439 |
kwargs["deferred_help"], |
| 440 |
_shell.options.deferred_help, |
| 441 |
) |
| 442 |
|
| 443 |
|
| 444 |
class TestShellOptions(TestShell): |
| 445 |
"""Test the option handling by argparse and openstack.config.loader |
| 446 |
|
| 447 |
This covers getting the CLI options through the initial processing |
| 448 |
and validates the arguments to initialize_app() and occ_get_one() |
| 449 |
""" |
| 450 |
|
| 451 |
def setUp(self): |
| 452 |
super().setUp() |
| 453 |
self.useFixture(EnvFixture()) |
| 454 |
|
| 455 |
def test_empty_auth(self): |
| 456 |
os.environ = {} |
| 457 |
self._assert_initialize_app_arg("", {}) |
| 458 |
self._assert_cloud_region_arg("", {}) |
| 459 |
|
| 460 |
def test_no_options(self): |
| 461 |
os.environ = {} |
| 462 |
self._assert_initialize_app_arg("", {}) |
| 463 |
self._assert_cloud_region_arg("", {}) |
| 464 |
|
| 465 |
def test_global_options(self): |
| 466 |
self._test_options_init_app(global_options) |
| 467 |
self._test_options_get_one_cloud(global_options) |
| 468 |
|
| 469 |
def test_global_env(self): |
| 470 |
self._test_env_init_app(global_options) |
| 471 |
self._test_env_get_one_cloud(global_options) |
| 472 |
|
| 473 |
|
| 474 |
class TestShellCli(TestShell): |
| 475 |
"""Test handling of specific global options |
| 476 |
|
| 477 |
_shell.options is the parsed command line from argparse |
| 478 |
_shell.client_manager.* are the values actually used |
| 479 |
|
| 480 |
""" |
| 481 |
|
| 482 |
def setUp(self): |
| 483 |
super().setUp() |
| 484 |
env = {} |
| 485 |
self.useFixture(EnvFixture(env.copy())) |
| 486 |
|
| 487 |
def test_shell_args_no_options(self): |
| 488 |
_shell = make_shell() |
| 489 |
with mock.patch( |
| 490 |
"osc_lib.shell.OpenStackShell.initialize_app", |
| 491 |
self.app, |
| 492 |
): |
| 493 |
fake_execute(_shell, "list user") |
| 494 |
self.app.assert_called_with(["list", "user"]) |
| 495 |
|
| 496 |
def test_shell_args_tls_options(self): |
| 497 |
"""Test the TLS verify and CA cert file options""" |
| 498 |
_shell = make_shell() |
| 499 |
|
| 500 |
# Default |
| 501 |
fake_execute(_shell, "module list") |
| 502 |
self.assertIsNone(_shell.options.verify) |
| 503 |
self.assertIsNone(_shell.options.insecure) |
| 504 |
self.assertIsNone(_shell.options.cacert) |
| 505 |
self.assertTrue(_shell.client_manager.verify) |
| 506 |
self.assertIsNone(_shell.client_manager.cacert) |
| 507 |
|
| 508 |
# --verify |
| 509 |
fake_execute(_shell, "--verify module list") |
| 510 |
self.assertTrue(_shell.options.verify) |
| 511 |
self.assertIsNone(_shell.options.insecure) |
| 512 |
self.assertIsNone(_shell.options.cacert) |
| 513 |
self.assertTrue(_shell.client_manager.verify) |
| 514 |
self.assertIsNone(_shell.client_manager.cacert) |
| 515 |
|
| 516 |
# --insecure |
| 517 |
fake_execute(_shell, "--insecure module list") |
| 518 |
self.assertIsNone(_shell.options.verify) |
| 519 |
self.assertTrue(_shell.options.insecure) |
| 520 |
self.assertIsNone(_shell.options.cacert) |
| 521 |
self.assertFalse(_shell.client_manager.verify) |
| 522 |
self.assertIsNone(_shell.client_manager.cacert) |
| 523 |
|
| 524 |
# --os-cacert |
| 525 |
fake_execute(_shell, "--os-cacert foo module list") |
| 526 |
self.assertIsNone(_shell.options.verify) |
| 527 |
self.assertIsNone(_shell.options.insecure) |
| 528 |
self.assertEqual('foo', _shell.options.cacert) |
| 529 |
self.assertEqual('foo', _shell.client_manager.verify) |
| 530 |
self.assertEqual('foo', _shell.client_manager.cacert) |
| 531 |
|
| 532 |
# --os-cacert and --verify |
| 533 |
fake_execute(_shell, "--os-cacert foo --verify module list") |
| 534 |
self.assertTrue(_shell.options.verify) |
| 535 |
self.assertIsNone(_shell.options.insecure) |
| 536 |
self.assertEqual('foo', _shell.options.cacert) |
| 537 |
self.assertEqual('foo', _shell.client_manager.verify) |
| 538 |
self.assertEqual('foo', _shell.client_manager.cacert) |
| 539 |
|
| 540 |
# --os-cacert and --insecure |
| 541 |
# NOTE(dtroyer): Per bug https://bugs.launchpad.net/bugs/1447784 |
| 542 |
# in this combination --insecure now overrides any |
| 543 |
# --os-cacert setting, where before --insecure |
| 544 |
# was ignored if --os-cacert was set. |
| 545 |
fake_execute(_shell, "--os-cacert foo --insecure module list") |
| 546 |
self.assertIsNone(_shell.options.verify) |
| 547 |
self.assertTrue(_shell.options.insecure) |
| 548 |
self.assertEqual('foo', _shell.options.cacert) |
| 549 |
self.assertFalse(_shell.client_manager.verify) |
| 550 |
self.assertIsNone(_shell.client_manager.cacert) |
| 551 |
|
| 552 |
def test_shell_args_cert_options(self): |
| 553 |
"""Test client cert options""" |
| 554 |
_shell = make_shell() |
| 555 |
|
| 556 |
# Default |
| 557 |
fake_execute(_shell, "module list") |
| 558 |
self.assertIsNone(_shell.options.cert) |
| 559 |
self.assertIsNone(_shell.options.key) |
| 560 |
self.assertIsNone(_shell.client_manager.cert) |
| 561 |
|
| 562 |
# --os-cert |
| 563 |
fake_execute(_shell, "--os-cert mycert module list") |
| 564 |
self.assertEqual('mycert', _shell.options.cert) |
| 565 |
self.assertIsNone(_shell.options.key) |
| 566 |
self.assertEqual('mycert', _shell.client_manager.cert) |
| 567 |
|
| 568 |
# --os-key |
| 569 |
fake_execute(_shell, "--os-key mickey module list") |
| 570 |
self.assertIsNone(_shell.options.cert) |
| 571 |
self.assertEqual('mickey', _shell.options.key) |
| 572 |
self.assertIsNone(_shell.client_manager.cert) |
| 573 |
|
| 574 |
# --os-cert and --os-key |
| 575 |
fake_execute(_shell, "--os-cert mycert --os-key mickey module list") |
| 576 |
self.assertEqual('mycert', _shell.options.cert) |
| 577 |
self.assertEqual('mickey', _shell.options.key) |
| 578 |
self.assertEqual(('mycert', 'mickey'), _shell.client_manager.cert) |
| 579 |
|
| 580 |
@mock.patch("openstack.config.loader.OpenStackConfig._load_config_file") |
| 581 |
def test_shell_args_cloud_no_vendor(self, config_mock): |
| 582 |
"""Test cloud config options without the vendor file""" |
| 583 |
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_1)) |
| 584 |
_shell = make_shell() |
| 585 |
|
| 586 |
fake_execute( |
| 587 |
_shell, |
| 588 |
"--os-cloud scc module list", |
| 589 |
) |
| 590 |
self.assertEqual( |
| 591 |
'scc', |
| 592 |
_shell.cloud.name, |
| 593 |
) |
| 594 |
|
| 595 |
# These come from clouds.yaml |
| 596 |
# |
| 597 |
# To effectively assert values from clouds.yaml, |
| 598 |
# use values which differ from the defaults, |
| 599 |
# e.g. do not use public for interface because |
| 600 |
# osc_lib.shell.DEFAULT_INTERFACE is public. |
| 601 |
self.assertEqual( |
| 602 |
DEFAULT_AUTH_URL, |
| 603 |
_shell.cloud.config['auth']['auth_url'], |
| 604 |
) |
| 605 |
self.assertEqual( |
| 606 |
DEFAULT_PROJECT_NAME, |
| 607 |
_shell.cloud.config['auth']['project_name'], |
| 608 |
) |
| 609 |
self.assertEqual( |
| 610 |
'zaphod', |
| 611 |
_shell.cloud.config['auth']['username'], |
| 612 |
) |
| 613 |
self.assertEqual( |
| 614 |
'occ-cloud', |
| 615 |
_shell.cloud.config['region_name'], |
| 616 |
) |
| 617 |
self.assertEqual( |
| 618 |
'occ-cloud', |
| 619 |
_shell.client_manager.region_name, |
| 620 |
) |
| 621 |
self.assertEqual( |
| 622 |
'glazed', |
| 623 |
_shell.cloud.config['donut'], |
| 624 |
) |
| 625 |
self.assertEqual( |
| 626 |
'admin', |
| 627 |
_shell.cloud.config['interface'], |
| 628 |
) |
| 629 |
|
| 630 |
self.assertIsNone(_shell.cloud.config['cert']) |
| 631 |
self.assertIsNone(_shell.cloud.config['key']) |
| 632 |
self.assertIsNone(_shell.client_manager.cert) |
| 633 |
|
| 634 |
@mock.patch("openstack.config.loader.OpenStackConfig._load_vendor_file") |
| 635 |
@mock.patch("openstack.config.loader.OpenStackConfig._load_config_file") |
| 636 |
def test_shell_args_cloud_public(self, config_mock, public_mock): |
| 637 |
"""Test cloud config options with the vendor file""" |
| 638 |
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2)) |
| 639 |
public_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1)) |
| 640 |
_shell = make_shell() |
| 641 |
|
| 642 |
fake_execute( |
| 643 |
_shell, |
| 644 |
"--os-cloud megacloud module list", |
| 645 |
) |
| 646 |
self.assertEqual( |
| 647 |
'megacloud', |
| 648 |
_shell.cloud.name, |
| 649 |
) |
| 650 |
|
| 651 |
# These come from clouds-public.yaml |
| 652 |
self.assertEqual( |
| 653 |
DEFAULT_AUTH_URL, |
| 654 |
_shell.cloud.config['auth']['auth_url'], |
| 655 |
) |
| 656 |
self.assertEqual( |
| 657 |
'cake', |
| 658 |
_shell.cloud.config['donut'], |
| 659 |
) |
| 660 |
|
| 661 |
# These come from clouds.yaml |
| 662 |
self.assertEqual( |
| 663 |
'heart-o-gold', |
| 664 |
_shell.cloud.config['auth']['project_name'], |
| 665 |
) |
| 666 |
self.assertEqual( |
| 667 |
'zaphod', |
| 668 |
_shell.cloud.config['auth']['username'], |
| 669 |
) |
| 670 |
self.assertEqual( |
| 671 |
'occ-cloud', |
| 672 |
_shell.cloud.config['region_name'], |
| 673 |
) |
| 674 |
self.assertEqual( |
| 675 |
'occ-cloud', |
| 676 |
_shell.client_manager.region_name, |
| 677 |
) |
| 678 |
|
| 679 |
self.assertEqual('mycert', _shell.cloud.config['cert']) |
| 680 |
self.assertEqual('mickey', _shell.cloud.config['key']) |
| 681 |
self.assertEqual(('mycert', 'mickey'), _shell.client_manager.cert) |
| 682 |
|
| 683 |
@mock.patch("openstack.config.loader.OpenStackConfig._load_vendor_file") |
| 684 |
@mock.patch("openstack.config.loader.OpenStackConfig._load_config_file") |
| 685 |
def test_shell_args_precedence(self, config_mock, vendor_mock): |
| 686 |
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2)) |
| 687 |
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1)) |
| 688 |
_shell = make_shell() |
| 689 |
|
| 690 |
# Test command option overriding config file value |
| 691 |
fake_execute( |
| 692 |
_shell, |
| 693 |
"--os-cloud megacloud --os-region-name krikkit module list", |
| 694 |
) |
| 695 |
self.assertEqual( |
| 696 |
'megacloud', |
| 697 |
_shell.cloud.name, |
| 698 |
) |
| 699 |
|
| 700 |
# These come from clouds-public.yaml |
| 701 |
self.assertEqual( |
| 702 |
DEFAULT_AUTH_URL, |
| 703 |
_shell.cloud.config['auth']['auth_url'], |
| 704 |
) |
| 705 |
self.assertEqual( |
| 706 |
'cake', |
| 707 |
_shell.cloud.config['donut'], |
| 708 |
) |
| 709 |
|
| 710 |
# These come from clouds.yaml |
| 711 |
self.assertEqual( |
| 712 |
'heart-o-gold', |
| 713 |
_shell.cloud.config['auth']['project_name'], |
| 714 |
) |
| 715 |
self.assertEqual( |
| 716 |
'zaphod', |
| 717 |
_shell.cloud.config['auth']['username'], |
| 718 |
) |
| 719 |
self.assertEqual( |
| 720 |
'krikkit', |
| 721 |
_shell.cloud.config['region_name'], |
| 722 |
) |
| 723 |
self.assertEqual( |
| 724 |
'krikkit', |
| 725 |
_shell.client_manager.region_name, |
| 726 |
) |
| 727 |
|
| 728 |
|
| 729 |
class TestShellCliPrecedence(TestShell): |
| 730 |
"""Test option precedencr order""" |
| 731 |
|
| 732 |
def setUp(self): |
| 733 |
super().setUp() |
| 734 |
env = { |
| 735 |
'OS_CLOUD': 'megacloud', |
| 736 |
'OS_REGION_NAME': 'occ-env', |
| 737 |
} |
| 738 |
self.useFixture(EnvFixture(env.copy())) |
| 739 |
|
| 740 |
@mock.patch("openstack.config.loader.OpenStackConfig._load_vendor_file") |
| 741 |
@mock.patch("openstack.config.loader.OpenStackConfig._load_config_file") |
| 742 |
def test_shell_args_precedence_1(self, config_mock, vendor_mock): |
| 743 |
"""Test environment overriding occ""" |
| 744 |
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2)) |
| 745 |
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1)) |
| 746 |
_shell = make_shell() |
| 747 |
|
| 748 |
# Test env var |
| 749 |
fake_execute( |
| 750 |
_shell, |
| 751 |
"module list", |
| 752 |
) |
| 753 |
self.assertEqual( |
| 754 |
'megacloud', |
| 755 |
_shell.cloud.name, |
| 756 |
) |
| 757 |
|
| 758 |
# These come from clouds-public.yaml |
| 759 |
self.assertEqual( |
| 760 |
DEFAULT_AUTH_URL, |
| 761 |
_shell.cloud.config['auth']['auth_url'], |
| 762 |
) |
| 763 |
self.assertEqual( |
| 764 |
'cake', |
| 765 |
_shell.cloud.config['donut'], |
| 766 |
) |
| 767 |
|
| 768 |
# These come from clouds.yaml |
| 769 |
self.assertEqual( |
| 770 |
'heart-o-gold', |
| 771 |
_shell.cloud.config['auth']['project_name'], |
| 772 |
) |
| 773 |
self.assertEqual( |
| 774 |
'zaphod', |
| 775 |
_shell.cloud.config['auth']['username'], |
| 776 |
) |
| 777 |
|
| 778 |
# These come from the environment |
| 779 |
self.assertEqual( |
| 780 |
'occ-env', |
| 781 |
_shell.cloud.config['region_name'], |
| 782 |
) |
| 783 |
self.assertEqual( |
| 784 |
'occ-env', |
| 785 |
_shell.client_manager.region_name, |
| 786 |
) |
| 787 |
|
| 788 |
@mock.patch("openstack.config.loader.OpenStackConfig._load_vendor_file") |
| 789 |
@mock.patch("openstack.config.loader.OpenStackConfig._load_config_file") |
| 790 |
def test_shell_args_precedence_2(self, config_mock, vendor_mock): |
| 791 |
"""Test command line overriding environment and occ""" |
| 792 |
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_2)) |
| 793 |
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1)) |
| 794 |
_shell = make_shell() |
| 795 |
|
| 796 |
# Test command option overriding config file value |
| 797 |
fake_execute( |
| 798 |
_shell, |
| 799 |
"--os-region-name krikkit list user", |
| 800 |
) |
| 801 |
self.assertEqual( |
| 802 |
'megacloud', |
| 803 |
_shell.cloud.name, |
| 804 |
) |
| 805 |
|
| 806 |
# These come from clouds-public.yaml |
| 807 |
self.assertEqual( |
| 808 |
DEFAULT_AUTH_URL, |
| 809 |
_shell.cloud.config['auth']['auth_url'], |
| 810 |
) |
| 811 |
self.assertEqual( |
| 812 |
'cake', |
| 813 |
_shell.cloud.config['donut'], |
| 814 |
) |
| 815 |
|
| 816 |
# These come from clouds.yaml |
| 817 |
self.assertEqual( |
| 818 |
'heart-o-gold', |
| 819 |
_shell.cloud.config['auth']['project_name'], |
| 820 |
) |
| 821 |
self.assertEqual( |
| 822 |
'zaphod', |
| 823 |
_shell.cloud.config['auth']['username'], |
| 824 |
) |
| 825 |
|
| 826 |
# These come from the command line |
| 827 |
self.assertEqual( |
| 828 |
'krikkit', |
| 829 |
_shell.cloud.config['region_name'], |
| 830 |
) |
| 831 |
self.assertEqual( |
| 832 |
'krikkit', |
| 833 |
_shell.client_manager.region_name, |
| 834 |
) |
| 835 |
|
| 836 |
@mock.patch("openstack.config.loader.OpenStackConfig._load_vendor_file") |
| 837 |
@mock.patch("openstack.config.loader.OpenStackConfig._load_config_file") |
| 838 |
def test_shell_args_precedence_3(self, config_mock, vendor_mock): |
| 839 |
"""Test command line overriding environment and occ""" |
| 840 |
config_mock.return_value = ('file.yaml', copy.deepcopy(CLOUD_1)) |
| 841 |
vendor_mock.return_value = ('file.yaml', copy.deepcopy(PUBLIC_1)) |
| 842 |
_shell = make_shell() |
| 843 |
|
| 844 |
# Test command option overriding config file value |
| 845 |
fake_execute( |
| 846 |
_shell, |
| 847 |
"--os-cloud scc --os-region-name krikkit list user", |
| 848 |
) |
| 849 |
self.assertEqual( |
| 850 |
'scc', |
| 851 |
_shell.cloud.name, |
| 852 |
) |
| 853 |
|
| 854 |
# These come from clouds-public.yaml |
| 855 |
self.assertEqual( |
| 856 |
DEFAULT_AUTH_URL, |
| 857 |
_shell.cloud.config['auth']['auth_url'], |
| 858 |
) |
| 859 |
self.assertEqual( |
| 860 |
'glazed', |
| 861 |
_shell.cloud.config['donut'], |
| 862 |
) |
| 863 |
|
| 864 |
# These come from clouds.yaml |
| 865 |
self.assertEqual( |
| 866 |
DEFAULT_PROJECT_NAME, |
| 867 |
_shell.cloud.config['auth']['project_name'], |
| 868 |
) |
| 869 |
self.assertEqual( |
| 870 |
'zaphod', |
| 871 |
_shell.cloud.config['auth']['username'], |
| 872 |
) |
| 873 |
|
| 874 |
# These come from the command line |
| 875 |
self.assertEqual( |
| 876 |
'krikkit', |
| 877 |
_shell.cloud.config['region_name'], |
| 878 |
) |
| 879 |
self.assertEqual( |
| 880 |
'krikkit', |
| 881 |
_shell.client_manager.region_name, |
| 882 |
) |