Fuel UI
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_deployment_graph_object.py 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Mirantis, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. from nailgun.objects import deployment_graph
  16. from nailgun.test import base
  17. JSON_TASKS = [
  18. {
  19. 'id': 'post_deployment_end',
  20. 'type': 'stage',
  21. 'requires': ['post_deployment_start']
  22. },
  23. {
  24. 'id': 'primary-controller',
  25. 'parameters': {'strategy': {'type': 'one_by_one'}},
  26. 'required_for': ['deploy_end'],
  27. 'requires': ['deploy_start'],
  28. 'role': ['primary-controller'],
  29. 'type': 'group'
  30. },
  31. {
  32. 'id': 'test-roles',
  33. 'role': '*',
  34. 'type': 'group'
  35. },
  36. {
  37. 'id': 'cross-dep-test',
  38. 'type': 'puppet',
  39. 'cross-depended-by': ['a', 'b'],
  40. 'cross-depends': ['c', 'd'],
  41. },
  42. {
  43. 'id': 'custom-fields-test',
  44. 'type': 'puppet',
  45. 'CUSTOM_FIELD1': 'custom',
  46. 'CUSTOM_FIELD2': ['custom'],
  47. 'CUSTOM_FIELD3': {'custom': 'custom'},
  48. },
  49. {
  50. 'id': 'ssl-keys-saving',
  51. 'type': 'puppet',
  52. 'version': '2.0.0',
  53. 'groups': ['primary-controller', 'controller', 'compute',
  54. 'compute-vmware', 'cinder', 'cinder-vmware',
  55. 'primary-mongo', 'mongo', 'ceph-osd', 'virt'],
  56. 'requires': ['firewall'],
  57. 'condition': "(settings:public_ssl.horizon.value == true or settings:"
  58. "public_ssl.services.value == true) and settings:"
  59. "public_ssl.cert_source.value == 'user_uploaded'",
  60. 'required_for': ['deploy_end'],
  61. 'parameters': {
  62. 'puppet_manifest': '/etc/puppet/modules/osnailyfacter/'
  63. 'modular/ssl/ssl_keys_saving.pp',
  64. 'puppet_modules': '/etc/puppet/modules',
  65. 'timeout': 3600
  66. },
  67. 'test_pre': {
  68. 'cmd': 'ruby /etc/puppet/modules/osnailyfacter/'
  69. 'modular/ssl/ssl_keys_saving_pre.rb'
  70. }
  71. }
  72. ]
  73. EXPECTED_TASKS = [
  74. {
  75. 'task_name': 'cross-dep-test',
  76. 'id': 'cross-dep-test', # legacy
  77. 'type': 'puppet',
  78. 'version': '1.0.0',
  79. 'cross_depended_by': ['a', 'b'],
  80. 'cross_depends': ['c', 'd'],
  81. 'cross-depended-by': ['a', 'b'], # legacy
  82. 'cross-depends': ['c', 'd'], # legacy
  83. }, {
  84. 'task_name': 'post_deployment_end',
  85. 'id': 'post_deployment_end', # legacy
  86. 'type': 'stage',
  87. 'version': '1.0.0',
  88. 'requires': ['post_deployment_start'],
  89. }, {
  90. 'task_name': 'test-roles',
  91. 'id': 'test-roles',
  92. 'role': '*',
  93. 'roles': '*',
  94. 'version': '1.0.0',
  95. 'type': 'group'
  96. }, {
  97. 'task_name': 'primary-controller',
  98. 'id': 'primary-controller', # legacy
  99. 'type': 'group',
  100. 'version': '1.0.0',
  101. 'required_for': ['deploy_end'],
  102. 'requires': ['deploy_start'],
  103. 'roles': ['primary-controller'],
  104. 'role': ['primary-controller'], # legacy
  105. 'parameters': {'strategy': {'type': 'one_by_one'}},
  106. },
  107. {
  108. 'id': 'custom-fields-test',
  109. 'task_name': 'custom-fields-test',
  110. 'type': 'puppet',
  111. 'version': '1.0.0',
  112. 'CUSTOM_FIELD1': 'custom',
  113. 'CUSTOM_FIELD2': ['custom'],
  114. 'CUSTOM_FIELD3': {'custom': 'custom'},
  115. },
  116. {
  117. 'id': 'ssl-keys-saving',
  118. 'task_name': 'ssl-keys-saving',
  119. 'type': 'puppet',
  120. 'version': '2.0.0',
  121. 'groups': ['primary-controller', 'controller', 'compute',
  122. 'compute-vmware', 'cinder', 'cinder-vmware',
  123. 'primary-mongo', 'mongo', 'ceph-osd', 'virt'],
  124. 'requires': ['firewall'],
  125. 'condition': "(settings:public_ssl.horizon.value == true or "
  126. "settings:public_ssl.services.value == true) "
  127. "and settings:public_ssl.cert_source.value == "
  128. "'user_uploaded'",
  129. 'required_for': ['deploy_end'],
  130. 'parameters': {
  131. 'puppet_manifest': '/etc/puppet/modules/osnailyfacter/'
  132. 'modular/ssl/ssl_keys_saving.pp',
  133. 'puppet_modules': '/etc/puppet/modules',
  134. 'timeout': 3600
  135. },
  136. 'test_pre': {
  137. 'cmd': 'ruby /etc/puppet/modules/osnailyfacter/'
  138. 'modular/ssl/ssl_keys_saving_pre.rb'
  139. }
  140. }
  141. ]
  142. class TestDeploymentGraphModel(
  143. base.BaseTestCase,
  144. base.DeploymentTasksTestMixin
  145. ):
  146. def test_deployment_graph_creation(self):
  147. dg = deployment_graph.DeploymentGraph.create({
  148. 'tasks': JSON_TASKS,
  149. 'name': 'test_graph',
  150. 'node_filter': '$.pending_deletion',
  151. 'on_success': {'node_attributes': {'status': 'discover'}},
  152. 'on_error': {'node_attributes': {'status': 'error'}},
  153. 'on_stop': {}
  154. })
  155. serialized = deployment_graph.DeploymentGraph.to_dict(dg)
  156. self.assertEqual(serialized['name'], 'test_graph')
  157. self.assertEqual('$.pending_deletion', serialized['node_filter'])
  158. self.assertEqual(
  159. {'status': 'discover'}, serialized['on_success']['node_attributes']
  160. )
  161. self.assertEqual(
  162. {'status': 'error'}, serialized['on_error']['node_attributes']
  163. )
  164. self.assertEqual(
  165. {}, serialized['on_stop']
  166. )
  167. self.assertItemsEqual(serialized['tasks'], EXPECTED_TASKS)
  168. def test_create_deployment_graph_with_default_handlers(self):
  169. dg = deployment_graph.DeploymentGraph.create({
  170. 'tasks': JSON_TASKS, 'name': 'test_graph'
  171. })
  172. serialized = deployment_graph.DeploymentGraph.to_dict(dg)
  173. self.assertEqual(serialized['name'], 'test_graph')
  174. self.assertItemsEqual(serialized['tasks'], EXPECTED_TASKS)
  175. def test_deployment_graph_update(self):
  176. updated_tasks = [
  177. {
  178. 'task_name': 'updated',
  179. 'type': 'puppet'
  180. }
  181. ]
  182. expected_updated_tasks = [
  183. {
  184. 'task_name': 'updated',
  185. 'type': 'puppet'
  186. }
  187. ]
  188. dg = deployment_graph.DeploymentGraph.create(
  189. {'tasks': JSON_TASKS, 'name': 'test_graph'})
  190. deployment_graph.DeploymentGraph.update(dg, {'tasks': updated_tasks})
  191. serialized = deployment_graph.DeploymentGraph.to_dict(dg)
  192. self.assertEqual(serialized['name'], 'test_graph')
  193. self._compare_tasks(
  194. expected_updated_tasks, serialized['tasks'])
  195. def test_deployment_graph_delete(self):
  196. cluster = self.env.create()
  197. deployment_graph.DeploymentGraph.create_for_model(
  198. {'tasks': JSON_TASKS, 'name': 'test_graph'}, cluster, 'test_graph'
  199. )
  200. dg = deployment_graph.DeploymentGraph.get_for_model(
  201. cluster, 'test_graph'
  202. )
  203. self.assertIsNotNone(dg)
  204. deployment_graph.DeploymentGraph.delete(dg)
  205. dg = deployment_graph.DeploymentGraph.get_for_model(
  206. cluster, 'test_graph'
  207. )
  208. self.assertIsNone(dg)
  209. def test_deployment_graph_create_for_model(self):
  210. cluster = self.env.create()
  211. deployment_graph.DeploymentGraph.create_for_model(
  212. {'tasks': JSON_TASKS, 'name': 'test_graph'}, cluster, 'test_graph'
  213. )
  214. dg = deployment_graph.DeploymentGraph.get_for_model(
  215. cluster, 'test_graph'
  216. )
  217. self._compare_tasks(
  218. EXPECTED_TASKS, deployment_graph.DeploymentGraph.get_tasks(dg)
  219. )
  220. def test_create_tasks_with_yaql_condition(self):
  221. task = {
  222. 'task_name': 'test',
  223. 'type': 'puppet',
  224. 'condition': {'yaql_exp': 'changed($.nodes)'},
  225. 'roles': ['/.*/'],
  226. 'requires': ['other_task'],
  227. }
  228. graph = deployment_graph.DeploymentGraph.create(
  229. {'name': 'test_graph', 'tasks': [task]}
  230. )
  231. self._compare_tasks(
  232. [task], deployment_graph.DeploymentGraph.get_tasks(graph)
  233. )
  234. def test_get_metadata(self):
  235. graph = deployment_graph.DeploymentGraph.create({
  236. 'name': 'test_graph', 'tasks': [],
  237. 'node_filter': '$.deleted',
  238. 'on_success': {},
  239. 'on_error': {'node_attributes': {'error_type': 'deploy'}}
  240. })
  241. metadata = deployment_graph.DeploymentGraph.get_metadata(graph)
  242. self.assertEqual(
  243. {
  244. 'node_filter': '$.deleted',
  245. 'on_success': {},
  246. 'on_error': {'node_attributes': {'error_type': 'deploy'}}
  247. },
  248. metadata
  249. )