/usr/lib/python2.7/dist-packages/ironicclient/tests/unit/common/test_http.py is in python-ironicclient 2.2.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 | # Copyright 2012 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import time
import mock
from oslo_serialization import jsonutils
import requests
import six
from six.moves import http_client
from keystoneauth1 import exceptions as kexc
from ironicclient.common import filecache
from ironicclient.common import http
from ironicclient import exc
from ironicclient.tests.unit import utils
DEFAULT_TIMEOUT = 600
DEFAULT_HOST = 'localhost'
DEFAULT_PORT = '1234'
def _get_error_body(faultstring=None, debuginfo=None, description=None):
if description:
error_body = {'description': description}
else:
error_body = {
'faultstring': faultstring,
'debuginfo': debuginfo
}
raw_error_body = jsonutils.dump_as_bytes(error_body)
body = {'error_message': raw_error_body}
return jsonutils.dumps(body)
def _session_client(**kwargs):
return http.SessionClient(os_ironic_api_version='1.6',
api_version_select_state='default',
max_retries=5,
retry_interval=2,
auth=None,
interface='publicURL',
service_type='baremetal',
region_name='',
endpoint='http://%s:%s' % (DEFAULT_HOST,
DEFAULT_PORT),
**kwargs)
class VersionNegotiationMixinTest(utils.BaseTestCase):
def setUp(self):
super(VersionNegotiationMixinTest, self).setUp()
self.test_object = http.VersionNegotiationMixin()
self.test_object.os_ironic_api_version = '1.6'
self.test_object.api_version_select_state = 'default'
self.test_object.endpoint = "http://localhost:1234"
self.mock_mcu = mock.MagicMock()
self.test_object._make_connection_url = self.mock_mcu
self.response = utils.FakeResponse(
{}, status=http_client.NOT_ACCEPTABLE)
self.test_object.get_server = mock.MagicMock(
return_value=('localhost', '1234'))
def test__generic_parse_version_headers_has_headers(self):
response = {'X-OpenStack-Ironic-API-Minimum-Version': '1.1',
'X-OpenStack-Ironic-API-Maximum-Version': '1.6',
}
expected = ('1.1', '1.6')
result = self.test_object._generic_parse_version_headers(response.get)
self.assertEqual(expected, result)
def test__generic_parse_version_headers_missing_headers(self):
response = {}
expected = (None, None)
result = self.test_object._generic_parse_version_headers(response.get)
self.assertEqual(expected, result)
@mock.patch.object(filecache, 'save_data', autospec=True)
def test_negotiate_version_bad_state(self, mock_save_data):
# Test if bad api_version_select_state value
self.test_object.api_version_select_state = 'word of the day: augur'
self.assertRaises(
RuntimeError,
self.test_object.negotiate_version,
None, None)
self.assertEqual(0, mock_save_data.call_count)
@mock.patch.object(filecache, 'save_data', autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_parse_version_headers',
autospec=True)
def test_negotiate_version_server_older(self, mock_pvh, mock_save_data):
# Test newer client and older server
latest_ver = '1.5'
mock_pvh.return_value = ('1.1', latest_ver)
mock_conn = mock.MagicMock()
result = self.test_object.negotiate_version(mock_conn, self.response)
self.assertEqual(latest_ver, result)
self.assertEqual(1, mock_pvh.call_count)
host, port = http.get_server(self.test_object.endpoint)
mock_save_data.assert_called_once_with(host=host, port=port,
data=latest_ver)
@mock.patch.object(filecache, 'save_data', autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_parse_version_headers',
autospec=True)
def test_negotiate_version_server_newer(self, mock_pvh, mock_save_data):
# Test newer server and older client
mock_pvh.return_value = ('1.1', '1.10')
mock_conn = mock.MagicMock()
result = self.test_object.negotiate_version(mock_conn, self.response)
self.assertEqual('1.6', result)
self.assertEqual(1, mock_pvh.call_count)
mock_save_data.assert_called_once_with(host=DEFAULT_HOST,
port=DEFAULT_PORT,
data='1.6')
@mock.patch.object(filecache, 'save_data', autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_make_simple_request',
autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_parse_version_headers',
autospec=True)
def test_negotiate_version_server_no_version_on_error(
self, mock_pvh, mock_msr, mock_save_data):
# Test older Ironic version which errored with no version number and
# have to retry with simple get
mock_pvh.side_effect = iter([(None, None), ('1.1', '1.2')])
mock_conn = mock.MagicMock()
result = self.test_object.negotiate_version(mock_conn, self.response)
self.assertEqual('1.2', result)
self.assertTrue(mock_msr.called)
self.assertEqual(2, mock_pvh.call_count)
self.assertEqual(1, mock_save_data.call_count)
@mock.patch.object(filecache, 'save_data', autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_parse_version_headers',
autospec=True)
def test_negotiate_version_server_explicit_too_high(self, mock_pvh,
mock_save_data):
# requested version is not supported because it is too large
mock_pvh.return_value = ('1.1', '1.6')
mock_conn = mock.MagicMock()
self.test_object.api_version_select_state = 'user'
self.test_object.os_ironic_api_version = '99.99'
self.assertRaises(
exc.UnsupportedVersion,
self.test_object.negotiate_version,
mock_conn, self.response)
self.assertEqual(1, mock_pvh.call_count)
self.assertEqual(0, mock_save_data.call_count)
@mock.patch.object(filecache, 'save_data', autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_parse_version_headers',
autospec=True)
def test_negotiate_version_server_explicit_not_supported(self, mock_pvh,
mock_save_data):
# requested version is supported by the server but the server returned
# 406 because the requested operation is not supported with the
# requested version
mock_pvh.return_value = ('1.1', '1.6')
mock_conn = mock.MagicMock()
self.test_object.api_version_select_state = 'negotiated'
self.test_object.os_ironic_api_version = '1.5'
self.assertRaises(
exc.UnsupportedVersion,
self.test_object.negotiate_version,
mock_conn, self.response)
self.assertEqual(1, mock_pvh.call_count)
self.assertEqual(0, mock_save_data.call_count)
@mock.patch.object(filecache, 'save_data', autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_parse_version_headers',
autospec=True)
def test_negotiate_version_strict_version_comparison(self, mock_pvh,
mock_save_data):
# Test version comparison with StrictVersion
max_ver = '1.10'
mock_pvh.return_value = ('1.2', max_ver)
mock_conn = mock.MagicMock()
self.test_object.os_ironic_api_version = '1.10'
result = self.test_object.negotiate_version(mock_conn, self.response)
self.assertEqual(max_ver, result)
self.assertEqual(1, mock_pvh.call_count)
host, port = http.get_server(self.test_object.endpoint)
mock_save_data.assert_called_once_with(host=host, port=port,
data=max_ver)
@mock.patch.object(filecache, 'save_data', autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_make_simple_request',
autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_parse_version_headers',
autospec=True)
def test_negotiate_version_server_user_latest(
self, mock_pvh, mock_msr, mock_save_data):
# have to retry with simple get
mock_pvh.side_effect = iter([(None, None), ('1.1', '1.99')])
mock_conn = mock.MagicMock()
self.test_object.api_version_select_state = 'user'
self.test_object.os_ironic_api_version = 'latest'
result = self.test_object.negotiate_version(mock_conn, None)
self.assertEqual(http.LATEST_VERSION, result)
self.assertEqual('negotiated',
self.test_object.api_version_select_state)
self.assertEqual(http.LATEST_VERSION,
self.test_object.os_ironic_api_version)
self.assertTrue(mock_msr.called)
self.assertEqual(2, mock_pvh.call_count)
self.assertEqual(1, mock_save_data.call_count)
@mock.patch.object(filecache, 'save_data', autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_make_simple_request',
autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_parse_version_headers',
autospec=True)
def test_negotiate_version_server_user_list(
self, mock_pvh, mock_msr, mock_save_data):
# have to retry with simple get
mock_pvh.side_effect = iter([(None, None), ('1.1', '1.26')])
mock_conn = mock.MagicMock()
self.test_object.api_version_select_state = 'user'
self.test_object.os_ironic_api_version = ['1.1', '1.6', '1.25',
'1.26', '1.26.1', '1.27',
'1.30']
result = self.test_object.negotiate_version(mock_conn, self.response)
self.assertEqual('1.26', result)
self.assertEqual('negotiated',
self.test_object.api_version_select_state)
self.assertEqual('1.26',
self.test_object.os_ironic_api_version)
self.assertTrue(mock_msr.called)
self.assertEqual(2, mock_pvh.call_count)
self.assertEqual(1, mock_save_data.call_count)
@mock.patch.object(filecache, 'save_data', autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_make_simple_request',
autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_parse_version_headers',
autospec=True)
def test_negotiate_version_server_user_list_fails_nomatch(
self, mock_pvh, mock_msr, mock_save_data):
# have to retry with simple get
mock_pvh.side_effect = iter([(None, None), ('1.2', '1.26')])
mock_conn = mock.MagicMock()
self.test_object.api_version_select_state = 'user'
self.test_object.os_ironic_api_version = ['1.39', '1.1']
self.assertRaises(
exc.UnsupportedVersion,
self.test_object.negotiate_version,
mock_conn, self.response)
self.assertEqual('user',
self.test_object.api_version_select_state)
self.assertEqual(['1.39', '1.1'],
self.test_object.os_ironic_api_version)
self.assertEqual(2, mock_pvh.call_count)
self.assertEqual(0, mock_save_data.call_count)
@mock.patch.object(filecache, 'save_data', autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_make_simple_request',
autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_parse_version_headers',
autospec=True)
def test_negotiate_version_server_user_list_single_value(
self, mock_pvh, mock_msr, mock_save_data):
# have to retry with simple get
mock_pvh.side_effect = iter([(None, None), ('1.1', '1.26')])
mock_conn = mock.MagicMock()
self.test_object.api_version_select_state = 'user'
# NOTE(TheJulia): Lets test this value explicitly because the
# minor number is actually the same.
self.test_object.os_ironic_api_version = ['1.01']
result = self.test_object.negotiate_version(mock_conn, None)
self.assertEqual('1.1', result)
self.assertEqual('negotiated',
self.test_object.api_version_select_state)
self.assertEqual('1.1',
self.test_object.os_ironic_api_version)
self.assertTrue(mock_msr.called)
self.assertEqual(2, mock_pvh.call_count)
self.assertEqual(1, mock_save_data.call_count)
@mock.patch.object(filecache, 'save_data', autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_make_simple_request',
autospec=True)
@mock.patch.object(http.VersionNegotiationMixin, '_parse_version_headers',
autospec=True)
def test_negotiate_version_server_user_list_fails_latest(
self, mock_pvh, mock_msr, mock_save_data):
# have to retry with simple get
mock_pvh.side_effect = iter([(None, None), ('1.1', '1.2')])
mock_conn = mock.MagicMock()
self.test_object.api_version_select_state = 'user'
self.test_object.os_ironic_api_version = ['1.01', 'latest']
self.assertRaises(
ValueError,
self.test_object.negotiate_version,
mock_conn, self.response)
self.assertEqual('user',
self.test_object.api_version_select_state)
self.assertEqual(['1.01', 'latest'],
self.test_object.os_ironic_api_version)
self.assertEqual(2, mock_pvh.call_count)
self.assertEqual(0, mock_save_data.call_count)
def test_get_server(self):
host = 'ironic-host'
port = '6385'
endpoint = 'http://%s:%s/ironic/v1/' % (host, port)
self.assertEqual((host, port), http.get_server(endpoint))
class HttpClientTest(utils.BaseTestCase):
def test_url_generation_trailing_slash_in_base(self):
client = http.HTTPClient('http://localhost/')
url = client._make_connection_url('/v1/resources')
self.assertEqual('http://localhost/v1/resources', url)
def test_url_generation_without_trailing_slash_in_base(self):
client = http.HTTPClient('http://localhost')
url = client._make_connection_url('/v1/resources')
self.assertEqual('http://localhost/v1/resources', url)
def test_url_generation_without_prefix_slash_in_path(self):
client = http.HTTPClient('http://localhost')
url = client._make_connection_url('v1/resources')
self.assertEqual('http://localhost/v1/resources', url)
def test_server_https_request_with_application_octet_stream(self):
client = http.HTTPClient('https://localhost/')
client.session = utils.mockSession(
{'Content-Type': 'application/octet-stream'},
"Body",
version=1,
status_code=http_client.OK)
response, body = client.json_request('GET', '/v1/resources')
self.assertEqual(client.session.request.return_value, response)
self.assertIsNone(body)
def test_server_exception_empty_body(self):
error_body = _get_error_body()
client = http.HTTPClient('http://localhost/')
client.session = utils.mockSession(
{'Content-Type': 'application/json'},
error_body,
version=1,
status_code=http_client.INTERNAL_SERVER_ERROR)
self.assertRaises(exc.InternalServerError,
client.json_request,
'GET', '/v1/resources')
def test_server_exception_msg_only(self):
error_msg = 'test error msg'
error_body = _get_error_body(error_msg)
client = http.HTTPClient('http://localhost/')
client.session = utils.mockSession(
{'Content-Type': 'application/json'},
error_body,
version=1,
status_code=http_client.INTERNAL_SERVER_ERROR)
self.assertRaises(exc.InternalServerError,
client.json_request,
'GET', '/v1/resources')
def test_server_exception_description_only(self):
error_msg = 'test error msg'
error_body = _get_error_body(description=error_msg)
client = http.HTTPClient('http://localhost/')
client.session = utils.mockSession(
{'Content-Type': 'application/json'},
error_body,
version=1,
status_code=http_client.BAD_REQUEST)
self.assertRaisesRegex(exc.BadRequest, 'test error msg',
client.json_request,
'GET', '/v1/resources')
def test_server_https_request_ok(self):
client = http.HTTPClient('https://localhost/')
client.session = utils.mockSession(
{'Content-Type': 'application/json'},
"Body",
version=1,
status_code=http_client.OK)
client.json_request('GET', '/v1/resources')
def test_server_https_empty_body(self):
error_body = _get_error_body()
client = http.HTTPClient('https://localhost/')
client.session = utils.mockSession(
{'Content-Type': 'application/json'},
error_body,
version=1,
status_code=http_client.INTERNAL_SERVER_ERROR)
self.assertRaises(exc.InternalServerError,
client.json_request,
'GET', '/v1/resources')
def test_401_unauthorized_exception(self):
error_body = _get_error_body()
client = http.HTTPClient('http://localhost/')
client.session = utils.mockSession(
{'Content-Type': 'text/plain'},
error_body,
version=1,
status_code=http_client.UNAUTHORIZED)
self.assertRaises(exc.Unauthorized, client.json_request,
'GET', '/v1/resources')
def test_http_request_not_valid_request(self):
client = http.HTTPClient('http://localhost/')
client.session.request = mock.Mock(
side_effect=http.requests.exceptions.InvalidSchema)
self.assertRaises(exc.ValidationError, client._http_request,
'http://localhost/', 'GET')
def test__parse_version_headers(self):
# Test parsing of version headers from HTTPClient
error_body = _get_error_body()
expected_result = ('1.1', '1.6')
client = http.HTTPClient('http://localhost/')
fake_resp = utils.mockSessionResponse(
{'X-OpenStack-Ironic-API-Minimum-Version': '1.1',
'X-OpenStack-Ironic-API-Maximum-Version': '1.6',
'Content-Type': 'text/plain',
},
error_body,
version=1,
status_code=http_client.NOT_ACCEPTABLE)
result = client._parse_version_headers(fake_resp)
self.assertEqual(expected_result, result)
@mock.patch.object(filecache, 'save_data', autospec=True)
def test__http_request_client_fallback_fail(self, mock_save_data):
# Test when fallback to a supported version fails
host, port, latest_ver = 'localhost', '1234', '1.6'
error_body = _get_error_body()
client = http.HTTPClient('http://%s:%s/' % (host, port))
client.session = utils.mockSession(
{'X-OpenStack-Ironic-API-Minimum-Version': '1.1',
'X-OpenStack-Ironic-API-Maximum-Version': latest_ver,
'content-type': 'text/plain',
},
error_body,
version=1,
status_code=http_client.NOT_ACCEPTABLE)
self.assertRaises(
exc.UnsupportedVersion,
client._http_request,
'/v1/resources',
'GET')
mock_save_data.assert_called_once_with(host=host, data=latest_ver,
port=port)
@mock.patch.object(http.VersionNegotiationMixin, 'negotiate_version',
autospec=False)
def test__http_request_client_fallback_success(self, mock_negotiate):
# Test when fallback to a supported version succeeds
mock_negotiate.return_value = '1.6'
error_body = _get_error_body()
bad_resp = utils.mockSessionResponse(
{'X-OpenStack-Ironic-API-Minimum-Version': '1.1',
'X-OpenStack-Ironic-API-Maximum-Version': '1.6',
'content-type': 'text/plain',
},
error_body,
version=1,
status_code=http_client.NOT_ACCEPTABLE)
good_resp = utils.mockSessionResponse(
{'X-OpenStack-Ironic-API-Minimum-Version': '1.1',
'X-OpenStack-Ironic-API-Maximum-Version': '1.6',
'content-type': 'text/plain',
},
"We got some text",
version=1,
status_code=http_client.OK)
client = http.HTTPClient('http://localhost/')
with mock.patch.object(client, 'session',
autospec=True) as mock_session:
mock_session.request.side_effect = iter([bad_resp, good_resp])
response, body_iter = client._http_request('/v1/resources', 'GET')
self.assertEqual(http_client.OK, response.status_code)
self.assertEqual(1, mock_negotiate.call_count)
@mock.patch.object(http.LOG, 'debug', autospec=True)
def test_log_curl_request_mask_password(self, mock_log):
client = http.HTTPClient('http://localhost/')
kwargs = {'headers': {'foo-header': 'bar-header'},
'body': '{"password": "foo"}'}
client.log_curl_request('foo', '/v1/nodes', kwargs)
expected_log = ("curl -i -X foo -H 'foo-header: bar-header' "
"-d '{\"password\": \"***\"}' "
"http://localhost/v1/nodes")
mock_log.assert_called_once_with(expected_log)
@mock.patch.object(http.LOG, 'debug', autospec=True)
def test_log_http_response_mask_password(self, mock_log):
client = http.HTTPClient('http://localhost/')
fake_response = utils.FakeResponse({}, version=1, reason='foo',
status=200)
body = '{"password": "foo"}'
client.log_http_response(fake_response, body=body)
expected_log = ("\nHTTP/0.1 200 foo\n\n{\"password\": \"***\"}\n")
mock_log.assert_called_once_with(expected_log)
def test__https_init_ssl_args_insecure(self):
client = http.HTTPClient('https://localhost/', insecure=True)
self.assertEqual(False, client.session.verify)
def test__https_init_ssl_args_secure(self):
client = http.HTTPClient('https://localhost/', ca_file='test_ca',
key_file='test_key', cert_file='test_cert')
self.assertEqual('test_ca', client.session.verify)
self.assertEqual(('test_cert', 'test_key'), client.session.cert)
@mock.patch.object(http.LOG, 'debug', autospec=True)
def test_log_curl_request_with_body_and_header(self, mock_log):
client = http.HTTPClient('http://test')
headers = {'header1': 'value1'}
body = 'example body'
client.log_curl_request('GET', '/v1/nodes',
{'headers': headers, 'body': body})
self.assertTrue(mock_log.called)
self.assertTrue(mock_log.call_args[0])
self.assertEqual("curl -i -X GET -H 'header1: value1'"
" -d 'example body' http://test/v1/nodes",
mock_log.call_args[0][0])
@mock.patch.object(http.LOG, 'debug', autospec=True)
def test_log_curl_request_with_certs(self, mock_log):
headers = {'header1': 'value1'}
client = http.HTTPClient('https://test', key_file='key',
cert_file='cert', cacert='cacert',
token='fake-token')
client.log_curl_request('GET', '/v1/test', {'headers': headers})
self.assertTrue(mock_log.called)
self.assertTrue(mock_log.call_args[0])
self.assertEqual("curl -i -X GET -H 'header1: value1' "
"--cert cert --key key https://test/v1/test",
mock_log.call_args[0][0])
@mock.patch.object(http.LOG, 'debug', autospec=True)
def test_log_curl_request_with_insecure_param(self, mock_log):
headers = {'header1': 'value1'}
http_client_object = http.HTTPClient('https://test', insecure=True,
token='fake-token')
http_client_object.log_curl_request('GET', '/v1/test',
{'headers': headers})
self.assertTrue(mock_log.called)
self.assertTrue(mock_log.call_args[0])
self.assertEqual("curl -i -X GET -H 'header1: value1' -k "
"--cert None --key None https://test/v1/test",
mock_log.call_args[0][0])
class SessionClientTest(utils.BaseTestCase):
def test_server_exception_empty_body(self):
error_body = _get_error_body()
fake_session = utils.mockSession({'Content-Type': 'application/json'},
error_body,
http_client.INTERNAL_SERVER_ERROR)
client = _session_client(session=fake_session)
self.assertRaises(exc.InternalServerError,
client.json_request,
'GET', '/v1/resources')
def test_server_exception_description_only(self):
error_msg = 'test error msg'
error_body = _get_error_body(description=error_msg)
fake_session = utils.mockSession(
{'Content-Type': 'application/json'},
error_body, status_code=http_client.BAD_REQUEST)
client = _session_client(session=fake_session)
self.assertRaisesRegex(exc.BadRequest, 'test error msg',
client.json_request,
'GET', '/v1/resources')
def test__parse_version_headers(self):
# Test parsing of version headers from SessionClient
fake_session = utils.mockSession(
{'X-OpenStack-Ironic-API-Minimum-Version': '1.1',
'X-OpenStack-Ironic-API-Maximum-Version': '1.6',
'content-type': 'text/plain',
},
None,
http_client.HTTP_VERSION_NOT_SUPPORTED)
expected_result = ('1.1', '1.6')
client = _session_client(session=fake_session)
result = client._parse_version_headers(fake_session.request())
self.assertEqual(expected_result, result)
def _test_endpoint_override(self, endpoint):
fake_session = utils.mockSession({'content-type': 'application/json'},
status_code=http_client.NO_CONTENT)
request_mock = mock.Mock()
fake_session.request = request_mock
request_mock.return_value = utils.mockSessionResponse(
headers={'content-type': 'application/json'},
status_code=http_client.NO_CONTENT)
client = _session_client(session=fake_session,
endpoint_override=endpoint)
client.json_request('DELETE', '/v1/nodes/aa/maintenance')
expected_args_dict = {
'headers': {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-OpenStack-Ironic-API-Version': '1.6'
},
'auth': None, 'user_agent': 'python-ironicclient',
'endpoint_filter': {
'interface': 'publicURL',
'service_type': 'baremetal',
'region_name': ''
}
}
if isinstance(endpoint, six.string_types):
trimmed = http._trim_endpoint_api_version(endpoint)
expected_args_dict['endpoint_override'] = trimmed
request_mock.assert_called_once_with(
'/v1/nodes/aa/maintenance', 'DELETE', raise_exc=False,
**expected_args_dict
)
def test_endpoint_override(self):
self._test_endpoint_override('http://1.0.0.1:6385')
def test_endpoint_override_with_version(self):
self._test_endpoint_override('http://1.0.0.1:6385/v1')
def test_endpoint_override_not_valid(self):
self._test_endpoint_override(True)
def test_make_simple_request(self):
session = mock.Mock(spec=['request'])
client = _session_client(session=session,
endpoint_override='http://127.0.0.1')
res = client._make_simple_request(session, 'GET', 'url')
session.request.assert_called_once_with(
'url', 'GET', raise_exc=False,
endpoint_filter={
'interface': 'publicURL',
'service_type': 'baremetal',
'region_name': ''
},
user_agent=http.USER_AGENT)
self.assertEqual(res, session.request.return_value)
@mock.patch.object(time, 'sleep', lambda *_: None)
class RetriesTestCase(utils.BaseTestCase):
def test_http_no_retry(self):
error_body = _get_error_body()
bad_resp = utils.mockSessionResponse(
{'Content-Type': 'text/plain'},
error_body,
version=1,
status_code=http_client.CONFLICT)
client = http.HTTPClient('http://localhost/', max_retries=0)
with mock.patch.object(client.session, 'request', autospec=True,
return_value=bad_resp) as mock_request:
self.assertRaises(exc.Conflict, client._http_request,
'/v1/resources', 'GET')
self.assertEqual(1, mock_request.call_count)
def test_http_retry(self):
error_body = _get_error_body()
bad_resp = utils.mockSessionResponse(
{'Content-Type': 'text/plain'},
error_body,
version=1,
status_code=http_client.CONFLICT)
good_resp = utils.mockSessionResponse(
{'Content-Type': 'text/plain'},
"meow",
version=1,
status_code=http_client.OK)
client = http.HTTPClient('http://localhost/')
with mock.patch.object(client, 'session',
autospec=True) as mock_session:
mock_session.request.side_effect = iter([bad_resp, good_resp])
response, body_iter = client._http_request('/v1/resources', 'GET')
self.assertEqual(http_client.OK, response.status_code)
self.assertEqual(2, mock_session.request.call_count)
def test_http_retry_503(self):
error_body = _get_error_body()
bad_resp = utils.mockSessionResponse(
{'Content-Type': 'text/plain'},
error_body,
version=1,
status_code=http_client.SERVICE_UNAVAILABLE)
good_resp = utils.mockSessionResponse(
{'Content-Type': 'text/plain'},
"meow",
version=1,
status_code=http_client.OK)
client = http.HTTPClient('http://localhost/')
with mock.patch.object(client, 'session',
autospec=True) as mock_session:
mock_session.request.side_effect = iter([bad_resp, good_resp])
response, body_iter = client._http_request('/v1/resources', 'GET')
self.assertEqual(http_client.OK, response.status_code)
self.assertEqual(2, mock_session.request.call_count)
def test_http_retry_connection_refused(self):
good_resp = utils.mockSessionResponse(
{'content-type': 'text/plain'},
"meow",
version=1,
status_code=http_client.OK)
client = http.HTTPClient('http://localhost/')
with mock.patch.object(client, 'session',
autospec=True) as mock_session:
mock_session.request.side_effect = iter([exc.ConnectionRefused(),
good_resp])
response, body_iter = client._http_request('/v1/resources', 'GET')
self.assertEqual(http_client.OK, response.status_code)
self.assertEqual(2, mock_session.request.call_count)
def test_http_failed_retry(self):
error_body = _get_error_body()
bad_resp = utils.mockSessionResponse(
{'content-type': 'text/plain'},
error_body,
version=1,
status_code=http_client.CONFLICT)
client = http.HTTPClient('http://localhost/')
with mock.patch.object(client, 'session',
autospec=True) as mock_session:
mock_session.request.return_value = bad_resp
self.assertRaises(exc.Conflict, client._http_request,
'/v1/resources', 'GET')
self.assertEqual(http.DEFAULT_MAX_RETRIES + 1,
mock_session.request.call_count)
def test_http_max_retries_none(self):
error_body = _get_error_body()
bad_resp = utils.mockSessionResponse(
{'content-type': 'text/plain'},
error_body,
version=1,
status_code=http_client.CONFLICT)
client = http.HTTPClient('http://localhost/', max_retries=None)
with mock.patch.object(client, 'session',
autospec=True) as mock_session:
mock_session.request.return_value = bad_resp
self.assertRaises(exc.Conflict, client._http_request,
'/v1/resources', 'GET')
self.assertEqual(http.DEFAULT_MAX_RETRIES + 1,
mock_session.request.call_count)
def test_http_change_max_retries(self):
error_body = _get_error_body()
bad_resp = utils.mockSessionResponse(
{'content-type': 'text/plain'},
error_body,
version=1,
status_code=http_client.CONFLICT)
client = http.HTTPClient('http://localhost/',
max_retries=http.DEFAULT_MAX_RETRIES + 1)
with mock.patch.object(client, 'session',
autospec=True) as mock_session:
mock_session.request.return_value = bad_resp
self.assertRaises(exc.Conflict, client._http_request,
'/v1/resources', 'GET')
self.assertEqual(http.DEFAULT_MAX_RETRIES + 2,
mock_session.request.call_count)
def test_session_retry(self):
error_body = _get_error_body()
fake_resp = utils.mockSessionResponse(
{'Content-Type': 'application/json'},
error_body,
http_client.CONFLICT)
ok_resp = utils.mockSessionResponse(
{'Content-Type': 'application/json'},
b"OK",
http_client.OK)
fake_session = mock.Mock(spec=requests.Session)
fake_session.request.side_effect = iter((fake_resp, ok_resp))
client = _session_client(session=fake_session)
client.json_request('GET', '/v1/resources')
self.assertEqual(2, fake_session.request.call_count)
def test_session_retry_503(self):
error_body = _get_error_body()
fake_resp = utils.mockSessionResponse(
{'Content-Type': 'application/json'},
error_body,
http_client.SERVICE_UNAVAILABLE)
ok_resp = utils.mockSessionResponse(
{'Content-Type': 'application/json'},
b"OK",
http_client.OK)
fake_session = mock.Mock(spec=requests.Session)
fake_session.request.side_effect = iter((fake_resp, ok_resp))
client = _session_client(session=fake_session)
client.json_request('GET', '/v1/resources')
self.assertEqual(2, fake_session.request.call_count)
def test_session_retry_connection_refused(self):
ok_resp = utils.mockSessionResponse(
{'Content-Type': 'application/json'},
b"OK",
http_client.OK)
fake_session = mock.Mock(spec=requests.Session)
fake_session.request.side_effect = iter((exc.ConnectionRefused(),
ok_resp))
client = _session_client(session=fake_session)
client.json_request('GET', '/v1/resources')
self.assertEqual(2, fake_session.request.call_count)
def test_session_retry_retriable_connection_failure(self):
ok_resp = utils.mockSessionResponse(
{'Content-Type': 'application/json'},
b"OK",
http_client.OK)
fake_session = mock.Mock(spec=requests.Session)
fake_session.request.side_effect = iter(
(kexc.RetriableConnectionFailure(), ok_resp))
client = _session_client(session=fake_session)
client.json_request('GET', '/v1/resources')
self.assertEqual(2, fake_session.request.call_count)
def test_session_retry_fail(self):
error_body = _get_error_body()
fake_resp = utils.mockSessionResponse(
{'Content-Type': 'application/json'},
error_body,
http_client.CONFLICT)
fake_session = mock.Mock(spec=requests.Session)
fake_session.request.return_value = fake_resp
client = _session_client(session=fake_session)
self.assertRaises(exc.Conflict, client.json_request,
'GET', '/v1/resources')
self.assertEqual(http.DEFAULT_MAX_RETRIES + 1,
fake_session.request.call_count)
def test_session_max_retries_none(self):
error_body = _get_error_body()
fake_resp = utils.mockSessionResponse(
{'Content-Type': 'application/json'},
error_body,
http_client.CONFLICT)
fake_session = mock.Mock(spec=requests.Session)
fake_session.request.return_value = fake_resp
client = _session_client(session=fake_session)
client.conflict_max_retries = None
self.assertRaises(exc.Conflict, client.json_request,
'GET', '/v1/resources')
self.assertEqual(http.DEFAULT_MAX_RETRIES + 1,
fake_session.request.call_count)
def test_session_change_max_retries(self):
error_body = _get_error_body()
fake_resp = utils.mockSessionResponse(
{'Content-Type': 'application/json'},
error_body,
http_client.CONFLICT)
fake_session = mock.Mock(spec=requests.Session)
fake_session.request.return_value = fake_resp
client = _session_client(session=fake_session)
client.conflict_max_retries = http.DEFAULT_MAX_RETRIES + 1
self.assertRaises(exc.Conflict, client.json_request,
'GET', '/v1/resources')
self.assertEqual(http.DEFAULT_MAX_RETRIES + 2,
fake_session.request.call_count)
|