|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +import json |
| 13 | +import uuid |
| 14 | + |
| 15 | +import openstack |
| 16 | + |
| 17 | +from otcextensions.tests.functional import base |
| 18 | + |
| 19 | +_logger = openstack._log.setup_logging("openstack") |
| 20 | + |
| 21 | + |
| 22 | +def _get_access_policy(): |
| 23 | + return json.dumps({ |
| 24 | + "Version": "2016-09-07", |
| 25 | + "Id": "__default_policy_ID", |
| 26 | + "Statement": [ |
| 27 | + { |
| 28 | + "Sid": "__user_pub_0", |
| 29 | + "Effect": "Allow", |
| 30 | + "Principal": {"CSP": ["*"]}, |
| 31 | + "Action": ["SMN:Publish", "SMN:QueryTopicDetail"], |
| 32 | + "Resource": "*", |
| 33 | + } |
| 34 | + ], |
| 35 | + }) |
| 36 | + |
| 37 | + |
| 38 | +class TestTopicAttribute(base.BaseFunctionalTest): |
| 39 | + |
| 40 | + uuid_v4 = uuid.uuid4().hex[:8] |
| 41 | + topic_name = "sdk-test-smn-attr-{uuid}".format(uuid=uuid_v4) |
| 42 | + topic = None |
| 43 | + |
| 44 | + def setUp(self): |
| 45 | + super(TestTopicAttribute, self).setUp() |
| 46 | + self.client = self.conn.smn |
| 47 | + |
| 48 | + def tearDown(self): |
| 49 | + if self.topic: |
| 50 | + try: |
| 51 | + self.client.delete_topic(self.topic) |
| 52 | + except openstack.exceptions.SDKException as e: |
| 53 | + _logger.warning( |
| 54 | + "Got exception during clearing resources %s" % e.message |
| 55 | + ) |
| 56 | + super(TestTopicAttribute, self).tearDown() |
| 57 | + |
| 58 | + def _create_topic(self): |
| 59 | + try: |
| 60 | + self.topic = self.client.create_topic( |
| 61 | + name=self.topic_name, |
| 62 | + display_name="SDK Test Topic Attribute", |
| 63 | + ) |
| 64 | + except openstack.exceptions.BadRequestException: |
| 65 | + self.topic = self.client.find_topic(self.topic_name) |
| 66 | + |
| 67 | + def _set_access_policy(self): |
| 68 | + self.client.update_topic_attribute( |
| 69 | + self.topic, |
| 70 | + name="access_policy", |
| 71 | + attr_value=_get_access_policy(), |
| 72 | + ) |
| 73 | + |
| 74 | + def test_01_update_topic_attribute(self): |
| 75 | + self._create_topic() |
| 76 | + result = self.client.update_topic_attribute( |
| 77 | + self.topic, |
| 78 | + name="access_policy", |
| 79 | + attr_value=_get_access_policy(), |
| 80 | + ) |
| 81 | + self.assertIsNotNone(result.request_id) |
| 82 | + |
| 83 | + def test_02_list_topic_attributes(self): |
| 84 | + self._create_topic() |
| 85 | + self._set_access_policy() |
| 86 | + attrs = list(self.client.topic_attributes(self.topic)) |
| 87 | + self.assertGreaterEqual(len(attrs), 1) |
| 88 | + |
| 89 | + def test_03_delete_topic_attribute_by_name(self): |
| 90 | + self._create_topic() |
| 91 | + self._set_access_policy() |
| 92 | + self.client.delete_topic_attribute( |
| 93 | + self.topic, |
| 94 | + name="access_policy", |
| 95 | + ) |
| 96 | + attrs = list(self.client.topic_attributes(self.topic)) |
| 97 | + self.assertEqual(len(attrs), 1) |
| 98 | + self.assertEqual(attrs[0].attributes.access_policy, "") |
| 99 | + |
| 100 | + def test_04_delete_all_topic_attributes(self): |
| 101 | + self._create_topic() |
| 102 | + self._set_access_policy() |
| 103 | + self.client.delete_topic_attribute(self.topic) |
| 104 | + attrs = list(self.client.topic_attributes(self.topic)) |
| 105 | + self.assertEqual(len(attrs), 1) |
| 106 | + self.assertEqual(attrs[0].attributes.access_policy, "") |
0 commit comments