| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import argparse |
| 18 |
from collections import OrderedDict |
| 19 |
import hashlib |
| 20 |
import io |
| 21 |
import logging |
| 22 |
import os |
| 23 |
import sys |
| 24 |
import traceback |
| 25 |
from unittest import mock |
| 26 |
import uuid |
| 27 |
|
| 28 |
import fixtures |
| 29 |
from keystoneauth1 import exceptions as ks_exc |
| 30 |
from keystoneauth1 import fixture as ks_fixture |
| 31 |
from requests_mock.contrib import fixture as rm_fixture |
| 32 |
|
| 33 |
from glanceclient.common import utils |
| 34 |
from glanceclient import exc |
| 35 |
from glanceclient import shell as openstack_shell |
| 36 |
from glanceclient.tests.unit.v2.fixtures import image_show_fixture |
| 37 |
from glanceclient.tests.unit.v2.fixtures import image_versions_fixture |
| 38 |
from glanceclient.tests import utils as testutils |
| 39 |
|
| 40 |
|
| 41 |
from glanceclient.v2 import schemas as schemas |
| 42 |
import json |
| 43 |
|
| 44 |
|
| 45 |
DEFAULT_IMAGE_URL = 'http://127.0.0.1:9292/' |
| 46 |
DEFAULT_IMAGE_URL_INTERNAL = 'http://127.0.0.1:9191/' |
| 47 |
DEFAULT_USERNAME = 'username' |
| 48 |
DEFAULT_PAGE_SIZE = 200 |
| 49 |
DEFAULT_PASSWORD = 'password' |
| 50 |
DEFAULT_TENANT_ID = 'tenant_id' |
| 51 |
DEFAULT_TENANT_NAME = 'tenant_name' |
| 52 |
DEFAULT_PROJECT_ID = '0123456789' |
| 53 |
DEFAULT_USER_DOMAIN_NAME = 'user_domain_name' |
| 54 |
DEFAULT_UNVERSIONED_AUTH_URL = 'http://127.0.0.1:5000/' |
| 55 |
DEFAULT_V2_AUTH_URL = '%sv2.0' % DEFAULT_UNVERSIONED_AUTH_URL |
| 56 |
DEFAULT_V3_AUTH_URL = '%sv3' % DEFAULT_UNVERSIONED_AUTH_URL |
| 57 |
DEFAULT_AUTH_TOKEN = ' 3bcc3d3a03f44e3d8377f9247b0ad155' |
| 58 |
TEST_SERVICE_URL = 'http://127.0.0.1:5000/' |
| 59 |
DEFAULT_SERVICE_TYPE = 'image' |
| 60 |
DEFAULT_ENDPOINT_TYPE = 'public' |
| 61 |
|
| 62 |
FAKE_V2_ENV = {'OS_USERNAME': DEFAULT_USERNAME, |
| 63 |
'OS_PASSWORD': DEFAULT_PASSWORD, |
| 64 |
'OS_TENANT_NAME': DEFAULT_TENANT_NAME, |
| 65 |
'OS_AUTH_URL': DEFAULT_V2_AUTH_URL, |
| 66 |
'OS_IMAGE_URL': DEFAULT_IMAGE_URL} |
| 67 |
|
| 68 |
FAKE_V3_ENV = {'OS_USERNAME': DEFAULT_USERNAME, |
| 69 |
'OS_PASSWORD': DEFAULT_PASSWORD, |
| 70 |
'OS_PROJECT_ID': DEFAULT_PROJECT_ID, |
| 71 |
'OS_USER_DOMAIN_NAME': DEFAULT_USER_DOMAIN_NAME, |
| 72 |
'OS_AUTH_URL': DEFAULT_V3_AUTH_URL, |
| 73 |
'OS_IMAGE_URL': DEFAULT_IMAGE_URL} |
| 74 |
|
| 75 |
FAKE_V4_ENV = {'OS_USERNAME': DEFAULT_USERNAME, |
| 76 |
'OS_PASSWORD': DEFAULT_PASSWORD, |
| 77 |
'OS_PROJECT_ID': DEFAULT_PROJECT_ID, |
| 78 |
'OS_USER_DOMAIN_NAME': DEFAULT_USER_DOMAIN_NAME, |
| 79 |
'OS_AUTH_URL': DEFAULT_V3_AUTH_URL, |
| 80 |
'OS_SERVICE_TYPE': DEFAULT_SERVICE_TYPE, |
| 81 |
'OS_ENDPOINT_TYPE': DEFAULT_ENDPOINT_TYPE, |
| 82 |
'OS_AUTH_TOKEN': DEFAULT_AUTH_TOKEN} |
| 83 |
|
| 84 |
TOKEN_ID = uuid.uuid4().hex |
| 85 |
|
| 86 |
V2_TOKEN = ks_fixture.V2Token(token_id=TOKEN_ID) |
| 87 |
V2_TOKEN.set_scope() |
| 88 |
_s = V2_TOKEN.add_service('image', name='glance') |
| 89 |
_s.add_endpoint(DEFAULT_IMAGE_URL) |
| 90 |
|
| 91 |
V3_TOKEN = ks_fixture.V3Token() |
| 92 |
V3_TOKEN.set_project_scope() |
| 93 |
_s = V3_TOKEN.add_service('image', name='glance') |
| 94 |
_s.add_standard_endpoints(public=DEFAULT_IMAGE_URL, |
| 95 |
internal=DEFAULT_IMAGE_URL_INTERNAL) |
| 96 |
|
| 97 |
|
| 98 |
class ShellTest(testutils.TestCase): |
| 99 |
|
| 100 |
auth_env = FAKE_V2_ENV.copy() |
| 101 |
|
| 102 |
token_url = DEFAULT_V2_AUTH_URL + '/tokens' |
| 103 |
|
| 104 |
|
| 105 |
def make_env(self, exclude=None): |
| 106 |
env = dict((k, v) for k, v in self.auth_env.items() if k != exclude) |
| 107 |
self.useFixture(fixtures.MonkeyPatch('os.environ', env)) |
| 108 |
|
| 109 |
def setUp(self): |
| 110 |
super(ShellTest, self).setUp() |
| 111 |
global _old_env |
| 112 |
_old_env, os.environ = os.environ, self.auth_env |
| 113 |
|
| 114 |
self.requests = self.useFixture(rm_fixture.Fixture()) |
| 115 |
|
| 116 |
json_list = ks_fixture.DiscoveryList(DEFAULT_UNVERSIONED_AUTH_URL) |
| 117 |
self.requests.get(DEFAULT_UNVERSIONED_AUTH_URL, |
| 118 |
json=json_list, |
| 119 |
status_code=300) |
| 120 |
|
| 121 |
json_v2 = {'version': ks_fixture.V2Discovery(DEFAULT_V2_AUTH_URL)} |
| 122 |
self.requests.get(DEFAULT_V2_AUTH_URL, json=json_v2) |
| 123 |
|
| 124 |
json_v3 = {'version': ks_fixture.V3Discovery(DEFAULT_V3_AUTH_URL)} |
| 125 |
self.requests.get(DEFAULT_V3_AUTH_URL, json=json_v3) |
| 126 |
|
| 127 |
self.v2_auth = self.requests.post(DEFAULT_V2_AUTH_URL + '/tokens', |
| 128 |
json=V2_TOKEN) |
| 129 |
|
| 130 |
headers = {'X-Subject-Token': TOKEN_ID} |
| 131 |
self.v3_auth = self.requests.post(DEFAULT_V3_AUTH_URL + '/auth/tokens', |
| 132 |
headers=headers, |
| 133 |
json=V3_TOKEN) |
| 134 |
|
| 135 |
global shell, _shell, assert_called, assert_called_anytime |
| 136 |
_shell = openstack_shell.OpenStackImagesShell() |
| 137 |
shell = lambda cmd: _shell.main(cmd.split()) |
| 138 |
|
| 139 |
def tearDown(self): |
| 140 |
super(ShellTest, self).tearDown() |
| 141 |
global _old_env |
| 142 |
os.environ = _old_env |
| 143 |
|
| 144 |
def shell(self, argstr, exitcodes=(0,)): |
| 145 |
orig = sys.stdout |
| 146 |
orig_stderr = sys.stderr |
| 147 |
try: |
| 148 |
sys.stdout = io.StringIO() |
| 149 |
sys.stderr = io.StringIO() |
| 150 |
_shell = openstack_shell.OpenStackImagesShell() |
| 151 |
_shell.main(argstr.split()) |
| 152 |
except SystemExit: |
| 153 |
exc_type, exc_value, exc_traceback = sys.exc_info() |
| 154 |
self.assertIn(exc_value.code, exitcodes) |
| 155 |
finally: |
| 156 |
stdout = sys.stdout.getvalue() |
| 157 |
sys.stdout.close() |
| 158 |
sys.stdout = orig |
| 159 |
stderr = sys.stderr.getvalue() |
| 160 |
sys.stderr.close() |
| 161 |
sys.stderr = orig_stderr |
| 162 |
return (stdout, stderr) |
| 163 |
|
| 164 |
def test_help_unknown_command(self): |
| 165 |
shell = openstack_shell.OpenStackImagesShell() |
| 166 |
argstr = '--os-image-api-version 2 help foofoo' |
| 167 |
self.assertRaises(exc.CommandError, shell.main, argstr.split()) |
| 168 |
|
| 169 |
@mock.patch('sys.stdout', io.StringIO()) |
| 170 |
@mock.patch('sys.stderr', io.StringIO()) |
| 171 |
@mock.patch('sys.argv', ['glance', 'help', 'foofoo']) |
| 172 |
def test_no_stacktrace_when_debug_disabled(self): |
| 173 |
with mock.patch.object(traceback, 'print_exc') as mock_print_exc: |
| 174 |
try: |
| 175 |
openstack_shell.main() |
| 176 |
except SystemExit: |
| 177 |
pass |
| 178 |
self.assertFalse(mock_print_exc.called) |
| 179 |
|
| 180 |
@mock.patch('sys.stdout', io.StringIO()) |
| 181 |
@mock.patch('sys.stderr', io.StringIO()) |
| 182 |
@mock.patch('sys.argv', ['glance', 'help', 'foofoo']) |
| 183 |
def test_stacktrace_when_debug_enabled_by_env(self): |
| 184 |
old_environment = os.environ.copy() |
| 185 |
os.environ = {'GLANCECLIENT_DEBUG': '1'} |
| 186 |
try: |
| 187 |
with mock.patch.object(traceback, 'print_exc') as mock_print_exc: |
| 188 |
try: |
| 189 |
openstack_shell.main() |
| 190 |
except SystemExit: |
| 191 |
pass |
| 192 |
self.assertTrue(mock_print_exc.called) |
| 193 |
finally: |
| 194 |
os.environ = old_environment |
| 195 |
|
| 196 |
@mock.patch('sys.stdout', io.StringIO()) |
| 197 |
@mock.patch('sys.stderr', io.StringIO()) |
| 198 |
@mock.patch('sys.argv', ['glance', '--debug', 'help', 'foofoo']) |
| 199 |
def test_stacktrace_when_debug_enabled(self): |
| 200 |
with mock.patch.object(traceback, 'print_exc') as mock_print_exc: |
| 201 |
try: |
| 202 |
openstack_shell.main() |
| 203 |
except SystemExit: |
| 204 |
pass |
| 205 |
self.assertTrue(mock_print_exc.called) |
| 206 |
|
| 207 |
def test_help(self): |
| 208 |
shell = openstack_shell.OpenStackImagesShell() |
| 209 |
argstr = '--os-image-api-version 2 help' |
| 210 |
with mock.patch.object(shell, '_get_keystone_auth_plugin') as et_mock: |
| 211 |
actual = shell.main(argstr.split()) |
| 212 |
self.assertEqual(0, actual) |
| 213 |
self.assertFalse(et_mock.called) |
| 214 |
|
| 215 |
def test_help_no_subcommand(self): |
| 216 |
shell = openstack_shell.OpenStackImagesShell() |
| 217 |
argstr = '--os-image-api-version 2' |
| 218 |
with mock.patch.object(shell, '_get_keystone_auth_plugin') as et_mock: |
| 219 |
actual = shell.main(argstr.split()) |
| 220 |
self.assertEqual(0, actual) |
| 221 |
self.assertFalse(et_mock.called) |
| 222 |
|
| 223 |
def test_blank_call(self): |
| 224 |
shell = openstack_shell.OpenStackImagesShell() |
| 225 |
with mock.patch.object(shell, '_get_keystone_auth_plugin') as et_mock: |
| 226 |
actual = shell.main('') |
| 227 |
self.assertEqual(0, actual) |
| 228 |
self.assertFalse(et_mock.called) |
| 229 |
|
| 230 |
def test_help_on_subcommand_error(self): |
| 231 |
self.assertRaises(exc.CommandError, shell, |
| 232 |
'--os-image-api-version 2 help bad') |
| 233 |
|
| 234 |
def test_help_v2_no_schema(self): |
| 235 |
shell = openstack_shell.OpenStackImagesShell() |
| 236 |
argstr = '--os-image-api-version 2 help image-create' |
| 237 |
with mock.patch.object(shell, '_get_keystone_auth_plugin') as et_mock: |
| 238 |
actual = shell.main(argstr.split()) |
| 239 |
self.assertEqual(0, actual) |
| 240 |
self.assertNotIn('<unavailable>', actual) |
| 241 |
self.assertFalse(et_mock.called) |
| 242 |
|
| 243 |
argstr = '--os-image-api-version 2 help md-namespace-create' |
| 244 |
with mock.patch.object(shell, '_get_keystone_auth_plugin') as et_mock: |
| 245 |
actual = shell.main(argstr.split()) |
| 246 |
self.assertEqual(0, actual) |
| 247 |
self.assertNotIn('<unavailable>', actual) |
| 248 |
self.assertFalse(et_mock.called) |
| 249 |
|
| 250 |
argstr = '--os-image-api-version 2 help md-resource-type-associate' |
| 251 |
with mock.patch.object(shell, '_get_keystone_auth_plugin') as et_mock: |
| 252 |
actual = shell.main(argstr.split()) |
| 253 |
self.assertEqual(0, actual) |
| 254 |
self.assertNotIn('<unavailable>', actual) |
| 255 |
self.assertFalse(et_mock.called) |
| 256 |
|
| 257 |
def test_get_base_parser(self): |
| 258 |
test_shell = openstack_shell.OpenStackImagesShell() |
| 259 |
|
| 260 |
|
| 261 |
actual_parser = test_shell.get_base_parser(sys.argv) |
| 262 |
description = 'Command-line interface to the OpenStack Images API.' |
| 263 |
expected = argparse.ArgumentParser( |
| 264 |
prog='glance', usage=None, |
| 265 |
description=description, |
| 266 |
conflict_handler='error', |
| 267 |
add_help=False, |
| 268 |
formatter_class=openstack_shell.HelpFormatter,) |
| 269 |
|
| 270 |
|
| 271 |
self.assertEqual(str(expected), str(actual_parser)) |
| 272 |
|
| 273 |
@mock.patch.object(openstack_shell.OpenStackImagesShell, |
| 274 |
'_get_versioned_client') |
| 275 |
def test_cert_and_key_args_interchangeable(self, |
| 276 |
mock_versioned_client): |
| 277 |
|
| 278 |
args = ('--os-image-api-version 2 ' |
| 279 |
'--os-cert mycert ' |
| 280 |
'--os-key mykey image-list') |
| 281 |
shell(args) |
| 282 |
assert mock_versioned_client.called |
| 283 |
((api_version, args), kwargs) = mock_versioned_client.call_args |
| 284 |
self.assertEqual('mycert', args.os_cert) |
| 285 |
self.assertEqual('mykey', args.os_key) |
| 286 |
|
| 287 |
@mock.patch('glanceclient.v1.client.Client') |
| 288 |
def test_no_auth_with_token_and_image_url_with_v1(self, v1_client): |
| 289 |
|
| 290 |
|
| 291 |
args = ('--os-image-api-version 1 --os-auth-token mytoken' |
| 292 |
' --os-image-url https://image:1234/v1 image-list') |
| 293 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 294 |
glance_shell.main(args.split()) |
| 295 |
assert v1_client.called |
| 296 |
(args, kwargs) = v1_client.call_args |
| 297 |
self.assertEqual('mytoken', kwargs['token']) |
| 298 |
self.assertEqual('https://image:1234', args[0]) |
| 299 |
|
| 300 |
@mock.patch('glanceclient.v2.client.Client') |
| 301 |
def test_no_auth_with_token_and_image_url_with_v2(self, v2_client): |
| 302 |
|
| 303 |
|
| 304 |
args = ('--os-image-api-version 2 --os-auth-token mytoken ' |
| 305 |
'--os-image-url https://image:1234 image-list') |
| 306 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 307 |
glance_shell.main(args.split()) |
| 308 |
self.assertTrue(v2_client.called) |
| 309 |
(args, kwargs) = v2_client.call_args |
| 310 |
self.assertEqual('mytoken', kwargs['token']) |
| 311 |
self.assertEqual('https://image:1234', args[0]) |
| 312 |
|
| 313 |
def _assert_auth_plugin_args(self): |
| 314 |
|
| 315 |
self.assertFalse(self.v3_auth.called) |
| 316 |
|
| 317 |
body = json.loads(self.v2_auth.last_request.body) |
| 318 |
|
| 319 |
self.assertEqual(self.auth_env['OS_TENANT_NAME'], |
| 320 |
body['auth']['tenantName']) |
| 321 |
self.assertEqual(self.auth_env['OS_USERNAME'], |
| 322 |
body['auth']['passwordCredentials']['username']) |
| 323 |
self.assertEqual(self.auth_env['OS_PASSWORD'], |
| 324 |
body['auth']['passwordCredentials']['password']) |
| 325 |
|
| 326 |
@mock.patch.object(openstack_shell.OpenStackImagesShell, '_cache_schemas', |
| 327 |
return_value=False) |
| 328 |
@mock.patch('glanceclient.v2.client.Client') |
| 329 |
def test_auth_plugin_invocation_without_version(self, |
| 330 |
v2_client, |
| 331 |
cache_schemas): |
| 332 |
|
| 333 |
cli2 = mock.MagicMock() |
| 334 |
v2_client.return_value = cli2 |
| 335 |
cli2.http_client.get.return_value = (None, {'versions': |
| 336 |
[{'id': 'v2'}]}) |
| 337 |
|
| 338 |
args = 'image-list' |
| 339 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 340 |
glance_shell.main(args.split()) |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
@mock.patch('glanceclient.v1.client.Client') |
| 347 |
def test_auth_plugin_invocation_with_v1(self, v1_client): |
| 348 |
args = '--os-image-api-version 1 image-list' |
| 349 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 350 |
glance_shell.main(args.split()) |
| 351 |
self.assertEqual(0, self.v2_auth.call_count) |
| 352 |
|
| 353 |
@mock.patch('glanceclient.v2.client.Client') |
| 354 |
def test_auth_plugin_invocation_with_v2(self, |
| 355 |
v2_client): |
| 356 |
args = '--os-image-api-version 2 image-list' |
| 357 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 358 |
glance_shell.main(args.split()) |
| 359 |
self.assertEqual(0, self.v2_auth.call_count) |
| 360 |
|
| 361 |
@mock.patch('glanceclient.v1.client.Client') |
| 362 |
def test_auth_plugin_invocation_with_unversioned_auth_url_with_v1( |
| 363 |
self, v1_client): |
| 364 |
args = ('--os-image-api-version 1 --os-auth-url %s image-list' % |
| 365 |
DEFAULT_UNVERSIONED_AUTH_URL) |
| 366 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 367 |
glance_shell.main(args.split()) |
| 368 |
|
| 369 |
@mock.patch('glanceclient.v2.client.Client') |
| 370 |
@mock.patch.object(openstack_shell.OpenStackImagesShell, '_cache_schemas', |
| 371 |
return_value=False) |
| 372 |
def test_auth_plugin_invocation_with_unversioned_auth_url_with_v2( |
| 373 |
self, v2_client, cache_schemas): |
| 374 |
args = ('--os-auth-url %s --os-image-api-version 2 ' |
| 375 |
'image-list') % DEFAULT_UNVERSIONED_AUTH_URL |
| 376 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 377 |
glance_shell.main(args.split()) |
| 378 |
|
| 379 |
@mock.patch('glanceclient.Client') |
| 380 |
def test_endpoint_token_no_auth_req(self, mock_client): |
| 381 |
|
| 382 |
def verify_input(version=None, endpoint=None, *args, **kwargs): |
| 383 |
self.assertIn('token', kwargs) |
| 384 |
self.assertEqual(TOKEN_ID, kwargs['token']) |
| 385 |
self.assertEqual(DEFAULT_IMAGE_URL, endpoint) |
| 386 |
return mock.MagicMock() |
| 387 |
|
| 388 |
mock_client.side_effect = verify_input |
| 389 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 390 |
args = ['--os-image-api-version', '2', |
| 391 |
'--os-auth-token', TOKEN_ID, |
| 392 |
'--os-image-url', DEFAULT_IMAGE_URL, |
| 393 |
'image-list'] |
| 394 |
|
| 395 |
glance_shell.main(args) |
| 396 |
self.assertEqual(1, mock_client.call_count) |
| 397 |
|
| 398 |
@mock.patch('sys.stdin', side_effect=mock.MagicMock) |
| 399 |
@mock.patch('getpass.getpass', side_effect=EOFError) |
| 400 |
@mock.patch('glanceclient.v2.client.Client') |
| 401 |
def test_password_prompted_ctrlD_with_v2(self, v2_client, |
| 402 |
mock_getpass, mock_stdin): |
| 403 |
cli2 = mock.MagicMock() |
| 404 |
v2_client.return_value = cli2 |
| 405 |
cli2.http_client.get.return_value = (None, {'versions': []}) |
| 406 |
|
| 407 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 408 |
self.make_env(exclude='OS_PASSWORD') |
| 409 |
|
| 410 |
self.assertRaises(exc.CommandError, glance_shell.main, ['image-list']) |
| 411 |
|
| 412 |
mock_getpass.assert_called_with('OS Password: ') |
| 413 |
|
| 414 |
@mock.patch( |
| 415 |
'glanceclient.shell.OpenStackImagesShell._get_keystone_auth_plugin') |
| 416 |
@mock.patch.object(openstack_shell.OpenStackImagesShell, '_cache_schemas', |
| 417 |
return_value=False) |
| 418 |
def test_no_auth_with_proj_name(self, cache_schemas, session): |
| 419 |
with mock.patch('glanceclient.v2.client.Client'): |
| 420 |
args = ('--os-project-name myname ' |
| 421 |
'--os-project-domain-name mydomain ' |
| 422 |
'--os-project-domain-id myid ' |
| 423 |
'--os-image-api-version 2 image-list') |
| 424 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 425 |
glance_shell.main(args.split()) |
| 426 |
((args), kwargs) = session.call_args |
| 427 |
self.assertEqual('myname', kwargs['project_name']) |
| 428 |
self.assertEqual('mydomain', kwargs['project_domain_name']) |
| 429 |
self.assertEqual('myid', kwargs['project_domain_id']) |
| 430 |
|
| 431 |
@mock.patch.object(openstack_shell.OpenStackImagesShell, 'main') |
| 432 |
def test_shell_keyboard_interrupt(self, mock_glance_shell): |
| 433 |
|
| 434 |
try: |
| 435 |
mock_glance_shell.side_effect = KeyboardInterrupt() |
| 436 |
openstack_shell.main() |
| 437 |
except SystemExit as ex: |
| 438 |
self.assertEqual(130, ex.code) |
| 439 |
|
| 440 |
@mock.patch('glanceclient.common.utils.exit', side_effect=utils.exit) |
| 441 |
def test_shell_illegal_version(self, mock_exit): |
| 442 |
|
| 443 |
shell = openstack_shell.OpenStackImagesShell() |
| 444 |
argstr = '--os-image-api-version 1.1 image-list' |
| 445 |
try: |
| 446 |
shell.main(argstr.split()) |
| 447 |
except SystemExit as ex: |
| 448 |
self.assertEqual(1, ex.code) |
| 449 |
msg = ("Invalid API version parameter. " |
| 450 |
"Supported values are %s" % openstack_shell.SUPPORTED_VERSIONS) |
| 451 |
mock_exit.assert_called_with(msg=msg) |
| 452 |
|
| 453 |
@mock.patch('glanceclient.common.utils.exit', side_effect=utils.exit) |
| 454 |
def test_shell_unsupported_version(self, mock_exit): |
| 455 |
|
| 456 |
shell = openstack_shell.OpenStackImagesShell() |
| 457 |
argstr = '--os-image-api-version -1 image-list' |
| 458 |
try: |
| 459 |
shell.main(argstr.split()) |
| 460 |
except SystemExit as ex: |
| 461 |
self.assertEqual(1, ex.code) |
| 462 |
msg = ("Invalid API version parameter. " |
| 463 |
"Supported values are %s" % openstack_shell.SUPPORTED_VERSIONS) |
| 464 |
mock_exit.assert_called_with(msg=msg) |
| 465 |
|
| 466 |
@mock.patch.object(openstack_shell.OpenStackImagesShell, |
| 467 |
'get_subcommand_parser') |
| 468 |
def test_shell_import_error_with_mesage(self, mock_parser): |
| 469 |
msg = 'Unable to import module xxx' |
| 470 |
mock_parser.side_effect = ImportError('%s' % msg) |
| 471 |
shell = openstack_shell.OpenStackImagesShell() |
| 472 |
argstr = '--os-image-api-version 2 image-list' |
| 473 |
try: |
| 474 |
shell.main(argstr.split()) |
| 475 |
self.fail('No import error returned') |
| 476 |
except ImportError as e: |
| 477 |
self.assertEqual(msg, str(e)) |
| 478 |
|
| 479 |
@mock.patch.object(openstack_shell.OpenStackImagesShell, |
| 480 |
'get_subcommand_parser') |
| 481 |
def test_shell_import_error_default_message(self, mock_parser): |
| 482 |
mock_parser.side_effect = ImportError |
| 483 |
shell = openstack_shell.OpenStackImagesShell() |
| 484 |
argstr = '--os-image-api-version 2 image-list' |
| 485 |
try: |
| 486 |
shell.main(argstr.split()) |
| 487 |
self.fail('No import error returned') |
| 488 |
except ImportError as e: |
| 489 |
msg = 'Unable to import module. Re-run with --debug for more info.' |
| 490 |
self.assertEqual(msg, str(e)) |
| 491 |
|
| 492 |
@mock.patch('glanceclient.v2.client.Client') |
| 493 |
@mock.patch('glanceclient.v1.images.ImageManager.list') |
| 494 |
def test_shell_v1_fallback_from_v2(self, v1_imgs, v2_client): |
| 495 |
self.make_env() |
| 496 |
cli2 = mock.MagicMock() |
| 497 |
v2_client.return_value = cli2 |
| 498 |
cli2.http_client.get.return_value = (None, {'versions': []}) |
| 499 |
args = 'image-list' |
| 500 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 501 |
glance_shell.main(args.split()) |
| 502 |
self.assertFalse(cli2.schemas.get.called) |
| 503 |
self.assertTrue(v1_imgs.called) |
| 504 |
|
| 505 |
@mock.patch.object(openstack_shell.OpenStackImagesShell, |
| 506 |
'_cache_schemas') |
| 507 |
@mock.patch('glanceclient.v2.client.Client') |
| 508 |
def test_shell_no_fallback_from_v2(self, v2_client, cache_schemas): |
| 509 |
self.make_env() |
| 510 |
cli2 = mock.MagicMock() |
| 511 |
v2_client.return_value = cli2 |
| 512 |
cli2.http_client.get.return_value = (None, |
| 513 |
{'versions': [{'id': 'v2'}]}) |
| 514 |
cache_schemas.return_value = False |
| 515 |
args = 'image-list' |
| 516 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 517 |
glance_shell.main(args.split()) |
| 518 |
self.assertTrue(cli2.images.list.called) |
| 519 |
|
| 520 |
@mock.patch('glanceclient.v1.client.Client') |
| 521 |
def test_auth_plugin_invocation_without_username_with_v1(self, v1_client): |
| 522 |
self.make_env(exclude='OS_USERNAME') |
| 523 |
args = '--os-image-api-version 2 image-list' |
| 524 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 525 |
self.assertRaises(exc.CommandError, glance_shell.main, args.split()) |
| 526 |
|
| 527 |
@mock.patch('glanceclient.v2.client.Client') |
| 528 |
def test_auth_plugin_invocation_without_username_with_v2(self, v2_client): |
| 529 |
self.make_env(exclude='OS_USERNAME') |
| 530 |
args = '--os-image-api-version 2 image-list' |
| 531 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 532 |
self.assertRaises(exc.CommandError, glance_shell.main, args.split()) |
| 533 |
|
| 534 |
@mock.patch('glanceclient.v1.client.Client') |
| 535 |
def test_auth_plugin_invocation_without_auth_url_with_v1(self, v1_client): |
| 536 |
self.make_env(exclude='OS_AUTH_URL') |
| 537 |
args = '--os-image-api-version 1 image-list' |
| 538 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 539 |
self.assertRaises(exc.CommandError, glance_shell.main, args.split()) |
| 540 |
|
| 541 |
@mock.patch('glanceclient.v2.client.Client') |
| 542 |
def test_auth_plugin_invocation_without_auth_url_with_v2(self, v2_client): |
| 543 |
self.make_env(exclude='OS_AUTH_URL') |
| 544 |
args = '--os-image-api-version 2 image-list' |
| 545 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 546 |
self.assertRaises(exc.CommandError, glance_shell.main, args.split()) |
| 547 |
|
| 548 |
@mock.patch('glanceclient.v1.client.Client') |
| 549 |
def test_auth_plugin_invocation_without_tenant_with_v1(self, v1_client): |
| 550 |
if 'OS_TENANT_NAME' in os.environ: |
| 551 |
self.make_env(exclude='OS_TENANT_NAME') |
| 552 |
if 'OS_PROJECT_ID' in os.environ: |
| 553 |
self.make_env(exclude='OS_PROJECT_ID') |
| 554 |
args = '--os-image-api-version 1 image-list' |
| 555 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 556 |
self.assertRaises(exc.CommandError, glance_shell.main, args.split()) |
| 557 |
|
| 558 |
@mock.patch('glanceclient.v2.client.Client') |
| 559 |
@mock.patch.object(openstack_shell.OpenStackImagesShell, '_cache_schemas', |
| 560 |
return_value=False) |
| 561 |
def test_auth_plugin_invocation_without_tenant_with_v2(self, v2_client, |
| 562 |
cache_schemas): |
| 563 |
if 'OS_TENANT_NAME' in os.environ: |
| 564 |
self.make_env(exclude='OS_TENANT_NAME') |
| 565 |
if 'OS_PROJECT_ID' in os.environ: |
| 566 |
self.make_env(exclude='OS_PROJECT_ID') |
| 567 |
args = '--os-image-api-version 2 image-list' |
| 568 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 569 |
self.assertRaises(exc.CommandError, glance_shell.main, args.split()) |
| 570 |
|
| 571 |
@mock.patch('sys.argv', ['glance']) |
| 572 |
@mock.patch('sys.stdout', io.StringIO()) |
| 573 |
@mock.patch('sys.stderr', io.StringIO()) |
| 574 |
def test_main_noargs(self): |
| 575 |
|
| 576 |
try: |
| 577 |
openstack_shell.main() |
| 578 |
except SystemExit: |
| 579 |
self.fail('Unexpected SystemExit') |
| 580 |
|
| 581 |
|
| 582 |
expected = ['Command-line interface to the OpenStack Images API', |
| 583 |
'image-list', |
| 584 |
'image-deactivate', |
| 585 |
'location-add'] |
| 586 |
for output in expected: |
| 587 |
self.assertIn(output, |
| 588 |
sys.stdout.getvalue()) |
| 589 |
|
| 590 |
@mock.patch('glanceclient.v2.client.Client') |
| 591 |
@mock.patch('glanceclient.v1.shell.do_image_list') |
| 592 |
@mock.patch('glanceclient.shell.logging.basicConfig') |
| 593 |
def test_setup_debug(self, conf, func, v2_client): |
| 594 |
cli2 = mock.MagicMock() |
| 595 |
v2_client.return_value = cli2 |
| 596 |
cli2.http_client.get.return_value = (None, {'versions': []}) |
| 597 |
args = '--debug image-list' |
| 598 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 599 |
glance_shell.main(args.split()) |
| 600 |
glance_logger = logging.getLogger('glanceclient') |
| 601 |
self.assertEqual(glance_logger.getEffectiveLevel(), logging.DEBUG) |
| 602 |
conf.assert_called_with(level=logging.DEBUG) |
| 603 |
|
| 604 |
def test_subcommand_help(self): |
| 605 |
|
| 606 |
stdout, stderr = self.shell('help stores-delete') |
| 607 |
|
| 608 |
expected = 'usage: glance stores-delete --store <STORE_ID> ' \ |
| 609 |
'<IMAGE_ID>\n\nDelete image from specific store.' \ |
| 610 |
'\n\nPositional arguments:\n <IMAGE_ID> ' \ |
| 611 |
'ID of image to update.\n\nRequired arguments:\n ' \ |
| 612 |
'--store <STORE_ID> Store to delete image from.\n' |
| 613 |
self.assertEqual(expected, stdout) |
| 614 |
|
| 615 |
|
| 616 |
class ShellTestWithKeystoneV3Auth(ShellTest): |
| 617 |
|
| 618 |
auth_env = FAKE_V3_ENV.copy() |
| 619 |
token_url = DEFAULT_V3_AUTH_URL + '/auth/tokens' |
| 620 |
|
| 621 |
def _assert_auth_plugin_args(self): |
| 622 |
self.assertFalse(self.v2_auth.called) |
| 623 |
|
| 624 |
body = json.loads(self.v3_auth.last_request.body) |
| 625 |
user = body['auth']['identity']['password']['user'] |
| 626 |
|
| 627 |
self.assertEqual(self.auth_env['OS_USERNAME'], user['name']) |
| 628 |
self.assertEqual(self.auth_env['OS_PASSWORD'], user['password']) |
| 629 |
self.assertEqual(self.auth_env['OS_USER_DOMAIN_NAME'], |
| 630 |
user['domain']['name']) |
| 631 |
self.assertEqual(self.auth_env['OS_PROJECT_ID'], |
| 632 |
body['auth']['scope']['project']['id']) |
| 633 |
|
| 634 |
@mock.patch('glanceclient.v1.client.Client') |
| 635 |
def test_auth_plugin_invocation_with_v1(self, v1_client): |
| 636 |
args = '--os-image-api-version 1 image-list' |
| 637 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 638 |
glance_shell.main(args.split()) |
| 639 |
self.assertEqual(0, self.v3_auth.call_count) |
| 640 |
|
| 641 |
@mock.patch('glanceclient.v2.client.Client') |
| 642 |
def test_auth_plugin_invocation_with_v2(self, v2_client): |
| 643 |
args = '--os-image-api-version 2 image-list' |
| 644 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 645 |
glance_shell.main(args.split()) |
| 646 |
self.assertEqual(0, self.v3_auth.call_count) |
| 647 |
|
| 648 |
@mock.patch('keystoneauth1.discover.Discover', |
| 649 |
side_effect=ks_exc.ClientException()) |
| 650 |
def test_api_discovery_failed_with_unversioned_auth_url(self, |
| 651 |
discover): |
| 652 |
args = ('--os-image-api-version 2 --os-auth-url %s image-list' |
| 653 |
% DEFAULT_UNVERSIONED_AUTH_URL) |
| 654 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 655 |
self.assertRaises(exc.CommandError, glance_shell.main, args.split()) |
| 656 |
|
| 657 |
def test_bash_completion(self): |
| 658 |
stdout, stderr = self.shell('--os-image-api-version 2 bash_completion') |
| 659 |
|
| 660 |
required = [ |
| 661 |
'--status', |
| 662 |
'image-create', |
| 663 |
'help', |
| 664 |
'--size'] |
| 665 |
for r in required: |
| 666 |
self.assertIn(r, stdout.split()) |
| 667 |
avoided = [ |
| 668 |
'bash_completion', |
| 669 |
'bash-completion'] |
| 670 |
for r in avoided: |
| 671 |
self.assertNotIn(r, stdout.split()) |
| 672 |
|
| 673 |
|
| 674 |
class ShellTestWithNoOSImageURLPublic(ShellTestWithKeystoneV3Auth): |
| 675 |
|
| 676 |
|
| 677 |
auth_env = FAKE_V4_ENV.copy() |
| 678 |
|
| 679 |
def setUp(self): |
| 680 |
super(ShellTestWithNoOSImageURLPublic, self).setUp() |
| 681 |
self.image_url = DEFAULT_IMAGE_URL |
| 682 |
self.requests.get(DEFAULT_IMAGE_URL + 'v2/images', |
| 683 |
text='{"images": []}') |
| 684 |
|
| 685 |
@mock.patch('glanceclient.v1.client.Client') |
| 686 |
def test_auth_plugin_invocation_with_v1(self, v1_client): |
| 687 |
args = '--os-image-api-version 1 image-list' |
| 688 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 689 |
glance_shell.main(args.split()) |
| 690 |
self.assertEqual(1, self.v3_auth.call_count) |
| 691 |
self._assert_auth_plugin_args() |
| 692 |
|
| 693 |
@mock.patch('glanceclient.v2.client.Client') |
| 694 |
def test_auth_plugin_invocation_with_v2(self, v2_client): |
| 695 |
args = '--os-image-api-version 2 image-list' |
| 696 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 697 |
glance_shell.main(args.split()) |
| 698 |
self.assertEqual(1, self.v3_auth.call_count) |
| 699 |
self._assert_auth_plugin_args() |
| 700 |
|
| 701 |
@mock.patch('glanceclient.v2.client.Client') |
| 702 |
def test_endpoint_from_interface(self, v2_client): |
| 703 |
args = ('--os-image-api-version 2 image-list') |
| 704 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 705 |
glance_shell.main(args.split()) |
| 706 |
assert v2_client.called |
| 707 |
(args, kwargs) = v2_client.call_args |
| 708 |
self.assertEqual(self.image_url, kwargs['endpoint_override']) |
| 709 |
|
| 710 |
def test_endpoint_real_from_interface(self): |
| 711 |
args = ('--os-image-api-version 2 image-list') |
| 712 |
glance_shell = openstack_shell.OpenStackImagesShell() |
| 713 |
glance_shell.main(args.split()) |
| 714 |
self.assertEqual(self.requests.request_history[2].url, |
| 715 |
self.image_url + "v2/images?" |
| 716 |
f"limit={DEFAULT_PAGE_SIZE}&" |
| 717 |
"sort_key=name&sort_dir=asc") |
| 718 |
|
| 719 |
|
| 720 |
class ShellTestWithNoOSImageURLInternal(ShellTestWithNoOSImageURLPublic): |
| 721 |
|
| 722 |
|
| 723 |
FAKE_V5_ENV = FAKE_V4_ENV.copy() |
| 724 |
FAKE_V5_ENV['OS_ENDPOINT_TYPE'] = 'internal' |
| 725 |
auth_env = FAKE_V5_ENV.copy() |
| 726 |
|
| 727 |
def setUp(self): |
| 728 |
super(ShellTestWithNoOSImageURLPublic, self).setUp() |
| 729 |
self.image_url = DEFAULT_IMAGE_URL_INTERNAL |
| 730 |
self.requests.get(DEFAULT_IMAGE_URL_INTERNAL + 'v2/images', |
| 731 |
text='{"images": []}') |
| 732 |
|
| 733 |
|
| 734 |
class ShellCacheSchemaTest(testutils.TestCase): |
| 735 |
def setUp(self): |
| 736 |
super(ShellCacheSchemaTest, self).setUp() |
| 737 |
self._mock_client_setup() |
| 738 |
self._mock_shell_setup() |
| 739 |
self.cache_dir = '/dir_for_cached_schema' |
| 740 |
self.os_auth_url = 'http://localhost:5000/v2' |
| 741 |
url_hex = hashlib.sha1(self.os_auth_url.encode('utf-8')).hexdigest() |
| 742 |
self.prefix_path = (self.cache_dir + '/' + url_hex) |
| 743 |
self.cache_files = [self.prefix_path + '/image_schema.json', |
| 744 |
self.prefix_path + '/namespace_schema.json', |
| 745 |
self.prefix_path + '/resource_type_schema.json'] |
| 746 |
|
| 747 |
def tearDown(self): |
| 748 |
super(ShellCacheSchemaTest, self).tearDown() |
| 749 |
|
| 750 |
def _mock_client_setup(self): |
| 751 |
self.schema_dict = { |
| 752 |
'name': 'image', |
| 753 |
'properties': { |
| 754 |
'name': {'type': 'string', 'description': 'Name of image'}, |
| 755 |
}, |
| 756 |
} |
| 757 |
|
| 758 |
self.client = mock.Mock() |
| 759 |
schema_odict = OrderedDict(self.schema_dict) |
| 760 |
self.client.schemas.get.return_value = schemas.Schema(schema_odict) |
| 761 |
|
| 762 |
def _mock_shell_setup(self): |
| 763 |
self.shell = openstack_shell.OpenStackImagesShell() |
| 764 |
self.shell._get_versioned_client = mock.create_autospec( |
| 765 |
self.shell._get_versioned_client, return_value=self.client, |
| 766 |
spec_set=True |
| 767 |
) |
| 768 |
|
| 769 |
def _make_args(self, args): |
| 770 |
class Args(object): |
| 771 |
def __init__(self, entries): |
| 772 |
self.__dict__.update(entries) |
| 773 |
|
| 774 |
return Args(args) |
| 775 |
|
| 776 |
@mock.patch('builtins.open', new=mock.mock_open(), create=True) |
| 777 |
@mock.patch('os.path.exists', return_value=True) |
| 778 |
def test_cache_schemas_gets_when_forced(self, exists_mock): |
| 779 |
options = { |
| 780 |
'get_schema': True, |
| 781 |
'os_auth_url': self.os_auth_url |
| 782 |
} |
| 783 |
schema_odict = OrderedDict(self.schema_dict) |
| 784 |
|
| 785 |
args = self._make_args(options) |
| 786 |
client = self.shell._get_versioned_client('2', args) |
| 787 |
self.shell._cache_schemas(args, client, home_dir=self.cache_dir) |
| 788 |
|
| 789 |
self.assertEqual(len(self.cache_files), len(open.call_args_list)) |
| 790 |
self.assertEqual(mock.call(self.cache_files[0], 'w'), |
| 791 |
open.call_args_list[0]) |
| 792 |
self.assertEqual(mock.call(self.cache_files[1], 'w'), |
| 793 |
open.call_args_list[1]) |
| 794 |
actual = json.loads(open().write.call_args_list[0][0][0]) |
| 795 |
self.assertEqual(schema_odict, OrderedDict(actual)) |
| 796 |
actual = json.loads(open().write.call_args_list[1][0][0]) |
| 797 |
self.assertEqual(schema_odict, OrderedDict(actual)) |
| 798 |
|
| 799 |
@mock.patch('builtins.open', new=mock.mock_open(), create=True) |
| 800 |
@mock.patch('os.path.exists', side_effect=[True, False, False, False]) |
| 801 |
def test_cache_schemas_gets_when_not_exists(self, exists_mock): |
| 802 |
options = { |
| 803 |
'get_schema': False, |
| 804 |
'os_auth_url': self.os_auth_url |
| 805 |
} |
| 806 |
schema_odict = OrderedDict(self.schema_dict) |
| 807 |
|
| 808 |
args = self._make_args(options) |
| 809 |
client = self.shell._get_versioned_client('2', args) |
| 810 |
self.shell._cache_schemas(args, client, home_dir=self.cache_dir) |
| 811 |
|
| 812 |
self.assertEqual(len(self.cache_files), len(open.call_args_list)) |
| 813 |
self.assertEqual(mock.call(self.cache_files[0], 'w'), |
| 814 |
open.call_args_list[0]) |
| 815 |
self.assertEqual(mock.call(self.cache_files[1], 'w'), |
| 816 |
open.call_args_list[1]) |
| 817 |
actual = json.loads(open().write.call_args_list[0][0][0]) |
| 818 |
self.assertEqual(schema_odict, OrderedDict(actual)) |
| 819 |
actual = json.loads(open().write.call_args_list[1][0][0]) |
| 820 |
self.assertEqual(schema_odict, OrderedDict(actual)) |
| 821 |
|
| 822 |
@mock.patch('builtins.open', new=mock.mock_open(), create=True) |
| 823 |
@mock.patch('os.path.exists', return_value=True) |
| 824 |
def test_cache_schemas_leaves_when_present_not_forced(self, exists_mock): |
| 825 |
options = { |
| 826 |
'get_schema': False, |
| 827 |
'os_auth_url': self.os_auth_url |
| 828 |
} |
| 829 |
|
| 830 |
client = mock.MagicMock() |
| 831 |
self.shell._cache_schemas(self._make_args(options), |
| 832 |
client, home_dir=self.cache_dir) |
| 833 |
|
| 834 |
exists_mock.assert_has_calls([ |
| 835 |
mock.call(self.prefix_path), |
| 836 |
mock.call(self.cache_files[0]), |
| 837 |
mock.call(self.cache_files[1]), |
| 838 |
mock.call(self.cache_files[2]) |
| 839 |
]) |
| 840 |
self.assertEqual(4, exists_mock.call_count) |
| 841 |
self.assertEqual(0, open.mock_calls.__len__()) |
| 842 |
|
| 843 |
@mock.patch('builtins.open', new=mock.mock_open(), create=True) |
| 844 |
@mock.patch('os.path.exists', return_value=True) |
| 845 |
def test_cache_schemas_leaves_auto_switch(self, exists_mock): |
| 846 |
options = { |
| 847 |
'get_schema': True, |
| 848 |
'os_auth_url': self.os_auth_url |
| 849 |
} |
| 850 |
|
| 851 |
self.client.schemas.get.return_value = Exception() |
| 852 |
|
| 853 |
client = mock.MagicMock() |
| 854 |
switch_version = self.shell._cache_schemas(self._make_args(options), |
| 855 |
client, |
| 856 |
home_dir=self.cache_dir) |
| 857 |
self.assertEqual(True, switch_version) |
| 858 |
|
| 859 |
|
| 860 |
class ShellTestRequests(testutils.TestCase): |
| 861 |
"""Shell tests using the requests mock library.""" |
| 862 |
def _make_args(self, args): |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
class Args(object): |
| 868 |
def __init__(self, entries): |
| 869 |
self.__dict__.update(entries) |
| 870 |
|
| 871 |
return Args(args) |
| 872 |
|
| 873 |
def setUp(self): |
| 874 |
super(ShellTestRequests, self).setUp() |
| 875 |
self._old_env = os.environ |
| 876 |
os.environ = {} |
| 877 |
|
| 878 |
def tearDown(self): |
| 879 |
super(ShellTestRequests, self).tearDown() |
| 880 |
os.environ = self._old_env |
| 881 |
|
| 882 |
def test_download_has_no_stray_output_to_stdout(self): |
| 883 |
"""Regression test for bug 1488914""" |
| 884 |
saved_stdout = sys.stdout |
| 885 |
try: |
| 886 |
sys.stdout = output = testutils.FakeNoTTYStdout() |
| 887 |
id = image_show_fixture['id'] |
| 888 |
self.requests = self.useFixture(rm_fixture.Fixture()) |
| 889 |
self.requests.get('http://example.com/versions', |
| 890 |
json=image_versions_fixture) |
| 891 |
|
| 892 |
headers = {'Content-Length': '4', |
| 893 |
'Content-type': 'application/octet-stream'} |
| 894 |
fake = testutils.FakeResponse(headers, io.StringIO('DATA')) |
| 895 |
self.requests.get('http://example.com/v1/images/%s' % id, |
| 896 |
raw=fake) |
| 897 |
|
| 898 |
self.requests.get('http://example.com/v1/images/detail' |
| 899 |
'?sort_key=name&sort_dir=asc&limit=20') |
| 900 |
|
| 901 |
headers = {'X-Image-Meta-Id': id} |
| 902 |
self.requests.head('http://example.com/v1/images/%s' % id, |
| 903 |
headers=headers) |
| 904 |
|
| 905 |
with mock.patch.object(openstack_shell.OpenStackImagesShell, |
| 906 |
'_cache_schemas') as mocked_cache_schema: |
| 907 |
mocked_cache_schema.return_value = True |
| 908 |
shell = openstack_shell.OpenStackImagesShell() |
| 909 |
argstr = ('--os-auth-token faketoken ' |
| 910 |
'--os-image-url http://example.com ' |
| 911 |
'image-download %s' % id) |
| 912 |
shell.main(argstr.split()) |
| 913 |
self.assertTrue(mocked_cache_schema.called) |
| 914 |
|
| 915 |
self.assertEqual('DATA', output.getvalue()) |
| 916 |
finally: |
| 917 |
sys.stdout = saved_stdout |
| 918 |
|
| 919 |
def test_v1_download_has_no_stray_output_to_stdout(self): |
| 920 |
"""Ensure no stray print statements corrupt the image""" |
| 921 |
saved_stdout = sys.stdout |
| 922 |
try: |
| 923 |
sys.stdout = output = testutils.FakeNoTTYStdout() |
| 924 |
id = image_show_fixture['id'] |
| 925 |
|
| 926 |
self.requests = self.useFixture(rm_fixture.Fixture()) |
| 927 |
headers = {'X-Image-Meta-Id': id} |
| 928 |
self.requests.head('http://example.com/v1/images/%s' % id, |
| 929 |
headers=headers) |
| 930 |
|
| 931 |
headers = {'Content-Length': '4', |
| 932 |
'Content-type': 'application/octet-stream'} |
| 933 |
fake = testutils.FakeResponse(headers, io.StringIO('DATA')) |
| 934 |
self.requests.get('http://example.com/v1/images/%s' % id, |
| 935 |
headers=headers, raw=fake) |
| 936 |
|
| 937 |
shell = openstack_shell.OpenStackImagesShell() |
| 938 |
argstr = ('--os-image-api-version 1 --os-auth-token faketoken ' |
| 939 |
'--os-image-url http://example.com ' |
| 940 |
'image-download %s' % id) |
| 941 |
shell.main(argstr.split()) |
| 942 |
|
| 943 |
self.assertEqual('DATA', output.getvalue()) |
| 944 |
finally: |
| 945 |
sys.stdout = saved_stdout |
| 946 |
|
| 947 |
def test_v2_download_has_no_stray_output_to_stdout(self): |
| 948 |
"""Ensure no stray print statements corrupt the image""" |
| 949 |
saved_stdout = sys.stdout |
| 950 |
try: |
| 951 |
sys.stdout = output = testutils.FakeNoTTYStdout() |
| 952 |
id = image_show_fixture['id'] |
| 953 |
headers = {'Content-Length': '4', |
| 954 |
'Content-type': 'application/octet-stream'} |
| 955 |
fake = testutils.FakeResponse(headers, io.StringIO('DATA')) |
| 956 |
|
| 957 |
self.requests = self.useFixture(rm_fixture.Fixture()) |
| 958 |
self.requests.get('http://example.com/v2/images/%s/file' % id, |
| 959 |
headers=headers, raw=fake) |
| 960 |
self.requests.get('http://example.com/v2/images/%s' % id, |
| 961 |
headers={'Content-type': 'application/json'}, |
| 962 |
json=image_show_fixture) |
| 963 |
shell = openstack_shell.OpenStackImagesShell() |
| 964 |
argstr = ('--os-image-api-version 2 --os-auth-token faketoken ' |
| 965 |
'--os-image-url http://example.com ' |
| 966 |
'image-download %s' % id) |
| 967 |
shell.main(argstr.split()) |
| 968 |
|
| 969 |
self.assertEqual('DATA', output.getvalue()) |
| 970 |
finally: |
| 971 |
sys.stdout = saved_stdout |