/heat/tests/openstack/keystone/test_service.py

https://gitlab.com/syjulian/poc-heat-mversion
Python | 288 lines | 217 code | 50 blank | 21 comment | 2 complexity | 937f037a52b22ffaa2fc9b8f5c67417a MD5 | raw file
  1. #
  2. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  3. # not use this file except in compliance with the License. You may obtain
  4. # a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  11. # License for the specific language governing permissions and limitations
  12. # under the License.
  13. import copy
  14. import mock
  15. from heat.engine import properties
  16. from heat.engine import resource
  17. from heat.engine.resources.openstack.keystone import service
  18. from heat.engine import stack
  19. from heat.engine import template
  20. from heat.tests import common
  21. from heat.tests import fakes
  22. from heat.tests import utils
  23. keystone_service_template = {
  24. 'heat_template_version': '2015-04-30',
  25. 'resources': {
  26. 'test_service': {
  27. 'type': 'OS::Keystone::Service',
  28. 'properties': {
  29. 'name': 'test_service_1',
  30. 'description': 'Test service',
  31. 'type': 'orchestration',
  32. 'enabled': False
  33. }
  34. }
  35. }
  36. }
  37. RESOURCE_TYPE = 'OS::Keystone::Service'
  38. class KeystoneServiceTest(common.HeatTestCase):
  39. def setUp(self):
  40. super(KeystoneServiceTest, self).setUp()
  41. self.ctx = utils.dummy_context()
  42. # Mock client
  43. self.keystoneclient = mock.Mock()
  44. self.patchobject(resource.Resource, 'client',
  45. return_value=fakes.FakeKeystoneClient(
  46. client=self.keystoneclient))
  47. self.services = self.keystoneclient.services
  48. # Mock client plugin
  49. self.keystone_client_plugin = mock.MagicMock()
  50. def _setup_service_resource(self, stack_name, use_default=False):
  51. tmpl_data = copy.deepcopy(keystone_service_template)
  52. if use_default:
  53. props = tmpl_data['resources']['test_service']['properties']
  54. del props['name']
  55. del props['enabled']
  56. del props['description']
  57. test_stack = stack.Stack(
  58. self.ctx, stack_name,
  59. template.Template(tmpl_data)
  60. )
  61. r_service = test_stack['test_service']
  62. r_service.client = mock.MagicMock()
  63. r_service.client.return_value = self.keystoneclient
  64. r_service.client_plugin = mock.MagicMock()
  65. r_service.client_plugin.return_value = self.keystone_client_plugin
  66. return r_service
  67. def _get_mock_service(self):
  68. value = mock.MagicMock()
  69. value.id = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
  70. return value
  71. def test_service_handle_create(self):
  72. rsrc = self._setup_service_resource('test_service_create')
  73. mock_service = self._get_mock_service()
  74. self.services.create.return_value = mock_service
  75. # validate the properties
  76. self.assertEqual(
  77. 'test_service_1',
  78. rsrc.properties.get(service.KeystoneService.NAME))
  79. self.assertEqual(
  80. 'Test service',
  81. rsrc.properties.get(
  82. service.KeystoneService.DESCRIPTION))
  83. self.assertEqual(
  84. 'orchestration',
  85. rsrc.properties.get(service.KeystoneService.TYPE))
  86. self.assertFalse(rsrc.properties.get(
  87. service.KeystoneService.ENABLED))
  88. rsrc.handle_create()
  89. # validate service creation
  90. self.services.create.assert_called_once_with(
  91. name='test_service_1',
  92. description='Test service',
  93. type='orchestration',
  94. enabled=False)
  95. # validate physical resource id
  96. self.assertEqual(mock_service.id, rsrc.resource_id)
  97. def test_service_handle_create_default(self):
  98. rsrc = self._setup_service_resource('test_create_with_defaults',
  99. use_default=True)
  100. mock_service = self._get_mock_service()
  101. self.services.create.return_value = mock_service
  102. rsrc.physical_resource_name = mock.MagicMock()
  103. rsrc.physical_resource_name.return_value = 'foo'
  104. # validate the properties
  105. self.assertIsNone(
  106. rsrc.properties.get(service.KeystoneService.NAME))
  107. self.assertIsNone(rsrc.properties.get(
  108. service.KeystoneService.DESCRIPTION))
  109. self.assertEqual(
  110. 'orchestration',
  111. rsrc.properties.get(service.KeystoneService.TYPE))
  112. self.assertTrue(rsrc.properties.get(service.KeystoneService.ENABLED))
  113. rsrc.handle_create()
  114. # validate service creation with physical resource name
  115. self.services.create.assert_called_once_with(
  116. name='foo',
  117. description=None,
  118. type='orchestration',
  119. enabled=True)
  120. def test_service_handle_update(self):
  121. rsrc = self._setup_service_resource('test_update')
  122. rsrc.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
  123. prop_diff = {service.KeystoneService.NAME: 'test_service_1_updated',
  124. service.KeystoneService.DESCRIPTION:
  125. 'Test Service updated',
  126. service.KeystoneService.TYPE: 'heat_updated',
  127. service.KeystoneService.ENABLED: False}
  128. rsrc.handle_update(json_snippet=None,
  129. tmpl_diff=None,
  130. prop_diff=prop_diff)
  131. self.services.update.assert_called_once_with(
  132. service=rsrc.resource_id,
  133. name=prop_diff[service.KeystoneService.NAME],
  134. description=prop_diff[service.KeystoneService.DESCRIPTION],
  135. type=prop_diff[service.KeystoneService.TYPE],
  136. enabled=prop_diff[service.KeystoneService.ENABLED]
  137. )
  138. def test_service_handle_update_default_name(self):
  139. rsrc = self._setup_service_resource('test_update_default_name')
  140. rsrc.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
  141. rsrc.physical_resource_name = mock.MagicMock()
  142. rsrc.physical_resource_name.return_value = 'foo'
  143. # Name is reset to None, so default to physical resource name
  144. prop_diff = {service.KeystoneService.NAME: None}
  145. rsrc.handle_update(json_snippet=None,
  146. tmpl_diff=None,
  147. prop_diff=prop_diff)
  148. # validate default name to physical resource name
  149. self.services.update.assert_called_once_with(
  150. service=rsrc.resource_id,
  151. name='foo',
  152. type=None,
  153. description=None,
  154. enabled=None
  155. )
  156. def test_service_handle_update_only_enabled(self):
  157. rsrc = self._setup_service_resource('test_update_enabled_only')
  158. rsrc.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
  159. prop_diff = {service.KeystoneService.ENABLED: False}
  160. rsrc.handle_update(json_snippet=None,
  161. tmpl_diff=None,
  162. prop_diff=prop_diff)
  163. self.services.update.assert_called_once_with(
  164. service=rsrc.resource_id,
  165. name=None,
  166. description=None,
  167. type=None,
  168. enabled=prop_diff[service.KeystoneService.ENABLED]
  169. )
  170. def test_properties_title(self):
  171. property_title_map = {
  172. service.KeystoneService.NAME: 'name',
  173. service.KeystoneService.DESCRIPTION: 'description',
  174. service.KeystoneService.TYPE: 'type',
  175. service.KeystoneService.ENABLED: 'enabled'
  176. }
  177. for actual_title, expected_title in property_title_map.items():
  178. self.assertEqual(
  179. expected_title,
  180. actual_title,
  181. 'KeystoneService PROPERTIES(%s) title modified.' %
  182. actual_title)
  183. def test_property_name_validate_schema(self):
  184. schema = service.KeystoneService.properties_schema[
  185. service.KeystoneService.NAME]
  186. self.assertTrue(
  187. schema.update_allowed,
  188. 'update_allowed for property %s is modified' %
  189. service.KeystoneService.NAME)
  190. self.assertEqual(properties.Schema.STRING,
  191. schema.type,
  192. 'type for property %s is modified' %
  193. service.KeystoneService.NAME)
  194. self.assertEqual('Name of keystone service.',
  195. schema.description,
  196. 'description for property %s is modified' %
  197. service.KeystoneService.NAME)
  198. def test_property_description_validate_schema(self):
  199. schema = service.KeystoneService.properties_schema[
  200. service.KeystoneService.DESCRIPTION]
  201. self.assertTrue(
  202. schema.update_allowed,
  203. 'update_allowed for property %s is modified' %
  204. service.KeystoneService.DESCRIPTION)
  205. self.assertEqual(properties.Schema.STRING,
  206. schema.type,
  207. 'type for property %s is modified' %
  208. service.KeystoneService.DESCRIPTION)
  209. self.assertEqual('Description of keystone service.',
  210. schema.description,
  211. 'description for property %s is modified' %
  212. service.KeystoneService.DESCRIPTION)
  213. def test_property_type_validate_schema(self):
  214. schema = service.KeystoneService.properties_schema[
  215. service.KeystoneService.TYPE]
  216. self.assertTrue(
  217. schema.update_allowed,
  218. 'update_allowed for property %s is modified' %
  219. service.KeystoneService.TYPE)
  220. self.assertTrue(
  221. schema.required,
  222. 'required for property %s is modified' %
  223. service.KeystoneService.TYPE)
  224. self.assertEqual(properties.Schema.STRING,
  225. schema.type,
  226. 'type for property %s is modified' %
  227. service.KeystoneService.TYPE)
  228. self.assertEqual('Type of keystone Service.',
  229. schema.description,
  230. 'description for property %s is modified' %
  231. service.KeystoneService.TYPE)
  232. def test_show_resource(self):
  233. rsrc = self._setup_service_resource('test_show_resource')
  234. moc_service = mock.Mock()
  235. moc_service.to_dict.return_value = {'attr': 'val'}
  236. self.services.get.return_value = moc_service
  237. attributes = rsrc._show_resource()
  238. self.assertEqual({'attr': 'val'}, attributes)