|
13 | 13 |
|
14 | 14 | """Compute v2 API Library Tests""" |
15 | 15 |
|
| 16 | +import http |
| 17 | +from unittest import mock |
| 18 | +import uuid |
| 19 | + |
16 | 20 | from keystoneauth1 import session |
| 21 | +from openstack.compute.v2 import _proxy |
17 | 22 | from osc_lib import exceptions as osc_lib_exceptions |
18 | 23 | from requests_mock.contrib import fixture |
19 | 24 |
|
20 | 25 | from openstackclient.api import compute_v2 as compute |
| 26 | +from openstackclient.tests.unit import fakes |
21 | 27 | from openstackclient.tests.unit import utils |
22 | 28 |
|
23 | 29 |
|
@@ -648,3 +654,112 @@ def test_security_group_rule_delete(self): |
648 | 654 | ret = self.api.security_group_rule_delete('1') |
649 | 655 | self.assertEqual(202, ret.status_code) |
650 | 656 | self.assertEqual("", ret.text) |
| 657 | + |
| 658 | + |
| 659 | +class TestFindSecurityGroup(utils.TestCase): |
| 660 | + |
| 661 | + def setUp(self): |
| 662 | + super().setUp() |
| 663 | + |
| 664 | + self.compute_sdk_client = mock.Mock(_proxy.Proxy) |
| 665 | + |
| 666 | + def test_find_security_group_by_id(self): |
| 667 | + sg_id = uuid.uuid4().hex |
| 668 | + sg_name = 'name-' + uuid.uuid4().hex |
| 669 | + data = { |
| 670 | + 'security_group': { |
| 671 | + 'id': sg_id, |
| 672 | + 'name': sg_name, |
| 673 | + 'description': 'description-' + uuid.uuid4().hex, |
| 674 | + 'tenant_id': 'project-id-' + uuid.uuid4().hex, |
| 675 | + 'rules': [], |
| 676 | + } |
| 677 | + } |
| 678 | + self.compute_sdk_client.get.side_effect = [ |
| 679 | + fakes.FakeResponse(data=data), |
| 680 | + ] |
| 681 | + |
| 682 | + result = compute.find_security_group(self.compute_sdk_client, sg_id) |
| 683 | + |
| 684 | + self.compute_sdk_client.get.assert_has_calls( |
| 685 | + [ |
| 686 | + mock.call(f'/os-security-groups/{sg_id}', microversion='2.1'), |
| 687 | + ] |
| 688 | + ) |
| 689 | + self.assertEqual(data['security_group'], result) |
| 690 | + |
| 691 | + def test_find_security_group_by_name(self): |
| 692 | + sg_id = uuid.uuid4().hex |
| 693 | + sg_name = 'name-' + uuid.uuid4().hex |
| 694 | + data = { |
| 695 | + 'security_groups': [ |
| 696 | + { |
| 697 | + 'id': sg_id, |
| 698 | + 'name': sg_name, |
| 699 | + 'description': 'description-' + uuid.uuid4().hex, |
| 700 | + 'tenant_id': 'project-id-' + uuid.uuid4().hex, |
| 701 | + 'rules': [], |
| 702 | + } |
| 703 | + ], |
| 704 | + } |
| 705 | + self.compute_sdk_client.get.side_effect = [ |
| 706 | + fakes.FakeResponse(status_code=http.HTTPStatus.NOT_FOUND), |
| 707 | + fakes.FakeResponse(data=data), |
| 708 | + ] |
| 709 | + |
| 710 | + result = compute.find_security_group(self.compute_sdk_client, sg_name) |
| 711 | + |
| 712 | + self.compute_sdk_client.get.assert_has_calls( |
| 713 | + [ |
| 714 | + mock.call( |
| 715 | + f'/os-security-groups/{sg_name}', microversion='2.1' |
| 716 | + ), |
| 717 | + mock.call('/os-security-groups', microversion='2.1'), |
| 718 | + ] |
| 719 | + ) |
| 720 | + self.assertEqual(data['security_groups'][0], result) |
| 721 | + |
| 722 | + def test_find_security_group_not_found(self): |
| 723 | + data = {'security_groups': []} |
| 724 | + self.compute_sdk_client.get.side_effect = [ |
| 725 | + fakes.FakeResponse(status_code=http.HTTPStatus.NOT_FOUND), |
| 726 | + fakes.FakeResponse(data=data), |
| 727 | + ] |
| 728 | + self.assertRaises( |
| 729 | + osc_lib_exceptions.NotFound, |
| 730 | + compute.find_security_group, |
| 731 | + self.compute_sdk_client, |
| 732 | + 'invalid-sg', |
| 733 | + ) |
| 734 | + |
| 735 | + def test_find_security_group_by_name_duplicate(self): |
| 736 | + sg_name = 'name-' + uuid.uuid4().hex |
| 737 | + data = { |
| 738 | + 'security_groups': [ |
| 739 | + { |
| 740 | + 'id': uuid.uuid4().hex, |
| 741 | + 'name': sg_name, |
| 742 | + 'description': 'description-' + uuid.uuid4().hex, |
| 743 | + 'tenant_id': 'project-id-' + uuid.uuid4().hex, |
| 744 | + 'rules': [], |
| 745 | + }, |
| 746 | + { |
| 747 | + 'id': uuid.uuid4().hex, |
| 748 | + 'name': sg_name, |
| 749 | + 'description': 'description-' + uuid.uuid4().hex, |
| 750 | + 'tenant_id': 'project-id-' + uuid.uuid4().hex, |
| 751 | + 'rules': [], |
| 752 | + }, |
| 753 | + ], |
| 754 | + } |
| 755 | + self.compute_sdk_client.get.side_effect = [ |
| 756 | + fakes.FakeResponse(status_code=http.HTTPStatus.NOT_FOUND), |
| 757 | + fakes.FakeResponse(data=data), |
| 758 | + ] |
| 759 | + |
| 760 | + self.assertRaises( |
| 761 | + osc_lib_exceptions.NotFound, |
| 762 | + compute.find_security_group, |
| 763 | + self.compute_sdk_client, |
| 764 | + sg_name, |
| 765 | + ) |
0 commit comments